v-on-style.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. type: 'suggestion',
  17. docs: {
  18. description: 'enforce `v-on` directive style',
  19. categories: ['vue3-strongly-recommended', 'strongly-recommended'],
  20. url: 'https://eslint.vuejs.org/rules/v-on-style.html'
  21. },
  22. fixable: 'code',
  23. schema: [{ enum: ['shorthand', 'longform'] }]
  24. },
  25. /** @param {RuleContext} context */
  26. create(context) {
  27. const preferShorthand = context.options[0] !== 'longform'
  28. return utils.defineTemplateBodyVisitor(context, {
  29. /** @param {VDirective} node */
  30. "VAttribute[directive=true][key.name.name='on'][key.argument!=null]"(
  31. node
  32. ) {
  33. const shorthand = node.key.name.rawName === '@'
  34. if (shorthand === preferShorthand) {
  35. return
  36. }
  37. const pos = node.range[0]
  38. context.report({
  39. node,
  40. loc: node.loc,
  41. message: preferShorthand
  42. ? "Expected '@' instead of 'v-on:'."
  43. : "Expected 'v-on:' instead of '@'.",
  44. fix: (fixer) =>
  45. preferShorthand
  46. ? fixer.replaceTextRange([pos, pos + 5], '@')
  47. : fixer.replaceTextRange([pos, pos + 1], 'v-on:')
  48. })
  49. }
  50. })
  51. }
  52. }