Skip to content

Commit c741b59

Browse files
committed
refactor(prefer-use-template-ref): optimize array processing with flatMap
1 parent 7523350 commit c741b59

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ function convertDeclaratorToScriptRef(declarator) {
2929
* @returns {ScriptRef[]}
3030
* */
3131
function getScriptRefsFromSetupFunction(body) {
32-
/** @type {VariableDeclaration[]} */
33-
const variableDeclarations = body.filter(
34-
(child) => child.type === 'VariableDeclaration'
35-
)
36-
const variableDeclarators = variableDeclarations.map(
37-
(declaration) => declaration.declarations[0]
38-
)
39-
const refDeclarators = variableDeclarators.filter((declarator) =>
40-
// @ts-ignore
41-
['ref', 'shallowRef'].includes(declarator.init?.callee?.name)
42-
)
32+
return body.flatMap((child) => {
33+
if (child.type === 'VariableDeclaration') {
34+
const declarator = child.declarations[0]
35+
36+
if (
37+
declarator.init?.type === 'CallExpression' &&
38+
declarator.init.callee?.type === 'Identifier' &&
39+
['ref', 'shallowRef'].includes(declarator.init?.callee?.name)
40+
)
41+
return [convertDeclaratorToScriptRef(declarator)]
42+
}
4343

44-
return refDeclarators.map(convertDeclaratorToScriptRef)
44+
return []
45+
})
4546
}
4647

4748
/** @type {import("eslint").Rule.RuleModule} */

0 commit comments

Comments
 (0)