Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl<'a> Transformer<'a> {
let mut transformer = TransformerImpl {
common: Common::new(&self.env, &self.ctx),
decorator: Decorator::new(self.decorator, &self.ctx),
explicit_resource_management: ExplicitResourceManagement::new(&self.ctx),
explicit_resource_management: ExplicitResourceManagement::new(
&self.ctx,
self.env.es2017.async_to_generator,
),
x0_typescript: program
.source_type
.is_typescript()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use crate::{Helper, TransformCtx};
pub struct ExplicitResourceManagement<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,

async_to_generator: bool,

top_level_using: FxHashMap<Address, /* is await-using */ bool>,

/// keeps track of whether the current static block contains a `using` declaration
Expand All @@ -59,15 +61,21 @@ pub struct ExplicitResourceManagement<'a, 'ctx> {
/// keeps track of whether the current switch statement contains a `using` declaration
/// so that we can transform it in `exit_statement`
switch_stmt_stack: NonEmptyStack<bool>,

/// keeps track of whether the current block statement contains a `using` declaration
/// so that we can transform it in `exit_statement`
block_stmt_stack: NonEmptyStack<bool>,
}

impl<'a, 'ctx> ExplicitResourceManagement<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
pub fn new(ctx: &'ctx TransformCtx<'a>, async_to_generator: bool) -> Self {
Self {
ctx,
async_to_generator,
top_level_using: FxHashMap::default(),
static_blocks_stack: NonEmptyStack::new(false),
switch_stmt_stack: NonEmptyStack::new(false),
block_stmt_stack: NonEmptyStack::new(false),
}
}
}
Expand Down Expand Up @@ -178,6 +186,7 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
ctx: &mut TraverseCtx<'a>,
) {
if matches!(node.kind, VariableDeclarationKind::Using | VariableDeclarationKind::AwaitUsing)
|| self.top_level_using.contains_key(&Address::from_ptr(node))
{
match ctx.parent() {
Ancestor::StaticBlockBody(_) => {
Expand All @@ -186,6 +195,9 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
Ancestor::SwitchCaseConsequent(_) => {
*self.switch_stmt_stack.last_mut() = true;
}
Ancestor::BlockStatementBody(_) => {
*self.block_stmt_stack.last_mut() = true;
}
_ => {}
}
}
Expand Down Expand Up @@ -232,22 +244,32 @@ impl<'a> Traverse<'a> for ExplicitResourceManagement<'a, '_> {
// or `SwitchStatement`s. We want the common path for "nothing to do here" not to incur the cost of
// a function call.
#[inline]
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, _ctx: &mut TraverseCtx<'a>) {
match stmt {
// TODO: move this to exit_statement
Statement::BlockStatement(_) => self.transform_block_statement(stmt, ctx),
Statement::BlockStatement(_) => {
self.block_stmt_stack.push(false);
}
Statement::SwitchStatement(_) => {
self.switch_stmt_stack.push(false);
}
_ => {}
}
}

#[inline]
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
if let Statement::SwitchStatement(_) = stmt {
if self.switch_stmt_stack.pop() {
self.transform_switch_statement(stmt, ctx);
match stmt {
Statement::BlockStatement(_) => {
if self.block_stmt_stack.pop() {
self.transform_block_statement(stmt, ctx);
}
}
Statement::SwitchStatement(_) => {
if self.switch_stmt_stack.pop() {
self.transform_switch_statement(stmt, ctx);
}
}
_ => {}
}
}

Expand Down Expand Up @@ -653,7 +675,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
};

let catch = Self::create_catch_clause(&using_ctx, ctx.current_scope_id(), ctx);
let finally = Self::create_finally_block(&using_ctx, current_scope_id, needs_await, ctx);
let finally = self.create_finally_block(&using_ctx, current_scope_id, needs_await, ctx);
*stmt = ctx.ast.statement_try(SPAN, block, Some(catch), Some(finally));
}

Expand Down Expand Up @@ -775,7 +797,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
}

let catch = Self::create_catch_clause(&using_ctx, parent_scope_id, ctx);
let finally = Self::create_finally_block(&using_ctx, parent_scope_id, needs_await, ctx);
let finally = self.create_finally_block(&using_ctx, parent_scope_id, needs_await, ctx);

Some(ctx.ast.statement_try(SPAN, block, Some(catch), Some(finally)))
}
Expand Down Expand Up @@ -829,6 +851,7 @@ impl<'a> ExplicitResourceManagement<'a, '_> {

/// `{ _usingCtx.d(); }`
fn create_finally_block(
&self,
using_ctx: &BoundIdentifier<'a>,
parent_scope_id: ScopeId,
needs_await: bool,
Expand All @@ -850,7 +873,15 @@ impl<'a> ExplicitResourceManagement<'a, '_> {
false,
);

let stmt = if needs_await { ctx.ast.expression_await(SPAN, expr) } else { expr };
let stmt = if needs_await {
if self.async_to_generator {
ctx.ast.expression_yield(SPAN, false, Some(expr))
} else {
ctx.ast.expression_await(SPAN, expr)
}
} else {
expr
};

