diff --git a/packages/babel-types/src/validators/isReferenced.ts b/packages/babel-types/src/validators/isReferenced.ts index 519dd7c6d4dd..46c9cce7f6c3 100644 --- a/packages/babel-types/src/validators/isReferenced.ts +++ b/packages/babel-types/src/validators/isReferenced.ts @@ -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": @@ -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 {} @@ -136,6 +134,10 @@ export default function isReferenced( case "ImportSpecifier": return false; + // no: import "foo" assert { NODE: "json" } + case "ImportAttribute": + return false; + // no:
case "JSXAttribute": return false; diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js index 1c7aee901a22..30f600c1b4bd 100644 --- a/packages/babel-types/test/validators.js +++ b/packages/babel-types/test/validators.js @@ -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 () { @@ -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 () {