no-dupe-keys.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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', 'vue2-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. duplicateKey:
  76. "Duplicate key '{{name}}'. May cause name collision in script or template tag."
  77. }
  78. },
  79. /** @param {RuleContext} context */
  80. create(context) {
  81. const options = context.options[0] || {}
  82. const groups = new Set([...GROUP_NAMES, ...(options.groups || [])])
  83. return utils.compositingVisitors(
  84. utils.executeOnVue(context, (obj) => {
  85. const properties = utils.iterateProperties(obj, groups)
  86. /** @type {Set<string>} */
  87. const usedNames = new Set()
  88. for (const o of properties) {
  89. if (usedNames.has(o.name)) {
  90. context.report({
  91. node: o.node,
  92. messageId: 'duplicateKey',
  93. data: {
  94. name: o.name
  95. }
  96. })
  97. }
  98. usedNames.add(o.name)
  99. }
  100. }),
  101. utils.defineScriptSetupVisitor(context, {
  102. onDefinePropsEnter(node, props) {
  103. const propsNode = getPropsPattern(node)
  104. const propReferences = [
  105. ...(propsNode ? extractReferences(propsNode) : []),
  106. node
  107. ]
  108. for (const prop of props) {
  109. if (!prop.propName) continue
  110. const variable = findVariable(
  111. utils.getScope(context, node),
  112. prop.propName
  113. )
  114. if (!variable || variable.defs.length === 0) continue
  115. if (
  116. variable.defs.some((def) => {
  117. if (def.type !== 'Variable') return false
  118. return isInsideInitializer(def.node, propReferences)
  119. })
  120. ) {
  121. continue
  122. }
  123. context.report({
  124. node: variable.defs[0].node,
  125. messageId: 'duplicateKey',
  126. data: {
  127. name: prop.propName
  128. }
  129. })
  130. }
  131. }
  132. })
  133. )
  134. /**
  135. * Extracts references from the given node.
  136. * @param {Pattern} node
  137. * @returns {Identifier[]} References
  138. */
  139. function extractReferences(node) {
  140. if (node.type === 'Identifier') {
  141. const variable = findVariable(utils.getScope(context, node), node)
  142. if (!variable) {
  143. return []
  144. }
  145. return variable.references.map((ref) => ref.identifier)
  146. }
  147. if (node.type === 'ObjectPattern') {
  148. return node.properties.flatMap((prop) =>
  149. extractReferences(prop.type === 'Property' ? prop.value : prop)
  150. )
  151. }
  152. if (node.type === 'AssignmentPattern') {
  153. return extractReferences(node.left)
  154. }
  155. if (node.type === 'RestElement') {
  156. return extractReferences(node.argument)
  157. }
  158. return []
  159. }
  160. }
  161. }