Skip to content

Commit

Permalink
feat(linter): add oxc/no-async-endpoint-handlers (#5364)
Browse files Browse the repository at this point in the history
Adds `no-async-endpoint-handlers` rules, which bans async functions used as endpoint handlers in Express applications. These do not get caught by Express' error handler, causing the server to crash with an unhandled process rejection error.

```js
app.use(async (req, res) => {
  const foo = await api.getFoo(req.query) // server panics if this function rejects
  return res.json(foo)
})
```

I could not find this rule implemented in any ESLint plugin, but this is a problem I see quite often and I'm tired of dealing with it. I've added it to `oxc` for now, but we should consider adding an `express` or `api` plugin in the future.
  • Loading branch information
DonIsaac committed Aug 31, 2024
1 parent add1465 commit f81e8a1
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 7 deletions.
12 changes: 11 additions & 1 deletion crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{borrow::Cow, cell::Cell, fmt, hash::Hash};

use oxc_allocator::{Box, FromIn, Vec};
use oxc_span::{Atom, GetSpan, SourceType, Span};
use oxc_syntax::{operator::UnaryOperator, reference::ReferenceId, scope::ScopeFlags};
use oxc_syntax::{
operator::UnaryOperator, reference::ReferenceId, scope::ScopeFlags, symbol::SymbolId,
};

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -1056,6 +1058,14 @@ impl<'a> Function<'a> {
self.id.as_ref().map(|id| id.name.clone())
}

/// Get the [`SymbolId`] this [`Function`] is bound to.
///
/// Returns [`None`] for anonymous functions, or if semantic analysis was skipped.
#[inline]
pub fn symbol_id(&self) -> Option<SymbolId> {
self.id.as_ref().and_then(|id| id.symbol_id.get())
}

pub fn is_typescript_syntax(&self) -> bool {
matches!(
self.r#type,
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ mod oxc {
pub mod missing_throw;
pub mod no_accumulating_spread;
pub mod no_async_await;
pub mod no_async_endpoint_handlers;
pub mod no_barrel_file;
pub mod no_const_enum;
pub mod no_optional_chaining;
Expand Down Expand Up @@ -834,6 +835,7 @@ oxc_macros::declare_all_lint_rules! {
oxc::number_arg_out_of_range,
oxc::only_used_in_recursion,
oxc::no_async_await,
oxc::no_async_endpoint_handlers,
oxc::uninvoked_array_callback,
nextjs::google_font_display,
nextjs::google_font_preconnect,
Expand Down
Loading

0 comments on commit f81e8a1

Please sign in to comment.