]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-numeric-literals.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / prefer-numeric-literals.js
1 /**
2 * @fileoverview Tests for prefer-numeric-literals rule.
3 * @author Annie Zhang
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-numeric-literals"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
20
21 ruleTester.run("prefer-numeric-literals", rule, {
22 valid: [
23 "parseInt(1);",
24 "parseInt(1, 3);",
25 "Number.parseInt(1);",
26 "Number.parseInt(1, 3);",
27 "0b111110111 === 503;",
28 "0o767 === 503;",
29 "0x1F7 === 503;",
30 "a[parseInt](1,2);",
31 "parseInt(foo);",
32 "parseInt(foo, 2);",
33 "Number.parseInt(foo);",
34 "Number.parseInt(foo, 2);",
35 "parseInt(11, 2);",
36 "Number.parseInt(1, 8);",
37 "parseInt(1e5, 16);",
38 "parseInt('11', '2');",
39 "Number.parseInt('11', '8');",
40 "parseInt(/foo/, 2);",
41 "parseInt(`11${foo}`, 2);",
42 {
43 code: "parseInt('11', 2n);",
44 parserOptions: { ecmaVersion: 2020 }
45 },
46 {
47 code: "Number.parseInt('11', 8n);",
48 parserOptions: { ecmaVersion: 2020 }
49 },
50 {
51 code: "parseInt('11', 16n);",
52 parserOptions: { ecmaVersion: 2020 }
53 },
54 {
55 code: "parseInt(`11`, 16n);",
56 parserOptions: { ecmaVersion: 2020 }
57 },
58 {
59 code: "parseInt(1n, 2);",
60 parserOptions: { ecmaVersion: 2020 }
61 }
62 ],
63 invalid: [
64 {
65 code: "parseInt(\"111110111\", 2) === 503;",
66 output: "0b111110111 === 503;",
67 errors: [{ message: "Use binary literals instead of parseInt()." }]
68 }, {
69 code: "parseInt(\"767\", 8) === 503;",
70 output: "0o767 === 503;",
71 errors: [{ message: "Use octal literals instead of parseInt()." }]
72 }, {
73 code: "parseInt(\"1F7\", 16) === 255;",
74 output: "0x1F7 === 255;",
75 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
76 }, {
77 code: "Number.parseInt(\"111110111\", 2) === 503;",
78 output: "0b111110111 === 503;",
79 errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
80 }, {
81 code: "Number.parseInt(\"767\", 8) === 503;",
82 output: "0o767 === 503;",
83 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
84 }, {
85 code: "Number.parseInt(\"1F7\", 16) === 255;",
86 output: "0x1F7 === 255;",
87 errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
88 }, {
89 code: "parseInt('7999', 8);",
90 output: null, // not fixed, unexpected 9 in parseInt string
91 errors: [{ message: "Use octal literals instead of parseInt()." }]
92 }, {
93 code: "parseInt('1234', 2);",
94 output: null, // not fixed, invalid binary string
95 errors: [{ message: "Use binary literals instead of parseInt()." }]
96 }, {
97 code: "parseInt('1234.5', 8);",
98 output: null, // not fixed, this isn't an integer
99 errors: [{ message: "Use octal literals instead of parseInt()." }]
100 }, {
101 code: "parseInt('1️⃣3️⃣3️⃣7️⃣', 16);",
102 output: null, // not fixed, javascript doesn't support emoji literals
103 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
104 }, {
105 code: "Number.parseInt('7999', 8);",
106 output: null, // not fixed, unexpected 9 in parseInt string
107 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
108 }, {
109 code: "Number.parseInt('1234', 2);",
110 output: null, // not fixed, invalid binary string
111 errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
112 }, {
113 code: "Number.parseInt('1234.5', 8);",
114 output: null, // not fixed, this isn't an integer
115 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
116 }, {
117 code: "Number.parseInt('1️⃣3️⃣3️⃣7️⃣', 16);",
118 output: null, // not fixed, javascript doesn't support emoji literals
119 errors: [{ message: "Use hexadecimal literals instead of Number.parseInt()." }]
120 },
121 {
122 code: "parseInt(`111110111`, 2) === 503;",
123 output: "0b111110111 === 503;",
124 errors: [{ message: "Use binary literals instead of parseInt()." }]
125 }, {
126 code: "parseInt(`767`, 8) === 503;",
127 output: "0o767 === 503;",
128 errors: [{ message: "Use octal literals instead of parseInt()." }]
129 }, {
130 code: "parseInt(`1F7`, 16) === 255;",
131 output: "0x1F7 === 255;",
132 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
133 },
134 {
135 code: "parseInt('', 8);",
136 output: null, // not fixed, it's empty string
137 errors: [{ message: "Use octal literals instead of parseInt()." }]
138 },
139 {
140 code: "parseInt(``, 8);",
141 output: null, // not fixed, it's empty string
142 errors: [{ message: "Use octal literals instead of parseInt()." }]
143 },
144 {
145 code: "parseInt(`7999`, 8);",
146 output: null, // not fixed, unexpected 9 in parseInt string
147 errors: [{ message: "Use octal literals instead of parseInt()." }]
148 }, {
149 code: "parseInt(`1234`, 2);",
150 output: null, // not fixed, invalid binary string
151 errors: [{ message: "Use binary literals instead of parseInt()." }]
152 }, {
153 code: "parseInt(`1234.5`, 8);",
154 output: null, // not fixed, this isn't an integer
155 errors: [{ message: "Use octal literals instead of parseInt()." }]
156 },
157
158 // Adjacent tokens tests
159 {
160 code: "parseInt('11', 2)",
161 output: "0b11",
162 errors: [{ message: "Use binary literals instead of parseInt()." }]
163 },
164 {
165 code: "Number.parseInt('67', 8)",
166 output: "0o67",
167 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
168 },
169 {
170 code: "5+parseInt('A', 16)",
171 output: "5+0xA",
172 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
173 },
174 {
175 code: "function *f(){ yield(Number).parseInt('11', 2) }",
176 output: "function *f(){ yield 0b11 }",
177 parserOptions: { ecmaVersion: 6 },
178 errors: [{ message: "Use binary literals instead of (Number).parseInt()." }]
179 },
180 {
181 code: "function *f(){ yield(Number.parseInt)('67', 8) }",
182 output: "function *f(){ yield 0o67 }",
183 parserOptions: { ecmaVersion: 6 },
184 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
185 },
186 {
187 code: "function *f(){ yield(parseInt)('A', 16) }",
188 output: "function *f(){ yield 0xA }",
189 parserOptions: { ecmaVersion: 6 },
190 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
191 },
192 {
193 code: "function *f(){ yield Number.parseInt('11', 2) }",
194 output: "function *f(){ yield 0b11 }",
195 parserOptions: { ecmaVersion: 6 },
196 errors: [{ message: "Use binary literals instead of Number.parseInt()." }]
197 },
198 {
199 code: "function *f(){ yield/**/Number.parseInt('67', 8) }",
200 output: "function *f(){ yield/**/0o67 }",
201 parserOptions: { ecmaVersion: 6 },
202 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
203 },
204 {
205 code: "function *f(){ yield(parseInt('A', 16)) }",
206 output: "function *f(){ yield(0xA) }",
207 parserOptions: { ecmaVersion: 6 },
208 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
209 },
210 {
211 code: "parseInt('11', 2)+5",
212 output: "0b11+5",
213 errors: [{ message: "Use binary literals instead of parseInt()." }]
214 },
215 {
216 code: "Number.parseInt('17', 8)+5",
217 output: "0o17+5",
218 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
219 },
220 {
221 code: "parseInt('A', 16)+5",
222 output: "0xA+5",
223 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
224 },
225 {
226 code: "parseInt('11', 2)in foo",
227 output: "0b11 in foo",
228 errors: [{ message: "Use binary literals instead of parseInt()." }]
229 },
230 {
231 code: "Number.parseInt('17', 8)in foo",
232 output: "0o17 in foo",
233 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
234 },
235 {
236 code: "parseInt('A', 16)in foo",
237 output: "0xA in foo",
238 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
239 },
240 {
241 code: "parseInt('11', 2) in foo",
242 output: "0b11 in foo",
243 errors: [{ message: "Use binary literals instead of parseInt()." }]
244 },
245 {
246 code: "Number.parseInt('17', 8)/**/in foo",
247 output: "0o17/**/in foo",
248 errors: [{ message: "Use octal literals instead of Number.parseInt()." }]
249 },
250 {
251 code: "(parseInt('A', 16))in foo",
252 output: "(0xA)in foo",
253 errors: [{ message: "Use hexadecimal literals instead of parseInt()." }]
254 },
255
256 // Should not autofix if it would remove comments
257 {
258 code: "/* comment */Number.parseInt('11', 2);",
259 output: "/* comment */0b11;",
260 errors: 1
261 },
262 {
263 code: "Number/**/.parseInt('11', 2);",
264 output: null,
265 errors: 1
266 },
267 {
268 code: "Number//\n.parseInt('11', 2);",
269 output: null,
270 errors: 1
271 },
272 {
273 code: "Number./**/parseInt('11', 2);",
274 output: null,
275 errors: 1
276 },
277 {
278 code: "Number.parseInt(/**/'11', 2);",
279 output: null,
280 errors: 1
281 },
282 {
283 code: "Number.parseInt('11', /**/2);",
284 output: null,
285 errors: 1
286 },
287 {
288 code: "Number.parseInt('11', 2)/* comment */;",
289 output: "0b11/* comment */;",
290 errors: 1
291 },
292 {
293 code: "parseInt/**/('11', 2);",
294 output: null,
295 errors: 1
296 },
297 {
298 code: "parseInt(//\n'11', 2);",
299 output: null,
300 errors: 1
301 },
302 {
303 code: "parseInt('11'/**/, 2);",
304 output: null,
305 errors: 1
306 },
307 {
308 code: "parseInt(`11`/**/, 2);",
309 output: null,
310 errors: 1
311 },
312 {
313 code: "parseInt('11', 2 /**/);",
314 output: null,
315 errors: 1
316 },
317 {
318 code: "parseInt('11', 2)//comment\n;",
319 output: "0b11//comment\n;",
320 errors: 1
321 }
322 ]
323 });