id in import attributes should not be referenced (#13733)

This commit is contained in:
Huáng Jùnliàng 2021-09-07 08:29:15 -04:00 committed by GitHub
parent 1ebc5b7f7b
commit d87a3d9e13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 25 deletions

View File

@ -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: <div NODE="foo" />
case "JSXAttribute":
return false;

View File

@ -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 () {
@ -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 () {