Skip to content
Permalink
Browse files

Fix: `no-dupe-class-members` false positive (fixes #4981)

  • Loading branch information...
mysticatea committed Jan 17, 2016
1 parent 1d517fa commit 4173baa088e5fe02c5f242469868333134da5a4d
Showing with 36 additions and 2 deletions.
  1. +17 −1 lib/rules/no-dupe-class-members.js
  2. +19 −1 tests/lib/rules/no-dupe-class-members.js
@@ -36,6 +36,22 @@ module.exports = function(context) {
return stateMap[key][isStatic ? "static" : "nonStatic"];
}

/**
* Gets the name text of a given node.
*
* @param {ASTNode} node - A node to get the name.
* @returns {string} The name text of the node.
*/
function getName(node) {
switch (node.type) {
case "Identifier": return node.name;
case "Literal": return String(node.value);

/* istanbul ignore next: syntax error */
default: return "";
}
}

return {
// Initializes the stack of state of member declarations.
"Program": function() {
@@ -58,7 +74,7 @@ module.exports = function(context) {
return;
}

var name = node.key.name;
var name = getName(node.key);
var state = getState(name, node.static);
var isDuplicate = false;
if (node.kind === "get") {
@@ -25,7 +25,11 @@ ruleTester.run("no-dupe-class-members", rule, {
{code: "class A { get foo() {} set foo(value) {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { static foo() {} get foo() {} set foo(value) {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { foo() { } } class B { foo() { } }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { [foo]() {} foo() {} }", parserOptions: { ecmaVersion: 6 }}
{code: "class A { [foo]() {} foo() {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { 'foo'() {} 'bar'() {} baz() {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { *'foo'() {} *'bar'() {} *baz() {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { get 'foo'() {} get 'bar'() {} get baz() {} }", parserOptions: { ecmaVersion: 6 }},
{code: "class A { 1() {} 2() {} }", parserOptions: { ecmaVersion: 6 }}
],
invalid: [
{
@@ -42,6 +46,20 @@ ruleTester.run("no-dupe-class-members", rule, {
{type: "MethodDefinition", line: 1, column: 21, message: "Duplicate name 'foo'."}
]
},
{
code: "class A { 'foo'() {} 'foo'() {} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{type: "MethodDefinition", line: 1, column: 22, message: "Duplicate name 'foo'."}
]
},
{
code: "class A { 10() {} 1e1() {} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{type: "MethodDefinition", line: 1, column: 19, message: "Duplicate name '10'."}
]
},
{
code: "class A { foo() {} foo() {} foo() {} }",
parserOptions: { ecmaVersion: 6 },

0 comments on commit 4173baa

Please sign in to comment.
You can’t perform that action at this time.