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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. // ------------------------------------------------------------------------------
  11. // Helpers
  12. // ------------------------------------------------------------------------------
  13. const VALID_MODIFIERS = new Set(['lazy', 'number', 'trim'])
  14. // ------------------------------------------------------------------------------
  15. // Rule Definition
  16. // ------------------------------------------------------------------------------
  17. module.exports = {
  18. meta: {
  19. type: 'problem',
  20. docs: {
  21. description: 'disallow custom modifiers on v-model used on the component',
  22. categories: ['essential'],
  23. url: 'https://eslint.vuejs.org/rules/no-custom-modifiers-on-v-model.html'
  24. },
  25. fixable: null,
  26. schema: [],
  27. messages: {
  28. notSupportedModifier:
  29. "'v-model' directives don't support the modifier '{{name}}'."
  30. }
  31. },
  32. /** @param {RuleContext} context */
  33. create(context) {
  34. return utils.defineTemplateBodyVisitor(context, {
  35. "VAttribute[directive=true][key.name.name='model']"(node) {
  36. const element = node.parent.parent
  37. if (utils.isCustomComponent(element)) {
  38. for (const modifier of node.key.modifiers) {
  39. if (!VALID_MODIFIERS.has(modifier.name)) {
  40. context.report({
  41. node,
  42. loc: node.loc,
  43. messageId: 'notSupportedModifier',
  44. data: { name: modifier.name }
  45. })
  46. }
  47. }
  48. }
  49. }
  50. })
  51. }
  52. }