Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(use-v-on-exact): Don't flag events with different key codes #904

Merged
merged 1 commit into from Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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