From d9ea466cd3e287fba399015b61fd18f09151eb2e Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 16 Jan 2017 11:52:55 -0600 Subject: [PATCH] Fix FunctionExpression parens issues (#250) --- src/fast-path.js | 1 + tests/arrows/__snapshots__/jsfmt.spec.js.snap | 6 ++++++ tests/arrows/arrow_function_expression.js | 2 ++ tests/function/__snapshots__/jsfmt.spec.js.snap | 17 +++++++++++++++++ tests/function/function_expression.js | 4 ++++ 5 files changed, 30 insertions(+) create mode 100644 tests/function/function_expression.js diff --git a/src/fast-path.js b/src/fast-path.js index dd4b2b55e..cc195dc92 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -366,6 +366,7 @@ FPp.needsParens = function(assumeExpressionContext) { return n.ObjectPattern.check(node.left) && this.firstInStatement(); } + case "FunctionExpression": case "ArrowFunctionExpression": if (parent.type === "CallExpression" && name === "callee") { return true; diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index bf224ddd7..3e6449a4b 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -28,11 +28,17 @@ var ident = (x: T): T => x; exports[`test arrow_function_expression.js 1`] = ` "(a => {}).length typeof (() => {}); +export default (() => {})(); +(() => {})()\`\`; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (a => { }).length; typeof (() => { }); +export default (() => { +})() +(() => { +})()\`\`; " `; diff --git a/tests/arrows/arrow_function_expression.js b/tests/arrows/arrow_function_expression.js index 57a6e9209..f0309346f 100644 --- a/tests/arrows/arrow_function_expression.js +++ b/tests/arrows/arrow_function_expression.js @@ -1,2 +1,4 @@ (a => {}).length typeof (() => {}); +export default (() => {})(); +(() => {})()``; diff --git a/tests/function/__snapshots__/jsfmt.spec.js.snap b/tests/function/__snapshots__/jsfmt.spec.js.snap index e33bd50d5..a776342cc 100644 --- a/tests/function/__snapshots__/jsfmt.spec.js.snap +++ b/tests/function/__snapshots__/jsfmt.spec.js.snap @@ -351,6 +351,23 @@ var e = (d.bind(1): Function)(); " `; +exports[`test function_expression.js 1`] = ` +"(function() {}).length +typeof (function() {}); +export default (function() {})(); +(function() {})()\`\`; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +(function() { +}).length; +typeof (function() { +}); +export default (function() { +})() +(function() { +})()\`\`; +" +`; + exports[`test rest.js 1`] = ` "/* regression tests */ diff --git a/tests/function/function_expression.js b/tests/function/function_expression.js new file mode 100644 index 000000000..786e8bdb5 --- /dev/null +++ b/tests/function/function_expression.js @@ -0,0 +1,4 @@ +(function() {}).length +typeof (function() {}); +export default (function() {})(); +(function() {})()``;