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 2 commits
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
4 changes: 4 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,9 @@ export default function isReferenced(
if (parent.value === node) {
return !grandparent || grandparent.type !== "ObjectPattern";
}
if (parent.params && parent.params.indexOf(node) !== -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (parent.params && parent.params.indexOf(node) !== -1) {
if (parent.params.includes(node)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolo-ribaudo you asked me to remove the use of .includes for the sake of Node 6 support, but now that I check, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes indicates it's supported there. Is using .includes ok?

Copy link
Member

Choose a reason for hiding this comment

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

Oh I thought that it wasn't 😅

If it works, it's ok to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I checked a node 6 repl and .includes seems to work.

@jridgewell removing the check for whether parent.params is present before calling .includes, does mean an error would be thrown if we reach this line when parent is an ObjectProperty or ClassProperty. The only way that could happen is if node is neither the key nor the value of the parent Object/ClassProperty, which I think currently could only happen if it's a decorator or type annotation. I don't know whether it's important for isReferenced to be able to handle being called with non-identifier-like nodes such as Decorator.

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 refactoring the switch like this then?

case "ClassMethod":
case "ClassPrivateMethod":
case "ObjectMethod":
  check parent.params.includes(node);
  /* falls through */
case "ObjectProperty":
case "ClassProperty":
case "ClassPrivateProperty":
  // all the existing checks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That works, updated.

I'm now getting some failing tests in babel-preset-env related to "edge" being 18 instead of the fixture's expectation of 17, but I get them on master too - I'm guessing it was caused by a new version of caniuse-lite being released today.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah ignore those tests

return false;
}
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