Skip to content
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

docs(transformer): add documentation for optional-catch-binding plugin #5064

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions crates/oxc_transformer/src/es2019/optional_catch_binding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
//! ES2019: Optional Catch Binding
//!
//! This plugin transform catch clause without parameter to add a parameter called `unused` in catch clause.
//!
//! > This plugin is included in `preset-env`, in ES2019
//!
//! ## Example
//!
//! Input:
//! ```js
//! try {
//! throw 0;
//! } catch {
//! doSomethingWhichDoesNotCareAboutTheValueThrown();
//! }
//! ```
//!
//! Output:
//! ```js
//! try {
//! throw 0;
//! } catch (_unused) {
//! doSomethingWhichDoesNotCareAboutTheValueThrown();
//! }
//! ```
//!
//! ## Implementation
//!
//! Implementation based on [@babel/plugin-transform-optional-catch-binding](https://babel.dev/docs/babel-plugin-transform-optional-catch-binding).
//!
//! ## References:
//! * Babel plugin implementation: <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-optional-catch-binding>
//! * Optional catch binding TC39 proposal: <https://github.com/tc39/proposal-optional-catch-binding>

use std::cell::Cell;

use oxc_ast::ast::*;
Expand All @@ -7,11 +41,6 @@ use oxc_traverse::TraverseCtx;

use crate::context::Ctx;

/// ES2019: Optional Catch Binding
///
/// References:
/// * <https://babel.dev/docs/babel-plugin-transform-optional-catch-binding>
/// * <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-optional-catch-binding>
pub struct OptionalCatchBinding<'a> {
_ctx: Ctx<'a>,
}
Expand All @@ -22,16 +51,6 @@ impl<'a> OptionalCatchBinding<'a> {
}

/// If CatchClause has no param, add a parameter called `unused`.
///
/// ```ts
/// try {}
/// catch {}
/// ```
/// too
/// ```ts
/// try {}
/// catch (_unused) {}
/// ```
#[allow(clippy::unused_self)]
pub fn transform_catch_clause(&self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
if clause.param.is_some() {
Expand Down