From 8e715044f84ab7ebe5ae18ee204d658f455edb39 Mon Sep 17 00:00:00 2001 From: ntnyq Date: Wed, 4 Dec 2024 00:43:00 +0800 Subject: [PATCH] fix(prefer-use-template-ref): fix crash when use non-block setup function --- lib/rules/prefer-use-template-ref.js | 5 +-- tests/lib/rules/prefer-use-template-ref.js | 37 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-use-template-ref.js b/lib/rules/prefer-use-template-ref.js index 7d01958b7..eb8b8c06d 100644 --- a/lib/rules/prefer-use-template-ref.js +++ b/lib/rules/prefer-use-template-ref.js @@ -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) } }), diff --git a/tests/lib/rules/prefer-use-template-ref.js b/tests/lib/rules/prefer-use-template-ref.js index 77020cdcf..414c69830 100644 --- a/tests/lib/rules/prefer-use-template-ref.js +++ b/tests/lib/rules/prefer-use-template-ref.js @@ -252,6 +252,17 @@ tester.run('prefer-use-template-ref', rule, { } ` + }, + { + filename: 'non-block-arrow-setup-function.vue', + code: ` + + ` } ], invalid: [ @@ -383,6 +394,32 @@ tester.run('prefer-use-template-ref', rule, { column: 22 } ] + }, + { + filename: 'block-arrow-setup-function.vue', + code: ` + + + `, + errors: [ + { + messageId: 'preferUseTemplateRef', + data: { + name: 'ref' + }, + line: 9, + column: 28 + } + ] } ] })