From 8cfdbdfbed2913b1a7abf10dd523952fbef72bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 3 Sep 2021 16:05:23 -0400 Subject: [PATCH 1/4] fix ts-expect-error --- .../src/validators/isReferenced.ts | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/babel-types/src/validators/isReferenced.ts b/packages/babel-types/src/validators/isReferenced.ts index 519dd7c6d4dd..7d2fb5e0a3d3 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,31 +44,34 @@ 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; + } + if (parent.value === node) { + return !grandparent || grandparent.type !== "ObjectPattern"; + } + return true; // 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": + if (parent.key === node) { + return false; } return true; From d8bf4f519f055c74b5a215ad541030998176a0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 3 Sep 2021 16:08:02 -0400 Subject: [PATCH 2/4] chore: reformat tests --- packages/babel-types/test/validators.js | 40 ++++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js index 1c7aee901a22..e7cbbd4d036a 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); + 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(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 () { From ee7a9e8a0906560ac84bbaf656625ecd5828a1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 3 Sep 2021 16:17:28 -0400 Subject: [PATCH 3/4] fix: id in import attributes should not be referenced isReferenced should return false for identifiers used in import attributes --- packages/babel-types/src/validators/isReferenced.ts | 4 ++++ packages/babel-types/test/validators.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/packages/babel-types/src/validators/isReferenced.ts b/packages/babel-types/src/validators/isReferenced.ts index 7d2fb5e0a3d3..5595920e44d9 100644 --- a/packages/babel-types/src/validators/isReferenced.ts +++ b/packages/babel-types/src/validators/isReferenced.ts @@ -139,6 +139,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 e7cbbd4d036a..30f600c1b4bd 100644 --- a/packages/babel-types/test/validators.js +++ b/packages/babel-types/test/validators.js @@ -287,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 () { From a9335f3282f274e745a67b2b3f4a803ad1cef58d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 6 Sep 2021 10:13:45 -0400 Subject: [PATCH 4/4] review comments --- packages/babel-types/src/validators/isReferenced.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/babel-types/src/validators/isReferenced.ts b/packages/babel-types/src/validators/isReferenced.ts index 5595920e44d9..46c9cce7f6c3 100644 --- a/packages/babel-types/src/validators/isReferenced.ts +++ b/packages/babel-types/src/validators/isReferenced.ts @@ -57,10 +57,8 @@ export default function isReferenced( if (parent.key === node) { return !!parent.computed; } - if (parent.value === node) { - return !grandparent || grandparent.type !== "ObjectPattern"; - } - return true; + // parent.value === node + return !grandparent || grandparent.type !== "ObjectPattern"; // no: class { NODE = value; } // yes: class { [NODE] = value; } // yes: class { key = NODE; } @@ -70,10 +68,7 @@ export default function isReferenced( } return true; case "ClassPrivateProperty": - if (parent.key === node) { - return false; - } - return true; + return parent.key !== node; // no: class NODE {} // yes: class Foo extends NODE {}