Skip to content

Commit d0fd01f

Browse files
armano2michalsnik
authored andcommitted
Fix #519 add custom modifiers to vue/valid-v-on (#579)
1 parent 8474308 commit d0fd01f

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

docs/rules/valid-v-on.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,24 @@ This rule does not check syntax errors in directives because it's checked by [no
3636

3737
## :wrench: Options
3838

39-
Nothing.
39+
This rule has an object option:
40+
41+
`"modifiers"`: [] (default) array of additional allowed modifiers.
42+
43+
### Example:
44+
45+
```json
46+
"vue/valid-v-on": [2, {
47+
modifiers: ['foo']
48+
}]
49+
```
50+
51+
:+1: Examples of **correct** code for this rule:
52+
53+
```html
54+
<div @click.foo="foo"/>
55+
<div v-on:click.foo="foo"/>
56+
```
4057

4158
## :couple: Related rules
4259

lib/rules/valid-v-on.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const KEY_ALIASES = new Set([
9393
'media-previous-track', 'power', 'unidentified'
9494
])
9595

96-
function isValidModifier (modifier) {
96+
function isValidModifier (modifier, customModifiers) {
9797
return (
9898
// built-in aliases
9999
VALID_MODIFIERS.has(modifier) ||
@@ -102,7 +102,9 @@ function isValidModifier (modifier) {
102102
// keyAlias (an Unicode character)
103103
Array.from(modifier).length === 1 ||
104104
// keyAlias (special keys)
105-
KEY_ALIASES.has(modifier)
105+
KEY_ALIASES.has(modifier) ||
106+
// custom modifiers
107+
customModifiers.has(modifier)
106108
)
107109
}
108110

@@ -118,14 +120,27 @@ module.exports = {
118120
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/valid-v-on.md'
119121
},
120122
fixable: null,
121-
schema: []
123+
schema: [
124+
{
125+
type: 'object',
126+
properties: {
127+
modifiers: {
128+
type: 'array'
129+
}
130+
},
131+
additionalProperties: false
132+
}
133+
]
122134
},
123135

124136
create (context) {
137+
const options = context.options[0] || {}
138+
const customModifiers = new Set(options.modifiers || [])
139+
125140
return utils.defineTemplateBodyVisitor(context, {
126141
"VAttribute[directive=true][key.name='on']" (node) {
127142
for (const modifier of node.key.modifiers) {
128-
if (!isValidModifier(modifier)) {
143+
if (!isValidModifier(modifier, customModifiers)) {
129144
context.report({
130145
node,
131146
loc: node.loc,

tests/lib/rules/valid-v-on.js

+22
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ tester.run('valid-v-on', rule, {
8686
{
8787
filename: 'test.vue',
8888
code: '<template><div v-on="{a, b, c: d}"></div></template>'
89+
},
90+
{
91+
filename: 'test.vue',
92+
code: '<template><div @keydown.bar="foo"></div></template>',
93+
options: [{ modifiers: ['bar'] }]
94+
},
95+
{
96+
filename: 'test.vue',
97+
code: '<template><div v-on:keydown.bar.aaa="foo"></div></template>',
98+
options: [{ modifiers: ['bar', 'aaa'] }]
8999
}
90100
],
91101
invalid: [
@@ -103,6 +113,18 @@ tester.run('valid-v-on', rule, {
103113
filename: 'test.vue',
104114
code: '<template><div @click></div></template>',
105115
errors: ["'v-on' directives require that attribute value or verb modifiers."]
116+
},
117+
{
118+
filename: 'test.vue',
119+
code: '<template><div @keydown.bar.aaa="foo"></div></template>',
120+
errors: ["'v-on' directives don't support the modifier 'aaa'."],
121+
options: [{ modifiers: ['bar'] }]
122+
},
123+
{
124+
filename: 'test.vue',
125+
code: '<template><div @keydown.bar.aaa="foo"></div></template>',
126+
errors: ["'v-on' directives don't support the modifier 'bar'."],
127+
options: [{ modifiers: ['aaa'] }]
106128
}
107129
]
108130
})

0 commit comments

Comments
 (0)