katoikia-app/web-ui/web-react/node_modules/eslint/lib/rules/no-duplicate-case.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-07-06 04:15:11 +00:00
/**
* @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);
}
}
}
}
};
}
};