Arrow functions can have StatementBlocks

Fixes #854
This commit is contained in:
Liam Newman 2016-01-26 16:35:44 -08:00
parent 181e9b3ef0
commit 442cc05cec
6 changed files with 52 additions and 4 deletions

View File

@ -766,6 +766,9 @@
} else {
set_mode(MODE.BlockStatement);
}
} else if (last_type === 'TK_OPERATOR' && flags.last_text === '=>') {
// 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']))
) {

View File

@ -1416,6 +1416,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' return 1;\n' +
' }\n' +
'}.fn2()');
// Issue 854 - Arrow function with statement block
bt(
'test(() => {\n' +
' var a = {}\n' +
'\n' +
' a.what = () => true ? 1 : 2\n' +
'\n' +
' a.thing = () => {\n' +
' b();\n' +
' }\n' +
'})');

View File

@ -1,7 +1,11 @@
#!/usr/bin/env python
import sys
import unittest
if __name__ == "__main__":
def run_tests():
suite = unittest.TestLoader().discover('.', pattern = "test*.py")
unittest.TextTestRunner(verbosity=2).run(suite)
return unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == "__main__":
sys.exit(not run_tests().wasSuccessful())

View File

@ -736,16 +736,19 @@ class Beautifier:
# We don't support TypeScript,but we didn't break it for a very long time.
# We'll try to keep not breaking it.
if not self.last_last_text in ['class','interface']:
self.set_mode(MODE.ObjectLiteral);
self.set_mode(MODE.ObjectLiteral)
else:
self.set_mode(MODE.BlockStatement)
elif self.last_type == 'TK_OPERATOR' and self.flags.last_text == '=>':
# 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']):
# Detecting shorthand function syntax is difficult by scanning forward,
# so check the surrounding context.
# If the block is being returned, imported, passed as arg,
# assigned with = or assigned in a nested object, treat as an ObjectLiteral.
self.set_mode(MODE.ObjectLiteral);
self.set_mode(MODE.ObjectLiteral)
else:
self.set_mode(MODE.BlockStatement)

View File

@ -1191,6 +1191,18 @@ class TestJSBeautifier(unittest.TestCase):
' return 1;\n' +
' }\n' +
'}.fn2()')
# Issue 854 - Arrow function with statement block
bt(
'test(() => {\n' +
' var a = {}\n' +
'\n' +
' a.what = () => true ? 1 : 2\n' +
'\n' +
' a.thing = () => {\n' +
' b();\n' +
' }\n' +
'})')
# Destructured and related
self.options.brace_style = 'collapse-preserve-inline'

View File

@ -1420,6 +1420,20 @@ exports.test_data = {
' }',
'}.fn2()'
]
},
{
comment: "Issue 854 - Arrow function with statement block",
unchanged: [
'test(() => {',
' var a = {}',
'',
' a.what = () => true ? 1 : 2',
'',
' a.thing = () => {',
' b();',
' }',
'})'
]
}
]
}, {