Skip to content

Commit

Permalink
chore: follow the suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Oct 16, 2024
1 parent a4e7332 commit 67d39f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
60 changes: 27 additions & 33 deletions crates/oxc_linter/src/rules/promise/no_callback_in_promise.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{
ast::{Expression, MemberExpression},
ast::{CallExpression, Expression, MemberExpression},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand All @@ -17,11 +17,17 @@ fn no_callback_in_promise_diagnostic(span: Span) -> OxcDiagnostic {
#[derive(Debug, Default, Clone)]
pub struct NoCallbackInPromise(Box<NoCallbackInPromiseConfig>);

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
pub struct NoCallbackInPromiseConfig {
callbacks: Vec<CompactStr>,
}

impl Default for NoCallbackInPromiseConfig {
fn default() -> Self {
Self { callbacks: vec!["callback".into(), "cb".into(), "done".into(), "next".into()] }
}
}

impl std::ops::Deref for NoCallbackInPromise {
type Target = NoCallbackInPromiseConfig;

Expand Down Expand Up @@ -69,8 +75,7 @@ declare_oxc_lint!(

impl Rule for NoCallbackInPromise {
fn from_configuration(value: serde_json::Value) -> Self {
let default_callbacks: Vec<CompactStr> =
["done", "cb", "callback", "next"].map(Into::into).to_vec();
let mut default_config = NoCallbackInPromiseConfig::default();

let exceptions: Vec<String> = value
.get(0)
Expand All @@ -81,29 +86,31 @@ impl Rule for NoCallbackInPromise {
})
.unwrap_or_default();

Self(Box::new(NoCallbackInPromiseConfig {
callbacks: default_callbacks
.into_iter()
.filter(|item| !exceptions.contains(&item.to_string()))
.collect(),
}))
default_config.callbacks.retain(|item| !exceptions.contains(&item.to_string()));

Self(Box::new(default_config))
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if !self.is_callback(node) {
if Self::has_promise_callback(node) {
let Some(call_expr) = node.kind().as_call_expression() else {
return;
};
let Some(call_expr) = node.kind().as_call_expression() else {
return;
};

let is_not_callback = call_expr
.callee
.get_identifier_reference()
.map_or(true, |id| self.callbacks.binary_search(&id.name.as_str().into()).is_err());

if is_not_callback {
if Self::has_promise_callback(call_expr) {
let Some(id) = call_expr.arguments.first().and_then(|arg| {
arg.as_expression().and_then(Expression::get_identifier_reference)
}) else {
return;
};

let name = id.name.as_str();
if self.callbacks.contains(&name.into()) {
if self.callbacks.binary_search(&name.into()).is_ok() {
ctx.diagnostic(no_callback_in_promise_diagnostic(id.span));
}
}
Expand All @@ -126,14 +133,12 @@ impl NoCallbackInPromise {
return false;
}

ctx.nodes().iter_parents(node.id()).nth(2).is_some_and(Self::has_promise_callback)
ctx.nodes().iter_parents(node.id()).nth(2).is_some_and(|node| {
node.kind().as_call_expression().is_some_and(Self::has_promise_callback)
})
}

fn has_promise_callback(node: &AstNode) -> bool {
let AstKind::CallExpression(call_expr) = node.kind() else {
return false;
};

fn has_promise_callback(call_expr: &CallExpression) -> bool {
matches!(
call_expr
.callee
Expand All @@ -142,17 +147,6 @@ impl NoCallbackInPromise {
Some("then" | "catch")
)
}

fn is_callback(&self, node: &AstNode) -> bool {
let AstKind::CallExpression(call_expr) = node.kind() else {
return false;
};

call_expr
.callee
.get_identifier_reference()
.is_some_and(|id| self.callbacks.contains(&id.name.as_str().into()))
}
}

#[test]
Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_linter/src/snapshots/no_callback_in_promise.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,81 @@ source: crates/oxc_linter/src/tester.rs
1a.then(cb)
· ──
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:14]
1a.then(() => cb())
· ────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:24]
1a.then(function(err) { cb(err) })
· ───────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:25]
1a.then(function(data) { cb(data) }, function(err) { cb(err) })
· ────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:53]
1a.then(function(data) { cb(data) }, function(err) { cb(err) })
· ───────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:25]
1a.catch(function(err) { cb(err) })
· ───────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:8]
1a.then(callback)
· ────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:14]
1a.then(() => callback())
· ──────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:24]
1a.then(function(err) { callback(err) })
· ─────────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:25]
1a.then(function(data) { callback(data) }, function(err) { callback(err) })
· ──────────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:59]
1a.then(function(data) { callback(data) }, function(err) { callback(err) })
· ─────────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

eslint-plugin-promise(no-callback-in-promise): Avoid calling back inside of a promise
╭─[no_callback_in_promise.tsx:1:25]
1a.catch(function(err) { callback(err) })
· ─────────────
╰────
help: Use `then` and `catch` directly, avoid callbacks
help: Use `then` and `catch` directly

0 comments on commit 67d39f0

Please sign in to comment.