Skip to content

Commit 43aa7e2

Browse files
Don't throw when destructuring into a var named as an import (#10628)
1 parent 5e24016 commit 43aa7e2

3 files changed

Lines changed: 48 additions & 15 deletions

File tree

packages/babel-helper-module-transforms/src/rewrite-live-references.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ const rewriteReferencesVisitor = {
224224
seen.add(path.node);
225225

226226
const left = path.get("left");
227+
228+
// No change needed
229+
if (left.isMemberExpression()) return;
230+
227231
if (left.isIdentifier()) {
228232
// Simple update-assign foo += 1; export { foo };
229233
// => exports.foo = (foo += 1);
@@ -234,9 +238,9 @@ const rewriteReferencesVisitor = {
234238
return;
235239
}
236240

237-
const exportedNames = exported.get(localName) || [];
241+
const exportedNames = exported.get(localName);
238242
const importData = imported.get(localName);
239-
if (exportedNames.length > 0 || importData) {
243+
if (exportedNames?.length > 0 || importData) {
240244
assert(path.node.operator === "=", "Path was not simplified");
241245

242246
const assignment = path.node;
@@ -259,13 +263,14 @@ const rewriteReferencesVisitor = {
259263
);
260264
requeueInParent(path);
261265
}
262-
} else if (left.isMemberExpression()) {
263-
// No change needed
264266
} else {
265267
const ids = left.getOuterBindingIdentifiers();
266-
const id = Object.keys(ids)
267-
.filter(localName => imported.has(localName))
268-
.pop();
268+
const programScopeIds = Object.keys(ids).filter(
269+
localName =>
270+
scope.getBinding(localName) === path.scope.getBinding(localName),
271+
);
272+
const id = programScopeIds.find(localName => imported.has(localName));
273+
269274
if (id) {
270275
path.node.right = t.sequenceExpression([
271276
path.node.right,
@@ -276,14 +281,7 @@ const rewriteReferencesVisitor = {
276281
// Complex ({a, b, c} = {}); export { a, c };
277282
// => ({a, b, c} = {}), (exports.a = a, exports.c = c);
278283
const items = [];
279-
Object.keys(ids).forEach(localName => {
280-
// redeclared in this scope
281-
if (
282-
scope.getBinding(localName) !== path.scope.getBinding(localName)
283-
) {
284-
return;
285-
}
286-
284+
programScopeIds.forEach(localName => {
287285
const exportedNames = exported.get(localName) || [];
288286
if (exportedNames.length > 0) {
289287
items.push(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { foo } from "x";
2+
3+
function f(foo) {
4+
foo = 2;
5+
[foo] = [];
6+
({ foo } = {});
7+
}
8+
9+
10+
foo = 2;
11+
[foo] = [];
12+
({ foo } = {});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
var _x = require("x");
4+
5+
function f(foo) {
6+
foo = 2;
7+
[foo] = [];
8+
({
9+
foo
10+
} = {});
11+
}
12+
13+
_x.foo = (2, function () {
14+
throw new Error('"' + "foo" + '" is read-only.');
15+
}());
16+
[foo] = ([], function () {
17+
throw new Error('"' + "foo" + '" is read-only.');
18+
}());
19+
({
20+
foo
21+
} = ({}, function () {
22+
throw new Error('"' + "foo" + '" is read-only.');
23+
}()));

0 commit comments

Comments
 (0)