From 125cc477df578f130bf75c88b4fe45aea84c0a5b Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 4 May 2021 21:30:07 +0100 Subject: [PATCH] Throw if assign to import in loop --- .../src/rewrite-live-references.ts | 40 ++++++++++------ .../misc/import-const-throw/input.mjs | 10 ++++ .../misc/import-const-throw/output.js | 46 +++++++++++++++++-- 3 files changed, 78 insertions(+), 18 deletions(-) diff --git a/packages/babel-helper-module-transforms/src/rewrite-live-references.ts b/packages/babel-helper-module-transforms/src/rewrite-live-references.ts index 0c9d8cedc1f6..2206d406af7b 100644 --- a/packages/babel-helper-module-transforms/src/rewrite-live-references.ts +++ b/packages/babel-helper-module-transforms/src/rewrite-live-references.ts @@ -355,23 +355,26 @@ const rewriteReferencesVisitor: Visitor = { ) { const { scope, node } = path; const { left } = node; - const { exported, scope: programScope } = this; + const { exported, imported, scope: programScope } = this; if (!t.isVariableDeclaration(left)) { - let didTransform = false; + let didTransformExport = false, + importConstViolationName; const loopBodyScope = path.get("body").scope; for (const name of Object.keys(t.getOuterBindingIdentifiers(left))) { - if ( - exported.get(name) && - programScope.getBinding(name) === scope.getBinding(name) - ) { - didTransform = true; - if (loopBodyScope.hasOwnBinding(name)) { - loopBodyScope.rename(name); + if (programScope.getBinding(name) === scope.getBinding(name)) { + if (exported.get(name)) { + didTransformExport = true; + if (loopBodyScope.hasOwnBinding(name)) { + loopBodyScope.rename(name); + } + } + if (imported.get(name) && !importConstViolationName) { + importConstViolationName = name; } } } - if (!didTransform) { + if (!didTransformExport && !importConstViolationName) { return; } @@ -379,10 +382,6 @@ const rewriteReferencesVisitor: Visitor = { const bodyPath = path.get("body"); const newLoopId = scope.generateUidIdentifierBasedOnNode(left); - bodyPath.unshiftContainer( - "body", - t.expressionStatement(t.assignmentExpression("=", left, newLoopId)), - ); path .get("left") .replaceWith( @@ -391,6 +390,19 @@ const rewriteReferencesVisitor: Visitor = { ]), ); scope.registerDeclaration(path.get("left")); + + if (didTransformExport) { + bodyPath.unshiftContainer( + "body", + t.expressionStatement(t.assignmentExpression("=", left, newLoopId)), + ); + } + if (importConstViolationName) { + bodyPath.unshiftContainer( + "body", + t.expressionStatement(buildImportThrow(importConstViolationName)), + ); + } } }, }; diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/input.mjs index 01f93be0c990..650e21b272d8 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/input.mjs +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/input.mjs @@ -35,3 +35,13 @@ Baz &&= 4; Foo++; Bar++; Baz++; + +for (Foo in {}) ; +for (Bar in {}) {} +for (Baz of []) { + let Baz; +} + +for ({Foo} in {}) {} +for ([Bar] in {}) {} +for ([...Baz] in {}) {} diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/output.js index 3b91b30569a8..79d37c0932bf 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/output.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/misc/import-const-throw/output.js @@ -6,8 +6,6 @@ var Bar = _interopRequireDefault(require("bar")); var _baz = require("baz"); -var _Baz; - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } _foo.default = (42, function () { @@ -91,6 +89,46 @@ _foo.default = (_foo.default + 1, function () { Bar = (Bar + 1, function () { throw new Error('"' + "Bar" + '" is read-only.'); }()); -_Baz = +_baz.Baz, _baz.Baz = (_Baz + 1, function () { +_baz.Baz = (_baz.Baz + 1, function () { throw new Error('"' + "Baz" + '" is read-only.'); -}()), _Baz; +}()); + +for (let _Foo in {}) { + (function () { + throw new Error('"' + "Foo" + '" is read-only.'); + })(); + + ; +} + +for (let _Bar in {}) { + (function () { + throw new Error('"' + "Bar" + '" is read-only.'); + })(); +} + +for (let _Baz of []) { + (function () { + throw new Error('"' + "Baz" + '" is read-only.'); + })(); + + let Baz; +} + +for (let _Foo2 in {}) { + (function () { + throw new Error('"' + "Foo" + '" is read-only.'); + })(); +} + +for (let _ref in {}) { + (function () { + throw new Error('"' + "Bar" + '" is read-only.'); + })(); +} + +for (let _ref2 in {}) { + (function () { + throw new Error('"' + "Baz" + '" is read-only.'); + })(); +}