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
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;