Skip to content

Commit 54a99c5

Browse files
authored
refactor: store the defineEmits variable name (#2592)
1 parent 9205cc8 commit 54a99c5

File tree

2 files changed

+26
-51
lines changed

2 files changed

+26
-51
lines changed

lib/rules/no-unused-emit-declarations.js

+15-26
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ function hasReferenceId(value, setupContext) {
5656
)
5757
}
5858

59-
/**
60-
* Check if the given name matches emitReferenceIds variable name
61-
* @param {string} name
62-
* @param {Set<Identifier>} emitReferenceIds
63-
* @returns {boolean}
64-
*/
65-
function isEmitVariableName(name, emitReferenceIds) {
66-
if (emitReferenceIds.size === 0) return false
67-
const emitVariable = emitReferenceIds.values().next().value.name
68-
return emitVariable === name
69-
}
70-
7159
module.exports = {
7260
meta: {
7361
type: 'suggestion',
@@ -91,6 +79,7 @@ module.exports = {
9179
/** @type {Map<ObjectExpression | Program, SetupContext>} */
9280
const setupContexts = new Map()
9381
const programNode = context.getSourceCode().ast
82+
let emitParamName = ''
9483

9584
/**
9685
* @param {CallExpression} node
@@ -204,14 +193,6 @@ module.exports = {
204193

205194
const { contextReferenceIds, emitReferenceIds } = setupContext
206195

207-
// verify defineEmits variable in template
208-
if (
209-
callee.type === 'Identifier' &&
210-
isEmitVariableName(callee.name, emitReferenceIds)
211-
) {
212-
addEmitCall(node)
213-
}
214-
215196
// verify setup(props,{emit}) {emit()}
216197
addEmitCallByReference(callee, emitReferenceIds, node)
217198
if (emit && emit.name === 'emit') {
@@ -229,8 +210,11 @@ module.exports = {
229210
}
230211
}
231212

232-
// verify $emit() in template
233-
if (callee.type === 'Identifier' && callee.name === '$emit') {
213+
// verify $emit() and defineEmits variable in template
214+
if (
215+
callee.type === 'Identifier' &&
216+
(callee.name === '$emit' || callee.name === emitParamName)
217+
) {
234218
addEmitCall(node)
235219
}
236220
}
@@ -316,10 +300,15 @@ module.exports = {
316300
}
317301

318302
const emitParam = node.parent.id
319-
const variable =
320-
emitParam.type === 'Identifier'
321-
? findVariable(utils.getScope(context, emitParam), emitParam)
322-
: null
303+
if (emitParam.type !== 'Identifier') {
304+
return
305+
}
306+
307+
emitParamName = emitParam.name
308+
const variable = findVariable(
309+
utils.getScope(context, emitParam),
310+
emitParam
311+
)
323312
if (!variable) {
324313
return
325314
}

lib/rules/require-explicit-emits.js

+11-25
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,6 @@ function getNameParamNode(node) {
7171
return null
7272
}
7373

74-
/**
75-
* Check if the given name matches defineEmitsNode variable name
76-
* @param {string} name
77-
* @param {CallExpression | undefined} defineEmitsNode
78-
* @returns {boolean}
79-
*/
80-
function isEmitVariableName(name, defineEmitsNode) {
81-
const node = defineEmitsNode?.parent
82-
83-
if (node?.type === 'VariableDeclarator' && node.id.type === 'Identifier') {
84-
return name === node.id.name
85-
}
86-
87-
return false
88-
}
89-
9074
module.exports = {
9175
meta: {
9276
type: 'suggestion',
@@ -128,6 +112,7 @@ module.exports = {
128112
const vueEmitsDeclarations = new Map()
129113
/** @type {Map<ObjectExpression | Program, ComponentProp[]>} */
130114
const vuePropsDeclarations = new Map()
115+
let emitParamName = ''
131116

132117
/**
133118
* @typedef {object} VueTemplateDefineData
@@ -271,11 +256,7 @@ module.exports = {
271256
// e.g. $emit() / emit() in template
272257
if (
273258
callee.type === 'Identifier' &&
274-
(callee.name === '$emit' ||
275-
isEmitVariableName(
276-
callee.name,
277-
vueTemplateDefineData.defineEmits
278-
))
259+
(callee.name === '$emit' || callee.name === emitParamName)
279260
) {
280261
verifyEmit(
281262
vueTemplateDefineData.emits,
@@ -308,10 +289,15 @@ module.exports = {
308289
}
309290

310291
const emitParam = node.parent.id
311-
const variable =
312-
emitParam.type === 'Identifier'
313-
? findVariable(utils.getScope(context, emitParam), emitParam)
314-
: null
292+
if (emitParam.type !== 'Identifier') {
293+
return
294+
}
295+
296+
emitParamName = emitParam.name
297+
const variable = findVariable(
298+
utils.getScope(context, emitParam),
299+
emitParam
300+
)
315301
if (!variable) {
316302
return
317303
}

0 commit comments

Comments
 (0)