From 23fd0b7c0f47b3f3bd0ee2dfcacfb5979a10ab4f Mon Sep 17 00:00:00 2001 From: Einar Lielmanis Date: Thu, 9 Feb 2012 18:17:09 +0200 Subject: [PATCH] Add support for getters and setters --- beautify.js | 4 ++++ python/jsbeautifier/__init__.py | 3 +++ python/jsbeautifier/tests/testjsbeautifier.py | 8 ++++++++ tests/beautify-tests.js | 8 ++++++++ 4 files changed, 23 insertions(+) diff --git a/beautify.js b/beautify.js index 08895569d..933bd2af6 100755 --- a/beautify.js +++ b/beautify.js @@ -920,6 +920,10 @@ function js_beautify(js_source_text, options) { } else { prefix = 'NEWLINE'; } + + if (token_text === 'function' && (last_text === 'get' || last_text === 'set')) { + prefix = 'SPACE'; + } } if (flags.if_line && last_type === 'TK_END_EXPR') { diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 547919084..a096d0345 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -823,6 +823,9 @@ def handle_word(self, token_text): else: prefix = 'NEWLINE' + if token_text == 'function' and self.last_text in ['get', 'set']: + prefix = 'SPACE' + if token_text in ['else', 'catch', 'finally']: if self.last_type != 'TK_END_BLOCK' \ or self.opts.brace_style == 'expand' \ diff --git a/python/jsbeautifier/tests/testjsbeautifier.py b/python/jsbeautifier/tests/testjsbeautifier.py index 031eabb31..293879a0a 100644 --- a/python/jsbeautifier/tests/testjsbeautifier.py +++ b/python/jsbeautifier/tests/testjsbeautifier.py @@ -409,6 +409,14 @@ def test_beautifier(self): bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"') bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'") + bt("{\n get foo() {}\n}") + bt("{\n var a = get\n foo();\n}") + bt("{\n set foo() {}\n}") + bt("{\n var a = set\n foo();\n}") + bt("var x = {\n get function()\n}") + bt("var x = {\n set function()\n}") + bt("var x = set\n\nfunction() {}") + def decodesto(self, input, expectation=None): self.assertEqual( diff --git a/tests/beautify-tests.js b/tests/beautify-tests.js index 1a441516c..2e3ca33e7 100644 --- a/tests/beautify-tests.js +++ b/tests/beautify-tests.js @@ -460,6 +460,14 @@ function run_beautifier_tests(test_obj) bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"'); bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'"); + bt("{\n get foo() {}\n}"); + bt("{\n var a = get\n foo();\n}"); + bt("{\n set foo() {}\n}"); + bt("{\n var a = set\n foo();\n}"); + bt("var x = {\n get function()\n}"); + bt("var x = {\n set function()\n}"); + bt("var x = set\n\nfunction() {}"); + opts.space_before_conditional = false; bt('if(a) b()');