valid-define-props.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @author Yosuke Ota <https://github.com/ota-meshi>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const { findVariable } = require('eslint-utils')
  7. const utils = require('../utils')
  8. module.exports = {
  9. meta: {
  10. type: 'problem',
  11. docs: {
  12. description: 'enforce valid `defineProps` compiler macro',
  13. categories: ['vue3-essential'],
  14. url: 'https://eslint.vuejs.org/rules/valid-define-props.html'
  15. },
  16. fixable: null,
  17. schema: [],
  18. messages: {
  19. hasTypeAndArg:
  20. '`defineProps` has both a type-only props and an argument.',
  21. referencingLocally:
  22. '`defineProps` are referencing locally declared variables.',
  23. multiple: '`defineProps` has been called multiple times.',
  24. notDefined: 'Props are not defined.',
  25. definedInBoth:
  26. 'Props are defined in both `defineProps` and `export default {}`.'
  27. }
  28. },
  29. /** @param {RuleContext} context */
  30. create(context) {
  31. const scriptSetup = utils.getScriptSetupElement(context)
  32. if (!scriptSetup) {
  33. return {}
  34. }
  35. /** @type {Set<Expression | SpreadElement>} */
  36. const propsDefExpressions = new Set()
  37. let hasDefaultExport = false
  38. /** @type {CallExpression[]} */
  39. const definePropsNodes = []
  40. /** @type {CallExpression | null} */
  41. let emptyDefineProps = null
  42. return utils.compositingVisitors(
  43. utils.defineScriptSetupVisitor(context, {
  44. onDefinePropsEnter(node) {
  45. definePropsNodes.push(node)
  46. if (node.arguments.length >= 1) {
  47. if (node.typeParameters && node.typeParameters.params.length >= 1) {
  48. // `defineProps` has both a literal type and an argument.
  49. context.report({
  50. node,
  51. messageId: 'hasTypeAndArg'
  52. })
  53. return
  54. }
  55. propsDefExpressions.add(node.arguments[0])
  56. } else {
  57. if (
  58. !node.typeParameters ||
  59. node.typeParameters.params.length === 0
  60. ) {
  61. emptyDefineProps = node
  62. }
  63. }
  64. },
  65. Identifier(node) {
  66. for (const defineProps of propsDefExpressions) {
  67. if (utils.inRange(defineProps.range, node)) {
  68. const variable = findVariable(context.getScope(), node)
  69. if (
  70. variable &&
  71. variable.references.some((ref) => ref.identifier === node)
  72. ) {
  73. if (
  74. variable.defs.length &&
  75. variable.defs.every(
  76. (def) =>
  77. def.type !== 'ImportBinding' &&
  78. utils.inRange(scriptSetup.range, def.name) &&
  79. !utils.inRange(defineProps.range, def.name)
  80. )
  81. ) {
  82. if (utils.withinTypeNode(node)) {
  83. continue
  84. }
  85. //`defineProps` are referencing locally declared variables.
  86. context.report({
  87. node,
  88. messageId: 'referencingLocally'
  89. })
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }),
  96. utils.defineVueVisitor(context, {
  97. onVueObjectEnter(node, { type }) {
  98. if (type !== 'export' || utils.inRange(scriptSetup.range, node)) {
  99. return
  100. }
  101. hasDefaultExport = Boolean(utils.findProperty(node, 'props'))
  102. }
  103. }),
  104. {
  105. 'Program:exit'() {
  106. if (!definePropsNodes.length) {
  107. return
  108. }
  109. if (definePropsNodes.length > 1) {
  110. // `defineProps` has been called multiple times.
  111. for (const node of definePropsNodes) {
  112. context.report({
  113. node,
  114. messageId: 'multiple'
  115. })
  116. }
  117. return
  118. }
  119. if (emptyDefineProps) {
  120. if (!hasDefaultExport) {
  121. // Props are not defined.
  122. context.report({
  123. node: emptyDefineProps,
  124. messageId: 'notDefined'
  125. })
  126. }
  127. } else {
  128. if (hasDefaultExport) {
  129. // Props are defined in both `defineProps` and `export default {}`.
  130. for (const node of definePropsNodes) {
  131. context.report({
  132. node,
  133. messageId: 'definedInBoth'
  134. })
  135. }
  136. }
  137. }
  138. }
  139. }
  140. )
  141. }
  142. }