v-on-style.js 1.4 KB

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