forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-v-on-exact.js
65 lines (59 loc) · 2.11 KB
/
use-v-on-exact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @fileoverview enforce usage of `exact` modifier on `v-on`.
* @author Armano
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'enforce usage of `exact` modifier on `v-on`',
category: undefined, // essential
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/use-v-on-exact.md'
},
fixable: null,
schema: []
},
/**
* Creates AST event handlers for use-v-on-exact.
*
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
create (context) {
return utils.defineTemplateBodyVisitor(context, {
VStartTag (node) {
if (node.attributes.length > 1) {
const groups = node.attributes
.map(item => item.key)
.filter(item => item && item.type === 'VDirectiveKey' && item.name === 'on')
.reduce((rv, item) => {
(rv[item.argument] = rv[item.argument] || []).push(item)
return rv
}, {})
const directives = Object.keys(groups).map(key => groups[key])
// const directives = Object.values(groups) // Uncomment after node 6 is dropped
.filter(item => item.length > 1)
for (const group of directives) {
if (group.some(item => item.modifiers.length > 0)) { // check if there are directives with modifiers
const invalid = group.filter(item => item.modifiers.length === 0)
for (const node of invalid) {
context.report({
node,
loc: node.loc,
message: "Consider to use '.exact' modifier."
})
}
}
}
}
}
})
}
}