ctx.ast.alloc_block_statement_with_scope_id(
SPAN,
Expand Down
14 changes: 7 additions & 7 deletions tasks/coverage/snapshots/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41524,24 +41524,24 @@ rebuilt : ScopeId(9): []

tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarations.1.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["C1", "C2", "C3", "N", "_af", "_ag", "_asyncToGenerator", "_awaitAsyncGenerator", "_defineProperty", "_usingCtx2", "_usingCtx20", "_usingCtx23", "_usingCtx24", "_usingCtx25", "_usingCtx26", "_usingCtx27", "_usingCtx28", "_usingCtx29", "_usingCtx6", "_wrapAsyncGenerator", "a", "af", "ag", "d1", "d19", "d20", "d21", "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31", "d32", "f", "g"]
rebuilt : ScopeId(0): ["C1", "C2", "C3", "N", "_af", "_ag", "_asyncToGenerator", "_awaitAsyncGenerator", "_defineProperty", "_usingCtx2", "_usingCtx20", "_usingCtx21", "_usingCtx22", "_usingCtx23", "_usingCtx24", "_usingCtx25", "_usingCtx26", "_usingCtx27", "_usingCtx28", "_usingCtx29", "_usingCtx6", "_wrapAsyncGenerator", "a", "af", "ag", "d1", "d19", "d20", "d21", "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31", "d32", "f", "g"]
after transform: ScopeId(0): ["C1", "C2", "C3", "N", "_af", "_ag", "_asyncToGenerator", "_awaitAsyncGenerator", "_defineProperty", "_usingCtx19", "_usingCtx2", "_usingCtx22", "_usingCtx23", "_usingCtx24", "_usingCtx25", "_usingCtx26", "_usingCtx27", "_usingCtx28", "_usingCtx29", "_wrapAsyncGenerator", "a", "af", "ag", "d1", "d19", "d20", "d21", "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31", "d32", "f", "g"]
rebuilt : ScopeId(0): ["C1", "C2", "C3", "N", "_af", "_ag", "_asyncToGenerator", "_awaitAsyncGenerator", "_defineProperty", "_usingCtx19", "_usingCtx2", "_usingCtx20", "_usingCtx21", "_usingCtx22", "_usingCtx23", "_usingCtx24", "_usingCtx25", "_usingCtx26", "_usingCtx27", "_usingCtx28", "_usingCtx29", "_wrapAsyncGenerator", "a", "af", "ag", "d1", "d19", "d20", "d21", "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31", "d32", "f", "g"]
Bindings mismatch:
after transform: ScopeId(70): ["_usingCtx21", "_usingCtx22"]
after transform: ScopeId(70): ["_usingCtx20", "_usingCtx21"]
rebuilt : ScopeId(29): []
Symbol span mismatch for "N":
after transform: SymbolId(26): Span { start: 1385, end: 1386 }
rebuilt : SymbolId(66): Span { start: 0, end: 0 }
Symbol scope ID mismatch for "_usingCtx20":
after transform: SymbolId(88): ScopeId(70)
rebuilt : SymbolId(74): ScopeId(0)
Symbol scope ID mismatch for "_usingCtx21":
after transform: SymbolId(90): ScopeId(70)
rebuilt : SymbolId(74): ScopeId(0)
Symbol scope ID mismatch for "_usingCtx22":
after transform: SymbolId(92): ScopeId(70)
rebuilt : SymbolId(78): ScopeId(0)

tasks/coverage/typescript/tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsDeclarationEmit.2.ts
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(2), ScopeId(4), ScopeId(5), ScopeId(6), ScopeId(8)]
after transform: ScopeId(0): [ScopeId(2), ScopeId(4), ScopeId(5), ScopeId(7), ScopeId(9)]
rebuilt : ScopeId(0): [ScopeId(1), ScopeId(5), ScopeId(7)]
Symbol reference IDs mismatch for "r1":
after transform: SymbolId(0): [ReferenceId(1)]
Expand Down
Copy link
Contributor Author

@camc314 camc314 Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, for this our output is the same, but:

  • our _usingCtx3 is babel's _usingCtx
  • our _usingCtx is babel's _usingCtx3

as we transform on exit, not on enter

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
try {
var _usingCtx3 = babelHelpers.usingCtx();
const x = _usingCtx3.u(obj);
try {
var _usingCtx2 = babelHelpers.usingCtx();
const y = _usingCtx2.u(
call(() => {
try {
var _usingCtx = babelHelpers.usingCtx();
const z = _usingCtx.u(obj);
return z;
} catch (_) {
_usingCtx.e = _;
} finally {
_usingCtx.d();
}
})
);
stmt;
} catch (_) {
_usingCtx2.e = _;
} finally {
_usingCtx2.d();
}
stmt;
} catch (_) {
_usingCtx3.e = _;
} finally {
_usingCtx3.d();
}
Loading