valid-define-emits.js 4.5 KB

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