@@ -5,15 +5,7 @@ import {getTester} from './utils/test.mjs';
5
5
6
6
const { test} = getTester ( import . meta) ;
7
7
8
- const enforceNewError = builtin => ( {
9
- message : `Use \`new ${ builtin } ()\` instead of \`${ builtin } ()\`.` ,
10
- } ) ;
11
-
12
- const disallowNewError = builtin => ( {
13
- message : `Use \`${ builtin } ()\` instead of \`new ${ builtin } ()\`.` ,
14
- } ) ;
15
-
16
- test ( {
8
+ test . snapshot ( {
17
9
valid : [
18
10
'const foo = new Object()' ,
19
11
'const foo = new Array()' ,
@@ -106,214 +98,7 @@ test({
106
98
'const isObject = v => Object(v) === v;' ,
107
99
'const isObject = v => globalThis.Object(v) === v;' ,
108
100
'(x) !== Object(x)' ,
109
- ] ,
110
- invalid : [
111
- {
112
- code : 'const foo = Object()' ,
113
- errors : [ enforceNewError ( 'Object' ) ] ,
114
- output : 'const foo = new Object()' ,
115
- } ,
116
- {
117
- code : 'const foo = Array()' ,
118
- errors : [ enforceNewError ( 'Array' ) ] ,
119
- output : 'const foo = new Array()' ,
120
- } ,
121
- {
122
- code : 'const foo = ArrayBuffer()' ,
123
- errors : [ enforceNewError ( 'ArrayBuffer' ) ] ,
124
- output : 'const foo = new ArrayBuffer()' ,
125
- } ,
126
- {
127
- code : 'const foo = BigInt64Array()' ,
128
- errors : [ enforceNewError ( 'BigInt64Array' ) ] ,
129
- output : 'const foo = new BigInt64Array()' ,
130
- } ,
131
- {
132
- code : 'const foo = BigUint64Array()' ,
133
- errors : [ enforceNewError ( 'BigUint64Array' ) ] ,
134
- output : 'const foo = new BigUint64Array()' ,
135
- } ,
136
- {
137
- code : 'const foo = DataView()' ,
138
- errors : [ enforceNewError ( 'DataView' ) ] ,
139
- output : 'const foo = new DataView()' ,
140
- } ,
141
- {
142
- code : 'const foo = Date()' ,
143
- errors : [ enforceNewError ( 'Date' ) ] ,
144
- output : 'const foo = new Date()' ,
145
- } ,
146
- {
147
- code : 'const foo = Error()' ,
148
- errors : [ enforceNewError ( 'Error' ) ] ,
149
- output : 'const foo = new Error()' ,
150
- } ,
151
- {
152
- code : 'const foo = Error(\'Foo bar\')' ,
153
- errors : [ enforceNewError ( 'Error' ) ] ,
154
- output : 'const foo = new Error(\'Foo bar\')' ,
155
- } ,
156
- {
157
- code : 'const foo = Float32Array()' ,
158
- errors : [ enforceNewError ( 'Float32Array' ) ] ,
159
- output : 'const foo = new Float32Array()' ,
160
- } ,
161
- {
162
- code : 'const foo = Float64Array()' ,
163
- errors : [ enforceNewError ( 'Float64Array' ) ] ,
164
- output : 'const foo = new Float64Array()' ,
165
- } ,
166
- {
167
- code : 'const foo = Function()' ,
168
- errors : [ enforceNewError ( 'Function' ) ] ,
169
- output : 'const foo = new Function()' ,
170
- } ,
171
- {
172
- code : 'const foo = Int8Array()' ,
173
- errors : [ enforceNewError ( 'Int8Array' ) ] ,
174
- output : 'const foo = new Int8Array()' ,
175
- } ,
176
- {
177
- code : 'const foo = Int16Array()' ,
178
- errors : [ enforceNewError ( 'Int16Array' ) ] ,
179
- output : 'const foo = new Int16Array()' ,
180
- } ,
181
- {
182
- code : 'const foo = Int32Array()' ,
183
- errors : [ enforceNewError ( 'Int32Array' ) ] ,
184
- output : 'const foo = new Int32Array()' ,
185
- } ,
186
- {
187
- code : 'const foo = (( Map ))()' ,
188
- errors : [ enforceNewError ( 'Map' ) ] ,
189
- output : 'const foo = new (( Map ))()' ,
190
- } ,
191
- {
192
- code : 'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])' ,
193
- errors : [ enforceNewError ( 'Map' ) ] ,
194
- output : 'const foo = new Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])' ,
195
- } ,
196
- {
197
- code : 'const foo = WeakMap()' ,
198
- errors : [ enforceNewError ( 'WeakMap' ) ] ,
199
- output : 'const foo = new WeakMap()' ,
200
- } ,
201
- {
202
- code : 'const foo = Set()' ,
203
- errors : [ enforceNewError ( 'Set' ) ] ,
204
- output : 'const foo = new Set()' ,
205
- } ,
206
- {
207
- code : 'const foo = WeakSet()' ,
208
- errors : [ enforceNewError ( 'WeakSet' ) ] ,
209
- output : 'const foo = new WeakSet()' ,
210
- } ,
211
- {
212
- code : 'const foo = Promise()' ,
213
- errors : [ enforceNewError ( 'Promise' ) ] ,
214
- output : 'const foo = new Promise()' ,
215
- } ,
216
- {
217
- code : 'const foo = RegExp()' ,
218
- errors : [ enforceNewError ( 'RegExp' ) ] ,
219
- output : 'const foo = new RegExp()' ,
220
- } ,
221
- {
222
- code : 'const foo = Uint8Array()' ,
223
- errors : [ enforceNewError ( 'Uint8Array' ) ] ,
224
- output : 'const foo = new Uint8Array()' ,
225
- } ,
226
- {
227
- code : 'const foo = Uint16Array()' ,
228
- errors : [ enforceNewError ( 'Uint16Array' ) ] ,
229
- output : 'const foo = new Uint16Array()' ,
230
- } ,
231
- {
232
- code : 'const foo = Uint32Array()' ,
233
- errors : [ enforceNewError ( 'Uint32Array' ) ] ,
234
- output : 'const foo = new Uint32Array()' ,
235
- } ,
236
- {
237
- code : 'const foo = Uint8ClampedArray()' ,
238
- errors : [ enforceNewError ( 'Uint8ClampedArray' ) ] ,
239
- output : 'const foo = new Uint8ClampedArray()' ,
240
- } ,
241
- {
242
- code : 'const foo = new BigInt(123)' ,
243
- errors : [ disallowNewError ( 'BigInt' ) ] ,
244
- output : 'const foo = BigInt(123)' ,
245
- } ,
246
- {
247
- code : 'const foo = new Boolean()' ,
248
- errors : [ disallowNewError ( 'Boolean' ) ] ,
249
- } ,
250
- {
251
- code : 'const foo = new Number()' ,
252
- errors : [ disallowNewError ( 'Number' ) ] ,
253
- } ,
254
- {
255
- code : 'const foo = new Number(\'123\')' ,
256
- errors : [ disallowNewError ( 'Number' ) ] ,
257
- } ,
258
- {
259
- code : 'const foo = new String()' ,
260
- errors : [ disallowNewError ( 'String' ) ] ,
261
- } ,
262
- {
263
- code : 'const foo = new Symbol()' ,
264
- errors : [ disallowNewError ( 'Symbol' ) ] ,
265
- output : 'const foo = Symbol()' ,
266
- } ,
267
- {
268
- code : `
269
- function varCheck() {
270
- {
271
- var WeakMap = function() {};
272
- }
273
- // This should not reported
274
- return WeakMap()
275
- }
276
- function constCheck() {
277
- {
278
- const Array = function() {};
279
- }
280
- return Array()
281
- }
282
- function letCheck() {
283
- {
284
- let Map = function() {};
285
- }
286
- return Map()
287
- }
288
- ` ,
289
- errors : [ enforceNewError ( 'Array' ) , enforceNewError ( 'Map' ) ] ,
290
- output : `
291
- function varCheck() {
292
- {
293
- var WeakMap = function() {};
294
- }
295
- // This should not reported
296
- return WeakMap()
297
- }
298
- function constCheck() {
299
- {
300
- const Array = function() {};
301
- }
302
- return new Array()
303
- }
304
- function letCheck() {
305
- {
306
- let Map = function() {};
307
- }
308
- return new Map()
309
- }
310
- ` ,
311
- } ,
312
- ] ,
313
- } ) ;
314
101
315
- test . snapshot ( {
316
- valid : [
317
102
{
318
103
code : 'new Symbol("")' ,
319
104
globals : { Symbol : 'off' } ,
@@ -414,5 +199,58 @@ test.snapshot({
414
199
` ,
415
200
globals : { Array : 'off' } ,
416
201
} ,
202
+ 'const foo = Object()' ,
203
+ 'const foo = Array()' ,
204
+ 'const foo = ArrayBuffer()' ,
205
+ 'const foo = BigInt64Array()' ,
206
+ 'const foo = BigUint64Array()' ,
207
+ 'const foo = DataView()' ,
208
+ 'const foo = Date()' ,
209
+ 'const foo = Error()' ,
210
+ 'const foo = Error(\'Foo bar\')' ,
211
+ 'const foo = Float32Array()' ,
212
+ 'const foo = Float64Array()' ,
213
+ 'const foo = Function()' ,
214
+ 'const foo = Int8Array()' ,
215
+ 'const foo = Int16Array()' ,
216
+ 'const foo = Int32Array()' ,
217
+ 'const foo = (( Map ))()' ,
218
+ 'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])' ,
219
+ 'const foo = WeakMap()' ,
220
+ 'const foo = Set()' ,
221
+ 'const foo = WeakSet()' ,
222
+ 'const foo = Promise()' ,
223
+ 'const foo = RegExp()' ,
224
+ 'const foo = Uint8Array()' ,
225
+ 'const foo = Uint16Array()' ,
226
+ 'const foo = Uint32Array()' ,
227
+ 'const foo = Uint8ClampedArray()' ,
228
+ 'const foo = new BigInt(123)' ,
229
+ 'const foo = new Boolean()' ,
230
+ 'const foo = new Number()' ,
231
+ 'const foo = new Number(\'123\')' ,
232
+ 'const foo = new String()' ,
233
+ 'const foo = new Symbol()' ,
234
+ `
235
+ function varCheck() {
236
+ {
237
+ var WeakMap = function() {};
238
+ }
239
+ // This should not reported
240
+ return WeakMap()
241
+ }
242
+ function constCheck() {
243
+ {
244
+ const Array = function() {};
245
+ }
246
+ return Array()
247
+ }
248
+ function letCheck() {
249
+ {
250
+ let Map = function() {};
251
+ }
252
+ return Map()
253
+ }
254
+ ` ,
417
255
] ,
418
256
} ) ;
0 commit comments