]>
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", | |
79 | type: "CallExpression" | |
80 | }] | |
81 | }, | |
82 | { | |
83 | code: "parseInt(\"10\", null);", | |
84 | errors: [{ | |
85 | messageId: "invalidRadix", | |
86 | type: "CallExpression" | |
87 | }] | |
88 | }, | |
89 | { | |
90 | code: "parseInt(\"10\", undefined);", | |
91 | errors: [{ | |
92 | messageId: "invalidRadix", | |
93 | type: "CallExpression" | |
94 | }] | |
95 | }, | |
96 | { | |
97 | code: "parseInt(\"10\", true);", | |
98 | errors: [{ | |
99 | messageId: "invalidRadix", | |
100 | type: "CallExpression" | |
101 | }] | |
102 | }, | |
103 | { | |
104 | code: "parseInt(\"10\", \"foo\");", | |
105 | errors: [{ | |
106 | messageId: "invalidRadix", | |
107 | type: "CallExpression" | |
108 | }] | |
109 | }, | |
110 | { | |
111 | code: "parseInt(\"10\", \"123\");", | |
112 | errors: [{ | |
113 | messageId: "invalidRadix", | |
114 | type: "CallExpression" | |
115 | }] | |
116 | }, | |
117 | { | |
118 | code: "parseInt(\"10\", 1);", | |
119 | errors: [{ | |
120 | messageId: "invalidRadix", | |
121 | type: "CallExpression" | |
122 | }] | |
123 | }, | |
124 | { | |
125 | code: "parseInt(\"10\", 37);", | |
126 | errors: [{ | |
127 | messageId: "invalidRadix", | |
128 | type: "CallExpression" | |
129 | }] | |
130 | }, | |
131 | { | |
132 | code: "parseInt(\"10\", 10.5);", | |
133 | errors: [{ | |
134 | messageId: "invalidRadix", | |
135 | type: "CallExpression" | |
136 | }] | |
137 | }, | |
138 | { | |
139 | code: "Number.parseInt();", | |
140 | errors: [{ | |
141 | messageId: "missingParameters", | |
142 | type: "CallExpression" | |
143 | }] | |
144 | }, | |
145 | { | |
146 | code: "Number.parseInt();", | |
147 | options: ["as-needed"], | |
148 | errors: [{ | |
149 | messageId: "missingParameters", | |
150 | type: "CallExpression" | |
151 | }] | |
152 | }, | |
153 | { | |
154 | code: "Number.parseInt(\"10\");", | |
155 | errors: [{ | |
156 | messageId: "missingRadix", | |
157 | type: "CallExpression" | |
158 | }] | |
159 | }, | |
160 | { | |
161 | code: "Number.parseInt(\"10\", 1);", | |
162 | errors: [{ | |
163 | messageId: "invalidRadix", | |
164 | type: "CallExpression" | |
165 | }] | |
166 | }, | |
167 | { | |
168 | code: "Number.parseInt(\"10\", 37);", | |
169 | errors: [{ | |
170 | messageId: "invalidRadix", | |
171 | type: "CallExpression" | |
172 | }] | |
173 | }, | |
174 | { | |
175 | code: "Number.parseInt(\"10\", 10.5);", | |
176 | errors: [{ | |
177 | messageId: "invalidRadix", | |
178 | type: "CallExpression" | |
179 | }] | |
180 | }, | |
181 | { | |
182 | code: "parseInt(\"10\", 10);", | |
183 | options: ["as-needed"], | |
184 | errors: [{ | |
185 | messageId: "redundantRadix", | |
186 | type: "CallExpression" | |
187 | }] | |
6f036462 TL |
188 | }, |
189 | ||
190 | // Optional chaining | |
191 | { | |
192 | code: "parseInt?.(\"10\");", | |
193 | parserOptions: { ecmaVersion: 2020 }, | |
194 | errors: [{ messageId: "missingRadix" }] | |
195 | }, | |
196 | { | |
197 | code: "Number.parseInt?.(\"10\");", | |
198 | parserOptions: { ecmaVersion: 2020 }, | |
199 | errors: [{ messageId: "missingRadix" }] | |
200 | }, | |
201 | { | |
202 | code: "Number?.parseInt(\"10\");", | |
203 | parserOptions: { ecmaVersion: 2020 }, | |
204 | errors: [{ messageId: "missingRadix" }] | |
205 | }, | |
206 | { | |
207 | code: "(Number?.parseInt)(\"10\");", | |
208 | parserOptions: { ecmaVersion: 2020 }, | |
209 | errors: [{ messageId: "missingRadix" }] | |
eb39fafa DC |
210 | } |
211 | ] | |
212 | }); |