From 8956e2a3d9fbc960c749e5992009d25ce6ae529a Mon Sep 17 00:00:00 2001 From: Einar Lielmanis Date: Wed, 29 Feb 2012 19:45:02 +0200 Subject: [PATCH] Fix #43 if (x) { // ... } (function() { // ... setTimeout(arguments.callee, 5); })(); => ... }(function() { ... --- beautify.js | 3 +++ python/jsbeautifier/__init__.py | 3 ++- python/jsbeautifier/tests/testjsbeautifier.py | 1 + tests/beautify-tests.js | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/beautify.js b/beautify.js index 933bd2af6..7a0301d34 100755 --- a/beautify.js +++ b/beautify.js @@ -706,6 +706,9 @@ function js_beautify(js_source_text, options) { if (last_text === ';' || last_type === 'TK_START_BLOCK') { print_newline(); } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') { + if (wanted_newline) { + print_newline(); + } // do nothing on (( and )( and ][ and ]( and .( } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { print_single_space(); diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index a096d0345..6572a4397 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -664,7 +664,8 @@ def handle_start_expr(self, token_text): self.append_newline() elif self.last_type in ['TK_END_EXPR', 'TK_START_EXPR', 'TK_END_BLOCK'] or self.last_text == '.': # do nothing on (( and )( and ][ and ]( and .( - pass + if self.wanted_newline: + self.append_newline(); elif self.last_type not in ['TK_WORD', 'TK_OPERATOR']: self.append(' ') elif self.last_word == 'function' or self.last_word == 'typeof': diff --git a/python/jsbeautifier/tests/testjsbeautifier.py b/python/jsbeautifier/tests/testjsbeautifier.py index 293879a0a..577cabb0e 100644 --- a/python/jsbeautifier/tests/testjsbeautifier.py +++ b/python/jsbeautifier/tests/testjsbeautifier.py @@ -406,6 +406,7 @@ def test_beautifier(self): bt('a = <%= external() %> ;'); test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};'); + test_fragment("if (..) {\n // ....\n}\n(function"); bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"') bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'") diff --git a/tests/beautify-tests.js b/tests/beautify-tests.js index 2e3ca33e7..8a285601f 100644 --- a/tests/beautify-tests.js +++ b/tests/beautify-tests.js @@ -460,6 +460,9 @@ function run_beautifier_tests(test_obj) bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"'); bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'"); + + test_fragment("if (..) {\n // ....\n}\n(function"); + bt("{\n get foo() {}\n}"); bt("{\n var a = get\n foo();\n}"); bt("{\n set foo() {}\n}");