Skip to content

Commit

Permalink
Merge pull request #812 from timothyeburke/master
Browse files Browse the repository at this point in the history
Resolving #810: ES6 Object Literal Shorthand Detection
  • Loading branch information
bitwiseman committed Nov 19, 2015
2 parents ea51c1b + dc472ee commit ee03546
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@
var next_token = get_token(1)
var second_token = get_token(2)
if (second_token && (
(second_token.text === ':' && in_array(next_token.type, ['TK_STRING', 'TK_WORD', 'TK_RESERVED']))
(in_array(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']))
)) {
// We don't support TypeScript,but we didn't break it for a very long time.
Expand Down
26 changes: 26 additions & 0 deletions js/test/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,32 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'a.b("c",\n' +
' () => d.e\n' +
')');

// Issue 810 - es6 object literal detection
bt(
'function badFormatting() {\n' +
' return {\n' +
' a,\n' +
' b: c,\n' +
' d: e,\n' +
' f: g,\n' +
' h,\n' +
' i,\n' +
' j: k\n' +
' }\n' +
'}\n' +
'\n' +
'function goodFormatting() {\n' +
' return {\n' +
' a: b,\n' +
' c,\n' +
' d: e,\n' +
' f: g,\n' +
' h,\n' +
' i,\n' +
' j: k\n' +
' }\n' +
'}');



Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def handle_start_block(self, current_token):
next_token = self.get_token(1)
second_token = self.get_token(2)
if second_token != None and \
((second_token.text == ':' and next_token.type in ['TK_STRING', 'TK_WORD', 'TK_RESERVED']) \
((second_token.text in [':', ','] 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'])):
# We don't support TypeScript,but we didn't break it for a very long time.
# We'll try to keep not breaking it.
Expand Down
30 changes: 28 additions & 2 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_unescape(self):
bt('a = /\s+/')
#bt('a = /\\x41/','a = /A/')
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
bt('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')

self.options.unescape_strings = True

bt('"\\x41\\x42\\x43\\x01"', '"ABC\\x01"')
bt('"\\u2022"', '"\\u2022"')
bt('a = /\s+/')
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"ABC\\x01".match(/\\x41/);')
bt('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\"\'", \'"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \' \\\\ \\uffff \\uzzzz"')
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\"\'", \'"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \' \\\\ \\uffff \\uzzzz"')

self.options.unescape_strings = False

Expand Down Expand Up @@ -1053,6 +1053,32 @@ def unicode_char(value):
'a.b("c",\n' +
' () => d.e\n' +
')')

# Issue 810 - es6 object literal detection
bt(
'function badFormatting() {\n' +
' return {\n' +
' a,\n' +
' b: c,\n' +
' d: e,\n' +
' f: g,\n' +
' h,\n' +
' i,\n' +
' j: k\n' +
' }\n' +
'}\n' +
'\n' +
'function goodFormatting() {\n' +
' return {\n' +
' a: b,\n' +
' c,\n' +
' d: e,\n' +
' f: g,\n' +
' h,\n' +
' i,\n' +
' j: k\n' +
' }\n' +
'}')

# Old tests
bt('')
Expand Down
28 changes: 28 additions & 0 deletions test/data/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,34 @@ exports.test_data = {
')'
]
},
{
comment: "Issue 810 - es6 object literal detection",
unchanged: [
'function badFormatting() {',
' return {',
' a,',
' b: c,',
' d: e,',
' f: g,',
' h,',
' i,',
' j: k',
' }',
'}',
'',
'function goodFormatting() {',
' return {',
' a: b,',
' c,',
' d: e,',
' f: g,',
' h,',
' i,',
' j: k',
' }',
'}'
]
}
]
},

Expand Down
4 changes: 2 additions & 2 deletions test/template/python-javascript.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class TestJSBeautifier(unittest.TestCase):
bt('a = /\s+/')
#bt('a = /\\x41/','a = /A/')
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);')
bt('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"')

self.options.unescape_strings = True

bt('"\\x41\\x42\\x43\\x01"', '"ABC\\x01"')
bt('"\\u2022"', '"\\u2022"')
bt('a = /\s+/')
bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"ABC\\x01".match(/\\x41/);')
bt('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\"\'", \'"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \' \\\\ \\uffff \\uzzzz"')
test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\"\'", \'"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \' \\\\ \\uffff \\uzzzz"')

self.options.unescape_strings = False

Expand Down

0 comments on commit ee03546

Please sign in to comment.