-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): support optional-catch-binding plugin
- Loading branch information
Showing
9 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
mod optional_catch_binding; | ||
mod options; | ||
|
||
pub use optional_catch_binding::OptionalCatchBinding; | ||
pub use options::ES2019Options; | ||
use oxc_ast::ast::*; | ||
use oxc_traverse::TraverseCtx; | ||
use std::rc::Rc; | ||
|
||
use crate::context::Ctx; | ||
|
||
#[allow(dead_code)] | ||
pub struct ES2019<'a> { | ||
ctx: Ctx<'a>, | ||
options: ES2019Options, | ||
|
||
// Plugins | ||
optional_catch_binding: OptionalCatchBinding<'a>, | ||
} | ||
|
||
impl<'a> ES2019<'a> { | ||
pub fn new(options: ES2019Options, ctx: Ctx<'a>) -> Self { | ||
Self { optional_catch_binding: OptionalCatchBinding::new(Rc::clone(&ctx)), ctx, options } | ||
} | ||
|
||
pub fn transform_catch_clause( | ||
&mut self, | ||
clause: &mut CatchClause<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if self.options.optional_catch_binding { | ||
self.optional_catch_binding.transform_catch_clause(clause, ctx); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
crates/oxc_transformer/src/es2019/optional_catch_binding.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::cell::Cell; | ||
|
||
use oxc_ast::ast::*; | ||
use oxc_semantic::SymbolFlags; | ||
use oxc_span::SPAN; | ||
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>, | ||
} | ||
|
||
impl<'a> OptionalCatchBinding<'a> { | ||
pub fn new(ctx: Ctx<'a>) -> Self { | ||
Self { _ctx: ctx } | ||
} | ||
|
||
/// 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() { | ||
return; | ||
} | ||
let symbol_id = | ||
ctx.generate_uid("unused", ctx.scoping.current_scope_id(), SymbolFlags::CatchVariable); | ||
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id)); | ||
let binding_identifier = | ||
BindingIdentifier { span: SPAN, symbol_id: Cell::new(Some(symbol_id)), name }; | ||
let binding_pattern_kind = | ||
ctx.ast.binding_pattern_kind_from_binding_identifier(binding_identifier); | ||
let binding_pattern = | ||
ctx.ast.binding_pattern(binding_pattern_kind, None::<TSTypeAnnotation<'a>>, false); | ||
let param = ctx.ast.catch_parameter(SPAN, binding_pattern); | ||
clause.param = Some(param); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Default, Clone, Deserialize)] | ||
#[serde(default, rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct ES2019Options { | ||
#[serde(skip)] | ||
pub optional_catch_binding: bool, | ||
} | ||
|
||
impl ES2019Options { | ||
#[must_use] | ||
pub fn with_optional_catch_binding(mut self, enable: bool) -> Self { | ||
self.optional_catch_binding = enable; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters