valid-define-props.js 4.5 KB

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