v-bind-style.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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-bind` directive style',
  18. category: 'strongly-recommended',
  19. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/v-bind-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='bind'][key.argument!=null]" (node) {
  30. if (node.key.shorthand === shorthand) {
  31. return
  32. }
  33. context.report({
  34. node,
  35. loc: node.loc,
  36. message: shorthand
  37. ? "Unexpected 'v-bind' before ':'."
  38. : "Expected 'v-bind' before ':'.",
  39. fix: (fixer) => shorthand
  40. ? fixer.removeRange([node.range[0], node.range[0] + 6])
  41. : fixer.insertTextBefore(node, 'v-bind')
  42. })
  43. }
  44. })
  45. }
  46. }