Skip to content

Commit

Permalink
Fix: no-duplicate-case false positives on Object.prototype keys (#12107)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Aug 18, 2019
1 parent fe631af commit 9a043ff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
18 changes: 10 additions & 8 deletions lib/rules/no-duplicate-case.js
Expand Up @@ -33,17 +33,19 @@ module.exports = {

return {
SwitchStatement(node) {
const mapping = {};
const previousKeys = new Set();

node.cases.forEach(switchCase => {
const key = sourceCode.getText(switchCase.test);
for (const switchCase of node.cases) {
if (switchCase.test) {
const key = sourceCode.getText(switchCase.test);

if (mapping[key]) {
context.report({ node: switchCase, messageId: "unexpected" });
} else {
mapping[key] = switchCase;
if (previousKeys.has(key)) {
context.report({ node: switchCase, messageId: "unexpected" });
} else {
previousKeys.add(key);
}
}
});
}
}
};
}
Expand Down
54 changes: 44 additions & 10 deletions tests/lib/rules/no-duplicate-case.js
Expand Up @@ -29,71 +29,105 @@ ruleTester.run("no-duplicate-case", rule, {
"var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a + 1).p1: break; case f(a + 2).p1: break; default: break;}",
"var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a == 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
"var a = 1, f1 = function() { return { p1: 1 } }, f2 = function() { return { p1: 2 } }; switch (a) {case f1().p1: break; case f2().p1: break; default: break;}",
"var a = [1,2]; switch(a.toString()){case ([1,2]).toString():break; case ([1]).toString():break; default:break;}"
"var a = [1,2]; switch(a.toString()){case ([1,2]).toString():break; case ([1]).toString():break; default:break;}",
"switch(a) { case a: break; } switch(a) { case a: break; }",
"switch(a) { case toString: break; }"
],
invalid: [
{
code: "var a = 1; switch (a) {case 1: break; case 1: break; case 2: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 39
}]
},
{
code: "var a = '1'; switch (a) {case '1': break; case '1': break; case '2': break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 43
}]
},
{
code: "var a = 1, one = 1; switch (a) {case one: break; case one: break; case 2: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 50
}]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p.p.p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 69
}]
},
{
code: "var a = 1, f = function(b) { return b ? { p1: 1 } : { p1: 2 }; }; switch (a) {case f(true).p1: break; case f(true).p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 103
}]
},
{
code: "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a + 1).p1: break; case f(a + 1).p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 87
}]
},
{
code: "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a === 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 97
}]
},
{
code: "var a = 1, f1 = function() { return { p1: 1 } }; switch (a) {case f1().p1: break; case f1().p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 83
}]
},
{
code: "var a = [1, 2]; switch(a.toString()){case ([1, 2]).toString():break; case ([1, 2]).toString():break; default:break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase"
type: "SwitchCase",
column: 70
}]
},
{
code: "switch (a) { case a: case a: }",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 22
}]
},
{
code: "switch (a) { case a: break; case b: break; case a: break; case c: break; case a: break; }",
errors: [
{
messageId: "unexpected",
type: "SwitchCase",
column: 44
},
{
messageId: "unexpected",
type: "SwitchCase",
column: 74
}
]
}
]
});

0 comments on commit 9a043ff

Please sign in to comment.