Skip to content

Commit

Permalink
refactor: store the defineEmits variable name (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh authored Nov 11, 2024
1 parent 9205cc8 commit 54a99c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 51 deletions.
41 changes: 15 additions & 26 deletions lib/rules/no-unused-emit-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ function hasReferenceId(value, setupContext) {
)
}

/**
* Check if the given name matches emitReferenceIds variable name
* @param {string} name
* @param {Set<Identifier>} emitReferenceIds
* @returns {boolean}
*/
function isEmitVariableName(name, emitReferenceIds) {
if (emitReferenceIds.size === 0) return false
const emitVariable = emitReferenceIds.values().next().value.name
return emitVariable === name
}

module.exports = {
meta: {
type: 'suggestion',
Expand All @@ -91,6 +79,7 @@ module.exports = {
/** @type {Map<ObjectExpression | Program, SetupContext>} */
const setupContexts = new Map()
const programNode = context.getSourceCode().ast
let emitParamName = ''

/**
* @param {CallExpression} node
Expand Down Expand Up @@ -204,14 +193,6 @@ module.exports = {

const { contextReferenceIds, emitReferenceIds } = setupContext

// verify defineEmits variable in template
if (
callee.type === 'Identifier' &&
isEmitVariableName(callee.name, emitReferenceIds)
) {
addEmitCall(node)
}

// verify setup(props,{emit}) {emit()}
addEmitCallByReference(callee, emitReferenceIds, node)
if (emit && emit.name === 'emit') {
Expand All @@ -229,8 +210,11 @@ module.exports = {
}
}

// verify $emit() in template
if (callee.type === 'Identifier' && callee.name === '$emit') {
// verify $emit() and defineEmits variable in template
if (
callee.type === 'Identifier' &&
(callee.name === '$emit' || callee.name === emitParamName)
) {
addEmitCall(node)
}
}
Expand Down Expand Up @@ -316,10 +300,15 @@ module.exports = {
}

const emitParam = node.parent.id
const variable =
emitParam.type === 'Identifier'
? findVariable(utils.getScope(context, emitParam), emitParam)
: null
if (emitParam.type !== 'Identifier') {
return
}

emitParamName = emitParam.name
const variable = findVariable(
utils.getScope(context, emitParam),
emitParam
)
if (!variable) {
return
}
Expand Down
36 changes: 11 additions & 25 deletions lib/rules/require-explicit-emits.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,6 @@ function getNameParamNode(node) {
return null
}

/**
* Check if the given name matches defineEmitsNode variable name
* @param {string} name
* @param {CallExpression | undefined} defineEmitsNode
* @returns {boolean}
*/
function isEmitVariableName(name, defineEmitsNode) {
const node = defineEmitsNode?.parent

if (node?.type === 'VariableDeclarator' && node.id.type === 'Identifier') {
return name === node.id.name
}

return false
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -128,6 +112,7 @@ module.exports = {
const vueEmitsDeclarations = new Map()
/** @type {Map<ObjectExpression | Program, ComponentProp[]>} */
const vuePropsDeclarations = new Map()
let emitParamName = ''

/**
* @typedef {object} VueTemplateDefineData
Expand Down Expand Up @@ -271,11 +256,7 @@ module.exports = {
// e.g. $emit() / emit() in template
if (
callee.type === 'Identifier' &&
(callee.name === '$emit' ||
isEmitVariableName(
callee.name,
vueTemplateDefineData.defineEmits
))
(callee.name === '$emit' || callee.name === emitParamName)
) {
verifyEmit(
vueTemplateDefineData.emits,
Expand Down Expand Up @@ -308,10 +289,15 @@ module.exports = {
}

const emitParam = node.parent.id
const variable =
emitParam.type === 'Identifier'
? findVariable(utils.getScope(context, emitParam), emitParam)
: null
if (emitParam.type !== 'Identifier') {
return
}

emitParamName = emitParam.name
const variable = findVariable(
utils.getScope(context, emitParam),
emitParam
)
if (!variable) {
return
}
Expand Down

0 comments on commit 54a99c5

Please sign in to comment.