From f8506c592769578d1ed899ee217e9b222680b53c Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Wed, 5 Feb 2020 15:59:16 -0800 Subject: [PATCH] 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 * 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 --- .../src/validators/isReferenced.js | 16 +++-- packages/babel-types/test/validators.js | 68 +++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/packages/babel-types/src/validators/isReferenced.js b/packages/babel-types/src/validators/isReferenced.js index 699b291a01d6..847738fa4c43 100644 --- a/packages/babel-types/src/validators/isReferenced.js +++ b/packages/babel-types/src/validators/isReferenced.js @@ -45,6 +45,17 @@ export default function isReferenced( case "PrivateName": 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]: "" } // no: { NODE: "" } // depends: { NODE } @@ -55,11 +66,6 @@ export default function isReferenced( // yes: class { key = NODE; } case "ClassProperty": case "ClassPrivateProperty": - // no: class { NODE() {} } - // yes: class { [NODE]() {} } - case "ClassMethod": - case "ClassPrivateMethod": - case "ObjectMethod": if (parent.key === node) { return !!parent.computed; } diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js index f08dedab1a87..b8ff2e7d8b04 100644 --- a/packages/babel-types/test/validators.js +++ b/packages/babel-types/test/validators.js @@ -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() {