-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
eqeqeq.js
175 lines (164 loc) · 10.2 KB
/
eqeqeq.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @fileoverview Tests for eqeqeq rule.
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/eqeqeq"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
const wantedEqEqEq = { expectedOperator: "===", actualOperator: "==" };
const wantedNotEqEq = { expectedOperator: "!==", actualOperator: "!=" };
const wantedEqEq = { expectedOperator: "==", actualOperator: "===" };
const wantedNotEq = { expectedOperator: "!=", actualOperator: "!==" };
ruleTester.run("eqeqeq", rule, {
valid: [
"a === b",
"a !== b",
{ code: "a === b", options: ["always"] },
{ code: "typeof a == 'number'", options: ["smart"] },
{ code: "'string' != typeof a", options: ["smart"] },
{ code: "'hello' != 'world'", options: ["smart"] },
{ code: "2 == 3", options: ["smart"] },
{ code: "true == true", options: ["smart"] },
{ code: "null == a", options: ["smart"] },
{ code: "a == null", options: ["smart"] },
{ code: "null == a", options: ["allow-null"] },
{ code: "a == null", options: ["allow-null"] },
{ code: "a == null", options: ["always", { null: "ignore" }] },
{ code: "a != null", options: ["always", { null: "ignore" }] },
{ code: "a !== null", options: ["always", { null: "ignore" }] },
{ code: "a === null", options: ["always", { null: "always" }] },
{ code: "a !== null", options: ["always", { null: "always" }] },
{ code: "null === null", options: ["always", { null: "always" }] },
{ code: "null !== null", options: ["always", { null: "always" }] },
{ code: "a == null", options: ["always", { null: "never" }] },
{ code: "a != null", options: ["always", { null: "never" }] },
{ code: "null == null", options: ["always", { null: "never" }] },
{ code: "null != null", options: ["always", { null: "never" }] },
// https://github.com/eslint/eslint/issues/8020
{ code: "foo === /abc/u", options: ["always", { null: "never" }], parserOptions: { ecmaVersion: 2015 } },
// bigint
{ code: "foo === 1n", options: ["always", { null: "never" }], parserOptions: { ecmaVersion: 2020 } }
],
invalid: [
{ code: "a == b", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "a != b", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "typeof a == 'number'", output: "typeof a === 'number'", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "typeof a == 'number'", output: "typeof a === 'number'", options: ["always"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "'string' != typeof a", output: "'string' !== typeof a", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "true == true", output: "true === true", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "2 == 3", output: "2 === 3", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "2 == 3", output: "2 === 3", options: ["always"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "'hello' != 'world'", output: "'hello' !== 'world'", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "'hello' != 'world'", output: "'hello' !== 'world'", options: ["always"], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "a == null", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "a == null", options: ["always"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "null != a", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "true == 1", options: ["smart"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "0 != '1'", options: ["smart"], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "'wee' == /wee/", options: ["smart"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "typeof a == 'number'", output: "typeof a === 'number'", options: ["allow-null"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "'string' != typeof a", output: "'string' !== typeof a", options: ["allow-null"], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "'hello' != 'world'", output: "'hello' !== 'world'", options: ["allow-null"], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "2 == 3", output: "2 === 3", options: ["allow-null"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "true == true", output: "true === true", options: ["allow-null"], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "true == null", options: ["always", { null: "always" }], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "true != null", options: ["always", { null: "always" }], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "null == null", output: "null === null", options: ["always", { null: "always" }], errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression" }] },
{ code: "null != null", output: "null !== null", options: ["always", { null: "always" }], errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression" }] },
{ code: "true === null", options: ["always", { null: "never" }], errors: [{ messageId: "unexpected", data: wantedEqEq, type: "BinaryExpression" }] },
{ code: "true !== null", options: ["always", { null: "never" }], errors: [{ messageId: "unexpected", data: wantedNotEq, type: "BinaryExpression" }] },
{ code: "null === null", output: "null == null", options: ["always", { null: "never" }], errors: [{ messageId: "unexpected", data: wantedEqEq, type: "BinaryExpression" }] },
{ code: "null !== null", output: "null != null", options: ["always", { null: "never" }], errors: [{ messageId: "unexpected", data: wantedNotEq, type: "BinaryExpression" }] },
{ code: "a\n==\nb", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 2 }] },
{ code: "(a) == b", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 1 }] },
{ code: "(a) != b", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 }] },
{ code: "a == (b)", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 1 }] },
{ code: "a != (b)", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 }] },
{ code: "(a) == (b)", errors: [{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 1 }] },
{ code: "(a) != (b)", errors: [{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 }] },
{
code: "(a == b) == (c)",
errors: [
{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 1 },
{ messageId: "unexpected", data: wantedEqEqEq, type: "BinaryExpression", line: 1 }
]
},
{
code: "(a != b) != (c)",
errors: [
{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 },
{ messageId: "unexpected", data: wantedNotEqEq, type: "BinaryExpression", line: 1 }
]
},
// location tests
{
code: "a == b;",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 3,
endColumn: 5
}
]
},
{
code: "a!=b;",
errors: [
{
messageId: "unexpected",
data: wantedNotEqEq,
type: "BinaryExpression",
column: 2,
endColumn: 4
}
]
},
{
code: "(a + b) == c;",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 9,
endColumn: 11
}
]
},
{
code: "(a + b) != c;",
errors: [
{
messageId: "unexpected",
data: wantedNotEqEq,
type: "BinaryExpression",
column: 10,
endColumn: 12
}
]
},
{
code: "((1) ) == (2);",
output: "((1) ) === (2);",
errors: [
{
messageId: "unexpected",
data: wantedEqEqEq,
type: "BinaryExpression",
column: 9,
endColumn: 11
}
]
}
// If no output is provided, assert that no output is produced.
].map(invalidCase => Object.assign({ output: null }, invalidCase))
});