valid-define-emits.js 4.4 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 `defineEmits` compiler macro',
  13. categories: ['vue3-essential', '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. if (node.arguments.length > 0) {
  46. if (node.typeParameters && node.typeParameters.params.length > 0) {
  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. variable.defs.length > 0 &&
  72. variable.defs.every(
  73. (def) =>
  74. def.type !== 'ImportBinding' &&
  75. utils.inRange(scriptSetup.range, def.name) &&
  76. !utils.inRange(defineEmits.range, def.name)
  77. )
  78. ) {
  79. if (utils.withinTypeNode(node)) {
  80. continue
  81. }
  82. //`defineEmits` 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, 'emits'))
  98. }
  99. }),
  100. {
  101. 'Program:exit'() {
  102. if (defineEmitsNodes.length === 0) {
  103. return
  104. }
  105. if (defineEmitsNodes.length > 1) {
  106. // `defineEmits` has been called multiple times.
  107. for (const node of defineEmitsNodes) {
  108. context.report({
  109. node,
  110. messageId: 'multiple'
  111. })
  112. }
  113. return
  114. }
  115. if (emptyDefineEmits) {
  116. if (!hasDefaultExport) {
  117. // Custom events are not defined.
  118. context.report({
  119. node: emptyDefineEmits,
  120. messageId: 'notDefined'
  121. })
  122. }
  123. } else {
  124. if (hasDefaultExport) {
  125. // Custom events are defined in both `defineEmits` and `export default {}`.
  126. for (const node of defineEmitsNodes) {
  127. context.report({
  128. node,
  129. messageId: 'definedInBoth'
  130. })
  131. }
  132. }
  133. }
  134. }
  135. }
  136. )
  137. }
  138. }