From cbc73c0ccefc9467d6ba7e2b774214a2fba2bb45 Mon Sep 17 00:00:00 2001 From: Arun Kumar Mohan Date: Thu, 2 Apr 2020 18:42:31 -0500 Subject: [PATCH] fix(logical-assignment): Do not assign names to anonymous functions --- .../src/index.js | 27 ++++++++++++++----- .../anonymous-function/input.js | 2 ++ .../anonymous-function/options.json | 3 +++ .../anonymous-function/output.js | 2 ++ 4 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/input.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/output.js diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js b/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js index 813dcd761f7c..2f2c84ff3271 100644 --- a/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js +++ b/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js @@ -39,13 +39,26 @@ export default declare(api => { } } - path.replaceWith( - t.logicalExpression( - operator.slice(0, -1), - lhs, - t.assignmentExpression("=", left, right), - ), - ); + const isRHSAnonymousFunction = t.isFunctionExpression(right, { + id: null, + }); + if (isRHSAnonymousFunction && operator === "??=") { + path.replaceWith( + t.assignmentExpression( + "=", + lhs, + t.logicalExpression("??", left, right), + ), + ); + } else { + path.replaceWith( + t.logicalExpression( + operator.slice(0, -1), + lhs, + t.assignmentExpression("=", left, right), + ), + ); + } }, }, }; diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/input.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/input.js new file mode 100644 index 000000000000..f3eb2ecbfb5c --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/input.js @@ -0,0 +1,2 @@ +var f; +console.log((f ??= function () {}).name); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/options.json new file mode 100644 index 000000000000..259c77d8e49c --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-logical-assignment-operators"] +} diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/output.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/output.js new file mode 100644 index 000000000000..0e137241373e --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-function/output.js @@ -0,0 +1,2 @@ +var f; +console.log((f = f ?? function () {}).name);