Skip to content

Commit ebff8d9

Browse files
committed
refactor: generate Map
1 parent 3f4795c commit ebff8d9

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const utils = require('../utils')
88

99
/**
1010
* @typedef ScriptRef
11-
* @type {{node: Expression, ref: string}}
11+
* @type {[string, CallExpression]}
1212
*/
1313

1414
/**
@@ -26,10 +26,7 @@ function getScriptRefsFromSetupFunction(body) {
2626
declarator.id.type === 'Identifier' &&
2727
['ref', 'shallowRef'].includes(declarator.init.callee.name)
2828
)
29-
return {
30-
node: declarator.init,
31-
ref: declarator.id.name
32-
}
29+
return [[declarator.id.name, declarator.init]]
3330
}
3431

3532
return []
@@ -86,21 +83,21 @@ module.exports = {
8683
}),
8784
{
8885
'Program:exit'() {
86+
const scriptRefsMap = new Map(scriptRefs)
87+
8988
for (const templateRef of templateRefs) {
90-
const scriptRef = scriptRefs.find(
91-
(scriptRef) => scriptRef.ref === templateRef
92-
)
89+
const scriptRef = scriptRefsMap.get(templateRef)
9390

9491
if (!scriptRef) {
9592
continue
9693
}
9794

9895
context.report({
99-
node: scriptRef.node,
96+
node: scriptRef,
10097
messageId: 'preferUseTemplateRef',
10198
data: {
102-
// @ts-ignore
103-
name: scriptRef.node?.callee?.name
99+
// @ts-expect-error `scriptRef.callee` is `Identifier`
100+
name: scriptRef.callee.name
104101
}
105102
})
106103
}

0 commit comments

Comments
 (0)