Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

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
}]
}
]
});