Skip to content

Commit

Permalink
fix(transformer): strip TS statements from for in/of statement bodies (
Browse files Browse the repository at this point in the history
…#4686)

Remove TS statements in body of `for of` and `for in` statements, as well as plain `for`.
  • Loading branch information
overlookmotel committed Aug 6, 2024
1 parent 54f9897 commit 4797eaa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
8 changes: 8 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x0_typescript.transform_for_statement(stmt, ctx);
}

fn enter_for_of_statement(&mut self, stmt: &mut ForOfStatement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.transform_for_of_statement(stmt, ctx);
}

fn enter_for_in_statement(&mut self, stmt: &mut ForInStatement<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.transform_for_in_statement(stmt, ctx);
}

fn enter_ts_export_assignment(
&mut self,
export_assignment: &mut TSExportAssignment<'a>,
Expand Down
40 changes: 38 additions & 2 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,35 @@ impl<'a> TypeScriptAnnotations<'a> {
stmt: &mut ForStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let scope_id = stmt.scope_id.get().unwrap_or(ctx.current_scope_id());
Self::replace_with_empty_block_if_ts(&mut stmt.body, scope_id, ctx);
Self::replace_for_statement_body_with_empty_block_if_ts(
&mut stmt.body,
&stmt.scope_id,
ctx,
);
}

pub fn transform_for_in_statement(
&mut self,
stmt: &mut ForInStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
Self::replace_for_statement_body_with_empty_block_if_ts(
&mut stmt.body,
&stmt.scope_id,
ctx,
);
}

pub fn transform_for_of_statement(
&mut self,
stmt: &mut ForOfStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
Self::replace_for_statement_body_with_empty_block_if_ts(
&mut stmt.body,
&stmt.scope_id,
ctx,
);
}

pub fn transform_while_statement(
Expand All @@ -465,6 +492,15 @@ impl<'a> TypeScriptAnnotations<'a> {
Self::replace_with_empty_block_if_ts(&mut stmt.body, ctx.current_scope_id(), ctx);
}

fn replace_for_statement_body_with_empty_block_if_ts(
body: &mut Statement<'a>,
scope_id: &Cell<Option<ScopeId>>,
ctx: &mut TraverseCtx<'a>,
) {
let scope_id = scope_id.get().unwrap_or(ctx.current_scope_id());
Self::replace_with_empty_block_if_ts(body, scope_id, ctx);
}

fn replace_with_empty_block_if_ts(
stmt: &mut Statement<'a>,
parent_scope_id: ScopeId,
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ impl<'a> TypeScript<'a> {
self.annotations.transform_for_statement(stmt, ctx);
}

pub fn transform_for_in_statement(
&mut self,
stmt: &mut ForInStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.annotations.transform_for_in_statement(stmt, ctx);
}

pub fn transform_for_of_statement(
&mut self,
stmt: &mut ForOfStatement<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.annotations.transform_for_of_statement(stmt, ctx);
}

pub fn transform_tagged_template_expression(
&mut self,
expr: &mut TaggedTemplateExpression<'a>,
Expand Down
3 changes: 1 addition & 2 deletions tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ commit: d8086f14

transformer_typescript Summary:
AST Parsed : 6456/6456 (100.00%)
Positive Passed: 6452/6456 (99.94%)
Positive Passed: 6453/6456 (99.95%)
Mismatch: "compiler/constEnumNamespaceReferenceCausesNoImport2.ts"
Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts"
Mismatch: "conformance/externalModules/typeOnly/exportDeclaration.ts"
Mismatch: "conformance/jsx/inline/inlineJsxAndJsxFragPragmaOverridesCompilerOptions.tsx"

0 comments on commit 4797eaa

Please sign in to comment.