Skip to content

Commit

Permalink
Finished adding implementation of promise/prefer-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
therewillbecode committed Mar 2, 2025
1 parent 827ab6f commit 822a56b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 43 deletions.
58 changes: 15 additions & 43 deletions crates/oxc_linter/src/rules/promise/prefer_catch.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use oxc_ast::{
AstKind,
ast::{CallExpression, Expression},
};
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_span::Span;

use crate::{
AstNode,
context::LintContext,
fixer::{RuleFix, RuleFixer},
rule::Rule,
};
use crate::{AstNode, context::LintContext, rule::Rule};

fn prefer_catch_diagnostic(span: Span) -> OxcDiagnostic {
// See <https://oxc.rs/docs/contribute/linter/adding-rules.html#diagnostics> for details
OxcDiagnostic::warn("Should be an imperative statement about what is wrong")
.with_help("Should be a command-like statement that tells the user how to fix the issue")
OxcDiagnostic::warn("Prefer `catch` to `then(a, b)` or `then(null, b)`")
.with_help(
"Handle promise errors in a `catch` instead of using the second argument of `then`.",
)
.with_label(span)
}

Expand Down Expand Up @@ -59,12 +52,8 @@ declare_oxc_lint!(
/// ```
PreferCatch,
promise,
nursery, // TODO: change category to `correctness`, `suspicious`, `pedantic`, `perf`, `restriction`, or `style`
// See <https://oxc.rs/docs/contribute/linter.html#rule-category> for details

pending // TODO: describe fix capabilities. Remove if no fix can be done,
// keep at 'pending' if you think one could be added but don't know how.
// Options are 'fix', 'fix_dangerous', 'suggestion', and 'conditional_fix_suggestion'
style,
pending
);

impl Rule for PreferCatch {
Expand All @@ -85,25 +74,8 @@ impl Rule for PreferCatch {
.static_property_name()
.map_or_else(|| false, |prop_name| matches!(prop_name, "then"));

if !is_promise_then_call {
println!("_______");
let s = node.span().source_text(ctx.source_text());
println!("NOT -> {s:?}");


// println!("not a promise then: {node:?}");
println!("_______");
}

if is_promise_then_call {
let s = node.span().source_text(ctx.source_text());
println!("IS - > {s:?}");

if call_expr.arguments.len() >= 2 {
println!("{0:?}",call_expr.arguments.len() >= 2);
ctx.diagnostic(prefer_catch_diagnostic(call_expr.span));
}

if is_promise_then_call && call_expr.arguments.len() >= 2 {
ctx.diagnostic(prefer_catch_diagnostic(call_expr.span));
}
}
}
Expand All @@ -130,11 +102,11 @@ fn test() {
"prom.then(undefined, fn2)",
"function foo() { prom.then(x => {}, () => {}) }",
"function foo() {
prom.then(function a() { }, function b() {}).then(fn1, fn2)
}",
prom.then(function a() { }, function b() {}).then(fn1, fn2)
}",
];

/* Todo
/* Pending
let fix = vec![
("prom.then(fn1, fn2)", "prom.catch(fn2).then(fn1)", None),
("prom.then(fn1, (fn2))", "prom.catch(fn2).then(fn1)", None),
Expand All @@ -157,6 +129,6 @@ fn test() {
];
*/
Tester::new(PreferCatch::NAME, PreferCatch::PLUGIN, pass, fail)
// .expect_fix(fix)
// .expect_fix(fix)
.test_and_snapshot();
}
46 changes: 46 additions & 0 deletions crates/oxc_linter/src/snapshots/promise_prefer_catch.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: crates/oxc_linter/src/tester.rs
---
eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:1:1]
1prom.then(fn1, fn2)
· ───────────────────
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:1:1]
1prom.then(fn1, (fn2))
· ─────────────────────
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:1:1]
1prom.then(null, fn2)
· ────────────────────
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:1:1]
1prom.then(undefined, fn2)
· ─────────────────────────
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:1:18]
1function foo() { prom.then(x => {}, () => {}) }
· ────────────────────────────
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

eslint-plugin-promise(prefer-catch): Prefer `catch` to `then(a, b)` or `then(null, b)`
╭─[prefer_catch.tsx:2:9]
1function foo() {
2prom.then(function a() { }, function b() {}).then(fn1, fn2)
· ───────────────────────────────────────────────────────────
3 │ }
╰────
help: Handle promise errors in a `.catch` instead of using the second argument of `then`.

0 comments on commit 822a56b

Please sign in to comment.