Skip to content

Commit f89fb25

Browse files
authored
prefer-spread: Refactor tests (#1028)
1 parent 2203ed2 commit f89fb25

File tree

3 files changed

+542
-290
lines changed

3 files changed

+542
-290
lines changed

test/prefer-spread.js

Lines changed: 80 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import {outdent} from 'outdent';
22
import {test} from './utils/test.js';
33

4-
const MESSAGE_ID = 'prefer-spread';
5-
6-
test({
4+
test.visualize({
75
valid: [
86
'[...set].map(() => {});',
97
// TypedArray.from
@@ -42,303 +40,95 @@ test({
4240
'Array.from({length: 10});'
4341
],
4442
invalid: [
45-
{
46-
code: 'const x = Array.from(set);',
47-
errors: [
48-
{
49-
messageId: MESSAGE_ID,
50-
column: 11,
51-
line: 1
52-
}
53-
],
54-
output: 'const x = [...set];'
55-
},
56-
{
57-
code: 'Array.from(set).map(() => {});',
58-
errors: [
59-
{
60-
messageId: MESSAGE_ID,
61-
column: 1,
62-
line: 1
63-
}
64-
],
65-
output: '[...set].map(() => {});'
66-
},
67-
{
68-
code: 'Array.from(set, mapFn).reduce(() => {});',
69-
errors: [
70-
{
71-
messageId: MESSAGE_ID,
72-
column: 1,
73-
line: 1
74-
}
75-
],
76-
output: '[...set].map(mapFn).reduce(() => {});'
77-
},
78-
{
79-
code: 'Array.from(set, mapFn, thisArg).reduce(() => {});',
80-
errors: [
81-
{
82-
messageId: MESSAGE_ID,
83-
column: 1,
84-
line: 1
85-
}
86-
],
87-
output: '[...set].map(mapFn, thisArg).reduce(() => {});'
88-
},
89-
{
90-
code: 'Array.from(set, () => {}, thisArg).reduce(() => {});',
91-
errors: [
92-
{
93-
messageId: MESSAGE_ID,
94-
column: 1,
95-
line: 1
96-
}
97-
],
98-
output: '[...set].map(() => {}, thisArg).reduce(() => {});'
99-
},
100-
{
101-
code: 'Array.from(new Set([1, 2])).map(() => {});',
102-
errors: [
103-
{
104-
messageId: MESSAGE_ID,
105-
column: 1,
106-
line: 1
107-
}
108-
],
109-
output: '[...new Set([1, 2])].map(() => {});'
110-
},
111-
{
112-
code: 'Array.from(document.querySelectorAll("*")).map(() => {});',
113-
errors: [
114-
{
115-
messageId: MESSAGE_ID,
116-
column: 1,
117-
line: 1
118-
}
119-
],
120-
output: '[...document.querySelectorAll("*")].map(() => {});'
121-
},
43+
'const x = Array.from(set);',
44+
'Array.from(set).map(() => {});',
45+
'Array.from(set, mapFn).reduce(() => {});',
46+
'Array.from(set, mapFn, thisArg).reduce(() => {});',
47+
'Array.from(set, () => {}, thisArg).reduce(() => {});',
48+
'Array.from(new Set([1, 2])).map(() => {});',
49+
'Array.from(document.querySelectorAll("*")).map(() => {});',
12250

12351
// Semicolon
12452
// #254
125-
{
126-
code: outdent`
127-
const foo = []
128-
Array.from(arrayLike).forEach(doSomething)
129-
`,
130-
errors: [
131-
{
132-
messageId: MESSAGE_ID
133-
}
134-
],
135-
output: outdent`
136-
const foo = []
137-
;[...arrayLike].forEach(doSomething)
138-
`
139-
},
140-
{
141-
code: outdent`
142-
const foo = "1"
143-
Array.from(arrayLike).forEach(doSomething)
144-
`,
145-
errors: [{}],
146-
output: outdent`
147-
const foo = "1"
148-
;[...arrayLike].forEach(doSomething)
149-
`
150-
},
151-
{
152-
code: outdent`
153-
const foo = null
154-
Array.from(arrayLike).forEach(doSomething)
155-
`,
156-
errors: [{}],
157-
output: outdent`
158-
const foo = null
159-
;[...arrayLike].forEach(doSomething)
160-
`
161-
},
162-
{
163-
code: outdent`
164-
const foo = true
165-
Array.from(arrayLike).forEach(doSomething)
166-
`,
167-
errors: [{}],
168-
output: outdent`
169-
const foo = true
170-
;[...arrayLike].forEach(doSomething)
171-
`
172-
},
173-
{
174-
code: outdent`
175-
const foo = 1
176-
Array.from(arrayLike).forEach(doSomething)
177-
`,
178-
errors: [{}],
179-
output: outdent`
180-
const foo = 1
181-
;[...arrayLike].forEach(doSomething)
182-
`
183-
},
184-
{
185-
code: outdent`
186-
const foo = /./
187-
Array.from(arrayLike).forEach(doSomething)
188-
`,
189-
errors: [{}],
190-
output: outdent`
191-
const foo = /./
192-
;[...arrayLike].forEach(doSomething)
193-
`
194-
},
195-
{
196-
code: outdent`
197-
const foo = /./g
198-
Array.from(arrayLike).forEach(doSomething)
199-
`,
200-
errors: [{}],
201-
output: outdent`
202-
const foo = /./g
203-
;[...arrayLike].forEach(doSomething)
204-
`
205-
},
206-
{
207-
code: outdent`
208-
const foo = bar
209-
Array.from(arrayLike).forEach(doSomething)
210-
`,
211-
errors: [{}],
212-
output: outdent`
213-
const foo = bar
214-
;[...arrayLike].forEach(doSomething)
215-
`
216-
},
217-
{
218-
code: outdent`
219-
const foo = bar.baz
220-
Array.from(arrayLike).forEach(doSomething)
221-
`,
222-
errors: [{}],
223-
output: outdent`
224-
const foo = bar.baz
225-
;[...arrayLike].forEach(doSomething)
226-
`
227-
},
228-
{
229-
code: outdent`
230-
function* foo() {
231-
yield Array.from(arrayLike).forEach(doSomething)
232-
}
233-
`,
234-
errors: [{}],
235-
output: outdent`
236-
function* foo() {
237-
yield [...arrayLike].forEach(doSomething)
238-
}
239-
`
240-
},
241-
{
242-
code: outdent`
243-
const foo = \`bar\`
244-
Array.from(arrayLike).forEach(doSomething)
245-
`,
246-
errors: [{}],
247-
output: outdent`
248-
const foo = \`bar\`
249-
;[...arrayLike].forEach(doSomething)
250-
`
251-
},
252-
{
253-
code: outdent`
254-
const foo = [];
255-
Array.from(arrayLike).forEach(doSomething)
256-
`,
257-
errors: [{}],
258-
output: outdent`
259-
const foo = [];
260-
[...arrayLike].forEach(doSomething)
261-
`
262-
},
53+
outdent`
54+
const foo = []
55+
Array.from(arrayLike).forEach(doSomething)
56+
`,
57+
outdent`
58+
const foo = "1"
59+
Array.from(arrayLike).forEach(doSomething)
60+
`,
61+
outdent`
62+
const foo = null
63+
Array.from(arrayLike).forEach(doSomething)
64+
`,
65+
outdent`
66+
const foo = true
67+
Array.from(arrayLike).forEach(doSomething)
68+
`,
69+
outdent`
70+
const foo = 1
71+
Array.from(arrayLike).forEach(doSomething)
72+
`,
73+
outdent`
74+
const foo = /./
75+
Array.from(arrayLike).forEach(doSomething)
76+
`,
77+
outdent`
78+
const foo = /./g
79+
Array.from(arrayLike).forEach(doSomething)
80+
`,
81+
outdent`
82+
const foo = bar
83+
Array.from(arrayLike).forEach(doSomething)
84+
`,
85+
outdent`
86+
const foo = bar.baz
87+
Array.from(arrayLike).forEach(doSomething)
88+
`,
89+
outdent`
90+
function* foo() {
91+
yield Array.from(arrayLike).forEach(doSomething)
92+
}
93+
`,
94+
outdent`
95+
const foo = \`bar\`
96+
Array.from(arrayLike).forEach(doSomething)
97+
`,
98+
outdent`
99+
const foo = [];
100+
Array.from(arrayLike).forEach(doSomething)
101+
`,
263102
// https://github.com/angular/angular/blob/9e70bcb34f91d439f5203dc22a44f323d02c4648/packages/benchpress/src/webdriver/selenium_webdriver_adapter.ts#L37
264103
// TokenType of `of` is `Identifier`
265-
{
266-
code: outdent`
267-
for (const key of Array.from(arrayLike)) {
268-
}
269-
`,
270-
errors: [{}],
271-
output: outdent`
272-
for (const key of [...arrayLike]) {
273-
}
274-
`
275-
},
104+
outdent`
105+
for (const key of Array.from(arrayLike)) {
106+
}
107+
`,
276108
// TokenType of `in` is `Keyword`
277-
{
278-
code: outdent`
279-
for (const key in Array.from(arrayLike)) {
280-
}
281-
`,
282-
errors: [{}],
283-
output: outdent`
284-
for (const key in [...arrayLike]) {
285-
}
286-
`
287-
},
109+
outdent`
110+
for (const key in Array.from(arrayLike)) {
111+
}
112+
`,
288113
// https://github.com/facebook/relay/blob/c7dd4cc33eb2dba82629884bff865f0905fc269e/packages/relay-compiler/transforms/ValidateUnusedVariablesTransform.js#L57
289-
{
290-
// eslint-disable-next-line no-template-curly-in-string
291-
code: 'const foo = `${Array.from(arrayLike)}`',
292-
errors: [{}],
293-
// eslint-disable-next-line no-template-curly-in-string
294-
output: 'const foo = `${[...arrayLike]}`'
295-
},
114+
// eslint-disable-next-line no-template-curly-in-string
115+
'const foo = `${Array.from(arrayLike)}`',
296116
// https://github.com/angular/angular/blob/9e70bcb34f91d439f5203dc22a44f323d02c4648/packages/service-worker/worker/testing/cache.ts#L48
297-
{
298-
code: outdent`
299-
async function foo(){
300-
return await Array.from(arrayLike)
301-
}
302-
`,
303-
errors: [{}],
304-
output: outdent`
305-
async function foo(){
306-
return await [...arrayLike]
307-
}
308-
`
309-
},
117+
outdent`
118+
async function foo(){
119+
return await Array.from(arrayLike)
120+
}
121+
`,
310122

311123
// https://github.com/gatsbyjs/gatsby/blob/e720d8efe58eba0f6fae9f26ec8879128967d0b5/packages/gatsby/src/bootstrap/page-hot-reloader.js#L30
312-
{
313-
code: outdent`
314-
foo()
315-
Array.from(arrayLike).forEach(doSomething)
316-
`,
317-
errors: [
318-
{
319-
messageId: MESSAGE_ID
320-
}
321-
],
322-
output: outdent`
323-
foo()
324-
;[...arrayLike].forEach(doSomething)
325-
`
326-
},
124+
outdent`
125+
foo()
126+
Array.from(arrayLike).forEach(doSomething)
127+
`,
327128
// https://github.com/gatsbyjs/gatsby/blob/4ab3f194cf5d6dcafcb2a75d9604aac79d963554/packages/gatsby/src/redux/__tests__/nodes.js#L277
328-
{
329-
code: outdent`
330-
const foo = {}
331-
Array.from(arrayLike).forEach(doSomething)
332-
`,
333-
errors: [
334-
{
335-
messageId: MESSAGE_ID
336-
}
337-
],
338-
output: outdent`
339-
const foo = {}
340-
;[...arrayLike].forEach(doSomething)
341-
`
342-
}
129+
outdent`
130+
const foo = {}
131+
Array.from(arrayLike).forEach(doSomething)
132+
`
343133
]
344134
});

0 commit comments

Comments
 (0)