Skip to content

Commit

Permalink
fix(custom-event-name-casing): check defineEmits variable name in tem…
Browse files Browse the repository at this point in the history
…plate (#2585)
  • Loading branch information
waynzh authored Oct 28, 2024
1 parent 207eb98 commit 86300c4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rules/custom-event-name-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
create(context) {
/** @type {Map<ObjectExpression|Program, {contextReferenceIds:Set<Identifier>,emitReferenceIds:Set<Identifier>}>} */
const setupContexts = new Map()
let emitParamName = ''
const options =
context.options.length === 1 && typeof context.options[0] !== 'string'
? // For backward compatibility
Expand Down Expand Up @@ -189,7 +190,11 @@ module.exports = {
// cannot check
return
}
if (callee.type === 'Identifier' && callee.name === '$emit') {

if (
callee.type === 'Identifier' &&
(callee.name === '$emit' || callee.name === emitParamName)
) {
verify(nameWithLoc)
}
}
Expand All @@ -209,6 +214,8 @@ module.exports = {
if (emitParam.type !== 'Identifier') {
return
}
emitParamName = emitParam.name

// const emit = defineEmits()
const variable = findVariable(
utils.getScope(context, emitParam),
Expand Down
45 changes: 45 additions & 0 deletions tests/lib/rules/custom-event-name-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ tester.run('custom-event-name-casing', rule, {
</script>
`,
options: ['kebab-case']
},
// setup defineEmits
{
filename: 'test.vue',
code: `
<script setup>
const emit = defineEmits({})
emit('foo-bar')
</script>
<template>
<button @click="emit('foo-bar')">Foo</button>
<button @click="$emit('foo-bar')">Foo</button>
</template>
`,
options: ['kebab-case']
}
],
invalid: [
Expand Down Expand Up @@ -605,6 +621,35 @@ tester.run('custom-event-name-casing', rule, {
line: 4
}
]
},
{
// https://github.com/vuejs/eslint-plugin-vue/issues/2577
filename: 'test.vue',
code: `
<script setup>
const emit = defineEmits({})
emit('foo-bar')
</script>
<template>
<button @click="emit('foo-bar')">Foo</button>
<button @click="$emit('foo-bar')">Foo</button>
</template>
`,
errors: [
{
message: "Custom event name 'foo-bar' must be camelCase.",
line: 4
},
{
message: "Custom event name 'foo-bar' must be camelCase.",
line: 8
},
{
message: "Custom event name 'foo-bar' must be camelCase.",
line: 9
}
]
}
]
})

0 comments on commit 86300c4

Please sign in to comment.