valid-define-props.js 4.4 KB

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