valid-define-props.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-community/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', 'vue2-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` is 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. const typeArguments =
  47. 'typeArguments' in node ? node.typeArguments : node.typeParameters
  48. if (node.arguments.length > 0) {
  49. if (typeArguments && typeArguments.params.length > 0) {
  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 (!typeArguments || typeArguments.params.length === 0) {
  60. emptyDefineProps = node
  61. }
  62. }
  63. },
  64. Identifier(node) {
  65. for (const defineProps of propsDefExpressions) {
  66. if (utils.inRange(defineProps.range, node)) {
  67. const variable = findVariable(utils.getScope(context, node), node)
  68. if (
  69. variable &&
  70. variable.references.some((ref) => ref.identifier === node) &&
  71. variable.defs.length > 0 &&
  72. variable.defs.every(
  73. (def) =>
  74. def.type !== 'ImportBinding' &&
  75. utils.inRange(scriptSetup.range, def.name) &&
  76. !utils.inRange(defineProps.range, def.name)
  77. )
  78. ) {
  79. if (utils.withinTypeNode(node)) {
  80. continue
  81. }
  82. //`defineProps` is referencing locally declared variables.
  83. context.report({
  84. node,
  85. messageId: 'referencingLocally'
  86. })
  87. }
  88. }
  89. }
  90. }
  91. }),
  92. utils.defineVueVisitor(context, {
  93. onVueObjectEnter(node, { type }) {
  94. if (type !== 'export' || utils.inRange(scriptSetup.range, node)) {
  95. return
  96. }
  97. hasDefaultExport = Boolean(utils.findProperty(node, 'props'))
  98. }
  99. }),
  100. {
  101. 'Program:exit'() {
  102. if (definePropsNodes.length === 0) {
  103. return
  104. }
  105. if (definePropsNodes.length > 1) {
  106. // `defineProps` has been called multiple times.
  107. for (const node of definePropsNodes) {
  108. context.report({
  109. node,
  110. messageId: 'multiple'
  111. })
  112. }
  113. return
  114. }
  115. if (emptyDefineProps) {
  116. if (!hasDefaultExport) {
  117. // Props are not defined.
  118. context.report({
  119. node: emptyDefineProps,
  120. messageId: 'notDefined'
  121. })
  122. }
  123. } else {
  124. if (hasDefaultExport) {
  125. // Props are defined in both `defineProps` and `export default {}`.
  126. for (const node of definePropsNodes) {
  127. context.report({
  128. node,
  129. messageId: 'definedInBoth'
  130. })
  131. }
  132. }
  133. }
  134. }
  135. }
  136. )
  137. }
  138. }