diff --git a/crates/oxc_transformer/src/typescript/namespace.rs b/crates/oxc_transformer/src/typescript/namespace.rs index 87d2f3ead..4f5a70f9f 100644 --- a/crates/oxc_transformer/src/typescript/namespace.rs +++ b/crates/oxc_transformer/src/typescript/namespace.rs @@ -280,15 +280,34 @@ impl<'a> TypeScript<'a> { &self, arg_name: Atom<'a>, real_name: Atom<'a>, - stmts: Vec<'a, Statement<'a>>, + mut stmts: Vec<'a, Statement<'a>>, parent_export: Option>, scope_id: ScopeId, ctx: &mut TraverseCtx, ) -> Statement<'a> { + let mut directives = self.ctx.ast.new_vec(); + + // Check if the namespace has a `use strict` directive + if stmts.first().is_some_and(|stmt| { + matches!(stmt, Statement::ExpressionStatement(es) if + matches!(&es.expression, Expression::StringLiteral(literal) if + literal.value == "use strict") + ) + }) { + stmts.remove(0); + let directive = self.ctx.ast.new_atom("use strict"); + let directive = Directive { + span: SPAN, + expression: StringLiteral::new(SPAN, directive.clone()), + directive, + }; + directives.push(directive); + } + // `(function (_N) { var x; })(N || (N = {}))`; // ^^^^^^^^^^^^^^^^^^^^^^^^^^ let callee = { - let body = self.ctx.ast.function_body(SPAN, self.ctx.ast.new_vec(), stmts); + let body = self.ctx.ast.function_body(SPAN, directives, stmts); let params = { let ident = self.ctx.ast.binding_pattern_identifier(BindingIdentifier::new(SPAN, arg_name)); diff --git a/tasks/coverage/transformer_typescript.snap b/tasks/coverage/transformer_typescript.snap index b31f57b09..33778c92c 100644 --- a/tasks/coverage/transformer_typescript.snap +++ b/tasks/coverage/transformer_typescript.snap @@ -2,10 +2,9 @@ commit: 64d2eeea transformer_typescript Summary: AST Parsed : 5243/5243 (100.00%) -Positive Passed: 5237/5243 (99.89%) +Positive Passed: 5238/5243 (99.90%) Mismatch: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts" Mismatch: "compiler/jsxComplexSignatureHasApplicabilityError.tsx" Mismatch: "compiler/jsxEmptyExpressionNotCountedAsChild.tsx" -Mismatch: "compiler/sourceMapValidationClasses.ts" Mismatch: "compiler/styledComponentsInstantiaionLimitNotReached.ts" Mismatch: "compiler/tsxReactPropsInferenceSucceedsOnIntersections.tsx"