v-bind-style.js 1.5 KB

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