/
no-watch-after-await.js
137 lines (128 loc) · 3.56 KB
/
no-watch-after-await.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const { ReferenceTracker } = require('eslint-utils')
const utils = require('../utils')
function isMaybeUsedStopHandle (node) {
const parent = node.parent
if (parent) {
if (parent.type === 'VariableDeclarator') {
// var foo = watch()
return true
}
if (parent.type === 'AssignmentExpression') {
// foo = watch()
return true
}
if (parent.type === 'CallExpression') {
// foo(watch())
return true
}
if (parent.type === 'Property') {
// {foo: watch()}
return true
}
if (parent.type === 'ArrayExpression') {
// [watch()]
return true
}
}
return false
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow asynchronously registered `watch`',
categories: ['vue3-essential'],
url: 'https://eslint.vuejs.org/rules/no-watch-after-await.html'
},
fixable: null,
schema: [],
messages: {
forbidden: 'The `watch` after `await` expression are forbidden.'
}
},
create (context) {
const watchCallNodes = new Set()
const setupFunctions = new Map()
const forbiddenNodes = new Map()
function addForbiddenNode (property, node) {
let list = forbiddenNodes.get(property)
if (!list) {
list = []
forbiddenNodes.set(property, list)
}
list.push(node)
}
let scopeStack = { upper: null, functionNode: null }
return Object.assign(
{
'Program' () {
const tracker = new ReferenceTracker(context.getScope())
const traceMap = {
vue: {
[ReferenceTracker.ESM]: true,
watch: {
[ReferenceTracker.CALL]: true
},
watchEffect: {
[ReferenceTracker.CALL]: true
}
}
}
for (const { node } of tracker.iterateEsmReferences(traceMap)) {
watchCallNodes.add(node)
}
},
'Property[value.type=/^(Arrow)?FunctionExpression$/]' (node) {
if (utils.getStaticPropertyName(node) !== 'setup') {
return
}
setupFunctions.set(node.value, {
setupProperty: node,
afterAwait: false
})
},
':function' (node) {
scopeStack = { upper: scopeStack, functionNode: node }
},
'AwaitExpression' () {
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData) {
return
}
setupFunctionData.afterAwait = true
},
'CallExpression' (node) {
const setupFunctionData = setupFunctions.get(scopeStack.functionNode)
if (!setupFunctionData || !setupFunctionData.afterAwait) {
return
}
if (watchCallNodes.has(node) && !isMaybeUsedStopHandle(node)) {
addForbiddenNode(setupFunctionData.setupProperty, node)
}
},
':function:exit' (node) {
scopeStack = scopeStack.upper
setupFunctions.delete(node)
}
},
utils.executeOnVue(context, obj => {
const reportsList = obj.properties
.map(item => forbiddenNodes.get(item))
.filter(reports => !!reports)
for (const reports of reportsList) {
for (const node of reports) {
context.report({
node,
messageId: 'forbidden'
})
}
}
})
)
}
}