You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
1.4 KiB

/**
* @fileoverview Rule to disallow a duplicate case label.
* @author Dieter Oberkofler
* @author Burak Yigit Kaya
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow duplicate case labels",
category: "Possible Errors",
recommended: true,
url: "https://eslint.org/docs/rules/no-duplicate-case"
},
schema: [],
messages: {
unexpected: "Duplicate case label."
}
},
create(context) {
const sourceCode = context.getSourceCode();
return {
SwitchStatement(node) {
const previousKeys = new Set();
for (const switchCase of node.cases) {
if (switchCase.test) {
const key = sourceCode.getText(switchCase.test);
if (previousKeys.has(key)) {
context.report({ node: switchCase, messageId: "unexpected" });
} else {
previousKeys.add(key);
}
}
}
}
};
}
};