valid-v-bind.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. const VALID_MODIFIERS = new Set(['prop', 'camel', 'sync', 'attr'])
  9. module.exports = {
  10. meta: {
  11. type: 'problem',
  12. docs: {
  13. description: 'enforce valid `v-bind` directives',
  14. categories: ['vue3-essential', 'vue2-essential'],
  15. url: 'https://eslint.vuejs.org/rules/valid-v-bind.html'
  16. },
  17. fixable: null,
  18. schema: [],
  19. messages: {
  20. unsupportedModifier:
  21. "'v-bind' directives don't support the modifier '{{name}}'.",
  22. expectedValue: "'v-bind' directives require an attribute value."
  23. }
  24. },
  25. /** @param {RuleContext} context */
  26. create(context) {
  27. return utils.defineTemplateBodyVisitor(context, {
  28. /** @param {VDirective} node */
  29. "VAttribute[directive=true][key.name.name='bind']"(node) {
  30. for (const modifier of node.key.modifiers) {
  31. if (!VALID_MODIFIERS.has(modifier.name)) {
  32. context.report({
  33. node: modifier,
  34. messageId: 'unsupportedModifier',
  35. data: { name: modifier.name }
  36. })
  37. }
  38. }
  39. if (!node.value || utils.isEmptyValueDirective(node, context)) {
  40. context.report({
  41. node,
  42. messageId: 'expectedValue'
  43. })
  44. }
  45. }
  46. })
  47. }
  48. }