valid-define-emits.js 4.5 KB

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