feat(ast, transformer): add AstBuilder::use_strict_directive method (#7770)

Add `AstBuilder::use_strict_directive` method, and use it in transformer.
This commit is contained in:
overlookmotel 2024-12-10 13:17:27 +00:00
parent 98afe6543f
commit 7dcf6b4d7c
3 changed files with 9 additions and 7 deletions

View file

@ -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,

View file

@ -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;`

View file

@ -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());
}
}
}