diff --git a/docs/rules/valid-v-on.md b/docs/rules/valid-v-on.md index 18580ccc0..02bc8ba93 100644 --- a/docs/rules/valid-v-on.md +++ b/docs/rules/valid-v-on.md @@ -36,7 +36,24 @@ This rule does not check syntax errors in directives because it's checked by [no ## :wrench: Options -Nothing. +This rule has an object option: + +`"modifiers"`: [] (default) array of additional allowed modifiers. + +### Example: + +```json +"vue/valid-v-on": [2, { + modifiers: ['foo'] +}] +``` + +:+1: Examples of **correct** code for this rule: + +```html +
+ +``` ## :couple: Related rules diff --git a/lib/rules/valid-v-on.js b/lib/rules/valid-v-on.js index 203a2ae33..13073d9ab 100644 --- a/lib/rules/valid-v-on.js +++ b/lib/rules/valid-v-on.js @@ -93,7 +93,7 @@ const KEY_ALIASES = new Set([ 'media-previous-track', 'power', 'unidentified' ]) -function isValidModifier (modifier) { +function isValidModifier (modifier, customModifiers) { return ( // built-in aliases VALID_MODIFIERS.has(modifier) || @@ -102,7 +102,9 @@ function isValidModifier (modifier) { // keyAlias (an Unicode character) Array.from(modifier).length === 1 || // keyAlias (special keys) - KEY_ALIASES.has(modifier) + KEY_ALIASES.has(modifier) || + // custom modifiers + customModifiers.has(modifier) ) } @@ -118,14 +120,27 @@ module.exports = { url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/valid-v-on.md' }, fixable: null, - schema: [] + schema: [ + { + type: 'object', + properties: { + modifiers: { + type: 'array' + } + }, + additionalProperties: false + } + ] }, create (context) { + const options = context.options[0] || {} + const customModifiers = new Set(options.modifiers || []) + return utils.defineTemplateBodyVisitor(context, { "VAttribute[directive=true][key.name='on']" (node) { for (const modifier of node.key.modifiers) { - if (!isValidModifier(modifier)) { + if (!isValidModifier(modifier, customModifiers)) { context.report({ node, loc: node.loc, diff --git a/tests/lib/rules/valid-v-on.js b/tests/lib/rules/valid-v-on.js index 3c02be86f..f24e10dcf 100644 --- a/tests/lib/rules/valid-v-on.js +++ b/tests/lib/rules/valid-v-on.js @@ -86,6 +86,16 @@ tester.run('valid-v-on', rule, { { filename: 'test.vue', code: '' + }, + { + filename: 'test.vue', + code: '', + options: [{ modifiers: ['bar'] }] + }, + { + filename: 'test.vue', + code: '', + options: [{ modifiers: ['bar', 'aaa'] }] } ], invalid: [ @@ -103,6 +113,18 @@ tester.run('valid-v-on', rule, { filename: 'test.vue', code: '', errors: ["'v-on' directives require that attribute value or verb modifiers."] + }, + { + filename: 'test.vue', + code: '', + errors: ["'v-on' directives don't support the modifier 'aaa'."], + options: [{ modifiers: ['bar'] }] + }, + { + filename: 'test.vue', + code: '', + errors: ["'v-on' directives don't support the modifier 'bar'."], + options: [{ modifiers: ['aaa'] }] } ] })