-
-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement most of the Eslint plugin promise #4252
Changes from all commits
aaecb33
f7b004f
0d4d3a4
38883c0
9d0ba51
271bbec
8245726
65cf0ed
86c8dfa
ffa143c
13510f3
1ffd302
c01a85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use oxc_ast::{ast::Expression, AstKind}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
fn avoid_new_promise_diagnostic(span0: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("eslint-plugin-promise(avoid-new): Avoid creating new promises") | ||
.with_label(span0) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct AvoidNew; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallow creating new promises outside of utility libs. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// If you dislike the new promise style promises. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// new Promise((resolve, reject) => { ... }); | ||
/// ``` | ||
AvoidNew, | ||
style, | ||
); | ||
|
||
impl Rule for AvoidNew { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::NewExpression(expr) = node.kind() else { | ||
return; | ||
}; | ||
|
||
let Expression::Identifier(ident) = &expr.callee else { | ||
return; | ||
}; | ||
|
||
if ident.name == "Promise" && ctx.semantic().is_reference_to_global_variable(ident) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering what is common in oxlint here, there is:
Which would also run for:
So that seems like it should be refactored? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe we should get |
||
ctx.diagnostic(avoid_new_promise_diagnostic(expr.span)); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
"Promise.resolve()", | ||
"Promise.reject()", | ||
"Promise.all()", | ||
"new Horse()", | ||
"new PromiseLikeThing()", | ||
"new Promise.resolve()", | ||
]; | ||
|
||
let fail = vec![ | ||
"var x = new Promise(function (x, y) {})", | ||
"new Promise()", | ||
"Thing(new Promise(() => {}))", | ||
]; | ||
|
||
Tester::new(AvoidNew::NAME, pass, fail).test_and_snapshot(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to utils::promise instead.