Skip to content

Commit

Permalink
Treat get and set outside of ObjectLiteral as non-reserved
Browse files Browse the repository at this point in the history
Fixes #508
  • Loading branch information
bitwiseman committed Sep 28, 2014
1 parent e0e725b commit 15197d3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
21 changes: 14 additions & 7 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@
}

function start_of_object_property() {
return flags.parent.mode === MODE.ObjectLiteral && flags.mode === MODE.Statement && flags.last_text === ':' &&
flags.ternary_depth === 0;
return flags.parent.mode === MODE.ObjectLiteral && flags.mode === MODE.Statement && (
(flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set'])));
}

function start_of_statement() {
Expand All @@ -497,8 +497,8 @@
&& !flags.in_case
&& !(current_token.text === '--' || current_token.text === '++')
&& current_token.type !== 'TK_WORD' && current_token.type !== 'TK_RESERVED') ||
(flags.mode === MODE.ObjectLiteral && flags.last_text === ':' && flags.ternary_depth === 0)

(flags.mode === MODE.ObjectLiteral && (
(flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set']))))
) {

set_mode(MODE.Statement);
Expand Down Expand Up @@ -682,8 +682,10 @@
// Check if this is should be treated as a ObjectLiteral
var next_token = get_token(1)
var second_token = get_token(2)
if (second_token && second_token.text == ':' &&
(next_token.type === 'TK_STRING' || next_token.type == 'TK_WORD' || next_token.type == 'TK_RESERVED')) {
if (second_token && (
(second_token.text === ':' && in_array(next_token.type, ['TK_STRING', 'TK_WORD', 'TK_RESERVED']))
|| (in_array(next_token.text, ['get', 'set']) && in_array(second_token.type, ['TK_WORD', 'TK_RESERVED']))
)) {
set_mode(MODE.ObjectLiteral);
} else {
set_mode(MODE.BlockStatement);
Expand Down Expand Up @@ -755,6 +757,11 @@
}

function handle_word() {
if (current_token.type === 'TK_RESERVED' && flags.mode !== MODE.ObjectLiteral &&
in_array(current_token.text, ['set', 'get'])) {
current_token.type = 'TK_WORD';
}

if (start_of_statement()) {
// The conditional starts the statement if appropriate.
} else if (current_token.wanted_newline && !is_expression(flags.mode) &&
Expand Down Expand Up @@ -840,7 +847,7 @@
}
}

if (current_token.type === 'TK_RESERVED' && current_token.text === 'function') {
if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['function', 'get', 'set'])) {
print_token();
flags.last_word = current_token.text;
return;
Expand Down
29 changes: 25 additions & 4 deletions js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,10 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
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\na() {}", "var x = set\n\n a() {}");
bt("var x = set\n\nfunction() {}", "var x = set\n\n function() {}");

// According to my current research get/set have no special meaning outside of an object literal
bt("var x = set\n\na() {}", "var x = set\n\na() {}");
bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}");

bt('<!-- foo\nbar();\n-->');
bt('<!-- dont crash');
Expand Down Expand Up @@ -1705,8 +1707,8 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('var c = "_ACTION_TO_NATIVEAPI_" + ++g++ + +new Date;');
bt('var c = "_ACTION_TO_NATIVEAPI_" - --g-- - -new Date;');
// END tests for issue 514
// START tests for issue 485

// START tests for issue 485
// ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +
' function() {\n' +
Expand All @@ -1722,6 +1724,25 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'}];');
// END tests for issue 485

// START tests for issue 508
bt('set["name"]');
bt('get["name"]');
test_fragment(
'a = {\n' +
' set b(x) {},\n' +
' c: 1,\n' +
' d: function() {}\n' +
'};');
test_fragment(
'a = {\n' +
' get b() {\n' +
' retun 0;\n' +
' },\n' +
' c: 1,\n' +
' d: function() {}\n' +
'};');
// END tests for issue 508

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
bt('var a={bing:1},b=2,c=3;',
Expand Down
18 changes: 12 additions & 6 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ def restore_mode(self):


def start_of_object_property(self):
return self.flags.parent.mode == MODE.ObjectLiteral and self.flags.mode == MODE.Statement and self.flags.last_text == ':' and \
self.flags.ternary_depth == 0
return self.flags.parent.mode == MODE.ObjectLiteral and self.flags.mode == MODE.Statement and \
((self.flags.last_text == ':' and self.flags.ternary_depth == 0) or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set']))

def start_of_statement(self, current_token):
if (
Expand All @@ -522,7 +522,8 @@ def start_of_statement(self, current_token):
and not self.flags.in_case
and not (current_token.text == '--' or current_token.text == '++')
and current_token.type != 'TK_WORD' and current_token.type != 'TK_RESERVED') \
or (self.flags.mode == MODE.ObjectLiteral and self.flags.last_text == ':' and self.flags.ternary_depth == 0) \
or (self.flags.mode == MODE.ObjectLiteral and \
((self.flags.last_text == ':' and self.flags.ternary_depth == 0) or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set'])))
):

self.set_mode(MODE.Statement)
Expand Down Expand Up @@ -658,8 +659,9 @@ def handle_start_block(self, current_token):
# Check if this is a BlockStatement that should be treated as a ObjectLiteral
next_token = self.get_token(1)
second_token = self.get_token(2)
if (not second_token == None) and second_token.text == ':' and \
(next_token.type == 'TK_STRING' or next_token.type == 'TK_WORD' or next_token.type == 'TK_RESERVED'):
if second_token != None and \
((second_token.text == ':' and next_token.type in ['TK_STRING', 'TK_WORD', 'TK_RESERVED']) \
or (next_token.text in ['get', 'set'] and second_token.type in ['TK_WORD', 'TK_RESERVED'])):
self.set_mode(MODE.ObjectLiteral)
else:
self.set_mode(MODE.BlockStatement)
Expand Down Expand Up @@ -718,6 +720,10 @@ def handle_end_block(self, current_token):


def handle_word(self, current_token):
if current_token.type == 'TK_RESERVED' and self.flags.mode != MODE.ObjectLiteral and \
current_token.text in ['set', 'get']:
current_token.type = 'TK_WORD'

if self.start_of_statement(current_token):
# The conditional starts the statement if appropriate.
pass
Expand Down Expand Up @@ -790,7 +796,7 @@ def handle_word(self, current_token):
if not self.start_of_object_property():
self.allow_wrap_or_preserved_newline(current_token)

if current_token.type == 'TK_RESERVED' and current_token.text == 'function':
if current_token.type == 'TK_RESERVED' and current_token.text in ['function', 'get', 'set']:
self.print_token(current_token)
self.flags.last_word = current_token.text
return
Expand Down
29 changes: 25 additions & 4 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,10 @@ def test_beautifier(self):
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\na() {}", "var x = set\n\n a() {}");
bt("var x = set\n\nfunction() {}", "var x = set\n\n function() {}")

# According to my current research get/set have no special meaning outside of an object literal
bt("var x = set\n\na() {}", "var x = set\n\na() {}");
bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}");

bt('<!-- foo\nbar();\n-->')
bt('<!-- dont crash')
Expand Down Expand Up @@ -1576,7 +1578,7 @@ def test_beautifier(self):
bt('var c = "_ACTION_TO_NATIVEAPI_" - --g-- - -new Date;');
# END tests for issue 514

# START tests for issue 485
# START tests for issue 485
# ensure function declarations behave the same in arrays as elsewhere
bt( 'var v = ["a",\n' +
' function() {\n' +
Expand All @@ -1591,7 +1593,26 @@ def test_beautifier(self):
' id: 1\n' +
'}];');
# END tests for issue 485


# START tests for issue 508
bt('set["name"]');
bt('get["name"]');
test_fragment(
'a = {\n' +
' set b(x) {},\n' +
' c: 1,\n' +
' d: function() {}\n' +
'};');
test_fragment(
'a = {\n' +
' get b() {\n' +
' retun 0;\n' +
' },\n' +
' c: 1,\n' +
' d: function() {}\n' +
'};');
# END tests for issue 508

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
bt('var a={bing:1},b=2,c=3;',
Expand Down

0 comments on commit 15197d3

Please sign in to comment.