diff --git a/lib/rules/no-duplicate-case.js b/lib/rules/no-duplicate-case.js index c8a0fa9da3c..e2d9665e7f5 100644 --- a/lib/rules/no-duplicate-case.js +++ b/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); } } } diff --git a/tests/lib/rules/no-duplicate-case.js b/tests/lib/rules/no-duplicate-case.js index a6e8e0ff34c..42dbda3f18d 100644 --- a/tests/lib/rules/no-duplicate-case.js +++ b/tests/lib/rules/no-duplicate-case.js @@ -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 + }] } ] });