-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-restricted-call-after-await.js
260 lines (245 loc) · 7 KB
/
no-restricted-call-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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const fs = require('fs')
const path = require('path')
const { ReferenceTracker } = require('eslint-utils')
const utils = require('../utils')
/**
* @typedef {import('eslint-utils').TYPES.TraceMap} TraceMap
* @typedef {import('eslint-utils').TYPES.TraceKind} TraceKind
*/
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow asynchronously called restricted methods',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-restricted-call-after-await.html'
},
fixable: null,
schema: {
type: 'array',
items: {
type: 'object',
properties: {
module: { type: 'string' },
path: {
anyOf: [
{ type: 'string' },
{
type: 'array',
items: {
type: 'string'
}
}
]
},
message: { type: 'string', minLength: 1 }
},
required: ['module'],
additionalProperties: false
},
uniqueItems: true,
minItems: 0
},
messages: {
// eslint-disable-next-line eslint-plugin/report-message-format
restricted: '{{message}}'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @typedef {object} SetupScopeData
* @property {boolean} afterAwait
* @property {[number,number]} range
*/
/** @type {Map<ESNode, string>} */
const restrictedCallNodes = new Map()
/** @type {Map<FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program, SetupScopeData>} */
const setupScopes = new Map()
/**x
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration | Program} scopeNode
*/
/** @type {ScopeStack | null} */
let scopeStack = null
/** @type {Record<string, string[]> | null} */
let allLocalImports = null
/**
* @param {string} id
*/
function safeRequireResolve(id) {
try {
if (fs.statSync(id).isDirectory()) {
return require.resolve(id)
}
} catch (_e) {
// ignore
}
return id
}
/**
* @param {Program} ast
*/
function getAllLocalImports(ast) {
if (!allLocalImports) {
allLocalImports = {}
const dir = path.dirname(context.getFilename())
for (const body of ast.body) {
if (body.type !== 'ImportDeclaration') {
continue
}
const source = String(body.source.value)
if (!source.startsWith('.')) {
continue
}
const modulePath = safeRequireResolve(path.join(dir, source))
const list =
allLocalImports[modulePath] || (allLocalImports[modulePath] = [])
list.push(source)
}
}
return allLocalImports
}
function getCwd() {
if (context.getCwd) {
return context.getCwd()
}
return path.resolve('')
}
/**
* @param {string} moduleName
* @param {Program} ast
* @returns {string[]}
*/
function normalizeModules(moduleName, ast) {
/** @type {string} */
let modulePath
if (moduleName.startsWith('.')) {
modulePath = safeRequireResolve(path.join(getCwd(), moduleName))
} else if (path.isAbsolute(moduleName)) {
modulePath = safeRequireResolve(moduleName)
} else {
return [moduleName]
}
return getAllLocalImports(ast)[modulePath] || []
}
return utils.compositingVisitors(
{
/** @param {Program} node */
Program(node) {
scopeStack = {
upper: scopeStack,
scopeNode: node
}
const tracker = new ReferenceTracker(context.getScope())
for (const option of context.options) {
const modules = normalizeModules(option.module, node)
for (const module of modules) {
/** @type {TraceMap} */
const traceMap = {
[module]: {
[ReferenceTracker.ESM]: true
}
}
/** @type {TraceKind & TraceMap} */
const mod = traceMap[module]
let local = mod
const paths = Array.isArray(option.path)
? option.path
: [option.path || 'default']
for (const path of paths) {
local = local[path] || (local[path] = {})
}
local[ReferenceTracker.CALL] = true
const message =
option.message ||
`The \`${[`import("${module}")`, ...paths].join(
'.'
)}\` after \`await\` expression are forbidden.`
for (const { node } of tracker.iterateEsmReferences(traceMap)) {
restrictedCallNodes.set(node, message)
}
}
}
},
/** @param {FunctionExpression | ArrowFunctionExpression | FunctionDeclaration} node */
':function'(node) {
scopeStack = {
upper: scopeStack,
scopeNode: node
}
},
':function:exit'() {
scopeStack = scopeStack && scopeStack.upper
},
/** @param {AwaitExpression} node */
AwaitExpression(node) {
if (!scopeStack) {
return
}
const setupScope = setupScopes.get(scopeStack.scopeNode)
if (!setupScope || !utils.inRange(setupScope.range, node)) {
return
}
setupScope.afterAwait = true
},
/** @param {CallExpression} node */
CallExpression(node) {
if (!scopeStack) {
return
}
const setupScope = setupScopes.get(scopeStack.scopeNode)
if (
!setupScope ||
!setupScope.afterAwait ||
!utils.inRange(setupScope.range, node)
) {
return
}
const message = restrictedCallNodes.get(node)
if (message) {
context.report({
node,
messageId: 'restricted',
data: { message }
})
}
}
},
(() => {
const scriptSetup = utils.getScriptSetupElement(context)
if (!scriptSetup) {
return {}
}
return {
/**
* @param {Program} node
*/
Program(node) {
setupScopes.set(node, {
afterAwait: false,
range: scriptSetup.range
})
}
}
})(),
utils.defineVueVisitor(context, {
onSetupFunctionEnter(node) {
setupScopes.set(node, {
afterAwait: false,
range: node.range
})
},
onSetupFunctionExit(node) {
setupScopes.delete(node)
}
})
)
}
}