From 340a39b414236e536dcb188f2690634b52004f19 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sun, 15 Jan 2017 22:56:40 -0600 Subject: [PATCH] Fix cases of missing parens with NewExpression (#230) --- src/fast-path.js | 2 ++ tests/new_expression/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/new_expression/jsfmt.spec.js | 1 + tests/new_expression/new_expression.js | 2 ++ 4 files changed, 13 insertions(+) create mode 100644 tests/new_expression/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/new_expression/jsfmt.spec.js create mode 100644 tests/new_expression/new_expression.js diff --git a/src/fast-path.js b/src/fast-path.js index bcc19b50..634bdda6 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -239,6 +239,7 @@ FPp.needsParens = function(assumeExpressionContext) { case "LogicalExpression": switch (parent.type) { case "CallExpression": + case "NewExpression": return name === "callee" && parent.callee === node; case "UnaryExpression": @@ -340,6 +341,7 @@ FPp.needsParens = function(assumeExpressionContext) { case "SpreadProperty": case "BinaryExpression": case "LogicalExpression": + case "NewExpression": return true; case "CallExpression": diff --git a/tests/new_expression/__snapshots__/jsfmt.spec.js.snap b/tests/new_expression/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..b5988749 --- /dev/null +++ b/tests/new_expression/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +exports[`test new_expression.js 1`] = ` +"new (memoize.Cache || MapCache) +new (typeof this == \"function\" ? this : Dict()) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +new (memoize.Cache || MapCache)(); +new (typeof this == \"function\" ? this : Dict())(); +" +`; diff --git a/tests/new_expression/jsfmt.spec.js b/tests/new_expression/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/new_expression/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); diff --git a/tests/new_expression/new_expression.js b/tests/new_expression/new_expression.js new file mode 100644 index 00000000..23166585 --- /dev/null +++ b/tests/new_expression/new_expression.js @@ -0,0 +1,2 @@ +new (memoize.Cache || MapCache) +new (typeof this == "function" ? this : Dict())