no-deprecated-v-on-number-modifiers.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @fileoverview disallow using deprecated number (keycode) modifiers
  3. * @author yoyo930021
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const keyCodeToKey = require('../utils/keycode-to-key')
  8. module.exports = {
  9. meta: {
  10. type: 'problem',
  11. docs: {
  12. description:
  13. 'disallow using deprecated number (keycode) modifiers (in Vue.js 3.0.0+)',
  14. categories: ['vue3-essential'],
  15. url: 'https://eslint.vuejs.org/rules/no-deprecated-v-on-number-modifiers.html'
  16. },
  17. fixable: 'code',
  18. schema: [],
  19. messages: {
  20. numberModifierIsDeprecated:
  21. "'KeyboardEvent.keyCode' modifier on 'v-on' directive is deprecated. Using 'KeyboardEvent.key' instead."
  22. }
  23. },
  24. /** @param {RuleContext} context */
  25. create(context) {
  26. return utils.defineTemplateBodyVisitor(context, {
  27. /** @param {VDirectiveKey} node */
  28. "VAttribute[directive=true][key.name.name='on'] > VDirectiveKey"(node) {
  29. const modifier = node.modifiers.find((mod) =>
  30. Number.isInteger(Number.parseInt(mod.name, 10))
  31. )
  32. if (!modifier) return
  33. const keyCodes = Number.parseInt(modifier.name, 10)
  34. if (keyCodes > 9 || keyCodes < 0) {
  35. context.report({
  36. node: modifier,
  37. messageId: 'numberModifierIsDeprecated',
  38. fix(fixer) {
  39. const key = keyCodeToKey[keyCodes]
  40. if (!key) return null
  41. return fixer.replaceText(modifier, `${key}`)
  42. }
  43. })
  44. }
  45. }
  46. })
  47. }
  48. }