/
prefer-numeric-literals.js
255 lines (247 loc) · 9.21 KB
/
prefer-numeric-literals.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @fileoverview Tests for prefer-numeric-literals rule.
* @author Annie Zhang
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/prefer-numeric-literals"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("prefer-numeric-literals", rule, {
valid: [
"parseInt(1);",
"parseInt(1, 3);",
"Number.parseInt(1);",
"Number.parseInt(1, 3);",
"0b111110111 === 503;",
"0o767 === 503;",
"0x1F7 === 503;",
"a[parseInt](1,2);",
"parseInt(foo);",
"parseInt(foo, 2);",
"Number.parseInt(foo);",
"Number.parseInt(foo, 2);"
],
invalid: [
{
code: "parseInt(\"111110111\", 2) === 503;",
output: "0b111110111 === 503;",
errors: [{ message: "Use binary literals instead of parseInt()." }]
}, {
code: "parseInt(\"767\", 8) === 503;",
output: "0o767 === 503;",
errors: [{ message: "Use octal literals instead of parseInt()." }]
}, {
code: "parseInt(\"1F7\", 16) === 255;",
output: "0x1F7 === 255;",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
}, {
code: "Number.parseInt(\"111110111\", 2) === 503;",
output: "0b111110111 === 503;",
errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
}, {
code: "Number.parseInt(\"767\", 8) === 503;",
output: "0o767 === 503;",
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
}, {
code: "Number.parseInt(\"1F7\", 16) === 255;",
output: "0x1F7 === 255;",
errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
}, {
code: "parseInt('7999', 8);",
output: null, // not fixed, unexpected 9 in parseInt string
errors: [{ message: "Use octal literals instead of parseInt()." }]
}, {
code: "parseInt('1234', 2);",
output: null, // not fixed, invalid binary string
errors: [{ message: "Use binary literals instead of parseInt()." }]
}, {
code: "parseInt('1234.5', 8);",
output: null, // not fixed, this isn't an integer
errors: [{ message: "Use octal literals instead of parseInt()." }]
}, {
code: "parseInt('1️⃣3️⃣3️⃣7️⃣', 16);",
output: null, // not fixed, javascript doesn't support emoji literals
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
}, {
code: "Number.parseInt('7999', 8);",
output: null, // not fixed, unexpected 9 in parseInt string
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
}, {
code: "Number.parseInt('1234', 2);",
output: null, // not fixed, invalid binary string
errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
}, {
code: "Number.parseInt('1234.5', 8);",
output: null, // not fixed, this isn't an integer
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
}, {
code: "Number.parseInt('1️⃣3️⃣3️⃣7️⃣', 16);",
output: null, // not fixed, javascript doesn't support emoji literals
errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
},
// Adjacent tokens tests
{
code: "parseInt('11', 2)",
output: "0b11",
errors: [{ message: "Use binary literals instead of parseInt()." }]
},
{
code: "Number.parseInt('67', 8)",
output: "0o67",
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "5+parseInt('A', 16)",
output: "5+0xA",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "function *f(){ yield(Number).parseInt('11', 2) }",
output: "function *f(){ yield 0b11 }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use binary literals instead of (Number).parseInt()." }]
},
{
code: "function *f(){ yield(Number.parseInt)('67', 8) }",
output: "function *f(){ yield 0o67 }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "function *f(){ yield(parseInt)('A', 16) }",
output: "function *f(){ yield 0xA }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "function *f(){ yield Number.parseInt('11', 2) }",
output: "function *f(){ yield 0b11 }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
},
{
code: "function *f(){ yield/**/Number.parseInt('67', 8) }",
output: "function *f(){ yield/**/0o67 }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "function *f(){ yield(parseInt('A', 16)) }",
output: "function *f(){ yield(0xA) }",
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "parseInt('11', 2)+5",
output: "0b11+5",
errors: [{ message: "Use binary literals instead of parseInt()." }]
},
{
code: "Number.parseInt('17', 8)+5",
output: "0o17+5",
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "parseInt('A', 16)+5",
output: "0xA+5",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "parseInt('11', 2)in foo",
output: "0b11 in foo",
errors: [{ message: "Use binary literals instead of parseInt()." }]
},
{
code: "Number.parseInt('17', 8)in foo",
output: "0o17 in foo",
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "parseInt('A', 16)in foo",
output: "0xA in foo",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
{
code: "parseInt('11', 2) in foo",
output: "0b11 in foo",
errors: [{ message: "Use binary literals instead of parseInt()." }]
},
{
code: "Number.parseInt('17', 8)/**/in foo",
output: "0o17/**/in foo",
errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
},
{
code: "(parseInt('A', 16))in foo",
output: "(0xA)in foo",
errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
},
// Should not autofix if it would remove comments
{
code: "/* comment */Number.parseInt('11', 2);",
output: "/* comment */0b11;",
errors: 1
},
{
code: "Number/**/.parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number//\n.parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number./**/parseInt('11', 2);",
output: null,
errors: 1
},
{
code: "Number.parseInt(/**/'11', 2);",
output: null,
errors: 1
},
{
code: "Number.parseInt('11', /**/2);",
output: null,
errors: 1
},
{
code: "Number.parseInt('11', 2)/* comment */;",
output: "0b11/* comment */;",
errors: 1
},
{
code: "parseInt/**/('11', 2);",
output: null,
errors: 1
},
{
code: "parseInt(//\n'11', 2);",
output: null,
errors: 1
},
{
code: "parseInt('11'/**/, 2);",
output: null,
errors: 1
},
{
code: "parseInt('11', 2 /**/);",
output: null,
errors: 1
},
{
code: "parseInt('11', 2)//comment\n;",
output: "0b11//comment\n;",
errors: 1
}
]
});