Don't throw when destructuring into a var named as an import (#10628)

This commit is contained in:
Nicolò Ribaudo 2019-11-04 22:50:36 +01:00 committed by GitHub
parent 5e24016623
commit 43aa7e262c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 15 deletions

View File

@ -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(

View File

@ -0,0 +1,12 @@
import { foo } from "x";
function f(foo) {
foo = 2;
[foo] = [];
({ foo } = {});
}
foo = 2;
[foo] = [];
({ foo } = {});

View File

@ -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.');
}()));