no-watch-after-await.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const { ReferenceTracker } = require('@eslint-community/eslint-utils')
  7. const utils = require('../utils')
  8. /**
  9. * @param {CallExpression | ChainExpression} node
  10. * @returns {boolean}
  11. */
  12. function isMaybeUsedStopHandle(node) {
  13. const parent = node.parent
  14. if (parent) {
  15. if (parent.type === 'VariableDeclarator') {
  16. // var foo = watch()
  17. return true
  18. }
  19. if (parent.type === 'AssignmentExpression') {
  20. // foo = watch()
  21. return true
  22. }
  23. if (parent.type === 'CallExpression') {
  24. // foo(watch())
  25. return true
  26. }
  27. if (parent.type === 'Property') {
  28. // {foo: watch()}
  29. return true
  30. }
  31. if (parent.type === 'ArrayExpression') {
  32. // [watch()]
  33. return true
  34. }
  35. if (parent.type === 'ChainExpression') {
  36. return isMaybeUsedStopHandle(parent)
  37. }
  38. }
  39. return false
  40. }
  41. module.exports = {
  42. meta: {
  43. type: 'suggestion',
  44. docs: {
  45. description: 'disallow asynchronously registered `watch`',
  46. categories: ['vue3-essential'],
  47. url: 'https://eslint.vuejs.org/rules/no-watch-after-await.html'
  48. },
  49. fixable: null,
  50. schema: [],
  51. messages: {
  52. forbidden: '`watch` is forbidden after an `await` expression.'
  53. }
  54. },
  55. /** @param {RuleContext} context */
  56. create(context) {
  57. const watchCallNodes = new Set()
  58. /**
  59. * @typedef {object} SetupScopeData
  60. * @property {boolean} afterAwait
  61. * @property {[number,number]} range
  62. */
  63. /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration, SetupScopeData>} */
  64. const setupScopes = new Map()
  65. /**
  66. * @typedef {object} ScopeStack
  67. * @property {ScopeStack | null} upper
  68. * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} scopeNode
  69. */
  70. /** @type {ScopeStack | null} */
  71. let scopeStack = null
  72. return utils.compositingVisitors(
  73. {
  74. Program() {
  75. const tracker = new ReferenceTracker(context.getScope())
  76. const traceMap = {
  77. vue: {
  78. [ReferenceTracker.ESM]: true,
  79. watch: {
  80. [ReferenceTracker.CALL]: true
  81. },
  82. watchEffect: {
  83. [ReferenceTracker.CALL]: true
  84. }
  85. }
  86. }
  87. for (const { node } of tracker.iterateEsmReferences(traceMap)) {
  88. watchCallNodes.add(node)
  89. }
  90. }
  91. },
  92. utils.defineVueVisitor(context, {
  93. onSetupFunctionEnter(node) {
  94. setupScopes.set(node, {
  95. afterAwait: false,
  96. range: node.range
  97. })
  98. },
  99. /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
  100. ':function'(node) {
  101. scopeStack = {
  102. upper: scopeStack,
  103. scopeNode: node
  104. }
  105. },
  106. ':function:exit'() {
  107. scopeStack = scopeStack && scopeStack.upper
  108. },
  109. /** @param {AwaitExpression} node */
  110. AwaitExpression(node) {
  111. if (!scopeStack) {
  112. return
  113. }
  114. const setupScope = setupScopes.get(scopeStack.scopeNode)
  115. if (!setupScope || !utils.inRange(setupScope.range, node)) {
  116. return
  117. }
  118. setupScope.afterAwait = true
  119. },
  120. /** @param {CallExpression} node */
  121. CallExpression(node) {
  122. if (!scopeStack) {
  123. return
  124. }
  125. const setupScope = setupScopes.get(scopeStack.scopeNode)
  126. if (
  127. !setupScope ||
  128. !setupScope.afterAwait ||
  129. !utils.inRange(setupScope.range, node)
  130. ) {
  131. return
  132. }
  133. if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) {
  134. context.report({
  135. node,
  136. messageId: 'forbidden'
  137. })
  138. }
  139. },
  140. onSetupFunctionExit(node) {
  141. setupScopes.delete(node)
  142. }
  143. })
  144. )
  145. }
  146. }