no-watch-after-await.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const { ReferenceTracker } = require('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: 'The `watch` after `await` expression are forbidden.'
  53. }
  54. },
  55. /** @param {RuleContext} context */
  56. create(context) {
  57. const watchCallNodes = new Set()
  58. /** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration, { setupProperty: Property, afterAwait: boolean }>} */
  59. const setupFunctions = new Map()
  60. /**
  61. * @typedef {object} ScopeStack
  62. * @property {ScopeStack | null} upper
  63. * @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} functionNode
  64. */
  65. /** @type {ScopeStack | null} */
  66. let scopeStack = null
  67. return Object.assign(
  68. {
  69. Program() {
  70. const tracker = new ReferenceTracker(context.getScope())
  71. const traceMap = {
  72. vue: {
  73. [ReferenceTracker.ESM]: true,
  74. watch: {
  75. [ReferenceTracker.CALL]: true
  76. },
  77. watchEffect: {
  78. [ReferenceTracker.CALL]: true
  79. }
  80. }
  81. }
  82. for (const { node } of tracker.iterateEsmReferences(traceMap)) {
  83. watchCallNodes.add(node)
  84. }
  85. }
  86. },
  87. utils.defineVueVisitor(context, {
  88. /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
  89. ':function'(node) {
  90. scopeStack = {
  91. upper: scopeStack,
  92. functionNode: node
  93. }
  94. },
  95. onSetupFunctionEnter(node) {
  96. setupFunctions.set(node, {
  97. setupProperty: node.parent,
  98. afterAwait: false
  99. })
  100. },
  101. AwaitExpression() {
  102. if (!scopeStack) {
  103. return
  104. }
  105. const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
  106. if (!setupFunctionData) {
  107. return
  108. }
  109. setupFunctionData.afterAwait = true
  110. },
  111. /** @param {CallExpression} node */
  112. CallExpression(node) {
  113. if (!scopeStack) {
  114. return
  115. }
  116. const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
  117. if (!setupFunctionData || !setupFunctionData.afterAwait) {
  118. return
  119. }
  120. if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) {
  121. context.report({
  122. node,
  123. messageId: 'forbidden'
  124. })
  125. }
  126. },
  127. /** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
  128. ':function:exit'(node) {
  129. scopeStack = scopeStack && scopeStack.upper
  130. setupFunctions.delete(node)
  131. }
  132. })
  133. )
  134. }
  135. }