no-watch-after-await.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /** @param {Program} program */
  75. Program(program) {
  76. const tracker = new ReferenceTracker(utils.getScope(context, program))
  77. for (const { node } of utils.iterateReferencesTraceMap(tracker, {
  78. watch: {
  79. [ReferenceTracker.CALL]: true
  80. },
  81. watchEffect: {
  82. [ReferenceTracker.CALL]: true
  83. }
  84. })) {
  85. watchCallNodes.add(node)
  86. }
  87. }
  88. },
  89. utils.defineVueVisitor(context, {
  90. onSetupFunctionEnter(node) {
  91. setupScopes.set(node, {
  92. afterAwait: false,
  93. range: node.range
  94. })
  95. },
  96. /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
  97. ':function'(node) {
  98. scopeStack = {
  99. upper: scopeStack,
  100. scopeNode: node
  101. }
  102. },
  103. ':function:exit'() {
  104. scopeStack = scopeStack && scopeStack.upper
  105. },
  106. /** @param {AwaitExpression} node */
  107. AwaitExpression(node) {
  108. if (!scopeStack) {
  109. return
  110. }
  111. const setupScope = setupScopes.get(scopeStack.scopeNode)
  112. if (!setupScope || !utils.inRange(setupScope.range, node)) {
  113. return
  114. }
  115. setupScope.afterAwait = true
  116. },
  117. /** @param {CallExpression} node */
  118. CallExpression(node) {
  119. if (!scopeStack) {
  120. return
  121. }
  122. const setupScope = setupScopes.get(scopeStack.scopeNode)
  123. if (
  124. !setupScope ||
  125. !setupScope.afterAwait ||
  126. !utils.inRange(setupScope.range, node)
  127. ) {
  128. return
  129. }
  130. if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) {
  131. context.report({
  132. node,
  133. messageId: 'forbidden'
  134. })
  135. }
  136. },
  137. onSetupFunctionExit(node) {
  138. setupScopes.delete(node)
  139. }
  140. })
  141. )
  142. }
  143. }