-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathuse-v-on-exact.js
222 lines (196 loc) · 5.67 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @fileoverview enforce usage of `exact` modifier on `v-on`.
* @author Armano
*/
'use strict'
/**
* @typedef { {name: string, node: VDirectiveKey, modifiers: string[] } } EventDirective
*/
const utils = require('../utils')
const SYSTEM_MODIFIERS = new Set(['ctrl', 'shift', 'alt', 'meta'])
const GLOBAL_MODIFIERS = new Set([
'stop',
'prevent',
'capture',
'self',
'once',
'passive',
'native'
])
/**
* Finds and returns all keys for event directives
*
* @param {VStartTag} startTag Element startTag
* @param {SourceCode} sourceCode The source code object.
* @returns {EventDirective[]} [{ name, node, modifiers }]
*/
function getEventDirectives(startTag, sourceCode) {
return utils.getDirectives(startTag, 'on').map((attribute) => ({
name: attribute.key.argument
? sourceCode.getText(attribute.key.argument)
: '',
node: attribute.key,
modifiers: attribute.key.modifiers.map((modifier) => modifier.name)
}))
}
/**
* Checks whether given modifier is key modifier
*
* @param {string} modifier
* @returns {boolean}
*/
function isKeyModifier(modifier) {
return !GLOBAL_MODIFIERS.has(modifier) && !SYSTEM_MODIFIERS.has(modifier)
}
/**
* Checks whether given modifier is system one
*
* @param {string} modifier
* @returns {boolean}
*/
function isSystemModifier(modifier) {
return SYSTEM_MODIFIERS.has(modifier)
}
/**
* Checks whether given any of provided modifiers
* has system modifier
*
* @param {string[]} modifiers
* @returns {boolean}
*/
function hasSystemModifier(modifiers) {
return modifiers.some(isSystemModifier)
}
/**
* Groups all events in object,
* with keys represinting each event name
*
* @param {EventDirective[]} events
* @returns { { [key: string]: EventDirective[] } } { click: [], keypress: [] }
*/
function groupEvents(events) {
/** @type { { [key: string]: EventDirective[] } } */
const grouped = {}
for (const event of events) {
if (!grouped[event.name]) {
grouped[event.name] = []
}
grouped[event.name].push(event)
}
return grouped
}
/**
* Creates alphabetically sorted string with system modifiers
*
* @param {string[]} modifiers
* @returns {string} e.g. "alt,ctrl,del,shift"
*/
function getSystemModifiersString(modifiers) {
return modifiers.filter(isSystemModifier).sort().join(',')
}
/**
* Creates alphabetically sorted string with key modifiers
*
* @param {string[]} modifiers
* @returns {string} e.g. "enter,tab"
*/
function getKeyModifiersString(modifiers) {
return modifiers.filter(isKeyModifier).sort().join(',')
}
/**
* Compares two events based on their modifiers
* to detect possible event leakeage
*
* @param {EventDirective} baseEvent
* @param {EventDirective} event
* @returns {boolean}
*/
function hasConflictedModifiers(baseEvent, event) {
if (event.node === baseEvent.node || event.modifiers.includes('exact'))
return false
const eventKeyModifiers = getKeyModifiersString(event.modifiers)
const baseEventKeyModifiers = getKeyModifiersString(baseEvent.modifiers)
if (
eventKeyModifiers &&
baseEventKeyModifiers &&
eventKeyModifiers !== baseEventKeyModifiers
)
return false
const eventSystemModifiers = getSystemModifiersString(event.modifiers)
const baseEventSystemModifiers = getSystemModifiersString(baseEvent.modifiers)
return (
baseEvent.modifiers.length > 0 &&
baseEventSystemModifiers !== eventSystemModifiers &&
baseEventSystemModifiers.includes(eventSystemModifiers)
)
}
/**
* Searches for events that might conflict with each other
*
* @param {EventDirective[]} events
* @returns {EventDirective[]} conflicted events, without duplicates
*/
function findConflictedEvents(events) {
/** @type {EventDirective[]} */
const conflictedEvents = []
for (const event of events) {
conflictedEvents.push(
...events
.filter((evt) => !conflictedEvents.includes(evt)) // No duplicates
.filter(hasConflictedModifiers.bind(null, event))
)
}
return conflictedEvents
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce usage of `exact` modifier on `v-on`',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/use-v-on-exact.html'
},
fixable: null,
schema: [],
messages: {
considerExact: "Consider to use '.exact' modifier."
}
},
/**
* Creates AST event handlers for use-v-on-exact.
*
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
create(context) {
const sourceCode = context.getSourceCode()
return utils.defineTemplateBodyVisitor(context, {
/** @param {VStartTag} node */
VStartTag(node) {
if (node.attributes.length === 0) return
const isCustomComponent = utils.isCustomComponent(node.parent)
let events = getEventDirectives(node, sourceCode)
if (isCustomComponent) {
// For components consider only events with `native` modifier
events = events.filter((event) => event.modifiers.includes('native'))
}
const grouppedEvents = groupEvents(events)
for (const eventName of Object.keys(grouppedEvents)) {
const eventsInGroup = grouppedEvents[eventName]
const hasEventWithKeyModifier = eventsInGroup.some((event) =>
hasSystemModifier(event.modifiers)
)
if (!hasEventWithKeyModifier) continue
const conflictedEvents = findConflictedEvents(eventsInGroup)
for (const e of conflictedEvents) {
context.report({
node: e.node,
loc: e.node.loc,
messageId: 'considerExact'
})
}
}
}
})
}
}