no-dupe-keys.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @fileoverview Prevents duplication of field names.
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. const GROUP_NAMES = ['props', 'computed', 'data', 'methods']
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. description: 'disallow duplication of field names',
  15. category: 'essential',
  16. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/no-dupe-keys.md'
  17. },
  18. fixable: null, // or "code" or "whitespace"
  19. schema: [
  20. {
  21. type: 'object',
  22. properties: {
  23. groups: {
  24. type: 'array'
  25. }
  26. },
  27. additionalProperties: false
  28. }
  29. ]
  30. },
  31. create (context) {
  32. const options = context.options[0] || {}
  33. const groups = new Set(GROUP_NAMES.concat(options.groups || []))
  34. // ----------------------------------------------------------------------
  35. // Public
  36. // ----------------------------------------------------------------------
  37. return utils.executeOnVue(context, (obj) => {
  38. const usedNames = []
  39. const properties = utils.iterateProperties(obj, groups)
  40. for (const o of properties) {
  41. if (usedNames.indexOf(o.name) !== -1) {
  42. context.report({
  43. node: o.node,
  44. message: "Duplicated key '{{name}}'.",
  45. data: {
  46. name: o.name
  47. }
  48. })
  49. }
  50. usedNames.push(o.name)
  51. }
  52. })
  53. }
  54. }