Fix t.isReferenced() for named re-exports (#12395)

This commit is contained in:
Nicolò Ribaudo 2020-11-25 19:39:45 +01:00 committed by GitHub
parent 695abb8dfc
commit 645fe637f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 9 deletions

View File

@ -29,15 +29,6 @@ export default function isReferenced(
case "ArrowFunctionExpression":
return parent.body === node;
// no: export { foo as NODE };
// yes: export { NODE as foo };
// no: export { NODE as foo } from "foo";
case "ExportSpecifier":
if (parent.source) {
return false;
}
return parent.local === node;
// no: class { #NODE; }
// no: class { get #NODE() {} }
// no: class { #NODE() {} }
@ -120,6 +111,15 @@ export default function isReferenced(
case "ExportDefaultSpecifier":
return false;
// no: export { foo as NODE };
// yes: export { NODE as foo };
// no: export { NODE as foo } from "foo";
case "ExportSpecifier":
if (grandparent?.source) {
return false;
}
return parent.local === node;
// no: import NODE from "foo";
// no: import * as NODE from "foo";
// no: import { NODE as foo } from "foo";

View File

@ -247,6 +247,28 @@ describe("validators", function () {
expect(t.isReferenced(node, parent)).toBe(false);
});
});
describe("exports", function () {
it("returns false for re-exports", function () {
const node = t.identifier("foo");
const parent = t.exportSpecifier(node, t.identifier("bar"));
const grandparent = t.exportNamedDeclaration(
null,
[parent],
t.stringLiteral("library"),
);
expect(t.isReferenced(node, parent, grandparent)).toBe(false);
});
it("returns true for local exports", function () {
const node = t.identifier("foo");
const parent = t.exportSpecifier(node, t.identifier("bar"));
const grandparent = t.exportNamedDeclaration(null, [parent]);
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
});
});
describe("isBinding", function () {