Skip to content

Commit 5c80828

Browse files
auvredJoshuaKGoldbergkirkwaiblinger
authored
feat(rule-tester): support multipass fixes (typescript-eslint#8883)
* feat(rule-tester): support multipass fixes * docs: mention multi-pass fixes in rule tester docs * Update docs/packages/Rule_Tester.mdx Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com> * Update packages/rule-tester/src/RuleTester.ts Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com> * refactor: break if fixer doesn't change the code --------- Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com> Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
1 parent 5753fe5 commit 5c80828

File tree

10 files changed

+603
-124
lines changed

10 files changed

+603
-124
lines changed

docs/packages/Rule_Tester.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ ruleTester.run('my-rule', rule, {
7575
/* ... */
7676
],
7777
},
78+
// Multi-pass fixes can be tested using the array form of output.
79+
// Note: this is unique to typescript-eslint, and doesn't exist in ESLint core.
80+
{
81+
code: 'const d = 1;',
82+
output: ['const e = 1;', 'const f = 1;'],
83+
errors: [
84+
/* ... */
85+
],
86+
},
7887

7988
// suggestions can be tested via errors
8089
{

packages/eslint-plugin-internal/tests/rules/plugin-test-formatting.test.ts

Lines changed: 133 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ const test = [
206206
},
207207
{
208208
code: wrap`'for (const x of y) {}'`,
209-
output: wrap`\`for (const x of y) {
209+
output: [
210+
wrap`\`for (const x of y) {
210211
}\``,
212+
wrap`\`
213+
for (const x of y) {
214+
}
215+
\``,
216+
wrap`\`
217+
for (const x of y) {
218+
}
219+
${PARENT_INDENT}\``,
220+
],
211221
errors: [
212222
{
213223
messageId: 'invalidFormatting',
@@ -217,8 +227,18 @@ const test = [
217227
{
218228
code: wrap`'for (const x of \`asdf\`) {}'`,
219229
// make sure it escapes the backticks
220-
output: wrap`\`for (const x of \\\`asdf\\\`) {
230+
output: [
231+
wrap`\`for (const x of \\\`asdf\\\`) {
221232
}\``,
233+
wrap`\`
234+
for (const x of \\\`asdf\\\`) {
235+
}
236+
\``,
237+
wrap`\`
238+
for (const x of \\\`asdf\\\`) {
239+
}
240+
${PARENT_INDENT}\``,
241+
],
222242
errors: [
223243
{
224244
messageId: 'invalidFormatting',
@@ -238,7 +258,7 @@ const test = [
238258
},
239259
{
240260
code: wrap`\`const a = '1'\``,
241-
output: wrap`"const a = '1'"`,
261+
output: [wrap`"const a = '1'"`, wrap`"const a = '1';"`],
242262
errors: [
243263
{
244264
messageId: 'singleLineQuotes',
@@ -247,7 +267,7 @@ const test = [
247267
},
248268
{
249269
code: wrap`\`const a = "1";\``,
250-
output: wrap`'const a = "1";'`,
270+
output: [wrap`'const a = "1";'`, wrap`"const a = '1';"`],
251271
errors: [
252272
{
253273
messageId: 'singleLineQuotes',
@@ -258,9 +278,14 @@ const test = [
258278
{
259279
code: wrap`\`const a = "1";
260280
${PARENT_INDENT}\``,
261-
output: wrap`\`
281+
output: [
282+
wrap`\`
262283
const a = "1";
263284
${PARENT_INDENT}\``,
285+
wrap`\`
286+
const a = '1';
287+
${PARENT_INDENT}\``,
288+
],
264289
errors: [
265290
{
266291
messageId: 'templateLiteralEmptyEnds',
@@ -270,9 +295,17 @@ ${PARENT_INDENT}\``,
270295
{
271296
code: wrap`\`
272297
${CODE_INDENT}const a = "1";\``,
273-
output: wrap`\`
298+
output: [
299+
wrap`\`
274300
${CODE_INDENT}const a = "1";
275301
\``,
302+
wrap`\`
303+
${CODE_INDENT}const a = "1";
304+
${PARENT_INDENT}\``,
305+
wrap`\`
306+
${CODE_INDENT}const a = '1';
307+
${PARENT_INDENT}\``,
308+
],
276309
errors: [
277310
{
278311
messageId: 'templateLiteralEmptyEnds',
@@ -282,10 +315,20 @@ ${CODE_INDENT}const a = "1";
282315
{
283316
code: wrap`\`const a = "1";
284317
${CODE_INDENT}const b = "2";\``,
285-
output: wrap`\`
318+
output: [
319+
wrap`\`
286320
const a = "1";
287321
${CODE_INDENT}const b = "2";
288322
\``,
323+
wrap`\`
324+
const a = "1";
325+
${CODE_INDENT}const b = "2";
326+
${PARENT_INDENT}\``,
327+
wrap`\`
328+
const a = '1';
329+
const b = '2';
330+
${PARENT_INDENT}\``,
331+
],
289332
errors: [
290333
{
291334
messageId: 'templateLiteralEmptyEnds',
@@ -297,9 +340,14 @@ ${CODE_INDENT}const b = "2";
297340
code: wrap`\`
298341
${CODE_INDENT}const a = "1";
299342
\``,
300-
output: wrap`\`
343+
output: [
344+
wrap`\`
301345
${CODE_INDENT}const a = "1";
302346
${PARENT_INDENT}\``,
347+
wrap`\`
348+
${CODE_INDENT}const a = '1';
349+
${PARENT_INDENT}\``,
350+
],
303351
errors: [
304352
{
305353
messageId: 'templateLiteralLastLineIndent',
@@ -310,9 +358,14 @@ ${PARENT_INDENT}\``,
310358
code: wrap`\`
311359
${CODE_INDENT}const a = "1";
312360
\``,
313-
output: wrap`\`
361+
output: [
362+
wrap`\`
314363
${CODE_INDENT}const a = "1";
315364
${PARENT_INDENT}\``,
365+
wrap`\`
366+
${CODE_INDENT}const a = '1';
367+
${PARENT_INDENT}\``,
368+
],
316369
errors: [
317370
{
318371
messageId: 'templateLiteralLastLineIndent',
@@ -483,7 +536,8 @@ ruleTester.run({
483536
],
484537
});
485538
`,
486-
output: `
539+
output: [
540+
`
487541
ruleTester.run({
488542
valid: [
489543
{
@@ -517,6 +571,75 @@ foo
517571
],
518572
});
519573
`,
574+
`
575+
ruleTester.run({
576+
valid: [
577+
{
578+
code: 'foo;',
579+
},
580+
{
581+
code: \`
582+
foo
583+
\`,
584+
},
585+
{
586+
code: \`
587+
foo
588+
\`,
589+
},
590+
],
591+
invalid: [
592+
{
593+
code: 'foo;',
594+
},
595+
{
596+
code: \`
597+
foo
598+
\`,
599+
},
600+
{
601+
code: \`
602+
foo
603+
\`,
604+
},
605+
],
606+
});
607+
`,
608+
`
609+
ruleTester.run({
610+
valid: [
611+
{
612+
code: 'foo;',
613+
},
614+
{
615+
code: \`
616+
foo;
617+
\`,
618+
},
619+
{
620+
code: \`
621+
foo
622+
\`,
623+
},
624+
],
625+
invalid: [
626+
{
627+
code: 'foo;',
628+
},
629+
{
630+
code: \`
631+
foo;
632+
\`,
633+
},
634+
{
635+
code: \`
636+
foo
637+
\`,
638+
},
639+
],
640+
});
641+
`,
642+
],
520643
errors: [
521644
{
522645
messageId: 'singleLineQuotes',

packages/eslint-plugin/tests/RuleTester.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export function batchedSingleLineTests<
4444
MessageIds extends string,
4545
Options extends readonly unknown[],
4646
>(
47-
options: InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>,
47+
options:
48+
| (Omit<InvalidTestCase<MessageIds, Options>, 'output'> & {
49+
output?: string | null;
50+
})
51+
| ValidTestCase<Options>,
4852
): (InvalidTestCase<MessageIds, Options> | ValidTestCase<Options>)[] {
4953
// -- eslint counts lines from 1
5054
const lineOffset = options.code.startsWith('\n') ? 2 : 1;

packages/eslint-plugin/tests/rules/no-useless-template-literals.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,16 @@ declare const nested: string, interpolation: string;
409409
\`le\${ \`ss\` }\`
410410
}\`;
411411
`,
412-
output: `
412+
output: [
413+
`
413414
\`use\${
414415
\`less\`
415416
}\`;
416417
`,
418+
`
419+
\`useless\`;
420+
`,
421+
],
417422
errors: [
418423
{
419424
messageId: 'noUselessTemplateLiteral',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy