valid-define-emits.js 4.5 KB

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