Skip to content

Commit b0d613c

Browse files
mysticateamichalsnik
authored andcommitted
Fix: allow v-on without attribute values if it has verb modifiers (fixes #49) (#50)
* Fix: allow `v-on` without attribute values if it has verb modifiers (fixes #49) * Chore: add more tests
1 parent a8bdf72 commit b0d613c

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

docs/rules/no-invalid-v-on.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This rule reports `v-on` directives in the following cases:
88

99
- The directive does not have that event name. E.g. `<div v-on="foo"></div>`
1010
- The directive has invalid modifiers. E.g. `<div v-on:click.bbb="foo"></div>`
11-
- The directive does not have that attribute value. E.g. `<div v-on:click></div>`
11+
- The directive does not have that attribute value and any verb modifiers. E.g. `<div v-on:click></div>`
1212

1313
This rule does not check syntax errors in directives because it's checked by [no-parsing-error] rule.
1414

@@ -31,7 +31,9 @@ This rule does not check syntax errors in directives because it's checked by [no
3131
<div>
3232
<div v-on:click="foo"></div>
3333
<div @click="foo"></div>
34-
<div @click.left.prevent="foo"></div>
34+
<div @click.left="foo"></div>
35+
<div @click.prevent></div>
36+
<div @click.stop></div>
3537
</div>
3638
</template>
3739
```

lib/rules/no-invalid-v-on.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const VALID_MODIFIERS = new Set([
2020
'native', 'once', 'left', 'right', 'middle', 'passive', 'esc', 'tab',
2121
'enter', 'space', 'up', 'left', 'right', 'down', 'delete'
2222
])
23+
const VERB_MODIFIERS = new Set([
24+
'stop', 'prevent'
25+
])
2326

2427
/**
2528
* Creates AST event handlers for no-invalid-v-on.
@@ -47,11 +50,11 @@ function create (context) {
4750
})
4851
}
4952
}
50-
if (!utils.hasAttributeValue(node)) {
53+
if (!utils.hasAttributeValue(node) && !node.key.modifiers.some(VERB_MODIFIERS.has, VERB_MODIFIERS)) {
5154
context.report({
5255
node,
5356
loc: node.loc,
54-
message: "'v-on' directives require that attribute value."
57+
message: "'v-on' directives require that attribute value or verb modifiers."
5558
})
5659
}
5760
}

tests/lib/rules/no-invalid-v-on.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ tester.run('no-invalid-v-on', rule, {
4242
{
4343
filename: 'test.vue',
4444
code: '<template><div @keydown.27="foo"></div></template>'
45+
},
46+
{
47+
filename: 'test.vue',
48+
code: '<template><el-from @submit.native.prevent></el-form></template>'
49+
},
50+
{
51+
filename: 'test.vue',
52+
code: '<template><div v-on:click.prevent></div></template>'
53+
},
54+
{
55+
filename: 'test.vue',
56+
code: '<template><div v-on:click.native.stop></div></template>'
4557
}
4658
],
4759
invalid: [
@@ -58,7 +70,12 @@ tester.run('no-invalid-v-on', rule, {
5870
{
5971
filename: 'test.vue',
6072
code: '<template><div v-on:click></div></template>',
61-
errors: ["'v-on' directives require that attribute value."]
73+
errors: ["'v-on' directives require that attribute value or verb modifiers."]
74+
},
75+
{
76+
filename: 'test.vue',
77+
code: '<template><div @click></div></template>',
78+
errors: ["'v-on' directives require that attribute value or verb modifiers."]
6279
}
6380
]
6481
})

0 commit comments

Comments
 (0)