diff --git a/docs/rules/custom-event-name-casing.md b/docs/rules/custom-event-name-casing.md index b4faacfbd..2b3dfb089 100644 --- a/docs/rules/custom-event-name-casing.md +++ b/docs/rules/custom-event-name-casing.md @@ -49,7 +49,47 @@ export default { ## :wrench: Options -Nothing. + +```json +{ + "vue/custom-event-name-casing": ["error", { + "ignores": [] + }] +} +``` +- `ignores` (`string[]`) ... The event names to ignore. Sets the event name to allow. For example, custom event names, Vue components event with special name, or Vue library component event name. You can set the regexp by writing it like `"/^name/"` or `click:row` or `fooBar`. + +### `"ignores": ["fooBar", "/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u"]` + + + +```vue + + +``` + + + ## :books: Further Reading diff --git a/lib/rules/custom-event-name-casing.js b/lib/rules/custom-event-name-casing.js index 6c5f072c3..7ed9335a2 100644 --- a/lib/rules/custom-event-name-casing.js +++ b/lib/rules/custom-event-name-casing.js @@ -11,6 +11,7 @@ const { findVariable } = require('eslint-utils') const utils = require('../utils') const { isKebabCase } = require('../utils/casing') +const { toRegExp } = require('../utils/regexp') // ------------------------------------------------------------------------------ // Helpers @@ -72,7 +73,20 @@ module.exports = { url: 'https://eslint.vuejs.org/rules/custom-event-name-casing.html' }, fixable: null, - schema: [], + schema: [ + { + type: 'object', + properties: { + ignores: { + type: 'array', + items: { type: 'string' }, + uniqueItems: true, + additionalItems: false + } + }, + additionalProperties: false + } + ], messages: { unexpected: "Custom event name '{{name}}' must be kebab-case." } @@ -80,13 +94,16 @@ module.exports = { /** @param {RuleContext} context */ create(context) { const setupContexts = new Map() + const options = context.options[0] || {} + /** @type {RegExp[]} */ + const ignores = (options.ignores || []).map(toRegExp) /** * @param { Literal & { value: string } } nameLiteralNode */ function verify(nameLiteralNode) { const name = nameLiteralNode.value - if (isValidEventName(name)) { + if (ignores.some((re) => re.test(name)) || isValidEventName(name)) { return } context.report({ diff --git a/tests/lib/rules/custom-event-name-casing.js b/tests/lib/rules/custom-event-name-casing.js index 934bb079f..ed0ba0eb5 100644 --- a/tests/lib/rules/custom-event-name-casing.js +++ b/tests/lib/rules/custom-event-name-casing.js @@ -166,6 +166,66 @@ tester.run('custom-event-name-casing', rule, { } ` + }, + { + filename: 'test.vue', + code: ` + + + `, + options: [{ ignores: ['fooBar', 'barBaz', 'bazQux'] }] + }, + { + filename: 'test.vue', + code: ` + + + `, + options: [{ ignores: ['/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u'] }] } ], invalid: [ @@ -276,6 +336,68 @@ tester.run('custom-event-name-casing', rule, { "Custom event name 'barBaz' must be kebab-case.", "Custom event name 'bazQux' must be kebab-case." ] + }, + { + filename: 'test.vue', + code: ` + + + `, + options: [{ ignores: ['/^[a-z]+(?:-[a-z]+)*:[a-z]+(?:-[a-z]+)*$/u'] }], + errors: [ + "Custom event name 'input/update' must be kebab-case.", + "Custom event name 'search/update' must be kebab-case.", + "Custom event name 'click/row' must be kebab-case." + ] + }, + { + filename: 'test.vue', + code: ` + + + `, + options: [{ ignores: ['input:update', 'search:update', 'click:row'] }], + errors: [ + "Custom event name 'input/update' must be kebab-case.", + "Custom event name 'search/update' must be kebab-case.", + "Custom event name 'click/row' must be kebab-case." + ] } ] })