Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make isReferenced return false for method parameter name #11089

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/babel-types/src/validators/isReferenced.js
Expand Up @@ -57,6 +57,7 @@ export default function isReferenced(
case "ClassPrivateProperty":
// no: class { NODE() {} }
// yes: class { [NODE]() {} }
// no: class { foo(NODE) {} }
case "ClassMethod":
case "ClassPrivateMethod":
case "ObjectMethod":
Expand All @@ -66,6 +67,13 @@ export default function isReferenced(
if (parent.value === node) {
return !grandparent || grandparent.type !== "ObjectPattern";
}
if (parent.params) {
for (const param of parent.params) {
if (param === node) {
return false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using parent.params.indexOf(node) !== -1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, that's much better. Updated.

}
return true;

// no: class NODE {}
Expand Down
68 changes: 68 additions & 0 deletions packages/babel-types/test/validators.js
Expand Up @@ -179,6 +179,74 @@ describe("validators", function() {
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() {
Expand Down