deno_lint 0.84.1

lint for deno
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use super::{Context, LintRule};
use crate::handler::{Handler, Traverse};
use crate::swc_util::StringRepr;
use crate::tags::{self, Tags};

use deno_ast::swc::parser::token::{Token, Word};
use deno_ast::view::{NodeTrait, Program};
use deno_ast::SourceRange;
use deno_ast::SourceRanged;
use deno_ast::SourceRangedForSpanned;
use derive_more::Display;

#[derive(Debug)]
pub struct RequireAwait;

const CODE: &str = "require-await";

#[derive(Display)]
enum RequireAwaitMessage {
  #[display(
    fmt = "Async function '{}' has no 'await' expression or 'await using' declaration.",
    _0
  )]
  Function(String),
  #[display(
    fmt = "Async function has no 'await' expression or 'await using' declaration."
  )]
  AnonymousFunction,
  #[display(
    fmt = "Async arrow function has no 'await' expression or 'await using' declaration."
  )]
  ArrowFunction,
  #[display(
    fmt = "Async method '{}' has no 'await' expression or 'await using' declaration.",
    _0
  )]
  Method(String),
  #[display(
    fmt = "Async method has no 'await' expression or 'await using' declaration."
  )]
  AnonymousMethod,
}

#[derive(Display)]
enum RequireAwaitHint {
  #[display(
    fmt = "Remove 'async' keyword from the function or use 'await' expression or 'await using' declaration inside."
  )]
  RemoveOrUse,
}

impl LintRule for RequireAwait {
  fn tags(&self) -> Tags {
    &[tags::RECOMMENDED]
  }

  fn code(&self) -> &'static str {
    CODE
  }

  fn lint_program_with_ast_view(
    &self,
    context: &mut Context,
    program: Program,
  ) {
    RequireAwaitHandler.traverse(program, context);
  }
}

enum FunctionKind {
  Function(Option<String>),
  ArrowFunction,
  Method(Option<String>),
}

impl From<FunctionKind> for RequireAwaitMessage {
  fn from(kind: FunctionKind) -> Self {
    match kind {
      FunctionKind::Function(Some(name)) => RequireAwaitMessage::Function(name),
      FunctionKind::Function(None) => RequireAwaitMessage::AnonymousFunction,
      FunctionKind::ArrowFunction => RequireAwaitMessage::ArrowFunction,
      FunctionKind::Method(Some(name)) => RequireAwaitMessage::Method(name),
      FunctionKind::Method(None) => RequireAwaitMessage::AnonymousMethod,
    }
  }
}

impl Default for FunctionKind {
  fn default() -> Self {
    FunctionKind::Function(None)
  }
}

struct FunctionInfo {
  kind: FunctionKind,
  is_async: bool,
  is_generator: bool,
  is_empty: bool,
  has_await: bool,
}

impl FunctionInfo {
  fn should_report(self) -> Option<RequireAwaitMessage> {
    if self.is_async && !self.is_generator && !self.is_empty && !self.has_await
    {
      Some(self.kind.into())
    } else {
      None
    }
  }
}

fn find_async_token_range(
  node: deno_ast::view::Node,
  ctx: &Context,
) -> SourceRange {
  node
    .tokens_fast(ctx.program())
    .iter()
    .find(|t| t.token == Token::Word(Word::Ident("async".into())))
    .expect("there must be a async span")
    .range()
}

struct RequireAwaitHandler;

impl Handler for RequireAwaitHandler {
  fn fn_decl(&mut self, fn_decl: &deno_ast::view::FnDecl, ctx: &mut Context) {
    let function_info = FunctionInfo {
      kind: FunctionKind::Function(Some(
        fn_decl.ident.sym().as_ref().to_string(),
      )),
      is_async: fn_decl.function.is_async(),
      is_generator: fn_decl.function.is_generator(),
      is_empty: is_body_empty(fn_decl.function.body),
      has_await: false,
    };

    let range = if function_info.is_async {
      find_async_token_range(fn_decl.as_node(), ctx)
    } else {
      fn_decl.ident.range()
    };

    process_function(fn_decl.as_node(), range, function_info, ctx);
  }

  fn fn_expr(&mut self, fn_expr: &deno_ast::view::FnExpr, ctx: &mut Context) {
    let function_info = FunctionInfo {
      kind: FunctionKind::Function(
        fn_expr.ident.as_ref().map(|i| i.sym().as_ref().to_string()),
      ),
      is_async: fn_expr.function.is_async(),
      is_generator: fn_expr.function.is_generator(),
      is_empty: is_body_empty(fn_expr.function.body),
      has_await: false,
    };
    let range = if function_info.is_async {
      find_async_token_range(fn_expr.as_node(), ctx)
    } else {
      fn_expr.range()
    };
    process_function(fn_expr.as_node(), range, function_info, ctx);
  }

