diff --git a/packages/babel-types/src/validators/isReferenced.js b/packages/babel-types/src/validators/isReferenced.js index 9ae72f9423d2..8048419bed5b 100644 --- a/packages/babel-types/src/validators/isReferenced.js +++ b/packages/babel-types/src/validators/isReferenced.js @@ -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"; diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js index 9e46dfc8f202..1c7aee901a22 100644 --- a/packages/babel-types/test/validators.js +++ b/packages/babel-types/test/validators.js @@ -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 () {