no-dupe-keys.js 1.7 KB

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