diff --git a/crates/oxc_ast/src/ast_builder_impl.rs b/crates/oxc_ast/src/ast_builder_impl.rs index 50868001c28ff..f1a9d4117fbc8 100644 --- a/crates/oxc_ast/src/ast_builder_impl.rs +++ b/crates/oxc_ast/src/ast_builder_impl.rs @@ -216,6 +216,13 @@ impl<'a> AstBuilder<'a> { ))) } + /// `"use strict"` directive + #[inline] + pub fn use_strict_directive(self) -> Directive<'a> { + let use_strict = Atom::from("use strict"); + self.directive(SPAN, self.string_literal(SPAN, use_strict.clone(), None), use_strict) + } + /* ---------- Functions ---------- */ /// Create a [`FormalParameter`] with no type annotations, modifiers, diff --git a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs index 989f29db95e9a..c1c00748d28c2 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs @@ -417,11 +417,7 @@ impl<'a, 'c> ConstructorParamsSuperReplacer<'a, 'c> { let directives = if ctx.scopes().get_flags(outer_scope_id).is_strict_mode() { ctx.ast.vec() } else { - ctx.ast.vec1(ctx.ast.directive( - SPAN, - ctx.ast.string_literal(SPAN, Atom::from("use strict"), None), - Atom::from("use strict"), - )) + ctx.ast.vec1(ctx.ast.use_strict_directive()) }; // `return this;` diff --git a/crates/oxc_transformer/src/typescript/module.rs b/crates/oxc_transformer/src/typescript/module.rs index 4280921e6b8ca..e9c4151d93927 100644 --- a/crates/oxc_transformer/src/typescript/module.rs +++ b/crates/oxc_transformer/src/typescript/module.rs @@ -23,8 +23,7 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptModule<'a, 'ctx> { if self.ctx.module.is_commonjs() { let has_use_strict = program.directives.iter().any(Directive::is_use_strict); if !has_use_strict { - let use_strict = ctx.ast.string_literal(SPAN, "use strict", None); - program.directives.insert(0, ctx.ast.directive(SPAN, use_strict, "use strict")); + program.directives.insert(0, ctx.ast.use_strict_directive()); } } }