no-reserved-keys.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Prevent overwrite reserved keys
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. const RESERVED_KEYS = require('../utils/vue-reserved.json')
  11. const GROUP_NAMES = ['props', 'computed', 'data', 'methods']
  12. module.exports = {
  13. meta: {
  14. type: 'suggestion',
  15. docs: {
  16. description: 'disallow overwriting reserved keys',
  17. category: 'essential',
  18. url: 'https://eslint.vuejs.org/rules/no-reserved-keys.html'
  19. },
  20. fixable: null,
  21. schema: [
  22. {
  23. type: 'object',
  24. properties: {
  25. reserved: {
  26. type: 'array'
  27. },
  28. groups: {
  29. type: 'array'
  30. }
  31. },
  32. additionalProperties: false
  33. }
  34. ]
  35. },
  36. create (context) {
  37. const options = context.options[0] || {}
  38. const reservedKeys = new Set(RESERVED_KEYS.concat(options.reserved || []))
  39. const groups = new Set(GROUP_NAMES.concat(options.groups || []))
  40. // ----------------------------------------------------------------------
  41. // Public
  42. // ----------------------------------------------------------------------
  43. return utils.executeOnVue(context, (obj) => {
  44. const properties = utils.iterateProperties(obj, groups)
  45. for (const o of properties) {
  46. if (o.groupName === 'data' && o.name[0] === '_') {
  47. context.report({
  48. node: o.node,
  49. message: "Keys starting with with '_' are reserved in '{{name}}' group.",
  50. data: {
  51. name: o.name
  52. }
  53. })
  54. } else if (reservedKeys.has(o.name)) {
  55. context.report({
  56. node: o.node,
  57. message: "Key '{{name}}' is reserved.",
  58. data: {
  59. name: o.name
  60. }
  61. })
  62. }
  63. }
  64. })
  65. }
  66. }