no-invalid-model-keys.js 1.5 KB

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