Skip to content

Commit 4baf3ab

Browse files
authored
fix(prefer-use-template-ref): fix crash when use non-block setup function (#2636)
1 parent 9d8f2de commit 4baf3ab

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ module.exports = {
7878
}),
7979
utils.defineVueVisitor(context, {
8080
onSetupFunctionEnter(node) {
81-
// @ts-ignore
81+
if (node.type === 'ArrowFunctionExpression' && node.expression) {
82+
return
83+
}
8284
const newScriptRefs = getScriptRefsFromSetupFunction(node.body.body)
83-
8485
scriptRefs.push(...newScriptRefs)
8586
}
8687
}),

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

+37
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ tester.run('prefer-use-template-ref', rule, {
252252
}
253253
</script>
254254
`
255+
},
256+
{
257+
filename: 'non-block-arrow-setup-function.vue',
258+
code: `
259+
<script>
260+
import { defineComponent } from 'vue';
261+
export default defineComponent({
262+
setup: () => ({})
263+
})
264+
</script>
265+
`
255266
}
256267
],
257268
invalid: [
@@ -383,6 +394,32 @@ tester.run('prefer-use-template-ref', rule, {
383394
column: 22
384395
}
385396
]
397+
},
398+
{
399+
filename: 'block-arrow-setup-function.vue',
400+
code: `
401+
<template>
402+
<button ref="button">Click</button>
403+
</template>
404+
<script>
405+
import { ref } from 'vue';
406+
export default {
407+
setup: () => {
408+
const button = ref();
409+
}
410+
}
411+
</script>
412+
`,
413+
errors: [
414+
{
415+
messageId: 'preferUseTemplateRef',
416+
data: {
417+
name: 'ref'
418+
},
419+
line: 9,
420+
column: 28
421+
}
422+
]
386423
}
387424
]
388425
})

0 commit comments

Comments
 (0)