Make isReferenced return false for method parameter name (#11089)
* Change isReferenced to return false for object/class method parameter names. * use indexOf instead of for-of loop * replace `.indexOf` check with `.includes` and assume `parent.params` exists Co-Authored-By: Justin Ridgewell <justin@ridgewell.name> * check .params within case block for ClassMethod/ClassPrivateMethod/ObjectMethod only * add comment clarifying that case clause fall-through is intentional Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
This commit is contained in:
parent
0ae61f060a
commit
761563571c
@ -45,6 +45,17 @@ export default function isReferenced(
|
|||||||
case "PrivateName":
|
case "PrivateName":
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// no: class { NODE() {} }
|
||||||
|
// yes: class { [NODE]() {} }
|
||||||
|
// no: class { foo(NODE) {} }
|
||||||
|
case "ClassMethod":
|
||||||
|
case "ClassPrivateMethod":
|
||||||
|
case "ObjectMethod":
|
||||||
|
if (parent.params.includes(node)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Fall-through to next case clause to check whether the node is the method's name.
|
||||||
|
|
||||||
// yes: { [NODE]: "" }
|
// yes: { [NODE]: "" }
|
||||||
// no: { NODE: "" }
|
// no: { NODE: "" }
|
||||||
// depends: { NODE }
|
// depends: { NODE }
|
||||||
@ -55,11 +66,6 @@ export default function isReferenced(
|
|||||||
// yes: class { key = NODE; }
|
// yes: class { key = NODE; }
|
||||||
case "ClassProperty":
|
case "ClassProperty":
|
||||||
case "ClassPrivateProperty":
|
case "ClassPrivateProperty":
|
||||||
// no: class { NODE() {} }
|
|
||||||
// yes: class { [NODE]() {} }
|
|
||||||
case "ClassMethod":
|
|
||||||
case "ClassPrivateMethod":
|
|
||||||
case "ObjectMethod":
|
|
||||||
if (parent.key === node) {
|
if (parent.key === node) {
|
||||||
return !!parent.computed;
|
return !!parent.computed;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -179,6 +179,74 @@ describe("validators", function() {
|
|||||||
expect(t.isReferenced(node, parent)).toBe(true);
|
expect(t.isReferenced(node, parent)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("ObjectMethod", function() {
|
||||||
|
it("returns false if node is method key", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.objectMethod("method", node, [], t.blockStatement([]));
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns true if node is computed method key", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.objectMethod(
|
||||||
|
"method",
|
||||||
|
node,
|
||||||
|
[],
|
||||||
|
t.blockStatement([]),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false if node is method param", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.objectMethod(
|
||||||
|
"method",
|
||||||
|
t.identifier("foo"),
|
||||||
|
[node],
|
||||||
|
t.blockStatement([]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ClassMethod", function() {
|
||||||
|
it("returns false if node is method key", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.classMethod("method", node, [], t.blockStatement([]));
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns true if node is computed method key", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.classMethod(
|
||||||
|
"method",
|
||||||
|
node,
|
||||||
|
[],
|
||||||
|
t.blockStatement([]),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false if node is method param", function() {
|
||||||
|
const node = t.identifier("A");
|
||||||
|
const parent = t.classMethod(
|
||||||
|
"method",
|
||||||
|
t.identifier("foo"),
|
||||||
|
[node],
|
||||||
|
t.blockStatement([]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(t.isReferenced(node, parent)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("isBinding", function() {
|
describe("isBinding", function() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user