valid-v-on.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2017 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const utils = require('../utils')
  11. const keyAliases = require('../utils/key-aliases.json')
  12. // ------------------------------------------------------------------------------
  13. // Helpers
  14. // ------------------------------------------------------------------------------
  15. const VALID_MODIFIERS = new Set([
  16. 'stop', 'prevent', 'capture', 'self', 'ctrl', 'shift', 'alt', 'meta',
  17. 'native', 'once', 'left', 'right', 'middle', 'passive', 'esc', 'tab',
  18. 'enter', 'space', 'up', 'left', 'right', 'down', 'delete', 'exact'
  19. ])
  20. const VERB_MODIFIERS = new Set([
  21. 'stop', 'prevent'
  22. ])
  23. // https://www.w3.org/TR/uievents-key/
  24. const KEY_ALIASES = new Set(keyAliases)
  25. function isValidModifier (modifier, customModifiers) {
  26. return (
  27. // built-in aliases
  28. VALID_MODIFIERS.has(modifier) ||
  29. // keyCode
  30. Number.isInteger(parseInt(modifier, 10)) ||
  31. // keyAlias (an Unicode character)
  32. Array.from(modifier).length === 1 ||
  33. // keyAlias (special keys)
  34. KEY_ALIASES.has(modifier) ||
  35. // custom modifiers
  36. customModifiers.has(modifier)
  37. )
  38. }
  39. // ------------------------------------------------------------------------------
  40. // Rule Definition
  41. // ------------------------------------------------------------------------------
  42. module.exports = {
  43. meta: {
  44. type: 'problem',
  45. docs: {
  46. description: 'enforce valid `v-on` directives',
  47. category: 'essential',
  48. url: 'https://eslint.vuejs.org/rules/valid-v-on.html'
  49. },
  50. fixable: null,
  51. schema: [
  52. {
  53. type: 'object',
  54. properties: {
  55. modifiers: {
  56. type: 'array'
  57. }
  58. },
  59. additionalProperties: false
  60. }
  61. ]
  62. },
  63. create (context) {
  64. const options = context.options[0] || {}
  65. const customModifiers = new Set(options.modifiers || [])
  66. const sourceCode = context.getSourceCode()
  67. return utils.defineTemplateBodyVisitor(context, {
  68. "VAttribute[directive=true][key.name='on']" (node) {
  69. for (const modifier of node.key.modifiers) {
  70. if (!isValidModifier(modifier, customModifiers)) {
  71. context.report({
  72. node,
  73. loc: node.loc,
  74. message: "'v-on' directives don't support the modifier '{{modifier}}'.",
  75. data: { modifier }
  76. })
  77. }
  78. }
  79. if (
  80. !utils.hasAttributeValue(node) &&
  81. !node.key.modifiers.some(VERB_MODIFIERS.has, VERB_MODIFIERS)
  82. ) {
  83. if (node.value && sourceCode.getText(node.value.expression)) {
  84. const value = sourceCode.getText(node.value)
  85. context.report({
  86. node,
  87. loc: node.loc,
  88. message: 'Avoid using JavaScript keyword as "v-on" value: {{value}}.',
  89. data: { value }
  90. })
  91. } else {
  92. context.report({
  93. node,
  94. loc: node.loc,
  95. message: "'v-on' directives require a value or verb modifier (like 'stop' or 'prevent')."
  96. })
  97. }
  98. }
  99. }
  100. })
  101. }
  102. }