Skip to content

Commit c2be9f1

Browse files
committed
refactor: optimize code
1 parent 78c21d2 commit c2be9f1

File tree

2 files changed

+100
-100
lines changed

2 files changed

+100
-100
lines changed

lib/rules/no-ref-as-operand.js

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ module.exports = {
124124
emitReferenceIds.has(node.callee)
125125
) {
126126
// verify setup(props,{emit}) {emit()}
127-
node.arguments
128-
.filter((node) => node.type === 'Identifier')
129-
.forEach((node) => {
130-
reportIfRefWrapped(node)
131-
})
127+
const nodes = node.arguments.filter(
128+
(node) => node.type === 'Identifier'
129+
)
130+
for (const node of nodes) {
131+
reportIfRefWrapped(node)
132+
}
132133
} else {
133134
const emit = getCalleeMemberNode(node)
134135
if (
@@ -138,11 +139,12 @@ module.exports = {
138139
contextReferenceIds.has(emit.member.object)
139140
) {
140141
// verify setup(props,context) {context.emit()}
141-
node.arguments
142-
.filter((node) => node.type === 'Identifier')
143-
.forEach((node) => {
144-
reportIfRefWrapped(node)
145-
})
142+
const nodes = node.arguments.filter(
143+
(node) => node.type === 'Identifier'
144+
)
145+
for (const node of nodes) {
146+
reportIfRefWrapped(node)
147+
}
146148
}
147149
}
148150
}
@@ -235,101 +237,99 @@ module.exports = {
235237
reportIfRefWrapped(node)
236238
}
237239
},
238-
utils.compositingVisitors(
239-
utils.defineScriptSetupVisitor(context, {
240-
onDefineEmitsEnter(node) {
241-
if (
242-
!node.parent ||
243-
node.parent.type !== 'VariableDeclarator' ||
244-
node.parent.init !== node
245-
) {
246-
return
247-
}
240+
utils.defineScriptSetupVisitor(context, {
241+
onDefineEmitsEnter(node) {
242+
if (
243+
!node.parent ||
244+
node.parent.type !== 'VariableDeclarator' ||
245+
node.parent.init !== node
246+
) {
247+
return
248+
}
249+
250+
const emitParam = node.parent.id
251+
if (emitParam.type !== 'Identifier') {
252+
return
253+
}
248254

249-
const emitParam = node.parent.id
250-
if (emitParam.type !== 'Identifier') {
255+
// const emit = defineEmits()
256+
const variable = findVariable(
257+
utils.getScope(context, emitParam),
258+
emitParam
259+
)
260+
if (!variable) {
261+
return
262+
}
263+
const emitReferenceIds = new Set()
264+
for (const reference of variable.references) {
265+
emitReferenceIds.add(reference.identifier)
266+
}
267+
setupContexts.set(programNode, {
268+
contextReferenceIds: new Set(),
269+
emitReferenceIds
270+
})
271+
},
272+
...callVisitor
273+
}),
274+
utils.defineVueVisitor(context, {
275+
onSetupFunctionEnter(node, { node: vueNode }) {
276+
const contextParam = utils.skipDefaultParamValue(node.params[1])
277+
if (!contextParam) {
278+
// no arguments
279+
return
280+
}
281+
if (
282+
contextParam.type === 'RestElement' ||
283+
contextParam.type === 'ArrayPattern'
284+
) {
285+
// cannot check
286+
return
287+
}
288+
const contextReferenceIds = new Set()
289+
const emitReferenceIds = new Set()
290+
if (contextParam.type === 'ObjectPattern') {
291+
const emitProperty = utils.findAssignmentProperty(
292+
contextParam,
293+
'emit'
294+
)
295+
if (!emitProperty || emitProperty.value.type !== 'Identifier') {
251296
return
252297
}
253-
254-
// const emit = defineEmits()
298+
const emitParam = emitProperty.value
299+
// `setup(props, {emit})`
255300
const variable = findVariable(
256301
utils.getScope(context, emitParam),
257302
emitParam
258303
)
259304
if (!variable) {
260305
return
261306
}
262-
const emitReferenceIds = new Set()
263307
for (const reference of variable.references) {
264308
emitReferenceIds.add(reference.identifier)
265309
}
266-
setupContexts.set(programNode, {
267-
contextReferenceIds: new Set(),
268-
emitReferenceIds
269-
})
270-
},
271-
...callVisitor
272-
}),
273-
utils.defineVueVisitor(context, {
274-
onSetupFunctionEnter(node, { node: vueNode }) {
275-
const contextParam = utils.skipDefaultParamValue(node.params[1])
276-
if (!contextParam) {
277-
// no arguments
278-
return
279-
}
280-
if (
281-
contextParam.type === 'RestElement' ||
282-
contextParam.type === 'ArrayPattern'
283-
) {
284-
// cannot check
310+
} else {
311+
// `setup(props, context)`
312+
const variable = findVariable(
313+
utils.getScope(context, contextParam),
314+
contextParam
315+
)
316+
if (!variable) {
285317
return
286318
}
287-
const contextReferenceIds = new Set()
288-
const emitReferenceIds = new Set()
289-
if (contextParam.type === 'ObjectPattern') {
290-
const emitProperty = utils.findAssignmentProperty(
291-
contextParam,
292-
'emit'
293-
)
294-
if (!emitProperty || emitProperty.value.type !== 'Identifier') {
295-
return
296-
}
297-
const emitParam = emitProperty.value
298-
// `setup(props, {emit})`
299-
const variable = findVariable(
300-
utils.getScope(context, emitParam),
301-
emitParam
302-
)
303-
if (!variable) {
304-
return
305-
}
306-
for (const reference of variable.references) {
307-
emitReferenceIds.add(reference.identifier)
308-
}
309-
} else {
310-
// `setup(props, context)`
311-
const variable = findVariable(
312-
utils.getScope(context, contextParam),
313-
contextParam
314-
)
315-
if (!variable) {
316-
return
317-
}
318-
for (const reference of variable.references) {
319-
contextReferenceIds.add(reference.identifier)
320-
}
319+
for (const reference of variable.references) {
320+
contextReferenceIds.add(reference.identifier)
321321
}
322-
setupContexts.set(vueNode, {
323-
contextReferenceIds,
324-
emitReferenceIds
325-
})
326-
},
327-
...callVisitor,
328-
onVueObjectExit(node) {
329-
setupContexts.delete(node)
330322
}
331-
})
332-
)
323+
setupContexts.set(vueNode, {
324+
contextReferenceIds,
325+
emitReferenceIds
326+
})
327+
},
328+
...callVisitor,
329+
onVueObjectExit(node) {
330+
setupContexts.delete(node)
331+
}
332+
})
333333
)
334334
}
335335
}

tests/lib/rules/no-ref-as-operand.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ tester.run('no-ref-as-operand', rule, {
291291
}
292292
})
293293
</script>
294-
`,
294+
`
295295
],
296296
invalid: [
297297
{
@@ -951,8 +951,8 @@ tester.run('no-ref-as-operand', rule, {
951951
message:
952952
'Must use `.value` to read or write the value wrapped by `ref()`.',
953953
line: 8,
954-
endLine: 8,
955-
},
954+
endLine: 8
955+
}
956956
]
957957
},
958958
{
@@ -974,7 +974,7 @@ tester.run('no-ref-as-operand', rule, {
974974
})
975975
</script>
976976
`,
977-
output:`
977+
output: `
978978
<script>
979979
import { ref, defineComponent } from 'vue'
980980
@@ -997,8 +997,8 @@ tester.run('no-ref-as-operand', rule, {
997997
message:
998998
'Must use `.value` to read or write the value wrapped by `ref()`.',
999999
line: 10,
1000-
endLine: 10,
1001-
},
1000+
endLine: 10
1001+
}
10021002
]
10031003
},
10041004
{
@@ -1020,7 +1020,7 @@ tester.run('no-ref-as-operand', rule, {
10201020
})
10211021
</script>
10221022
`,
1023-
output:`
1023+
output: `
10241024
<script>
10251025
import { ref, defineComponent } from 'vue'
10261026
@@ -1043,8 +1043,8 @@ tester.run('no-ref-as-operand', rule, {
10431043
message:
10441044
'Must use `.value` to read or write the value wrapped by `ref()`.',
10451045
line: 10,
1046-
endLine: 10,
1047-
},
1046+
endLine: 10
1047+
}
10481048
]
10491049
},
10501050
{
@@ -1066,7 +1066,7 @@ tester.run('no-ref-as-operand', rule, {
10661066
})
10671067
</script>
10681068
`,
1069-
output:`
1069+
output: `
10701070
<script>
10711071
import { ref, defineComponent } from 'vue'
10721072
@@ -1089,8 +1089,8 @@ tester.run('no-ref-as-operand', rule, {
10891089
message:
10901090
'Must use `.value` to read or write the value wrapped by `ref()`.',
10911091
line: 10,
1092-
endLine: 10,
1093-
},
1092+
endLine: 10
1093+
}
10941094
]
10951095
},
10961096
// Auto-import

0 commit comments

Comments
 (0)