Don't throw when destructuring into a var named as an import (#10628)
This commit is contained in:
parent
5e24016623
commit
43aa7e262c
@ -224,6 +224,10 @@ const rewriteReferencesVisitor = {
|
|||||||
seen.add(path.node);
|
seen.add(path.node);
|
||||||
|
|
||||||
const left = path.get("left");
|
const left = path.get("left");
|
||||||
|
|
||||||
|
// No change needed
|
||||||
|
if (left.isMemberExpression()) return;
|
||||||
|
|
||||||
if (left.isIdentifier()) {
|
if (left.isIdentifier()) {
|
||||||
// Simple update-assign foo += 1; export { foo };
|
// Simple update-assign foo += 1; export { foo };
|
||||||
// => exports.foo = (foo += 1);
|
// => exports.foo = (foo += 1);
|
||||||
@ -234,9 +238,9 @@ const rewriteReferencesVisitor = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const exportedNames = exported.get(localName) || [];
|
const exportedNames = exported.get(localName);
|
||||||
const importData = imported.get(localName);
|
const importData = imported.get(localName);
|
||||||
if (exportedNames.length > 0 || importData) {
|
if (exportedNames?.length > 0 || importData) {
|
||||||
assert(path.node.operator === "=", "Path was not simplified");
|
assert(path.node.operator === "=", "Path was not simplified");
|
||||||
|
|
||||||
const assignment = path.node;
|
const assignment = path.node;
|
||||||
@ -259,13 +263,14 @@ const rewriteReferencesVisitor = {
|
|||||||
);
|
);
|
||||||
requeueInParent(path);
|
requeueInParent(path);
|
||||||
}
|
}
|
||||||
} else if (left.isMemberExpression()) {
|
|
||||||
// No change needed
|
|
||||||
} else {
|
} else {
|
||||||
const ids = left.getOuterBindingIdentifiers();
|
const ids = left.getOuterBindingIdentifiers();
|
||||||
const id = Object.keys(ids)
|
const programScopeIds = Object.keys(ids).filter(
|
||||||
.filter(localName => imported.has(localName))
|
localName =>
|
||||||
.pop();
|
scope.getBinding(localName) === path.scope.getBinding(localName),
|
||||||
|
);
|
||||||
|
const id = programScopeIds.find(localName => imported.has(localName));
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
path.node.right = t.sequenceExpression([
|
path.node.right = t.sequenceExpression([
|
||||||
path.node.right,
|
path.node.right,
|
||||||
@ -276,14 +281,7 @@ const rewriteReferencesVisitor = {
|
|||||||
// Complex ({a, b, c} = {}); export { a, c };
|
// Complex ({a, b, c} = {}); export { a, c };
|
||||||
// => ({a, b, c} = {}), (exports.a = a, exports.c = c);
|
// => ({a, b, c} = {}), (exports.a = a, exports.c = c);
|
||||||
const items = [];
|
const items = [];
|
||||||
Object.keys(ids).forEach(localName => {
|
programScopeIds.forEach(localName => {
|
||||||
// redeclared in this scope
|
|
||||||
if (
|
|
||||||
scope.getBinding(localName) !== path.scope.getBinding(localName)
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const exportedNames = exported.get(localName) || [];
|
const exportedNames = exported.get(localName) || [];
|
||||||
if (exportedNames.length > 0) {
|
if (exportedNames.length > 0) {
|
||||||
items.push(
|
items.push(
|
||||||
|
|||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { foo } from "x";
|
||||||
|
|
||||||
|
function f(foo) {
|
||||||
|
foo = 2;
|
||||||
|
[foo] = [];
|
||||||
|
({ foo } = {});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foo = 2;
|
||||||
|
[foo] = [];
|
||||||
|
({ foo } = {});
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var _x = require("x");
|
||||||
|
|
||||||
|
function f(foo) {
|
||||||
|
foo = 2;
|
||||||
|
[foo] = [];
|
||||||
|
({
|
||||||
|
foo
|
||||||
|
} = {});
|
||||||
|
}
|
||||||
|
|
||||||
|
_x.foo = (2, function () {
|
||||||
|
throw new Error('"' + "foo" + '" is read-only.');
|
||||||
|
}());
|
||||||
|
[foo] = ([], function () {
|
||||||
|
throw new Error('"' + "foo" + '" is read-only.');
|
||||||
|
}());
|
||||||
|
({
|
||||||
|
foo
|
||||||
|
} = ({}, function () {
|
||||||
|
throw new Error('"' + "foo" + '" is read-only.');
|
||||||
|
}()));
|
||||||
Loading…
x
Reference in New Issue
Block a user