forked from denoland/deno_lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_await_in_loop.rs
More file actions
314 lines (301 loc) · 6.42 KB
/
no_await_in_loop.rs
File metadata and controls
314 lines (301 loc) · 6.42 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::Program;
use deno_ast::view::NodeTrait;
use deno_ast::{view as ast_view, SourceRanged};
#[derive(Debug)]
pub struct NoAwaitInLoop;
const CODE: &str = "no-await-in-loop";
const MESSAGE: &str = "Unexpected `await` inside a loop.";
const HINT: &str = "Remove `await` in loop body, store all promises generated and then `await Promise.all(storedPromises)` after the loop";
impl LintRule for NoAwaitInLoop {
fn code(&self) -> &'static str {
CODE
}
fn lint_program_with_ast_view(
&self,
context: &mut Context,
program: Program<'_>,
) {
NoAwaitInLoopHandler.traverse(program, context);
}
}
struct NoAwaitInLoopHandler;
impl Handler for NoAwaitInLoopHandler {
fn await_expr(
&mut self,
await_expr: &ast_view::AwaitExpr,
ctx: &mut Context,
) {
fn inside_loop(
await_expr: &ast_view::AwaitExpr,
node: ast_view::Node,
) -> bool {
use deno_ast::view::Node::*;
match node {
FnDecl(_) | FnExpr(_) | ArrowExpr(_) => false,
ForOfStmt(stmt) if stmt.is_await() => {
// `await` is allowed to use within the body of `for await (const x of y) { ... }`
false
}
ForInStmt(ast_view::ForInStmt { right, .. })
| ForOfStmt(ast_view::ForOfStmt { right, .. }) => {
// When it encounters `ForInStmt` or `ForOfStmt`, we should treat it as `inside_loop = true`
// except for the case where the given `await_expr` is contained in the `right` part.
// e.g. for (const x of await xs) { ... }
// ^^^^^^^^ <-------- `right` part
!right.range().contains(&await_expr.range())
}
ForStmt(stmt) => {
// When it encounters `ForStmt`, we should treat it as `inside_loop = true`
// except for the case where the given `await_expr` is contained in the `init` part.
// e.g. for (let i = await foo(); i < n; i++) { ... }
// ^^^^^^^^^^^^^^^^^^^ <---------- `init` part
stmt
.init
.as_ref()
.is_none_or(|init| !init.range().contains(&await_expr.range()))
}
WhileStmt(_) | DoWhileStmt(_) => true,
_ => {
let parent = match node.parent() {
Some(p) => p,
None => return false,
};
inside_loop(await_expr, parent)
}
}
}
if inside_loop(await_expr, await_expr.as_node()) {
ctx.add_diagnostic_with_hint(await_expr.range(), CODE, MESSAGE, HINT);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_await_in_loop_valid() {
assert_lint_ok! {
NoAwaitInLoop,
r#"
async function foo(things) {
const results = [];
for (const thing of things) {
results.push(bar(thing));
}
return baz(await Promise.all(results));
}
"#,
r#"
async function foo(things) {
for (const thing of things) {
const a = async () => await bar(thing);
}
}
"#,
r#"
async function foo(things) {
for (const thing of things) {
async function a() {
return await bar(42);
}
}
}
"#,
r#"
async function foo(things) {
for (const thing of things) {
const a = async function() {
return await bar(42);
}
}
}
"#,
r#"
async function foo(things) {
for await (const thing of things) {
console.log(await bar(thing));
}
}
"#,
r#"
async function foo(things) {
for await (const thing of await things) {
console.log(await bar(thing));
}
}
"#,
r#"
async function foo() {
for (let i = await bar(); i < n; i++) {
baz(i);
}
}
"#,
r#"
async function foo() {
for (const thing of await things) {
bar(thing);
}
}
"#,
r#"
async function foo() {
for (let thing in await things) {
bar(thing);
}
}
"#,
r#"
function foo() {
async function bar() {
for (const thing of await things) {}
}
}
"#,
// toplevel await
r#"
for (const thing of things) {
const a = async () => await bar(thing);
}
"#,
r#"
for (const thing of things) {
async function a() {
return await bar(42);
}
}
"#,
r#"
for (const thing of things) {
const a = async function() {
return await bar(42);
}
}
"#,
r#"
for await (const thing of things) {
console.log(await bar(thing));
}
"#,
r#"
for await (const thing of await things) {
console.log(await bar(thing));
}
"#,
r#"
for (let i = await bar(); i < n; i++) {
baz(i);
}
"#,
r#"
for (const thing of await things) {
bar(thing);
}
"#,
r#"
for (let thing in await things) {
bar(thing);
}
"#,
};
}
#[test]
fn no_await_in_loop_invalid() {
assert_lint_err! {
NoAwaitInLoop,
MESSAGE,
HINT,
r#"
async function foo(things) {
const results = [];
for (const thing of things) {
results.push(await bar(thing));
}
return baz(results);
}
"#: [{ line: 5, col: 17 }],
r#"
for (const thing of things) {
results.push(await foo(thing));
}
"#: [{ line: 3, col: 15 }],
r#"
for (let i = 0; i < await foo(); i++) {
bar();
}
"#: [{ line: 2, col: 20 }],
r#"
for (let i = 0; i < 42; await foo(i)) {
bar();
}
"#: [{ line: 2, col: 24 }],
r#"
for (let i = 0; i < 42; i++) {
await bar();
}
"#: [{ line: 3, col: 2 }],
r#"
for (const thing in things) {
await foo(thing);
}
"#: [{ line: 3, col: 2 }],
r#"
while (await foo()) {
bar();
}
"#: [{ line: 2, col: 7 }],
r#"
while (true) {
await foo();
}
"#: [{ line: 3, col: 2 }],
r#"
while (true) {
await foo();
}
"#: [{ line: 3, col: 2 }],
r#"
do {
foo();
} while (await bar());
"#: [{ line: 4, col: 9 }],
r#"
do {
await foo();
} while (true);
"#: [{ line: 3, col: 2 }],
r#"
for await (const thing of things) {
async function foo() {
for (const one of them) {
await bar(one);
}
}
await baz();
}
"#: [{ line: 5, col: 6 }],
r#"
function foo() {
async function bar() {
for (const thing of things) {
await baz(thing);
}
}
}
"#: [{ line: 5, col: 6 }],
r#"
async function foo() {
for (const thing of things) {
const xs = bar(thing);
for (const x in xs) {
await baz(x);
}
}
}
"#: [{ line: 6, col: 6 }]
}
}
}