Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/babel-types/src/validators/isReferenced.js
Expand Up @@ -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() {} }
Expand Down Expand Up @@ -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";
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-types/test/validators.js
Expand Up @@ -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 () {
Expand Down