Skip to content

Commit 1f8968a

Browse files
authored
feat(linter): Add eslint-plugin-promise rules: avoid-new, no-new-statics, params-names (#4293)
This introduces the `eslint-plugin-promise` plugin and implements three relatively simple rules. Split off from #4252
1 parent fc0b17d commit 1f8968a

15 files changed

Lines changed: 512 additions & 1 deletion

File tree

apps/oxlint/src/command/lint.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ pub struct EnablePlugins {
223223
/// Enable the React performance plugin and detect rendering performance problems
224224
#[bpaf(switch, hide_usage)]
225225
pub react_perf_plugin: bool,
226+
227+
/// Enable the promise plugin and detect promise usage problems
228+
#[bpaf(switch, hide_usage)]
229+
pub promise_plugin: bool,
226230
}
227231

228232
#[cfg(test)]

apps/oxlint/src/lint/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl Runner for LintRunner {
104104
.with_vitest_plugin(enable_plugins.vitest_plugin)
105105
.with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin)
106106
.with_nextjs_plugin(enable_plugins.nextjs_plugin)
107-
.with_react_perf_plugin(enable_plugins.react_perf_plugin);
107+
.with_react_perf_plugin(enable_plugins.react_perf_plugin)
108+
.with_promise_plugin(enable_plugins.promise_plugin);
108109

109110
let linter = match Linter::from_options(lint_options) {
110111
Ok(lint_service) => lint_service,

crates/oxc_linter/src/options.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct LintOptions {
2929
pub jsx_a11y_plugin: bool,
3030
pub nextjs_plugin: bool,
3131
pub react_perf_plugin: bool,
32+
pub promise_plugin: bool,
3233
}
3334

3435
impl Default for LintOptions {
@@ -48,6 +49,7 @@ impl Default for LintOptions {
4849
jsx_a11y_plugin: false,
4950
nextjs_plugin: false,
5051
react_perf_plugin: false,
52+
promise_plugin: false,
5153
}
5254
}
5355
}
@@ -138,6 +140,12 @@ impl LintOptions {
138140
self.react_perf_plugin = yes;
139141
self
140142
}
143+
144+
#[must_use]
145+
pub fn with_promise_plugin(mut self, yes: bool) -> Self {
146+
self.promise_plugin = yes;
147+
self
148+
}
141149
}
142150

143151
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -342,6 +350,7 @@ impl LintOptions {
342350
"react_perf" => self.react_perf_plugin,
343351
"oxc" => self.oxc_plugin,
344352
"eslint" | "tree_shaking" => true,
353+
"promise" => self.promise_plugin,
345354
name => panic!("Unhandled plugin: {name}"),
346355
})
347356
.cloned()

crates/oxc_linter/src/rules.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ mod tree_shaking {
432432
pub mod no_side_effects_in_initialization;
433433
}
434434

