From 5cedd1fddc8d115e2a0633a70750c8cde36ad098 Mon Sep 17 00:00:00 2001 From: waynzh Date: Sat, 26 Oct 2024 15:07:17 +0800 Subject: [PATCH] fix(custom-event-name-casing): check defineEmits variable name in template --- lib/rules/custom-event-name-casing.js | 9 ++++- tests/lib/rules/custom-event-name-casing.js | 45 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/rules/custom-event-name-casing.js b/lib/rules/custom-event-name-casing.js index 09cc2fc36..5c33980ec 100644 --- a/lib/rules/custom-event-name-casing.js +++ b/lib/rules/custom-event-name-casing.js @@ -101,6 +101,7 @@ module.exports = { create(context) { /** @type {Map,emitReferenceIds:Set}>} */ const setupContexts = new Map() + let emitParamName = '' const options = context.options.length === 1 && typeof context.options[0] !== 'string' ? // For backward compatibility @@ -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) } } @@ -209,6 +214,8 @@ module.exports = { if (emitParam.type !== 'Identifier') { return } + emitParamName = emitParam.name + // const emit = defineEmits() const variable = findVariable( utils.getScope(context, emitParam), diff --git a/tests/lib/rules/custom-event-name-casing.js b/tests/lib/rules/custom-event-name-casing.js index 76f9156fd..6b6e84c80 100644 --- a/tests/lib/rules/custom-event-name-casing.js +++ b/tests/lib/rules/custom-event-name-casing.js @@ -329,6 +329,22 @@ tester.run('custom-event-name-casing', rule, { `, options: ['kebab-case'] + }, + // setup defineEmits + { + filename: 'test.vue', + code: ` + + + + `, + options: ['kebab-case'] } ], invalid: [ @@ -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: ` + + + + `, + 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 + } + ] } ] })