Skip to content

Commit

Permalink
id in import attributes should not be referenced (#13733)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Sep 7, 2021
1 parent 1ebc5b7 commit d87a3d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
30 changes: 16 additions & 14 deletions packages/babel-types/src/validators/isReferenced.ts
Expand Up @@ -13,14 +13,14 @@ export default function isReferenced(
// yes: NODE.child
// no: parent.NODE
case "MemberExpression":
case "JSXMemberExpression":
case "OptionalMemberExpression":
if (parent.property === node) {
// @ts-expect-error todo(flow->ts): computed is missing on JSXMemberExpression
return !!parent.computed;
}
return parent.object === node;

case "JSXMemberExpression":
return parent.object === node;
// no: let NODE = init;
// yes: let id = NODE;
case "VariableDeclarator":
Expand All @@ -44,33 +44,31 @@ export default function isReferenced(
case "ClassMethod":
case "ClassPrivateMethod":
case "ObjectMethod":
// @ts-expect-error todo(flow->ts) params have more specific type comparing to node
if (parent.params.includes(node)) {
return false;
if (parent.key === node) {
return !!parent.computed;
}
// fall through
return false;

// yes: { [NODE]: "" }
// no: { NODE: "" }
// depends: { NODE }
// depends: { key: NODE }
// fall through
case "ObjectProperty":
if (parent.key === node) {
return !!parent.computed;
}
// parent.value === node
return !grandparent || grandparent.type !== "ObjectPattern";
// no: class { NODE = value; }
// yes: class { [NODE] = value; }
// yes: class { key = NODE; }
// fall through
case "ClassProperty":
case "ClassPrivateProperty":
if (parent.key === node) {
// @ts-expect-error todo(flow->ts): computed might not exist
return !!parent.computed;
}
// @ts-expect-error todo(flow->ts): ObjectMethod does not have value property
if (parent.value === node) {
return !grandparent || grandparent.type !== "ObjectPattern";
}
return true;
case "ClassPrivateProperty":
return parent.key !== node;

// no: class NODE {}
// yes: class Foo extends NODE {}
Expand Down Expand Up @@ -136,6 +134,10 @@ export default function isReferenced(
case "ImportSpecifier":
return false;

// no: import "foo" assert { NODE: "json" }
case "ImportAttribute":
return false;

// no: <div NODE="foo" />
case "JSXAttribute":
return false;
Expand Down
49 changes: 38 additions & 11 deletions packages/babel-types/test/validators.js
Expand Up @@ -122,20 +122,38 @@ describe("validators", function () {
expect(t.isReferenced(node, parent)).toBe(true);
});

it("returns true if node is a value of ObjectProperty of an expression", function () {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]);
describe("ObjectProperty", () => {
it("returns true if node is a value of ObjectProperty of an expression", function () {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectExpression([parent]);

expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});

it("returns false if node is a value of ObjectProperty of a pattern", function () {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]);
it("returns false if node is a value of ObjectProperty of a pattern", function () {
const node = t.identifier("a");
const parent = t.objectProperty(t.identifier("key"), node);
const grandparent = t.objectPattern([parent]);

expect(t.isReferenced(node, parent, grandparent)).toBe(false);
});

it("returns true if node is computed property key of an expression", function () {
const node = t.identifier("a");
const parent = t.objectProperty(node, t.identifier("value"), true);
const grandparent = t.objectExpression([parent]);

expect(t.isReferenced(node, parent, grandparent)).toBe(false);
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});

it("returns true if node is computed property key of a pattern", function () {
const node = t.identifier("a");
const parent = t.objectProperty(node, t.identifier("value"), true);
const grandparent = t.objectPattern([parent]);

expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
});

describe("TSPropertySignature", function () {
Expand Down Expand Up @@ -269,6 +287,15 @@ describe("validators", function () {
expect(t.isReferenced(node, parent, grandparent)).toBe(true);
});
});

describe("import attributes", function () {
it("returns false for import attributes", function () {
const node = t.identifier("foo");
const parent = t.importAttribute(node, t.stringLiteral("bar"));

expect(t.isReferenced(node, parent)).toBe(false);
});
});
});

describe("isBinding", function () {
Expand Down

0 comments on commit d87a3d9

Please sign in to comment.