Skip to content

Commit ff2f4aa

Browse files
committed
feat(no-unused-refs): support template ref api
1 parent 0fbe35e commit ff2f4aa

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/rules/no-unused-refs.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,26 @@ module.exports = {
233233
refsNode = id.parent
234234
}
235235
extractUsedForPattern(refsNode)
236+
},
237+
CallExpression(exp) {
238+
if (
239+
exp.callee.name !== 'useTemplateRef' &&
240+
exp.callee.name !== 'templateRef'
241+
) {
242+
return
243+
}
244+
const arg0 = exp.arguments[0]
245+
if (!arg0) {
246+
return
247+
}
248+
if (arg0.type === 'Literal') {
249+
usedRefs.add(arg0.value)
250+
} else if (
251+
arg0.type === 'TemplateLiteral' &&
252+
arg0.quasis.length === 1
253+
) {
254+
usedRefs.add(arg0.quasis[0].value.cooked)
255+
}
236256
}
237257
}
238258
)

tests/lib/rules/no-unused-refs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,20 @@ tester.run('no-unused-refs', rule, {
315315
const x = ref(null)
316316
</script>
317317
`
318+
},
319+
{
320+
filename: 'test.vue',
321+
code: `
322+
<template>
323+
<input ref="x" />
324+
<input ref="y" />
325+
</template>
326+
<script setup>
327+
import {useTemplateRef} from 'vue'
328+
const inputX = useTemplateRef('x')
329+
const inputY = templateRef(\`y\`)
330+
</script>
331+
`
318332
}
319333
],
320334

0 commit comments

Comments
 (0)