valid-v-bind.js 1.9 KB

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