Skip to content

Commit

Permalink
feat(ast, transformer): add AstBuilder::use_strict_directive method (
Browse files Browse the repository at this point in the history
…#7770)

Add `AstBuilder::use_strict_directive` method, and use it in transformer.
  • Loading branch information
overlookmotel committed Dec 10, 2024
1 parent 98afe65 commit 7dcf6b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 7 additions & 0 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;`
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_transformer/src/typescript/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 7dcf6b4

Please sign in to comment.