From b94b86331fd5035612976a86794193f8ba3d2bc9 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Tue, 17 Jan 2017 21:45:56 -0600 Subject: [PATCH] Ensure parens on NewExpression with function callee (#282) --- src/fast-path.js | 3 +++ tests/arrows/__snapshots__/jsfmt.spec.js.snap | 3 +++ tests/arrows/arrow_function_expression.js | 1 + tests/async/__snapshots__/jsfmt.spec.js.snap | 8 ++++---- tests/function/__snapshots__/jsfmt.spec.js.snap | 3 +++ tests/function/function_expression.js | 1 + 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/fast-path.js b/src/fast-path.js index 94b2fcf44..ca3e73e76 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -378,6 +378,9 @@ FPp.needsParens = function(assumeExpressionContext) { case "UnaryExpression": return true; + case "NewExpression": + return name === "callee"; + default: return isBinary(parent); } diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index 4858de59f..424b07289 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -32,6 +32,7 @@ typeof (() => {}); export default (() => {})(); (() => {})()\`\`; (() => {})\`\`; +new (() => {}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (a => { }).length; @@ -43,6 +44,8 @@ export default (() => { })()\`\`; (() => { })\`\`; +new (() => { +})(); " `; diff --git a/tests/arrows/arrow_function_expression.js b/tests/arrows/arrow_function_expression.js index dac425060..9b579fe04 100644 --- a/tests/arrows/arrow_function_expression.js +++ b/tests/arrows/arrow_function_expression.js @@ -3,3 +3,4 @@ typeof (() => {}); export default (() => {})(); (() => {})()``; (() => {})``; +new (() => {}); diff --git a/tests/async/__snapshots__/jsfmt.spec.js.snap b/tests/async/__snapshots__/jsfmt.spec.js.snap index ab791989f..d4b1a9e02 100644 --- a/tests/async/__snapshots__/jsfmt.spec.js.snap +++ b/tests/async/__snapshots__/jsfmt.spec.js.snap @@ -192,8 +192,8 @@ var e = async function() { var et = async function(a: T) { }; -var n = new async function() { -}(); +var n = new (async function() { +})(); var o = { async m() { @@ -516,9 +516,9 @@ var et = async function(a: T) { await 1; }; -var n = new async function() { +var n = new (async function() { await 1; -}(); +})(); var o = { async m() { diff --git a/tests/function/__snapshots__/jsfmt.spec.js.snap b/tests/function/__snapshots__/jsfmt.spec.js.snap index 8c581f246..8ad3ba76d 100644 --- a/tests/function/__snapshots__/jsfmt.spec.js.snap +++ b/tests/function/__snapshots__/jsfmt.spec.js.snap @@ -369,6 +369,7 @@ typeof (function() {}); export default (function() {})(); (function() {})()\`\`; (function() {})\`\`; +new (function() {}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (function() { }).length; @@ -380,6 +381,8 @@ export default (function() { })()\`\`; (function() { })\`\`; +new (function() { +})(); " `; diff --git a/tests/function/function_expression.js b/tests/function/function_expression.js index 93c6da8ec..e680a3d1a 100644 --- a/tests/function/function_expression.js +++ b/tests/function/function_expression.js @@ -3,3 +3,4 @@ typeof (function() {}); export default (function() {})(); (function() {})()``; (function() {})``; +new (function() {});