/
no-duplicate-case.js
133 lines (127 loc) · 5.24 KB
/
no-duplicate-case.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @fileoverview Tests for no-duplicate-case rule.
* @author Dieter Oberkofler
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-duplicate-case"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-duplicate-case", rule, {
valid: [
"var a = 1; switch (a) {case 1: break; case 2: break; default: break;}",
"var a = 1; switch (a) {case 1: break; case '1': break; default: break;}",
"var a = 1; switch (a) {case 1: break; case true: break; default: break;}",
"var a = 1; switch (a) {default: break;}",
"var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p.p.p2: break; default: break;}",
"var a = 1, f = function(b) { return b ? { p1: 1 } : { p1: 2 }; }; switch (a) {case f(true).p1: break; case f(true, false).p1: break; default: break;}",
"var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a + 1).p1: break; case f(a + 2).p1: break; default: break;}",
"var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a == 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
"var a = 1, f1 = function() { return { p1: 1 } }, f2 = function() { return { p1: 2 } }; switch (a) {case f1().p1: break; case f2().p1: break; default: break;}",
"var a = [1,2]; switch(a.toString()){case ([1,2]).toString():break; case ([1]).toString():break; default:break;}",
"switch(a) { case a: break; } switch(a) { case a: break; }",
"switch(a) { case toString: break; }"
],
invalid: [
{
code: "var a = 1; switch (a) {case 1: break; case 1: break; case 2: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 39
}]
},
{
code: "var a = '1'; switch (a) {case '1': break; case '1': break; case '2': break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 43
}]
},
{
code: "var a = 1, one = 1; switch (a) {case one: break; case one: break; case 2: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 50
}]
},
{
code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p.p.p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 69
}]
},
{
code: "var a = 1, f = function(b) { return b ? { p1: 1 } : { p1: 2 }; }; switch (a) {case f(true).p1: break; case f(true).p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 103
}]
},
{
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(a === 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 97
}]
},
{
code: "var a = 1, f1 = function() { return { p1: 1 } }; switch (a) {case f1().p1: break; case f1().p1: break; default: break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 83
}]
},
{
code: "var a = [1, 2]; switch(a.toString()){case ([1, 2]).toString():break; case ([1, 2]).toString():break; default:break;}",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 70
}]
},
{
code: "switch (a) { case a: case a: }",
errors: [{
messageId: "unexpected",
type: "SwitchCase",
column: 22
}]
},
{
code: "switch (a) { case a: break; case b: break; case a: break; case c: break; case a: break; }",
errors: [
{
messageId: "unexpected",
type: "SwitchCase",
column: 44
},
{
messageId: "unexpected",
type: "SwitchCase",
column: 74
}
]
}
]
});