Skip to content

Commit 0b8db67

Browse files
committed
feat: support typeArguments
1 parent 70eb38b commit 0b8db67

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = {
5151
},
5252
/** @param {RuleContext} context */
5353
create(context) {
54+
const sourceCode = context.sourceCode
5455
/** @type Set<string> */
5556
const templateRefs = new Set()
5657

@@ -100,9 +101,12 @@ module.exports = {
100101
name: /** @type {Identifier} */ (scriptRef.callee).name
101102
},
102103
fix(fixer) {
104+
const typeArgs =
105+
scriptRef.typeArguments ?? scriptRef.typeParameters
106+
103107
return fixer.replaceText(
104108
scriptRef,
105-
`useTemplateRef('${templateRef}')`
109+
`useTemplateRef${typeArgs ? sourceCode.getText(typeArgs) : ''}('${templateRef}')`
106110
)
107111
}
108112
})

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,42 @@ tester.run('prefer-use-template-ref', rule, {
623623
endColumn: 25
624624
}
625625
]
626+
},
627+
{
628+
filename: 'ref-with-typeArguments.vue',
629+
languageOptions: {
630+
parserOptions: {
631+
parser: require('@typescript-eslint/parser')
632+
}
633+
},
634+
code: `
635+
<template>
636+
<div ref="root" />
637+
</template>
638+
<script setup lang="ts">
639+
const root = ref<HTMLElement | null>()
640+
</script>
641+
`,
642+
output: `
643+
<template>
644+
<div ref="root" />
645+
</template>
646+
<script setup lang="ts">
647+
const root = useTemplateRef<HTMLElement | null>('root')
648+
</script>
649+
`,
650+
errors: [
651+
{
652+
messageId: 'preferUseTemplateRef',
653+
data: {
654+
name: 'ref'
655+
},
656+
line: 6,
657+
column: 22,
658+
endLine: 6,
659+
endColumn: 47
660+
}
661+
]
626662
}
627663
]
628664
})

0 commit comments

Comments
 (0)