no-dupe-keys.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. type: 'problem',
  14. docs: {
  15. description: 'disallow duplication of field names',
  16. category: 'essential',
  17. url: 'https://eslint.vuejs.org/rules/no-dupe-keys.html'
  18. },
  19. fixable: null, // or "code" or "whitespace"
  20. schema: [
  21. {
  22. type: 'object',
  23. properties: {
  24. groups: {
  25. type: 'array'
  26. }
  27. },
  28. additionalProperties: false
  29. }
  30. ]
  31. },
  32. create (context) {
  33. const options = context.options[0] || {}
  34. const groups = new Set(GROUP_NAMES.concat(options.groups || []))
  35. // ----------------------------------------------------------------------
  36. // Public
  37. // ----------------------------------------------------------------------
  38. return utils.executeOnVue(context, (obj) => {
  39. const usedNames = []
  40. const properties = utils.iterateProperties(obj, groups)
  41. for (const o of properties) {
  42. if (usedNames.indexOf(o.name) !== -1) {
  43. context.report({
  44. node: o.node,
  45. message: "Duplicated key '{{name}}'.",
  46. data: {
  47. name: o.name
  48. }
  49. })
  50. }
  51. usedNames.push(o.name)
  52. }
  53. })
  54. }
  55. }