valid-model-definition.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @fileoverview Requires valid keys in model option.
  3. * @author Alex Sokolov
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const VALID_MODEL_KEYS = new Set(['prop', 'event'])
  8. module.exports = {
  9. meta: {
  10. type: 'problem',
  11. docs: {
  12. description: 'require valid keys in model option',
  13. categories: ['vue2-essential'],
  14. url: 'https://eslint.vuejs.org/rules/valid-model-definition.html'
  15. },
  16. fixable: null,
  17. schema: [],
  18. messages: {
  19. invalidKey: "Invalid key '{{name}}' in model option."
  20. }
  21. },
  22. /** @param {RuleContext} context */
  23. create(context) {
  24. return utils.executeOnVue(context, (obj) => {
  25. const modelProperty = utils.findProperty(obj, 'model')
  26. if (!modelProperty || modelProperty.value.type !== 'ObjectExpression') {
  27. return
  28. }
  29. for (const p of modelProperty.value.properties) {
  30. if (p.type !== 'Property') {
  31. continue
  32. }
  33. const name = utils.getStaticPropertyName(p)
  34. if (!name) {
  35. continue
  36. }
  37. if (!VALID_MODEL_KEYS.has(name)) {
  38. context.report({
  39. node: p,
  40. messageId: 'invalidKey',
  41. data: {
  42. name
  43. }
  44. })
  45. }
  46. }
  47. })
  48. }
  49. }