Skip to content

Commit

Permalink
Fix t.isReferenced() for named re-exports (babel#12395)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 2, 2020
1 parent 5d0adab commit a66f89c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
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

0 comments on commit a66f89c

Please sign in to comment.