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
use super::Context;
use super::LintRule;
use swc_ecmascript::ast::{Expr, NewExpr, ParenExpr};
use swc_ecmascript::visit::noop_visit_type;
use swc_ecmascript::visit::Node;
use swc_ecmascript::visit::Visit;
use swc_ecmascript::visit::VisitWith;
pub struct NoAsyncPromiseExecutor;
impl LintRule for NoAsyncPromiseExecutor {
fn new() -> Box<Self> {
Box::new(NoAsyncPromiseExecutor)
}
fn tags(&self) -> &[&'static str] {
&["recommended"]
}
fn code(&self) -> &'static str {
"no-async-promise-executor"
}
fn lint_module(
&self,
context: &mut Context,
module: &swc_ecmascript::ast::Module,
) {
let mut visitor = NoAsyncPromiseExecutorVisitor::new(context);
visitor.visit_module(module, module);
}
}
struct NoAsyncPromiseExecutorVisitor<'c> {
context: &'c mut Context,
}
impl<'c> NoAsyncPromiseExecutorVisitor<'c> {
fn new(context: &'c mut Context) -> Self {
Self { context }
}
}
fn is_async_function(expr: &Expr) -> bool {
match expr {
Expr::Fn(fn_expr) => fn_expr.function.is_async,
Expr::Arrow(arrow_expr) => arrow_expr.is_async,
Expr::Paren(ParenExpr { ref expr, .. }) => is_async_function(&**expr),
_ => false,
}
}
impl<'c> Visit for NoAsyncPromiseExecutorVisitor<'c> {
noop_visit_type!();
fn visit_new_expr(&mut self, new_expr: &NewExpr, _parent: &dyn Node) {
new_expr.visit_children_with(self);
if let Expr::Ident(ident) = &*new_expr.callee {
let name = ident.sym.as_ref();
if name != "Promise" {
return;
}
if let Some(args) = &new_expr.args {
if let Some(first_arg) = args.get(0) {
if is_async_function(&*first_arg.expr) {
self.context.add_diagnostic(
new_expr.span,
"no-async-promise-executor",
"Async promise executors are not allowed",
);
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
#[test]
fn no_async_promise_executor_valid() {
assert_lint_ok_n::<NoAsyncPromiseExecutor>(vec![
"new Promise(function(resolve, reject) {});",
"new Promise((resolve, reject) => {});",
"new Promise((resolve, reject) => {}, async function unrelated() {})",
"new Foo(async (resolve, reject) => {})",
"new class { foo() { new Promise(function(resolve, reject) {}); } }",
]);
}
#[test]
fn no_async_promise_executor_invalid() {
assert_lint_err::<NoAsyncPromiseExecutor>(
"new Promise(async function(resolve, reject) {});",
0,
);
assert_lint_err::<NoAsyncPromiseExecutor>(
"new Promise(async function foo(resolve, reject) {});",
0,
);
assert_lint_err::<NoAsyncPromiseExecutor>(
"new Promise(async (resolve, reject) => {});",
0,
);
assert_lint_err::<NoAsyncPromiseExecutor>(
"new Promise(((((async () => {})))));",
0,
);
assert_lint_err_on_line::<NoAsyncPromiseExecutor>(
r#"
const a = new class {
foo() {
let b = new Promise(async function(resolve, reject) {});
}
}
"#,
4,
12,
);
}
}