1 | /**
|
2 | * @fileoverview Rule to disallow a duplicate case label.
|
3 | * @author Dieter Oberkofler
|
4 | * @author Burak Yigit Kaya
|
5 | */
|
6 |
|
7 | ;
|
8 |
|
9 | //------------------------------------------------------------------------------
|
10 | // Rule Definition
|
11 | //------------------------------------------------------------------------------
|
12 |
|
13 | module.exports = {
|
14 | meta: {
|
15 | docs: {
|
16 | description: "disallow duplicate case labels",
|
17 | category: "Possible Errors",
|
18 | recommended: true
|
19 | },
|
20 |
|
21 | schema: []
|
22 | },
|
23 |
|
24 | create(context) {
|
25 | const sourceCode = context.getSourceCode();
|
26 |
|
27 | return {
|
28 | SwitchStatement(node) {
|
29 | const mapping = {};
|
30 |
|
31 | node.cases.forEach(function(switchCase) {
|
32 | const key = sourceCode.getText(switchCase.test);
|
33 |
|
34 | if (mapping[key]) {
|
35 | context.report(switchCase, "Duplicate case label.");
|
36 | } else {
|
37 | mapping[key] = switchCase;
|
38 | }
|
39 | });
|
40 | }
|
41 | };
|
42 | }
|
43 | };
|