Skip to content

Commit cb02d04

Browse files
committed
feat(prefer-template-ref): support auto-fix
1 parent ebff8d9 commit cb02d04

File tree

2 files changed

+131
-9
lines changed

2 files changed

+131
-9
lines changed

lib/rules/prefer-use-template-ref.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = {
4343
categories: undefined,
4444
url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html'
4545
},
46+
fixable: 'code',
4647
schema: [],
4748
messages: {
4849
preferUseTemplateRef: "Replace '{{name}}' with 'useTemplateRef'."
@@ -98,6 +99,12 @@ module.exports = {
9899
data: {
99100
// @ts-expect-error `scriptRef.callee` is `Identifier`
100101
name: scriptRef.callee.name
102+
},
103+
fix(fixer) {
104+
return fixer.replaceText(
105+
scriptRef,
106+
`useTemplateRef('${templateRef}')`
107+
)
101108
}
102109
})
103110
}

tests/lib/rules/prefer-use-template-ref.js

Lines changed: 124 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,25 @@ tester.run('prefer-use-template-ref', rule, {
311311
const root = ref();
312312
</script>
313313
`,
314+
output: `
315+
<template>
316+
<div ref="root"/>
317+
</template>
318+
<script setup>
319+
import { ref } from 'vue';
320+
const root = useTemplateRef('root');
321+
</script>
322+
`,
314323
errors: [
315324
{
316325
messageId: 'preferUseTemplateRef',
317326
data: {
318327
name: 'ref'
319328
},
320329
line: 7,
321-
column: 22
330+
column: 22,
331+
endLine: 7,
332+
endColumn: 27
322333
}
323334
]
324335
},
@@ -335,14 +346,27 @@ tester.run('prefer-use-template-ref', rule, {
335346
const link = ref();
336347
</script>
337348
`,
349+
output: `
350+
<template>
351+
<button ref="button">Content</button>
352+
<a href="" ref="link">Link</a>
353+
</template>
354+
<script setup>
355+
import { ref } from 'vue';
356+
const buttonRef = ref();
357+
const link = useTemplateRef('link');
358+
</script>
359+
`,
338360
errors: [
339361
{
340362
messageId: 'preferUseTemplateRef',
341363
data: {
342364
name: 'ref'
343365
},
344366
line: 9,
345-
column: 22
367+
column: 22,
368+
endLine: 9,
369+
endColumn: 27
346370
}
347371
]
348372
},
@@ -359,22 +383,37 @@ tester.run('prefer-use-template-ref', rule, {
359383
const link = ref();
360384
</script>
361385
`,
386+
output: `
387+
<template>
388+
<h1 ref="heading">Heading</h1>
389+
<a href="" ref="link">Link</a>
390+
</template>
391+
<script setup>
392+
import { ref } from 'vue';
393+
const heading = useTemplateRef('heading');
394+
const link = useTemplateRef('link');
395+
</script>
396+
`,
362397
errors: [
363398
{
364399
messageId: 'preferUseTemplateRef',
365400
data: {
366401
name: 'ref'
367402
},
368403
line: 8,
369-
column: 25
404+
column: 25,
405+
endLine: 8,
406+
endColumn: 30
370407
},
371408
{
372409
messageId: 'preferUseTemplateRef',
373410
data: {
374411
name: 'ref'
375412
},
376413
line: 9,
377-
column: 22
414+
column: 22,
415+
endLine: 9,
416+
endColumn: 27
378417
}
379418
]
380419
},
@@ -396,14 +435,32 @@ tester.run('prefer-use-template-ref', rule, {
396435
}
397436
</script>
398437
`,
438+
output: `
439+
<template>
440+
<p>Button clicked {{counter}} times.</p>
441+
<button ref="button">Click</button>
442+
</template>
443+
<script>
444+
import { ref } from 'vue';
445+
export default {
446+
name: 'Counter',
447+
setup() {
448+
const counter = ref(0);
449+
const button = useTemplateRef('button');
450+
}
451+
}
452+
</script>
453+
`,
399454
errors: [
400455
{
401456
messageId: 'preferUseTemplateRef',
402457
data: {
403458
name: 'ref'
404459
},
405460
line: 12,
406-
column: 28
461+
column: 28,
462+
endLine: 12,
463+
endColumn: 33
407464
}
408465
]
409466
},
@@ -418,14 +475,25 @@ tester.run('prefer-use-template-ref', rule, {
418475
const root = shallowRef();
419476
</script>
420477
`,
478+
output: `
479+
<template>
480+
<div ref="root"/>
481+
</template>
482+
<script setup>
483+
import { shallowRef } from 'vue';
484+
const root = useTemplateRef('root');
485+
</script>
486+
`,
421487
errors: [
422488
{
423489
messageId: 'preferUseTemplateRef',
424490
data: {
425491
name: 'shallowRef'
426492
},
427493
line: 7,
428-
column: 22
494+
column: 22,
495+
endLine: 7,
496+
endColumn: 34
429497
}
430498
]
431499
},
@@ -444,14 +512,29 @@ tester.run('prefer-use-template-ref', rule, {
444512
}
445513
</script>
446514
`,
515+
output: `
516+
<template>
517+
<button ref="button">Click</button>
518+
</template>
519+
<script>
520+
import { ref } from 'vue';
521+
export default {
522+
setup: () => {
523+
const button = useTemplateRef('button');
524+
}
525+
}
526+
</script>
527+
`,
447528
errors: [
448529
{
449530
messageId: 'preferUseTemplateRef',
450531
data: {
451532
name: 'ref'
452533
},
453534
line: 9,
454-
column: 28
535+
column: 28,
536+
endLine: 9,
537+
endColumn: 33
455538
}
456539
]
457540
},
@@ -467,6 +550,20 @@ tester.run('prefer-use-template-ref', rule, {
467550
const root = ref()
468551
</script>
469552
553+
<script>
554+
const A = 'foo'
555+
</script>
556+
`,
557+
output: `
558+
<template>
559+
<div ref="root" :data-a="A" />
560+
</template>
561+
562+
<script setup>
563+
import { ref } from 'vue'
564+
const root = useTemplateRef('root')
565+
</script>
566+
470567
<script>
471568
const A = 'foo'
472569
</script>
@@ -478,7 +575,9 @@ tester.run('prefer-use-template-ref', rule, {
478575
name: 'ref'
479576
},
480577
line: 8,
481-
column: 20
578+
column: 20,
579+
endLine: 8,
580+
endColumn: 25
482581
}
483582
]
484583
},
@@ -498,14 +597,30 @@ tester.run('prefer-use-template-ref', rule, {
498597
const root = ref()
499598
</script>
500599
`,
600+
output: `
601+
<template>
602+
<div ref="root" :data-a="A" />
603+
</template>
604+
605+
<script>
606+
const A = 'foo'
607+
</script>
608+
609+
<script setup>
610+
import { ref } from 'vue'
611+
const root = useTemplateRef('root')
612+
</script>
613+
`,
501614
errors: [
502615
{
503616
messageId: 'preferUseTemplateRef',
504617
data: {
505618
name: 'ref'
506619
},
507620
line: 12,
508-
column: 20
621+
column: 20,
622+
endLine: 12,
623+
endColumn: 25
509624
}
510625
]
511626
}

0 commit comments

Comments
 (0)