  fn arrow_expr(
    &mut self,
    arrow_expr: &deno_ast::view::ArrowExpr,
    ctx: &mut Context,
  ) {
    let function_info = FunctionInfo {
      kind: FunctionKind::ArrowFunction,
      is_async: arrow_expr.is_async(),
      is_generator: arrow_expr.is_generator(),
      is_empty: matches!(
        &arrow_expr.body,
        deno_ast::view::BlockStmtOrExpr::BlockStmt(block_stmt) if block_stmt.stmts.is_empty()
      ),
      has_await: false,
    };
    let range = if function_info.is_async {
      find_async_token_range(arrow_expr.as_node(), ctx)
    } else {
      arrow_expr.range()
    };
    process_function(arrow_expr.as_node(), range, function_info, ctx);
  }

  fn method_prop(
    &mut self,
    method_prop: &deno_ast::view::MethodProp,
    ctx: &mut Context,
  ) {
    let function_info = FunctionInfo {
      kind: FunctionKind::Method(method_prop.key.string_repr()),
      is_async: method_prop.function.is_async(),
      is_generator: method_prop.function.is_generator(),
      is_empty: is_body_empty(method_prop.function.body),
      has_await: false,
    };

    let range = if function_info.is_async {
      find_async_token_range(method_prop.as_node(), ctx)
    } else {
      method_prop.inner.key.range()
    };

    process_function(method_prop.as_node(), range, function_info, ctx);
  }

  fn class_method(
    &mut self,
    class_method: &deno_ast::view::ClassMethod,
    ctx: &mut Context,
  ) {
    let function_info = FunctionInfo {
      kind: FunctionKind::Method(class_method.key.string_repr()),
      is_async: class_method.function.is_async(),
      is_generator: class_method.function.is_generator(),
      is_empty: is_body_empty(class_method.function.body),
      has_await: false,
    };

    let range = if function_info.is_async {
      find_async_token_range(class_method.as_node(), ctx)
    } else {
      class_method.inner.key.range()
    };

    process_function(class_method.as_node(), range, function_info, ctx);
  }

  fn private_method(
    &mut self,
    private_method: &deno_ast::view::PrivateMethod,
    ctx: &mut Context,
  ) {
    let function_info = FunctionInfo {
      kind: FunctionKind::Method(private_method.key.string_repr()),
      is_async: private_method.function.is_async(),
      is_generator: private_method.function.is_generator(),
      is_empty: is_body_empty(private_method.function.body),
      has_await: false,
    };
    let range = if function_info.is_async {
      find_async_token_range(private_method.as_node(), ctx)
    } else {
      private_method.inner.key.range()
    };
    process_function(private_method.as_node(), range, function_info, ctx);
  }
}

struct FunctionHandler {
  function_info: Option<Box<FunctionInfo>>,
}

impl Handler for FunctionHandler {
  fn fn_decl(&mut self, _n: &deno_ast::view::FnDecl, ctx: &mut Context) {
    ctx.stop_traverse();
  }

  fn fn_expr(&mut self, _n: &deno_ast::view::FnExpr, ctx: &mut Context) {
    ctx.stop_traverse();
  }

  fn arrow_expr(&mut self, _n: &deno_ast::view::ArrowExpr, ctx: &mut Context) {
    ctx.stop_traverse();
  }

  fn method_prop(
    &mut self,
    _n: &deno_ast::view::MethodProp,
    ctx: &mut Context,
  ) {
    ctx.stop_traverse();
  }

  fn class_method(
    &mut self,
    _n: &deno_ast::view::ClassMethod,
    ctx: &mut Context,
  ) {
    ctx.stop_traverse();
  }

  fn private_method(
    &mut self,
    _n: &deno_ast::view::PrivateMethod,
    ctx: &mut Context,
  ) {
    ctx.stop_traverse();
  }

  fn await_expr(&mut self, _n: &deno_ast::view::AwaitExpr, _ctx: &mut Context) {
    if let Some(info) = self.function_info.as_mut() {
      info.has_await = true;
    }
  }

