From b09826ce679b933011a7e64a1baf148ed7071f72 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 | 10 +++++++++- .../anonymous-functions-exec/exec.js | 9 +++++++++ .../anonymous-functions-exec/options.json | 6 ++++++ .../anonymous-functions-transform/input.js | 4 ++++ .../anonymous-functions-transform/options.json | 3 +++ .../anonymous-functions-transform/output.js | 4 ++++ .../logical-assignment/arrow-functions-exec/exec.js | 9 +++++++++ .../arrow-functions-exec/options.json | 6 ++++++ .../arrow-functions-transform/input.js | 4 ++++ .../arrow-functions-transform/options.json | 3 +++ .../arrow-functions-transform/output.js | 4 ++++ .../logical-assignment/named-functions-exec/exec.js | 9 +++++++++ .../named-functions-exec/options.json | 6 ++++++ .../named-functions-transform/input.js | 4 ++++ .../named-functions-transform/options.json | 3 +++ .../named-functions-transform/output.js | 4 ++++ 16 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/exec.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/input.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/output.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/exec.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/input.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/output.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/exec.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/input.js create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/options.json create mode 100644 packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/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..1530db439ea6 100644 --- a/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js +++ b/packages/babel-plugin-proposal-logical-assignment-operators/src/index.js @@ -39,11 +39,19 @@ export default declare(api => { } } + const isRHSAnonymousorArrowFunction = + t.isFunctionExpression(right, { + id: null, + }) || t.isArrowFunctionExpression(right); + const rightExpression = isRHSAnonymousorArrowFunction + ? t.sequenceExpression([t.numericLiteral(0), right]) + : right; + path.replaceWith( t.logicalExpression( operator.slice(0, -1), lhs, - t.assignmentExpression("=", left, right), + t.assignmentExpression("=", left, rightExpression), ), ); }, diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/exec.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/exec.js new file mode 100644 index 000000000000..2b41f3380fdd --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/exec.js @@ -0,0 +1,9 @@ +var a, b = true, c; + +a ||= function () {}; +b &&= function () {}; +c ??= function () {}; + +expect(a.name).toBe(""); +expect(b.name).toBe(""); +expect(c.name).toBe(""); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/options.json new file mode 100644 index 000000000000..f834cea89860 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-exec/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "proposal-logical-assignment-operators", + "proposal-nullish-coalescing-operator" + ] +} diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/input.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/input.js new file mode 100644 index 000000000000..8814181f4f6f --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/input.js @@ -0,0 +1,4 @@ +var a; +a ||= function () {}; +a &&= function () {}; +a ??= function () {}; diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/options.json new file mode 100644 index 000000000000..259c77d8e49c --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/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-functions-transform/output.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/output.js new file mode 100644 index 000000000000..2c6a0cfa8498 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/anonymous-functions-transform/output.js @@ -0,0 +1,4 @@ +var a; +a || (a = (0, function () {})); +a && (a = (0, function () {})); +a ?? (a = (0, function () {})); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/exec.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/exec.js new file mode 100644 index 000000000000..ea0c9127310e --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/exec.js @@ -0,0 +1,9 @@ +var a, b = true, c; + +a ||= () => {}; +b &&= () => {}; +c ??= () => {}; + +expect(a.name).toBe(""); +expect(b.name).toBe(""); +expect(c.name).toBe(""); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/options.json new file mode 100644 index 000000000000..f834cea89860 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-exec/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "proposal-logical-assignment-operators", + "proposal-nullish-coalescing-operator" + ] +} diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/input.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/input.js new file mode 100644 index 000000000000..e20999d46ede --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/input.js @@ -0,0 +1,4 @@ +var a; +a ||= () => {}; +a &&= () => {}; +a ??= () => {}; diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/options.json new file mode 100644 index 000000000000..259c77d8e49c --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/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/arrow-functions-transform/output.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/output.js new file mode 100644 index 000000000000..dab2ad853771 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/arrow-functions-transform/output.js @@ -0,0 +1,4 @@ +var a; +a || (a = (0, () => {})); +a && (a = (0, () => {})); +a ?? (a = (0, () => {})); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/exec.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/exec.js new file mode 100644 index 000000000000..0b20de8fca59 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/exec.js @@ -0,0 +1,9 @@ +var a, b = true, c; + +a ||= function d() {}; +b &&= function e() {}; +c ??= function f() {}; + +expect(a.name).toBe("d"); +expect(b.name).toBe("e"); +expect(c.name).toBe("f"); diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/options.json new file mode 100644 index 000000000000..f834cea89860 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-exec/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "proposal-logical-assignment-operators", + "proposal-nullish-coalescing-operator" + ] +} diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/input.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/input.js new file mode 100644 index 000000000000..6fbe4f3fe3f4 --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/input.js @@ -0,0 +1,4 @@ +var a; +a ||= function d() {}; +a &&= function e() {}; +a ??= function f() {}; diff --git a/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/options.json b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/options.json new file mode 100644 index 000000000000..259c77d8e49c --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/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/named-functions-transform/output.js b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/output.js new file mode 100644 index 000000000000..38654cbf111d --- /dev/null +++ b/packages/babel-plugin-proposal-logical-assignment-operators/test/fixtures/logical-assignment/named-functions-transform/output.js @@ -0,0 +1,4 @@ +var a; +a || (a = function d() {}); +a && (a = function e() {}); +a ?? (a = function f() {});