Skip to content
Permalink
Browse files

Update: Change no-duplicate-case to comparing tokens (fixes #13485) (#…

…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 e71e2980cd2e319afc70d8c859c7ffd59cf4157b
Showing with 83 additions and 4 deletions.
  1. +23 −4 lib/rules/no-duplicate-case.js
  2. +60 −0 tests/lib/rules/no-duplicate-case.js
@@ -6,6 +6,12 @@

"use strict";

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

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
@@ -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);
}
}
}
@@ -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.
You can’t perform that action at this time.