Skip to content

Commit

Permalink
Merge pull request #819 from r-murphy/issue602
Browse files Browse the repository at this point in the history
Resolves #602: Detect ObjectLiteral for ES6 shorthand function
  • Loading branch information
bitwiseman committed Nov 26, 2015
2 parents 3472464 + 8f221a3 commit 947fc27
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
9 changes: 8 additions & 1 deletion js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,17 @@
} else {
set_mode(MODE.BlockStatement);
}
} else if (in_array(last_type, ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA']) ||
(flags.last_text === ':') || (last_type === 'TK_RESERVED' && flags.last_text === 'return')
) {
// Detecting shorthand function syntax is difficult by scanning forward, so check the surrounding context.
// If the block is being returned, passed as arg, assigned with = or assigned in a nested object, treat as an ObjectLiteral.
set_mode(MODE.ObjectLiteral);
} else {
set_mode(MODE.BlockStatement);
}


var empty_braces = !next_token.comments_before.length && next_token.text === '}';
var empty_anonymous_function = empty_braces && flags.last_word === 'function' &&
last_type === 'TK_END_EXPR';
Expand Down Expand Up @@ -1544,7 +1551,7 @@
var digit_oct = /[01234567]/;
var digit_hex = /[0123456789abcdefABCDEF]/;

var punct = ('+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! ~ , : ? ^ ^= |= :: =>').split(' ');
var punct = ('+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! ~ , : ? ^ ^= |= :: =>').split(' ');
// words which should always start on new line.
this.line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
var reserved_words = this.line_starters.concat(['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await']);
Expand Down
50 changes: 50 additions & 0 deletions js/test/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,56 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' j: k\n' +
' }\n' +
'}');

// Issue 602 - ES6 object literal shorthand functions
bt(
'return {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'foo({\n' +
' fn1(a) {}\n' +
' fn2(a) {}\n' +
'})\n' +
'\n' +
'foo("text", {\n' +
' fn1(a) {}\n' +
' fn2(a) {}\n' +
'})\n' +
'\n' +
'oneArg = {\n' +
' fn1(a) {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'multiArg = {\n' +
' fn1(a, b, c) {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'noArgs = {\n' +
' fn1() {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'emptyFn = {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'nested = {\n' +
' fns: {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
' }\n' +
'}');



Expand Down
5 changes: 5 additions & 0 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,11 @@ def handle_start_block(self, current_token):
self.set_mode(MODE.ObjectLiteral);
else:
self.set_mode(MODE.BlockStatement)
elif self.last_type in ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA'] or \
(self.flags.last_text == ':') or (self.last_type == 'TK_RESERVED' and self.flags.last_text == 'return'):
# Detecting shorthand function syntax is difficult by scanning forward, so check the surrounding context.
# If the block is being returned, passed as arg, assigned with = or assigned in a nested object, treat as an ObjectLiteral.
self.set_mode(MODE.ObjectLiteral);
else:
self.set_mode(MODE.BlockStatement)

Expand Down
50 changes: 50 additions & 0 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,56 @@ def unicode_char(value):
' j: k\n' +
' }\n' +
'}')

# Issue 602 - ES6 object literal shorthand functions
bt(
'return {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'foo({\n' +
' fn1(a) {}\n' +
' fn2(a) {}\n' +
'})\n' +
'\n' +
'foo("text", {\n' +
' fn1(a) {}\n' +
' fn2(a) {}\n' +
'})\n' +
'\n' +
'oneArg = {\n' +
' fn1(a) {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'multiArg = {\n' +
' fn1(a, b, c) {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'noArgs = {\n' +
' fn1() {\n' +
' do();\n' +
' },\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'emptyFn = {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
'}\n' +
'\n' +
'nested = {\n' +
' fns: {\n' +
' fn1() {},\n' +
' fn2() {}\n' +
' }\n' +
'}')

# Old tests
bt('')
Expand Down
52 changes: 52 additions & 0 deletions test/data/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,58 @@ exports.test_data = {
' }',
'}'
]
},
{
comment: "Issue 602 - ES6 object literal shorthand functions",
unchanged: [
'return {',
' fn1() {},',
' fn2() {}',
'}',
'',
'foo({',
' fn1(a) {}',
' fn2(a) {}',
'})',
'',
'foo("text", {',
' fn1(a) {}',
' fn2(a) {}',
'})',
'',
'oneArg = {',
' fn1(a) {',
' do();',
' },',
' fn2() {}',
'}',
'',
'multiArg = {',
' fn1(a, b, c) {',
' do();',
' },',
' fn2() {}',
'}',
'',
'noArgs = {',
' fn1() {',
' do();',
' },',
' fn2() {}',
'}',
'',
'emptyFn = {',
' fn1() {},',
' fn2() {}',
'}',
'',
'nested = {',
' fns: {',
' fn1() {},',
' fn2() {}',
' }',
'}'
]
}
]
},
Expand Down

0 comments on commit 947fc27

Please sign in to comment.