Skip to content

Commit

Permalink
fix(prefer-use-template-ref): fix crash when use non-block setup func…
Browse files Browse the repository at this point in the history
…tion (#2636)
  • Loading branch information
ntnyq authored Jan 8, 2025
1 parent 9d8f2de commit 4baf3ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rules/prefer-use-template-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ module.exports = {
}),
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node) {
// @ts-ignore
if (node.type === 'ArrowFunctionExpression' && node.expression) {
return
}
const newScriptRefs = getScriptRefsFromSetupFunction(node.body.body)

scriptRefs.push(...newScriptRefs)
}
}),
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/prefer-use-template-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ tester.run('prefer-use-template-ref', rule, {
}
</script>
`
},
{
filename: 'non-block-arrow-setup-function.vue',
code: `
<script>
import { defineComponent } from 'vue';
export default defineComponent({
setup: () => ({})
})
</script>
`
}
],
invalid: [
Expand Down Expand Up @@ -383,6 +394,32 @@ tester.run('prefer-use-template-ref', rule, {
column: 22
}
]
},
{
filename: 'block-arrow-setup-function.vue',
code: `
<template>
<button ref="button">Click</button>
</template>
<script>
import { ref } from 'vue';
export default {
setup: () => {
const button = ref();
}
}
</script>
`,
errors: [
{
messageId: 'preferUseTemplateRef',
data: {
name: 'ref'
},
line: 9,
column: 28
}
]
}
]
})

0 comments on commit 4baf3ab

Please sign in to comment.