v-on-style.js 1.6 KB

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