Skip to content

Commit 86300c4

Browse files
authored
fix(custom-event-name-casing): check defineEmits variable name in template (#2585)
1 parent 207eb98 commit 86300c4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/rules/custom-event-name-casing.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module.exports = {
101101
create(context) {
102102
/** @type {Map<ObjectExpression|Program, {contextReferenceIds:Set<Identifier>,emitReferenceIds:Set<Identifier>}>} */
103103
const setupContexts = new Map()
104+
let emitParamName = ''
104105
const options =
105106
context.options.length === 1 && typeof context.options[0] !== 'string'
106107
? // For backward compatibility
@@ -189,7 +190,11 @@ module.exports = {
189190
// cannot check
190191
return
191192
}
192-
if (callee.type === 'Identifier' && callee.name === '$emit') {
193+
194+
if (
195+
callee.type === 'Identifier' &&
196+
(callee.name === '$emit' || callee.name === emitParamName)
197+
) {
193198
verify(nameWithLoc)
194199
}
195200
}
@@ -209,6 +214,8 @@ module.exports = {
209214
if (emitParam.type !== 'Identifier') {
210215
return
211216
}
217+
emitParamName = emitParam.name
218+
212219
// const emit = defineEmits()
213220
const variable = findVariable(
214221
utils.getScope(context, emitParam),

tests/lib/rules/custom-event-name-casing.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ tester.run('custom-event-name-casing', rule, {
329329
</script>
330330
`,
331331
options: ['kebab-case']
332+
},
333+
// setup defineEmits
334+
{
335+
filename: 'test.vue',
336+
code: `
337+
<script setup>
338+
const emit = defineEmits({})
339+
emit('foo-bar')
340+
</script>
341+
342+
<template>
343+
<button @click="emit('foo-bar')">Foo</button>
344+
<button @click="$emit('foo-bar')">Foo</button>
345+
</template>
346+
`,
347+
options: ['kebab-case']
332348
}
333349
],
334350
invalid: [
@@ -605,6 +621,35 @@ tester.run('custom-event-name-casing', rule, {
605621
line: 4
606622
}
607623
]
624+
},
625+
{
626+
// https://github.com/vuejs/eslint-plugin-vue/issues/2577
627+
filename: 'test.vue',
628+
code: `
629+
<script setup>
630+
const emit = defineEmits({})
631+
emit('foo-bar')
632+
</script>
633+
634+
<template>
635+
<button @click="emit('foo-bar')">Foo</button>
636+
<button @click="$emit('foo-bar')">Foo</button>
637+
</template>
638+
`,
639+
errors: [
640+
{
641+
message: "Custom event name 'foo-bar' must be camelCase.",
642+
line: 4
643+
},
644+
{
645+
message: "Custom event name 'foo-bar' must be camelCase.",
646+
line: 8
647+
},
648+
{
649+
message: "Custom event name 'foo-bar' must be camelCase.",
650+
line: 9
651+
}
652+
]
608653
}
609654
]
610655
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy