]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for radix rule. | |
3 | * @author James Allardice | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/radix"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
609c276f TL |
15 | //------------------------------------------------------------------------------ |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
eb39fafa DC |
19 | const ruleTester = new RuleTester(); |
20 | ||
21 | ruleTester.run("radix", rule, { | |
22 | ||
23 | valid: [ | |
24 | "parseInt(\"10\", 10);", | |
25 | "parseInt(\"10\", 2);", | |
26 | "parseInt(\"10\", 36);", | |
27 | "parseInt(\"10\", 0x10);", | |
28 | "parseInt(\"10\", 1.6e1);", | |
29 | "parseInt(\"10\", 10.0);", | |
30 | "parseInt(\"10\", foo);", | |
31 | "Number.parseInt(\"10\", foo);", | |
32 | { | |
33 | code: "parseInt(\"10\", 10);", | |
34 | options: ["always"] | |
35 | }, | |
36 | { | |
37 | code: "parseInt(\"10\");", | |
38 | options: ["as-needed"] | |
39 | }, | |
40 | { | |
41 | code: "parseInt(\"10\", 8);", | |
42 | options: ["as-needed"] | |
43 | }, | |
44 | { | |
45 | code: "parseInt(\"10\", foo);", | |
46 | options: ["as-needed"] | |
47 | }, | |
48 | "parseInt", | |
49 | "Number.foo();", | |
50 | "Number[parseInt]();", | |
609c276f TL |
51 | { code: "class C { #parseInt; foo() { Number.#parseInt(); } }", parserOptions: { ecmaVersion: 2022 } }, |
52 | { code: "class C { #parseInt; foo() { Number.#parseInt(foo); } }", parserOptions: { ecmaVersion: 2022 } }, | |
53 | { code: "class C { #parseInt; foo() { Number.#parseInt(foo, 'bar'); } }", parserOptions: { ecmaVersion: 2022 } }, | |
54 | { code: "class C { #parseInt; foo() { Number.#parseInt(foo, 10); } }", options: ["as-needed"], parserOptions: { ecmaVersion: 2022 } }, | |
eb39fafa DC |
55 | |
56 | // Ignores if it's shadowed or disabled. | |
57 | "var parseInt; parseInt();", | |
58 | { code: "var parseInt; parseInt(foo);", options: ["always"] }, | |
59 | { code: "var parseInt; parseInt(foo, 10);", options: ["as-needed"] }, | |
60 | "var Number; Number.parseInt();", | |
61 | { code: "var Number; Number.parseInt(foo);", options: ["always"] }, | |
62 | { code: "var Number; Number.parseInt(foo, 10);", options: ["as-needed"] }, | |
63 | { code: "/* globals parseInt:off */ parseInt(foo);", options: ["always"] }, | |
64 | { code: "Number.parseInt(foo, 10);", options: ["as-needed"], globals: { Number: "off" } } | |
65 | ], | |
66 | ||
67 | invalid: [ | |
68 | { | |
69 | code: "parseInt();", | |
70 | options: ["as-needed"], | |
71 | errors: [{ | |
72 | messageId: "missingParameters", | |
73 | type: "CallExpression" | |
74 | }] | |
75 | }, | |
76 | { | |
77 | code: "parseInt();", | |
78 | errors: [{ | |
79 | messageId: "missingParameters", | |
80 | type: "CallExpression" | |
81 | }] | |
82 | }, | |
83 | { | |
84 | code: "parseInt(\"10\");", | |
85 | errors: [{ | |
86 | messageId: "missingRadix", | |
5422a9cc TL |
87 | type: "CallExpression", |
88 | suggestions: [{ messageId: "addRadixParameter10", output: "parseInt(\"10\", 10);" }] | |
89 | }] | |
90 | }, | |
91 | { | |
92 | code: "parseInt(\"10\",);", // Function parameter with trailing comma | |
93 | parserOptions: { ecmaVersion: 2017 }, | |
94 | errors: [{ | |
95 | messageId: "missingRadix", | |
96 | type: "CallExpression", | |
97 | suggestions: [{ messageId: "addRadixParameter10", output: "parseInt(\"10\", 10,);" }] | |
98 | }] | |
99 | }, | |
100 | { | |
101 | code: "parseInt((0, \"10\"));", // Sequence expression (no trailing comma). | |
102 | errors: [{ | |
103 | messageId: "missingRadix", | |
104 | type: "CallExpression", | |
105 | suggestions: [{ messageId: "addRadixParameter10", output: "parseInt((0, \"10\"), 10);" }] | |
106 | }] | |
107 | }, | |
108 | { | |
109 | code: "parseInt((0, \"10\"),);", // Sequence expression (with trailing comma). | |
110 | parserOptions: { ecmaVersion: 2017 }, | |
111 | errors: [{ | |
112 | messageId: "missingRadix", | |
113 | type: "CallExpression", | |
114 | suggestions: [{ messageId: "addRadixParameter10", output: "parseInt((0, \"10\"), 10,);" }] | |
eb39fafa DC |
115 | }] |
116 | }, | |
117 | { | |
118 | code: "parseInt(\"10\", null);", | |
119 | errors: [{ | |
120 | messageId: "invalidRadix", | |
121 | type: "CallExpression" | |
122 | }] | |
123 | }, | |
124 | { | |
125 | code: "parseInt(\"10\", undefined);", | |
126 | errors: [{ | |
127 | messageId: "invalidRadix", | |
128 | type: "CallExpression" | |
129 | }] | |
130 | }, | |
131 | { | |
132 | code: "parseInt(\"10\", true);", | |
133 | errors: [{ | |
134 | messageId: "invalidRadix", | |
135 | type: "CallExpression" | |
136 | }] | |
137 | }, | |
138 | { | |
139 | code: "parseInt(\"10\", \"foo\");", | |
140 | errors: [{ | |
141 | messageId: "invalidRadix", | |
142 | type: "CallExpression" | |
143 | }] | |
144 | }, | |
145 | { | |
146 | code: "parseInt(\"10\", \"123\");", | |
147 | errors: [{ | |
148 | messageId: "invalidRadix", | |
149 | type: "CallExpression" | |
150 | }] | |
151 | }, | |
152 | { | |
153 | code: "parseInt(\"10\", 1);", | |
154 | errors: [{ | |
155 | messageId: "invalidRadix", | |
156 | type: "CallExpression" | |
157 | }] | |
158 | }, | |
159 | { | |
160 | code: "parseInt(\"10\", 37);", | |
161 | errors: [{ | |
162 | messageId: "invalidRadix", | |
163 | type: "CallExpression" | |
164 | }] | |
165 | }, | |
166 | { | |
167 | code: "parseInt(\"10\", 10.5);", | |
168 | errors: [{ | |
169 | messageId: "invalidRadix", | |
170 | type: "CallExpression" | |
171 | }] | |
172 | }, | |
173 | { | |
174 | code: "Number.parseInt();", | |
175 | errors: [{ | |
176 | messageId: "missingParameters", | |
177 | type: "CallExpression" | |
178 | }] | |
179 | }, | |
180 | { | |
181 | code: "Number.parseInt();", | |
182 | options: ["as-needed"], | |
183 | errors: [{ | |
184 | messageId: "missingParameters", | |
185 | type: "CallExpression" | |
186 | }] | |
187 | }, | |
188 | { | |
189 | code: "Number.parseInt(\"10\");", | |
190 | errors: [{ | |
191 | messageId: "missingRadix", | |
5422a9cc TL |
192 | type: "CallExpression", |
193 | suggestions: [{ messageId: "addRadixParameter10", output: "Number.parseInt(\"10\", 10);" }] | |
eb39fafa DC |
194 | }] |
195 | }, | |
196 | { | |
197 | code: "Number.parseInt(\"10\", 1);", | |
198 | errors: [{ | |
199 | messageId: "invalidRadix", | |
200 | type: "CallExpression" | |
201 | }] | |
202 | }, | |
203 | { | |
204 | code: "Number.parseInt(\"10\", 37);", | |
205 | errors: [{ | |
206 | messageId: "invalidRadix", | |
207 | type: "CallExpression" | |
208 | }] | |
209 | }, | |
210 | { | |
211 | code: "Number.parseInt(\"10\", 10.5);", | |
212 | errors: [{ | |
213 | messageId: "invalidRadix", | |
214 | type: "CallExpression" | |
215 | }] | |
216 | }, | |
217 | { | |
218 | code: "parseInt(\"10\", 10);", | |
219 | options: ["as-needed"], | |
220 | errors: [{ | |
221 | messageId: "redundantRadix", | |
222 | type: "CallExpression" | |
223 | }] | |
6f036462 TL |
224 | }, |
225 | ||
226 | // Optional chaining | |
227 | { | |
228 | code: "parseInt?.(\"10\");", | |
229 | parserOptions: { ecmaVersion: 2020 }, | |
5422a9cc TL |
230 | errors: [ |
231 | { | |
232 | messageId: "missingRadix", | |
233 | type: "CallExpression", | |
234 | suggestions: [{ messageId: "addRadixParameter10", output: "parseInt?.(\"10\", 10);" }] | |
235 | } | |
236 | ] | |
6f036462 TL |
237 | }, |
238 | { | |
239 | code: "Number.parseInt?.(\"10\");", | |
240 | parserOptions: { ecmaVersion: 2020 }, | |
5422a9cc TL |
241 | errors: [ |
242 | { | |
243 | messageId: "missingRadix", | |
244 | type: "CallExpression", | |
245 | suggestions: [{ messageId: "addRadixParameter10", output: "Number.parseInt?.(\"10\", 10);" }] | |
246 | } | |
247 | ] | |
6f036462 TL |
248 | }, |
249 | { | |
250 | code: "Number?.parseInt(\"10\");", | |
251 | parserOptions: { ecmaVersion: 2020 }, | |
5422a9cc TL |
252 | errors: [ |
253 | { | |
254 | messageId: "missingRadix", | |
255 | type: "CallExpression", | |
256 | suggestions: [{ messageId: "addRadixParameter10", output: "Number?.parseInt(\"10\", 10);" }] | |
257 | } | |
258 | ] | |
6f036462 TL |
259 | }, |
260 | { | |
261 | code: "(Number?.parseInt)(\"10\");", | |
262 | parserOptions: { ecmaVersion: 2020 }, | |
5422a9cc TL |
263 | errors: [ |
264 | { | |
265 | messageId: "missingRadix", | |
266 | type: "CallExpression", | |
267 | suggestions: [{ messageId: "addRadixParameter10", output: "(Number?.parseInt)(\"10\", 10);" }] | |
268 | } | |
269 | ] | |
eb39fafa DC |
270 | } |
271 | ] | |
272 | }); |