From 08b8a5f2e7d841aa6303dafaf1092800a8e07f2e Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 27 Feb 2017 13:46:17 -0800 Subject: [PATCH] Do not put parenthesis around not named default export (#819) We need to add parenthesis around function expressions if they are named otherwise the name leak, but if the function is not named then it's just superfluous. The conditional change is due to a bad use of switch case where it would fall through the next one. We don't want parenthesis there. --- src/fast-path.js | 9 ++++++++- .../conditional/__snapshots__/jsfmt.spec.js.snap | 16 ++++++++-------- .../es6modules/__snapshots__/jsfmt.spec.js.snap | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/fast-path.js b/src/fast-path.js index c980c5b7..b2decbe9 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -451,9 +451,16 @@ FPp.needsParens = function(assumeExpressionContext) { if (parent.test === node) { return true; } + return false; case "ExportDefaultDeclaration": - return node.type !== "ArrowFunctionExpression"; + if (node.type === "ArrowFunctionExpression") { + return false; + } + if (node.type === "FunctionExpression" && !node.id) { + return false; + } + return true; case "ExpressionStatement": case "MemberExpression": diff --git a/tests/conditional/__snapshots__/jsfmt.spec.js.snap b/tests/conditional/__snapshots__/jsfmt.spec.js.snap index c8095d6e..bce41884 100644 --- a/tests/conditional/__snapshots__/jsfmt.spec.js.snap +++ b/tests/conditional/__snapshots__/jsfmt.spec.js.snap @@ -41,23 +41,23 @@ const { configureStore } = process.env.NODE_ENV === \\"production\\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var inspect = 4 === util.inspect.length ? // node <= 0.8.x - (function(v, colors) { + function(v, colors) { return util.inspect(v, void 0, void 0, colors); - }) + } : // node > 0.8.x - (function(v, colors) { + function(v, colors) { return util.inspect(v, { colors: colors }); - }); + }; var inspect = 4 === util.inspect.length ? // node <= 0.8.x - (function(v, colors) { + function(v, colors) { return util.inspect(v, void 0, void 0, colors); - }) + } : // node > 0.8.x - (function(v, colors) { + function(v, colors) { return util.inspect(v, { colors: colors }); - }); + }; const extractTextPluginOptions = shouldUseRelativeAssetPaths ? // Making sure that the publicPath goes back to to build folder. diff --git a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap index d656f12e..6178a366 100644 --- a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap +++ b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap @@ -101,14 +101,14 @@ export default function f() {} exports[`export_default_function_expression.js 1`] = ` "export default (function() {}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -export default (function() {}); +export default function() {}; " `; exports[`export_default_function_expression.js 2`] = ` "export default (function() {}); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -export default (function() {}); +export default function() {}; " `;