no-custom-modifiers-on-v-model.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @author Przemyslaw Falowski (@przemkow)
  3. * @fileoverview This rule checks whether v-model used on the component do not have custom modifiers
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const VALID_MODIFIERS = new Set(['lazy', 'number', 'trim'])
  8. module.exports = {
  9. meta: {
  10. type: 'problem',
  11. docs: {
  12. description: 'disallow custom modifiers on v-model used on the component',
  13. categories: ['vue2-essential'],
  14. url: 'https://eslint.vuejs.org/rules/no-custom-modifiers-on-v-model.html'
  15. },
  16. fixable: null,
  17. schema: [],
  18. messages: {
  19. notSupportedModifier:
  20. "'v-model' directives don't support the modifier '{{name}}'."
  21. }
  22. },
  23. /** @param {RuleContext} context */
  24. create(context) {
  25. return utils.defineTemplateBodyVisitor(context, {
  26. "VAttribute[directive=true][key.name.name='model']"(node) {
  27. const element = node.parent.parent
  28. if (utils.isCustomComponent(element)) {
  29. for (const modifier of node.key.modifiers) {
  30. if (!VALID_MODIFIERS.has(modifier.name)) {
  31. context.report({
  32. node,
  33. loc: node.loc,
  34. messageId: 'notSupportedModifier',
  35. data: { name: modifier.name }
  36. })
  37. }
  38. }
  39. }
  40. }
  41. })
  42. }
  43. }