From 6661c76c36852e514a0d889d986c0306acc43853 Mon Sep 17 00:00:00 2001 From: Einar Lielmanis Date: Wed, 28 Oct 2009 14:16:52 +0200 Subject: [PATCH] Fix for missing space on return input: return ++i expected: return ++i returned: return++i --- beautify-tests.js | 7 +++++++ beautify.js | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/beautify-tests.js b/beautify-tests.js index c62aeb24..63d47f6a 100644 --- a/beautify-tests.js +++ b/beautify-tests.js @@ -217,6 +217,13 @@ function run_beautifier_tests(test_obj) bt('{[x()[0]];indent;}', '{\n [x()[0]];\n indent;\n}'); + bt('return ++i', 'return ++i'); + bt('return !!x', 'return !!x'); + bt('return !x', 'return !x'); + bt('return [1,2]', 'return [1, 2]'); + bt('return;', 'return;'); + bt('return\nfunc', 'return\nfunc'); + space_after_anon_function = true; bt("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start diff --git a/beautify.js b/beautify.js index 2ef3bbfc..89662ba6 100644 --- a/beautify.js +++ b/beautify.js @@ -498,6 +498,9 @@ function js_beautify(js_source_text, options) if (last_type === 'TK_WORD' || last_text === ')') { // this is array index specifier, break immediately // a[x], fn()[x] + if (last_word === 'return' || last_word === 'throw') { + print_space(); + } set_mode('(EXPRESSION)'); print_token(); break; @@ -725,6 +728,13 @@ function js_beautify(js_source_text, options) var_line_tainted = false; } + if (last_text === 'return' || last_text === 'throw') { + // "return" had a special handling in TK_WORD. Now we need to return the favor + print_space(); + print_token(); + break; + } + if (token_text === ':' && in_case) { print_token(); // colon really asks for separate treatment print_newline();