Skip to content

Commit ba68ae7

Browse files
cjpearsonkazupon
authored andcommitted
fix(use-v-on-exact): Don't flag events with different key codes (#904)
1 parent c429358 commit ba68ae7

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

Diff for: lib/rules/use-v-on-exact.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
const utils = require('../utils')
1212

1313
const SYSTEM_MODIFIERS = new Set(['ctrl', 'shift', 'alt', 'meta'])
14+
const GLOBAL_MODIFIERS = new Set(['stop', 'prevent', 'capture', 'self', 'once', 'passive', 'native'])
1415

1516
// ------------------------------------------------------------------------------
1617
// Helpers
@@ -36,6 +37,16 @@ function getEventDirectives (attributes, sourceCode) {
3637
}))
3738
}
3839

40+
/**
41+
* Checks whether given modifier is key modifier
42+
*
43+
* @param {string} modifier
44+
* @returns {boolean}
45+
*/
46+
function isKeyModifier (modifier) {
47+
return !GLOBAL_MODIFIERS.has(modifier) && !SYSTEM_MODIFIERS.has(modifier)
48+
}
49+
3950
/**
4051
* Checks whether given modifier is system one
4152
*
@@ -86,6 +97,16 @@ function getSystemModifiersString (modifiers) {
8697
return modifiers.filter(isSystemModifier).sort().join(',')
8798
}
8899

100+
/**
101+
* Creates alphabetically sorted string with key modifiers
102+
*
103+
* @param {array[string]} modifiers
104+
* @returns {string} e.g. "enter,tab"
105+
*/
106+
function getKeyModifiersString (modifiers) {
107+
return modifiers.filter(isKeyModifier).sort().join(',')
108+
}
109+
89110
/**
90111
* Compares two events based on their modifiers
91112
* to detect possible event leakeage
@@ -100,13 +121,21 @@ function hasConflictedModifiers (baseEvent, event) {
100121
event.modifiers.includes('exact')
101122
) return false
102123

103-
const eventModifiers = getSystemModifiersString(event.modifiers)
104-
const baseEventModifiers = getSystemModifiersString(baseEvent.modifiers)
124+
const eventKeyModifiers = getKeyModifiersString(event.modifiers)
125+
const baseEventKeyModifiers = getKeyModifiersString(baseEvent.modifiers)
126+
127+
if (
128+
eventKeyModifiers && baseEventKeyModifiers &&
129+
eventKeyModifiers !== baseEventKeyModifiers
130+
) return false
131+
132+
const eventSystemModifiers = getSystemModifiersString(event.modifiers)
133+
const baseEventSystemModifiers = getSystemModifiersString(baseEvent.modifiers)
105134

106135
return (
107136
baseEvent.modifiers.length >= 1 &&
108-
baseEventModifiers !== eventModifiers &&
109-
baseEventModifiers.indexOf(eventModifiers) > -1
137+
baseEventSystemModifiers !== eventSystemModifiers &&
138+
baseEventSystemModifiers.indexOf(eventSystemModifiers) > -1
110139
)
111140
}
112141

Diff for: tests/lib/rules/use-v-on-exact.js

+21
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ ruleTester.run('use-v-on-exact', rule, {
158158
},
159159
{
160160
code: `<template><button @[foo]="foo" @[bar].ctrl="bar"/></template>`
161+
},
162+
{
163+
code: `<template>
164+
<input
165+
@keydown.enter="foo"
166+
@keydown.shift.tab="bar"/>
167+
</template>`
168+
},
169+
{
170+
code: `<template>
171+
<input
172+
@keydown.enter="foo"
173+
@keydown.shift.tab.prevent="bar"/>
174+
</template>`
175+
},
176+
{
177+
code: `<template>
178+
<input-component
179+
@keydown.enter.native="foo"
180+
@keydown.shift.tab.native="bar"/>
181+
</template>`
161182
}
162183
],
163184

0 commit comments

Comments
 (0)