Skip to content

Commit

Permalink
Fixed "export default { ... }"
Browse files Browse the repository at this point in the history
Fixed #889
  • Loading branch information
bitwiseman committed Aug 28, 2016
1 parent cc7f8a7 commit 1f6943c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,11 @@ if (!Object.values) {
// arrow function: (param1, paramN) => { statements }
set_mode(MODE.BlockStatement);
} else if (in_array(last_type, ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA', 'TK_OPERATOR']) ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw', 'import']))
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw', 'import', 'default']))
) {
// Detecting shorthand function syntax is difficult by scanning forward,
// so check the surrounding context.
// If the block is being returned, imported, passed as arg,
// If the block is being returned, imported, export default, passed as arg,
// assigned with = or assigned in a nested object, treat as an ObjectLiteral.
set_mode(MODE.ObjectLiteral);
} else {
Expand Down
20 changes: 20 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,26 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' export default function div(x, y) {}\n' +
'}');

// Issue 889 - export default { ... }
bt(
'export default {\n' +
' func1() {},\n' +
' func2() {}\n' +
' func3() {}\n' +
'}');
bt(
'export default {\n' +
' a() {\n' +
' return 1;\n' +
' },\n' +
' b() {\n' +
' return 2;\n' +
' },\n' +
' c() {\n' +
' return 3;\n' +
' }\n' +
'}');

// Issue 508
bt('set["name"]');
bt('get["name"]');
Expand Down
4 changes: 2 additions & 2 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,10 @@ def handle_start_block(self, current_token):
# arrow function: (param1, paramN) => { statements }
self.set_mode(MODE.BlockStatement)
elif self.last_type in ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA', 'TK_OPERATOR'] or \
(self.last_type == 'TK_RESERVED' and self.flags.last_text in ['return', 'throw', 'import']):
(self.last_type == 'TK_RESERVED' and self.flags.last_text in ['return', 'throw', 'import', 'default']):
# Detecting shorthand function syntax is difficult by scanning forward,
# so check the surrounding context.
# If the block is being returned, imported, passed as arg,
# If the block is being returned, imported, export default, passed as arg,
# assigned with = or assigned in a nested object, treat as an ObjectLiteral.
self.set_mode(MODE.ObjectLiteral)
else:
Expand Down
20 changes: 20 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,26 @@ def unicode_char(value):
' export default function div(x, y) {}\n' +
'}')

# Issue 889 - export default { ... }
bt(
'export default {\n' +
' func1() {},\n' +
' func2() {}\n' +
' func3() {}\n' +
'}')
bt(
'export default {\n' +
' a() {\n' +
' return 1;\n' +
' },\n' +
' b() {\n' +
' return 2;\n' +
' },\n' +
' c() {\n' +
' return 3;\n' +
' }\n' +
'}')

# Issue 508
bt('set["name"]')
bt('get["name"]')
Expand Down
25 changes: 25 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,31 @@ exports.test_data = {
'}'
]
},
{
comment: 'Issue 889 - export default { ... }',
unchanged: [
'export default {',
' func1() {},',
' func2() {}',
' func3() {}',
'}'
]
},
{
unchanged: [
'export default {',
' a() {',
' return 1;',
' },',
' b() {',
' return 2;',
' },',
' c() {',
' return 3;',
' }',
'}'
]
},
{
comment: "Issue 508",
unchanged: 'set["name"]'
Expand Down

0 comments on commit 1f6943c

Please sign in to comment.