435+
mod promise {
436+
pub mod avoid_new;
437+
pub mod no_new_statics;
438+
pub mod param_names;
439+
}
440+
435441
oxc_macros::declare_all_lint_rules! {
436442
eslint::array_callback_return,
437443
eslint::constructor_super,
@@ -822,4 +828,7 @@ oxc_macros::declare_all_lint_rules! {
822828
jsdoc::require_returns_type,
823829
jsdoc::require_yields,
824830
tree_shaking::no_side_effects_in_initialization,
831+
promise::avoid_new,
832+
promise::no_new_statics,
833+
promise::param_names,
825834
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use oxc_ast::{ast::Expression, AstKind};
2+
use oxc_diagnostics::OxcDiagnostic;
3+
use oxc_macros::declare_oxc_lint;
4+
use oxc_span::Span;
5+
6+
use crate::{context::LintContext, rule::Rule, AstNode};
7+
8+
fn avoid_new_promise_diagnostic(span0: Span) -> OxcDiagnostic {
9+
OxcDiagnostic::warn("eslint-plugin-promise(avoid-new): Avoid creating new promises")
10+
.with_label(span0)
11+
}
12+
13+
#[derive(Debug, Default, Clone)]
14+
pub struct AvoidNew;
15+
16+
declare_oxc_lint!(
17+
/// ### What it does
18+
///
19+
/// Disallow creating new promises outside of utility libs.
20+
///
21+
/// ### Why is this bad?
22+
///
23+
/// If you dislike the new promise style promises.
24+
///
25+
/// ### Example
26+
/// ```javascript
27+
/// new Promise((resolve, reject) => { ... });
28+
/// ```
29+
AvoidNew,
30+
restriction,
31+
);
32+
33+
impl Rule for AvoidNew {
34+
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
35+
let AstKind::NewExpression(expr) = node.kind() else {
36+
return;
37+
};
38+
39+
let Expression::Identifier(ident) = &expr.callee else {
40+
return;
41+
};
42+
43+
if ident.name == "Promise" && ctx.semantic().is_reference_to_global_variable(ident) {
44+
ctx.diagnostic(avoid_new_promise_diagnostic(expr.span));
45+
}
46+
}
47+
}
48+
49+
#[test]
50+
fn test() {
51+
use crate::tester::Tester;
52+
53+
let pass = vec![
54+
"Promise.resolve()",
55+
"Promise.reject()",
56+
"Promise.all()",
57+
"new Horse()",
58+
"new PromiseLikeThing()",
59+
"new Promise.resolve()",
60+
];
61+
62+
let fail = vec![
63+
"var x = new Promise(function (x, y) {})",
64+
"new Promise()",
65+
"Thing(new Promise(() => {}))",
66+
];
67+
68+
Tester::new(AvoidNew::NAME, pass, fail).test_and_snapshot();
69+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use oxc_ast::{ast::Expression, AstKind};
2+
use oxc_diagnostics::OxcDiagnostic;
3+
use oxc_macros::declare_oxc_lint;
4+
use oxc_span::Span;
5+
6+
use crate::{context::LintContext, rule::Rule, AstNode};
7+
8+
fn static_promise_diagnostic(x0: &str, span0: Span) -> OxcDiagnostic {
9+
OxcDiagnostic::warn(format!(
10+
"eslint-plugin-promise(no-new-statics): Disallow calling `new` on a `Promise.{x0}`"
11+
))
12+
.with_label(span0)
13+
}
14+
15+
#[derive(Debug, Default, Clone)]
16+
pub struct NoNewStatics;
17+
18+
declare_oxc_lint!(
19+
/// ### What it does
20+
///
21+
/// Disallow calling new on a Promise static method.
22+
///
23+
/// ### Why is this bad?
24+
///
25+
/// Calling a Promise static method with new is invalid, resulting in a TypeError at runtime.
26+
///
27+
/// ### Example
28+
/// ```javascript
29+
/// new Promise.resolve(value);
30+
/// ```
31+
NoNewStatics,
32+
correctness
33+
);
34+
35+
impl Rule for NoNewStatics {
36+
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
37+
let AstKind::NewExpression(new_expr) = node.kind() else {
38+
return;
39+
};
40+
41+
let Some(member_expr) = &new_expr.callee.get_member_expr() else {
42+
return;
43+
};
44+
45+
let Expression::Identifier(ident) = &member_expr.object() else {
46+
return;
47+
};
48+
49+
if ident.name != "Promise" || !ctx.semantic().is_reference_to_global_variable(ident) {
50+
return;
51+
}
52+
53+
let Some(prop_name) = member_expr.static_property_name() else {
54+
return;
55+
};
56+
57+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
58+
if matches!(
59+
prop_name,
60+
"resolve" | "reject" | "all" | "allSettled" | "race" | "any" | "withResolvers"
61+
) {
62+
ctx.diagnostic_with_fix(
63+
static_promise_diagnostic(
64+
prop_name,
65+
Span::new(new_expr.span.start, ident.span.start - 1),
66+
),
67+
|fixer| fixer.delete_range(Span::new(new_expr.span.start, ident.span.start)),
68+
);
69+
}
70+
}
71+
}
72+
73+
#[test]
74+
fn test() {
75+
use crate::tester::Tester;
76+
77+
let pass = vec![
78+
"Promise.resolve()",
79+
"Promise.reject()",
80+
"Promise.all()",
81+
"Promise.race()",
82+
"new Promise(function (resolve, reject) {})",
83+
"new SomeClass()",
84+
"SomeClass.resolve()",
85+
"new SomeClass.resolve()",
86+
];
87+
88+
let fail = vec![
89+
"new Promise.resolve()",
90+
"new Promise.reject()",
91+
"new Promise.all()",
92+
"new Promise.allSettled()",
93+
"new Promise.any()",
94+
"new Promise.race()",
95+
"function foo() {
96+
var a = getA()
97+
return new Promise.resolve(a)
98+
}",
99+
];
100+
101+
let fix = vec![
102+
("new Promise.resolve()", "Promise.resolve()", None),
103+
("new Promise.reject()", "Promise.reject()", None),
104+
("new Promise.all()", "Promise.all()", None),
105+
("new Promise.allSettled()", "Promise.allSettled()", None),
106+
("new Promise.any()", "Promise.any()", None),
107+
("new Promise.race()", "Promise.race()", None),
108+
];
109+
Tester::new(NoNewStatics::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
110+
}

0 commit comments

Comments
 (0)