Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer): optional-catch-binding unused variable side effect (#…
…2822) Before this PR for this given case: ```javascript const _unused = "It's a lie, They gonna use me:("; try { throw 0; } catch { console.log(_unused); } ``` We would've generated this: ```javascript const _unused = "It's a lie, They gonna use me:("; try { throw 0; } catch (_unused) { console.log(_unused); } ``` This is incorrect, This PR aims to use the `CreateVars` trait in order to ensure the variable uniqueness. Now it would output this: ```javascript const _unused = "It's a lie, They gonna use me:("; try { throw 0; } catch (_unused2) { console.log(_unused); } ```
- Loading branch information
Showing
4 changed files
with
50 additions
and
27 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
29 changes: 20 additions & 9 deletions
29
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 |
---|---|---|
@@ -1,32 +1,43 @@ | ||
use std::rc::Rc; | ||
|
||
use oxc_ast::{ast::*, AstBuilder}; | ||
use oxc_allocator::Vec; | ||
use oxc_ast::ast::*; | ||
use oxc_span::SPAN; | ||
|
||
use crate::{context::TransformerCtx, options::TransformTarget}; | ||
use crate::{context::TransformerCtx, options::TransformTarget, utils::CreateVars}; | ||
|
||
/// 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> { | ||
ast: Rc<AstBuilder<'a>>, | ||
ctx: TransformerCtx<'a>, | ||
vars: Vec<'a, VariableDeclarator<'a>>, | ||
} | ||
|
||
impl<'a> CreateVars<'a> for OptionalCatchBinding<'a> { | ||
fn ctx(&self) -> &TransformerCtx<'a> { | ||
&self.ctx | ||
} | ||
|
||
fn vars_mut(&mut self) -> &mut Vec<'a, VariableDeclarator<'a>> { | ||
&mut self.vars | ||
} | ||
} | ||
|
||
impl<'a> OptionalCatchBinding<'a> { | ||
pub fn new(ctx: TransformerCtx<'a>) -> Option<Self> { | ||
(ctx.options.target < TransformTarget::ES2019 || ctx.options.optional_catch_binding) | ||
.then_some(Self { ast: ctx.ast }) | ||
.then_some(Self { vars: ctx.ast.new_vec(), ctx }) | ||
} | ||
|
||
pub fn transform_catch_clause<'b>(&mut self, clause: &'b mut CatchClause<'a>) { | ||
if clause.param.is_some() { | ||
return; | ||
} | ||
let binding_identifier = BindingIdentifier::new(SPAN, "_unused".into()); | ||
let binding_pattern_kind = self.ast.binding_pattern_identifier(binding_identifier); | ||
let binding_pattern = self.ast.binding_pattern(binding_pattern_kind, None, false); | ||
let unused = self.create_new_named_var("unused"); | ||
let binding_identifier = BindingIdentifier::new(SPAN, unused.name); | ||
let binding_pattern_kind = self.ctx.ast.binding_pattern_identifier(binding_identifier); | ||
let binding_pattern = self.ctx.ast.binding_pattern(binding_pattern_kind, None, false); | ||
clause.param = Some(binding_pattern); | ||
} | ||
} |
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