Skip to content

Commit

Permalink
Update: Change no-duplicate-case to comparing tokens (fixes #13485) (#…
Browse files Browse the repository at this point in the history
…13494)

Change `no-duplicate-case` rule to change from detecting duplicates comparing text to detecting duplicates comparing tokens, like `no-dupe-else-if` rule.
  • Loading branch information
ota-meshi committed Jul 22, 2020
1 parent 6c4aea4 commit e71e298
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/rules/no-duplicate-case.js
Expand Up @@ -6,6 +6,12 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -31,18 +37,31 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

/**
* Determines whether the two given nodes are considered to be equal.
* @param {ASTNode} a First node.
* @param {ASTNode} b Second node.
* @returns {boolean} `true` if the nodes are considered to be equal.
*/
function equal(a, b) {
if (a.type !== b.type) {
return false;
}

return astUtils.equalTokens(a, b, sourceCode);
}
return {
SwitchStatement(node) {
const previousKeys = new Set();
const previousTests = [];

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

if (previousKeys.has(key)) {
if (previousTests.some(previousTest => equal(previousTest, test))) {
context.report({ node: switchCase, messageId: "unexpected" });
} else {
previousKeys.add(key);
previousTests.push(test);
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/lib/rules/no-duplicate-case.js
Expand Up @@ -128,6 +128,66 @@ ruleTester.run("no-duplicate-case", rule, {
column: 74
}
]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p. p // comment\n .p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 69
}]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p .p\n/* comment */\n.p1: break; case p.p.p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
line: 3,
column: 13
}]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p .p\n/* comment */\n.p1: break; case p. p // comment\n .p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
line: 3,
column: 13
}]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p. p // comment\n .p1: break; case p .p\n/* comment */\n.p1: break; default: break;}",
errors: [
{
messageId: "unexpected",
type: "SwitchCase",
line: 1,
column: 69
},
{
messageId: "unexpected",
type: "SwitchCase",
line: 2,
column: 14
}
]
},
{
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",
column: 87
}]
},
{
code: "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(\na + 1 // comment\n).p1: break; case f(a+1)\n.p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
line: 3,
column: 14
}]
}
]
});

0 comments on commit e71e298

Please sign in to comment.