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