  fn for_of_stmt(
    &mut self,
    for_of_stmt: &deno_ast::view::ForOfStmt,
    _ctx: &mut Context,
  ) {
    if for_of_stmt.is_await() {
      if let Some(info) = self.function_info.as_mut() {
        info.has_await = true;
      }
    }
  }

  fn using_decl(
    &mut self,
    using_decl: &deno_ast::view::UsingDecl,
    _ctx: &mut Context,
  ) {
    if using_decl.is_await() {
      if let Some(info) = self.function_info.as_mut() {
        info.has_await = true;
      }
    }
  }
}

fn process_function<'a, N>(
  node: N,
  range: SourceRange,
  function_info: FunctionInfo,
  ctx: &mut Context,
) where
  N: NodeTrait<'a>,
{
  let mut function_handler = FunctionHandler {
    function_info: Some(Box::new(function_info)),
  };

  for child in node.children() {
    function_handler.traverse(child, ctx)
  }

  let function_info = function_handler.function_info.take().unwrap();

  if let Some(message) = function_info.should_report() {
    ctx.add_diagnostic_with_hint(
      range,
      CODE,
      message,
      RequireAwaitHint::RemoveOrUse,
    );
  }
}

fn is_body_empty(maybe_body: Option<&deno_ast::view::BlockStmt>) -> bool {
  maybe_body.is_none_or(|body| body.stmts.is_empty())
}

#[cfg(test)]
mod tests {
  use super::*;

  // Some tests are derived from
  // https://github.com/eslint/eslint/blob/v7.13.0/tests/lib/rules/require-await.js
  // MIT Licensed.

  #[test]
  fn require_await_valid() {
    assert_lint_ok! {
      RequireAwait,
      "async function foo() { await doSomething() }",
      "(async function() { await doSomething() })",
      "async () => { await doSomething() }",
      "async () => await doSomething()",
      "({ async foo() { await doSomething() } })",
      "class A { async foo() { await doSomething() } }",
      "(class { async foo() { await doSomething() } })",
      "async function foo() { await (async () => { await doSomething() }) }",
      "async function foo() { const bar = <number>await doSomething() }",

      // empty functions are ok.
      "async function foo() {}",
      "async () => {}",

      // normal functions are ok.
      "function foo() { doSomething() }",

      // for-await-of
      "async function foo() { for await (x of xs); }",

      // using-await
      "async function foo() { await using foo = doSomething(); }",

      // global await
      "await foo()",
      r#"
for await (let num of asyncIterable) {
    console.log(num);
}
      "#,

      // generator
      "async function* run() { yield * anotherAsyncGenerator() }",
      r#"
async function* run() {
  await new Promise(resolve => setTimeout(resolve, 100));
  yield 'Hello';
  console.log('World');
}
      "#,
      "async function* run() { }",
      "const foo = async function *(){}",
      r#"const foo = async function *(){ console.log("bar") }"#,
      r#"async function* run() { console.log("bar") }"#,
    };
  }

  #[test]
  fn require_await_invalid() {
    assert_lint_err! {
      RequireAwait,
      "async function foo() { doSomething() }": [
        {
          col: 0,
          message: variant!(RequireAwaitMessage, Function, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "(async function() { doSomething() })": [
        {
          col: 1,
          message: RequireAwaitMessage::AnonymousFunction,
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "async () => { doSomething() }": [
        {
          col: 0,
          message: RequireAwaitMessage::ArrowFunction,
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "async () => doSomething()": [
        {
          col: 0,
          message: RequireAwaitMessage::ArrowFunction,
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "({ async foo() { doSomething() } })": [
        {
          col: 3,
          message: variant!(RequireAwaitMessage, Method, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "class A { async foo() { doSomething() } }": [
        {
          col: 10,
          message: variant!(RequireAwaitMessage, Method, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "class A { private async foo() { doSomething() } }": [
        {
          col: 18,
          message: variant!(RequireAwaitMessage, Method, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "(class { async foo() { doSomething() } })": [
        {
          col: 9,
          message: variant!(RequireAwaitMessage, Method, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "(class { async ''() { doSomething() } })": [
        {
          col: 9,
          message: variant!(RequireAwaitMessage, Method, ""),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "async function foo() { async () => { await doSomething() } }": [
        {
          col: 0,
          message: variant!(RequireAwaitMessage, Function, "foo"),
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
      "async function foo() { await (async () => { doSomething() }) }": [
        {
          col: 30,
          message: RequireAwaitMessage::ArrowFunction,
          hint: RequireAwaitHint::RemoveOrUse,
        },
      ],
    };
  }
}