no-dupe-keys.js 1.4 KB

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