no-dupe-keys.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @fileoverview Prevents duplication of field names.
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const { findVariable } = require('@eslint-community/eslint-utils')
  7. const utils = require('../utils')
  8. /**
  9. * @typedef {import('../utils').GroupName} GroupName
  10. * @typedef {import('eslint').Scope.Variable} Variable
  11. * @typedef {import('../utils').ComponentProp} ComponentProp
  12. */
  13. /** @type {GroupName[]} */
  14. const GROUP_NAMES = ['props', 'computed', 'data', 'methods', 'setup']
  15. /**
  16. * Gets the props pattern node from given `defineProps()` node
  17. * @param {CallExpression} node
  18. * @returns {Pattern|null}
  19. */
  20. function getPropsPattern(node) {
  21. let target = node
  22. if (
  23. target.parent &&
  24. target.parent.type === 'CallExpression' &&
  25. target.parent.arguments[0] === target &&
  26. target.parent.callee.type === 'Identifier' &&
  27. target.parent.callee.name === 'withDefaults'
  28. ) {
  29. target = target.parent
  30. }
  31. if (
  32. !target.parent ||
  33. target.parent.type !== 'VariableDeclarator' ||
  34. target.parent.init !== target
  35. ) {
  36. return null
  37. }
  38. return target.parent.id
  39. }
  40. /**
  41. * Checks whether the initialization of the given variable declarator node contains one of the references.
  42. * @param {VariableDeclarator} node
  43. * @param {ESNode[]} references
  44. */
  45. function isInsideInitializer(node, references) {
  46. const init = node.init
  47. if (!init) {
  48. return false
  49. }
  50. return references.some(
  51. (id) => init.range[0] <= id.range[0] && id.range[1] <= init.range[1]
  52. )
  53. }
  54. module.exports = {
  55. meta: {
  56. type: 'problem',
  57. docs: {
  58. description: 'disallow duplication of field names',
  59. categories: ['vue3-essential', 'essential'],
  60. url: 'https://eslint.vuejs.org/rules/no-dupe-keys.html'
  61. },
  62. fixable: null,
  63. schema: [
  64. {
  65. type: 'object',
  66. properties: {
  67. groups: {
  68. type: 'array'
  69. }
  70. },
  71. additionalProperties: false
  72. }
  73. ],
  74. messages: {
  75. duplicatedKey: "Duplicated key '{{name}}'."
  76. }
  77. },
  78. /** @param {RuleContext} context */
  79. create(context) {
  80. const options = context.options[0] || {}
  81. const groups = new Set([...GROUP_NAMES, ...(options.groups || [])])
  82. return utils.compositingVisitors(
  83. utils.executeOnVue(context, (obj) => {
  84. const properties = utils.iterateProperties(obj, groups)
  85. /** @type {Set<string>} */
  86. const usedNames = new Set()
  87. for (const o of properties) {
  88. if (usedNames.has(o.name)) {
  89. context.report({
  90. node: o.node,
  91. messageId: 'duplicatedKey',
  92. data: {
  93. name: o.name
  94. }
  95. })
  96. }
  97. usedNames.add(o.name)
  98. }
  99. }),
  100. utils.defineScriptSetupVisitor(context, {
  101. onDefinePropsEnter(node, props) {
  102. const propsNode = getPropsPattern(node)
  103. const propReferences = [
  104. ...(propsNode ? extractReferences(propsNode) : []),
  105. node
  106. ]
  107. for (const prop of props) {
  108. if (!prop.propName) continue
  109. const variable = findVariable(context.getScope(), prop.propName)
  110. if (!variable || variable.defs.length === 0) continue
  111. if (
  112. variable.defs.some((def) => {
  113. if (def.type !== 'Variable') return false
  114. return isInsideInitializer(def.node, propReferences)
  115. })
  116. ) {
  117. continue
  118. }
  119. context.report({
  120. node: variable.defs[0].node,
  121. messageId: 'duplicatedKey',
  122. data: {
  123. name: prop.propName
  124. }
  125. })
  126. }
  127. }
  128. })
  129. )
  130. /**
  131. * Extracts references from the given node.
  132. * @param {Pattern} node
  133. * @returns {Identifier[]} References
  134. */
  135. function extractReferences(node) {
  136. if (node.type === 'Identifier') {
  137. const variable = findVariable(context.getScope(), node)
  138. if (!variable) {
  139. return []
  140. }
  141. return variable.references.map((ref) => ref.identifier)
  142. }
  143. if (node.type === 'ObjectPattern') {
  144. return node.properties.flatMap((prop) =>
  145. extractReferences(prop.type === 'Property' ? prop.value : prop)
  146. )
  147. }
  148. if (node.type === 'AssignmentPattern') {
  149. return extractReferences(node.left)
  150. }
  151. if (node.type === 'RestElement') {
  152. return extractReferences(node.argument)
  153. }
  154. return []
  155. }
  156. }
  157. }