mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(ast): add is_strict methods (#3227)
De-duplicate logic for checking for `"use strict"` directives.
This commit is contained in:
parent
132db7d2a1
commit
c84c116ac3
2 changed files with 19 additions and 7 deletions
|
|
@ -60,9 +60,7 @@ impl<'a> Program<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_strict(&self) -> bool {
|
pub fn is_strict(&self) -> bool {
|
||||||
self.source_type.is_module()
|
self.source_type.is_strict() || self.directives.iter().any(Directive::is_use_strict)
|
||||||
|| self.source_type.always_strict()
|
|
||||||
|| self.directives.iter().any(|d| d.directive == "use strict")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1483,11 +1481,19 @@ impl<'a> Statement<'a> {
|
||||||
pub struct Directive<'a> {
|
pub struct Directive<'a> {
|
||||||
#[cfg_attr(feature = "serialize", serde(flatten))]
|
#[cfg_attr(feature = "serialize", serde(flatten))]
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
|
/// Directive with any escapes unescaped
|
||||||
pub expression: StringLiteral<'a>,
|
pub expression: StringLiteral<'a>,
|
||||||
|
/// Raw content of directive as it appears in source, any escapes left as is
|
||||||
|
pub directive: Atom<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Directive<'a> {
|
||||||
/// A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either of the exact code point sequences "use strict" or 'use strict'.
|
/// A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either of the exact code point sequences "use strict" or 'use strict'.
|
||||||
/// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.
|
/// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.
|
||||||
/// <https://tc39.es/ecma262/#sec-directive-prologues-and-the-use-strict-directive>
|
/// <https://tc39.es/ecma262/#sec-directive-prologues-and-the-use-strict-directive>
|
||||||
pub directive: Atom<'a>,
|
pub fn is_use_strict(&self) -> bool {
|
||||||
|
self.directive == "use strict"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hashbang
|
/// Hashbang
|
||||||
|
|
@ -2179,9 +2185,7 @@ impl<'a> Function<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_strict(&self) -> bool {
|
pub fn is_strict(&self) -> bool {
|
||||||
self.body.as_ref().is_some_and(|body| {
|
self.body.as_ref().is_some_and(|body| body.has_use_strict_directive())
|
||||||
body.directives.iter().any(|directive| directive.directive == "use strict")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2281,6 +2285,10 @@ impl<'a> FunctionBody<'a> {
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.directives.is_empty() && self.statements.is_empty()
|
self.directives.is_empty() && self.statements.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn has_use_strict_directive(&self) -> bool {
|
||||||
|
self.directives.iter().any(Directive::is_use_strict)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Arrow Function Definitions
|
/// Arrow Function Definitions
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,10 @@ impl SourceType {
|
||||||
self.always_strict
|
self.always_strict
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_strict(self) -> bool {
|
||||||
|
self.is_module() || self.always_strict
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_script(mut self, yes: bool) -> Self {
|
pub fn with_script(mut self, yes: bool) -> Self {
|
||||||
if yes {
|
if yes {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue