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

fix(transformer): strip TS statements from for in/of statement bodies #4686

Merged
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
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"