Skip to content

Commit

Permalink
fix(use-v-on-exact): Don't flag events with different key codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Pearson committed Jun 11, 2019
1 parent c3111fe commit 443d0df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
37 changes: 33 additions & 4 deletions lib/rules/use-v-on-exact.js
Expand Up @@ -11,6 +11,7 @@
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'])

// ------------------------------------------------------------------------------
// Helpers
Expand All @@ -36,6 +37,16 @@ function getEventDirectives (attributes, sourceCode) {
}))
}

/**
* 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
*
Expand Down Expand Up @@ -86,6 +97,16 @@ function getSystemModifiersString (modifiers) {
return modifiers.filter(isSystemModifier).sort().join(',')
}

/**
* Creates alphabetically sorted string with key modifiers
*
* @param {array[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
Expand All @@ -100,13 +121,21 @@ function hasConflictedModifiers (baseEvent, event) {
event.modifiers.includes('exact')
) return false

const eventModifiers = getSystemModifiersString(event.modifiers)
const baseEventModifiers = getSystemModifiersString(baseEvent.modifiers)
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 >= 1 &&
baseEventModifiers !== eventModifiers &&
baseEventModifiers.indexOf(eventModifiers) > -1
baseEventSystemModifiers !== eventSystemModifiers &&
baseEventSystemModifiers.indexOf(eventSystemModifiers) > -1
)
}

Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/use-v-on-exact.js
Expand Up @@ -158,6 +158,27 @@ ruleTester.run('use-v-on-exact', rule, {
},
{
code: `<template><button @[foo]="foo" @[bar].ctrl="bar"/></template>`
},
{
code: `<template>
<input
@keydown.enter="foo"
@keydown.shift.tab="bar"/>
</template>`
},
{
code: `<template>
<input
@keydown.enter="foo"
@keydown.shift.tab.prevent="bar"/>
</template>`
},
{
code: `<template>
<input-component
@keydown.enter.native="foo"
@keydown.shift.tab.native="bar"/>
</template>`
}
],

Expand Down

0 comments on commit 443d0df

Please sign in to comment.