v-on-style.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'enforce `v-on` directive style',
  18. category: 'strongly-recommended',
  19. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/v-on-style.md'
  20. },
  21. fixable: 'code',
  22. schema: [
  23. { enum: ['shorthand', 'longform'] }
  24. ]
  25. },
  26. create (context) {
  27. const shorthand = context.options[0] !== 'longform'
  28. return utils.defineTemplateBodyVisitor(context, {
  29. "VAttribute[directive=true][key.name='on'][key.argument!=null]" (node) {
  30. if (node.key.shorthand === shorthand) {
  31. return
  32. }
  33. const pos = node.range[0]
  34. context.report({
  35. node,
  36. loc: node.loc,
  37. message: shorthand
  38. ? "Expected '@' instead of 'v-on:'."
  39. : "Expected 'v-on:' instead of '@'.",
  40. fix: (fixer) => shorthand
  41. ? fixer.replaceTextRange([pos, pos + 5], '@')
  42. : fixer.replaceTextRange([pos, pos + 1], 'v-on:')
  43. })
  44. }
  45. })
  46. }
  47. }