Skip to content

Commit

Permalink
Breaking: no-dupe-class-members checks some computed keys (fixes #12808)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jan 28, 2020
1 parent 533c114 commit 3ec34e4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/rules/no-dupe-class-members.js
Expand Up @@ -74,11 +74,12 @@ module.exports = {

// Reports the node if its name has been declared already.
MethodDefinition(node) {
if (node.computed) {
const name = astUtils.getStaticPropertyName(node);

if (name === null) {
return;
}

const name = astUtils.getStaticPropertyName(node) || "";
const state = getState(name, node.static);
let isDuplicate = false;

Expand Down
101 changes: 100 additions & 1 deletion tests/lib/rules/no-dupe-class-members.js
Expand Up @@ -29,7 +29,21 @@ ruleTester.run("no-dupe-class-members", rule, {
"class A { 'foo'() {} 'bar'() {} baz() {} }",
"class A { *'foo'() {} *'bar'() {} *baz() {} }",
"class A { get 'foo'() {} get 'bar'() {} get baz() {} }",
"class A { 1() {} 2() {} }"
"class A { 1() {} 2() {} }",
"class A { ['foo']() {} ['bar']() {} }",
"class A { [`foo`]() {} [`bar`]() {} }",
"class A { [12]() {} [123]() {} }",
"class A { [1.0]() {} ['1.0']() {} }",
"class A { [0x1]() {} [`0x1`]() {} }",
"class A { [null]() {} ['']() {} }",

// not assumed to be statically-known values
"class A { ['foo' + '']() {} ['foo']() {} }",
"class A { [`foo${''}`]() {} [`foo`]() {} }",
"class A { [-1]() {} ['-1']() {} }",

// not supported by this rule
"class A { [foo]() {} [foo]() {} }"
],
invalid: [
{
Expand All @@ -56,6 +70,91 @@ ruleTester.run("no-dupe-class-members", rule, {
{ type: "MethodDefinition", line: 1, column: 19, messageId: "unexpected", data: { name: "10" } }
]
},
{
code: "class A { ['foo']() {} ['foo']() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 24, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { static ['foo']() {} static foo() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 31, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { ''() {} ['']() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 19, messageId: "unexpected", data: { name: "" } }
]
},
{
code: "class A { [`foo`]() {} [`foo`]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 24, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { [`foo`]() {} ['foo']() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 24, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { foo() {} [`foo`]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 20, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { static 'foo'() {} static [`foo`]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 29, messageId: "unexpected", data: { name: "foo" } }
]
},
{
code: "class A { [123]() {} [123]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 22, messageId: "unexpected", data: { name: "123" } }
]
},
{
code: "class A { [0x10]() {} 16() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 23, messageId: "unexpected", data: { name: "16" } }
]
},
{
code: "class A { [100]() {} [1e2]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 22, messageId: "unexpected", data: { name: "100" } }
]
},
{
code: "class A { [123.00]() {} [`123`]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 25, messageId: "unexpected", data: { name: "123" } }
]
},
{
code: "class A { static '65'() {} static [0o101]() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 28, messageId: "unexpected", data: { name: "65" } }
]
},
{
code: "class A { [123n]() {} 123() {} }",
parserOptions: { ecmaVersion: 2020 },
errors: [
{ type: "MethodDefinition", line: 1, column: 23, messageId: "unexpected", data: { name: "123" } }
]
},
{
code: "class A { [null]() {} 'null'() {} }",
errors: [
{ type: "MethodDefinition", line: 1, column: 23, messageId: "unexpected", data: { name: "null" } }
]
},
{
code: "class A { foo() {} foo() {} foo() {} }",
errors: [
Expand Down

0 comments on commit 3ec34e4

Please sign in to comment.