Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: store the defineEmits variable name #2592

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading