]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-numeric-literals.js
2 * @fileoverview Tests for prefer-numeric-literals rule.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/prefer-numeric-literals"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 6 } });
21 ruleTester
.run("prefer-numeric-literals", rule
, {
25 "Number.parseInt(1);",
26 "Number.parseInt(1, 3);",
27 "0b111110111 === 503;",
33 "Number.parseInt(foo);",
34 "Number.parseInt(foo, 2);",
36 "Number.parseInt(1, 8);",
38 "parseInt('11', '2');",
39 "Number.parseInt('11', '8');",
40 "parseInt(/foo/, 2);",
41 "parseInt(`11${foo}`, 2);",
43 code
: "parseInt('11', 2n);",
44 parserOptions
: { ecmaVersion
: 2020 }
47 code
: "Number.parseInt('11', 8n);",
48 parserOptions
: { ecmaVersion
: 2020 }
51 code
: "parseInt('11', 16n);",
52 parserOptions
: { ecmaVersion
: 2020 }
55 code
: "parseInt(`11`, 16n);",
56 parserOptions
: { ecmaVersion
: 2020 }
59 code
: "parseInt(1n, 2);",
60 parserOptions
: { ecmaVersion
: 2020 }
65 code
: "parseInt(\"111110111\", 2) === 503;",
66 output
: "0b111110111 === 503;",
67 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
69 code
: "parseInt(\"767\", 8) === 503;",
70 output
: "0o767 === 503;",
71 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
73 code
: "parseInt(\"1F7\", 16) === 255;",
74 output
: "0x1F7 === 255;",
75 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
77 code
: "Number.parseInt(\"111110111\", 2) === 503;",
78 output
: "0b111110111 === 503;",
79 errors
: [{ message
: "Use binary literals instead of Number.parseInt()." }]
81 code
: "Number.parseInt(\"767\", 8) === 503;",
82 output
: "0o767 === 503;",
83 errors
: [{ message
: "Use octal literals instead of Number.parseInt()." }]
85 code
: "Number.parseInt(\"1F7\", 16) === 255;",
86 output
: "0x1F7 === 255;",
87 errors
: [{ message
: "Use hexadecimal literals instead of Number.parseInt()." }]
89 code
: "parseInt('7999', 8);",
90 output
: null, // not fixed, unexpected 9 in parseInt string
91 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
93 code
: "parseInt('1234', 2);",
94 output
: null, // not fixed, invalid binary string
95 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
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()." }]
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()." }]
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()." }]
109 code
: "Number.parseInt('1234', 2);",
110 output
: null, // not fixed, invalid binary string
111 errors
: [{ message
: "Use binary literals instead of Number.parseInt()." }]
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()." }]
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()." }]
122 code
: "parseInt(`111110111`, 2) === 503;",
123 output
: "0b111110111 === 503;",
124 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
126 code
: "parseInt(`767`, 8) === 503;",
127 output
: "0o767 === 503;",
128 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
130 code
: "parseInt(`1F7`, 16) === 255;",
131 output
: "0x1F7 === 255;",
132 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
135 code
: "parseInt('', 8);",
136 output
: null, // not fixed, it's empty string
137 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
140 code
: "parseInt(``, 8);",
141 output
: null, // not fixed, it's empty string
142 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
145 code
: "parseInt(`7999`, 8);",
146 output
: null, // not fixed, unexpected 9 in parseInt string
147 errors
: [{ message
: "Use octal literals instead of parseInt()." }]
149 code
: "parseInt(`1234`, 2);",
150 output
: null, // not fixed, invalid binary string
151 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
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()." }]
158 // Adjacent tokens tests
160 code
: "parseInt('11', 2)",
162 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
165 code
: "Number.parseInt('67', 8)",
167 errors
: [{ message
: "Use octal literals instead of Number.parseInt()." }]
170 code
: "5+parseInt('A', 16)",
172 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
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()." }]
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()." }]
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()." }]
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()." }]
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()." }]
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()." }]
211 code
: "parseInt('11', 2)+5",
213 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
216 code
: "Number.parseInt('17', 8)+5",
218 errors
: [{ message
: "Use octal literals instead of Number.parseInt()." }]
221 code
: "parseInt('A', 16)+5",
223 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
226 code
: "parseInt('11', 2)in foo",
227 output
: "0b11 in foo",
228 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
231 code
: "Number.parseInt('17', 8)in foo",
232 output
: "0o17 in foo",
233 errors
: [{ message
: "Use octal literals instead of Number.parseInt()." }]
236 code
: "parseInt('A', 16)in foo",
237 output
: "0xA in foo",
238 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
241 code
: "parseInt('11', 2) in foo",
242 output
: "0b11 in foo",
243 errors
: [{ message
: "Use binary literals instead of parseInt()." }]
246 code
: "Number.parseInt('17', 8)/**/in foo",
247 output
: "0o17/**/in foo",
248 errors
: [{ message
: "Use octal literals instead of Number.parseInt()." }]
251 code
: "(parseInt('A', 16))in foo",
252 output
: "(0xA)in foo",
253 errors
: [{ message
: "Use hexadecimal literals instead of parseInt()." }]
256 // Should not autofix if it would remove comments
258 code
: "/* comment */Number.parseInt('11', 2);",
259 output
: "/* comment */0b11;",
263 code
: "Number/**/.parseInt('11', 2);",
268 code
: "Number//\n.parseInt('11', 2);",
273 code
: "Number./**/parseInt('11', 2);",
278 code
: "Number.parseInt(/**/'11', 2);",
283 code
: "Number.parseInt('11', /**/2);",
288 code
: "Number.parseInt('11', 2)/* comment */;",
289 output
: "0b11/* comment */;",
293 code
: "parseInt/**/('11', 2);",
298 code
: "parseInt(//\n'11', 2);",
303 code
: "parseInt('11'/**/, 2);",
308 code
: "parseInt(`11`/**/, 2);",
313 code
: "parseInt('11', 2 /**/);",
318 code
: "parseInt('11', 2)//comment\n;",
319 output
: "0b11//comment\n;",