Skip to content

Commit

Permalink
Throw if assign to import in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed May 4, 2021
1 parent 4d510ad commit 125cc47
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
Expand Up @@ -355,34 +355,33 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
) {
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;
}

path.ensureBlock();
const bodyPath = path.get("body");

const newLoopId = scope.generateUidIdentifierBasedOnNode(left);
bodyPath.unshiftContainer(
"body",
t.expressionStatement(t.assignmentExpression("=", left, newLoopId)),
);
path
.get("left")
.replaceWith(
Expand All @@ -391,6 +390,19 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
]),
);
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)),
);
}
}
},
};
Expand Up @@ -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 {}) {}
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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.');
})();
}

0 comments on commit 125cc47

Please sign in to comment.