From b7612a34e09bd7340497c1957e38219b6b45d5c5 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 10 Dec 2024 16:46:44 +0000 Subject: [PATCH] refactor(ast)!: rename `is_strict` methods to `has_use_strict_directive` --- crates/oxc_ast/src/ast/js.rs | 2 +- crates/oxc_ast/src/ast/ts.rs | 2 +- crates/oxc_ast/src/ast_impl/js.rs | 11 +++++------ crates/oxc_ast/src/ast_impl/ts.rs | 15 +++++++-------- crates/oxc_ast/src/generated/visit.rs | 4 ++-- crates/oxc_ast/src/generated/visit_mut.rs | 4 ++-- crates/oxc_semantic/src/builder.rs | 4 ++-- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 39ca131d0b00a4..ec262669acbec6 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -1565,7 +1565,7 @@ pub struct BindingRestElement<'a> { #[scope( // `flags` passed in to visitor via parameter defined by `#[visit(args(flags = ...))]` on parents flags(flags), - strict_if(self.is_strict()), + strict_if(self.has_use_strict_directive()), )] #[derive(Debug)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)] diff --git a/crates/oxc_ast/src/ast/ts.rs b/crates/oxc_ast/src/ast/ts.rs index 99591479f25d84..b024d418e7cff0 100644 --- a/crates/oxc_ast/src/ast/ts.rs +++ b/crates/oxc_ast/src/ast/ts.rs @@ -1132,7 +1132,7 @@ pub enum TSTypePredicateName<'a> { #[ast(visit)] #[scope( flags(ScopeFlags::TsModuleBlock), - strict_if(self.body.as_ref().is_some_and(TSModuleDeclarationBody::is_strict)), + strict_if(self.body.as_ref().is_some_and(TSModuleDeclarationBody::has_use_strict_directive)), )] #[derive(Debug)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)] diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 22525e55d0dba4..f1aa1a27d3f120 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -14,10 +14,9 @@ impl Program<'_> { self.body.is_empty() && self.directives.is_empty() } - /// Returns `true` if this program uses strict mode semantics. Both source - /// type and `"use strict"` directives are considered. - pub fn is_strict(&self) -> bool { - self.source_type.is_strict() || self.directives.iter().any(Directive::is_use_strict) + /// Returns `true` if this program has a `"use strict"` directive. + pub fn has_use_strict_directive(&self) -> bool { + self.directives.iter().any(Directive::is_use_strict) } } @@ -996,8 +995,8 @@ impl<'a> Function<'a> { matches!(self.r#type, FunctionType::FunctionDeclaration | FunctionType::TSDeclareFunction) } - /// `true` if this function's body has a `"use strict"` directive. - pub fn is_strict(&self) -> bool { + /// Returns `true` if this function's body has a `"use strict"` directive. + pub fn has_use_strict_directive(&self) -> bool { self.body.as_ref().is_some_and(|body| body.has_use_strict_directive()) } } diff --git a/crates/oxc_ast/src/ast_impl/ts.rs b/crates/oxc_ast/src/ast_impl/ts.rs index fedad39112c3c5..3cbeb975ac61f4 100644 --- a/crates/oxc_ast/src/ast_impl/ts.rs +++ b/crates/oxc_ast/src/ast_impl/ts.rs @@ -171,10 +171,9 @@ impl fmt::Display for TSAccessibility { } impl TSModuleDeclaration<'_> { - /// Returns `true` if this module's body exists and uses strict mode - /// semantics (as determined by [`TSModuleDeclarationBody::is_strict`]). - pub fn is_strict(&self) -> bool { - self.body.as_ref().is_some_and(TSModuleDeclarationBody::is_strict) + /// Returns `true` if this module's body exists and has a `"use strict"` directive. + pub fn has_use_strict_directive(&self) -> bool { + self.body.as_ref().is_some_and(TSModuleDeclarationBody::has_use_strict_directive) } } @@ -233,9 +232,9 @@ impl fmt::Display for TSModuleDeclarationName<'_> { } impl<'a> TSModuleDeclarationBody<'a> { - /// Does the body of this module use strict mode semantics? - pub fn is_strict(&self) -> bool { - matches!(self, Self::TSModuleBlock(block) if block.is_strict()) + /// Returns `true` if this module has a `"use strict"` directive. + pub fn has_use_strict_directive(&self) -> bool { + matches!(self, Self::TSModuleBlock(block) if block.has_use_strict_directive()) } /// Returns `true` if this module contains no statements. @@ -260,7 +259,7 @@ impl<'a> TSModuleDeclarationBody<'a> { impl TSModuleBlock<'_> { /// Returns `true` if this module contains a `"use strict"` directive. - pub fn is_strict(&self) -> bool { + pub fn has_use_strict_directive(&self) -> bool { self.directives.iter().any(Directive::is_use_strict) } } diff --git a/crates/oxc_ast/src/generated/visit.rs b/crates/oxc_ast/src/generated/visit.rs index f2293447ea194f..6dcf504d0a2149 100644 --- a/crates/oxc_ast/src/generated/visit.rs +++ b/crates/oxc_ast/src/generated/visit.rs @@ -3022,7 +3022,7 @@ pub mod walk { visitor.enter_scope( { let mut flags = flags; - if it.is_strict() { + if it.has_use_strict_directive() { flags |= ScopeFlags::StrictMode; } flags @@ -3823,7 +3823,7 @@ pub mod walk { visitor.enter_scope( { let mut flags = ScopeFlags::TsModuleBlock; - if it.body.as_ref().is_some_and(TSModuleDeclarationBody::is_strict) { + if it.body.as_ref().is_some_and(TSModuleDeclarationBody::has_use_strict_directive) { flags |= ScopeFlags::StrictMode; } flags diff --git a/crates/oxc_ast/src/generated/visit_mut.rs b/crates/oxc_ast/src/generated/visit_mut.rs index 6b0e840445d8b4..fd95f7af3199b1 100644 --- a/crates/oxc_ast/src/generated/visit_mut.rs +++ b/crates/oxc_ast/src/generated/visit_mut.rs @@ -3159,7 +3159,7 @@ pub mod walk_mut { visitor.enter_scope( { let mut flags = flags; - if it.is_strict() { + if it.has_use_strict_directive() { flags |= ScopeFlags::StrictMode; } flags @@ -4041,7 +4041,7 @@ pub mod walk_mut { visitor.enter_scope( { let mut flags = ScopeFlags::TsModuleBlock; - if it.body.as_ref().is_some_and(TSModuleDeclarationBody::is_strict) { + if it.body.as_ref().is_some_and(TSModuleDeclarationBody::has_use_strict_directive) { flags |= ScopeFlags::StrictMode; } flags diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index ea4c6b7e4d628a..8f3669a6d8995d 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -606,7 +606,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { // Inline the specific logic for `Program` here instead. // This simplifies logic in `enter_scope`, as it doesn't have to handle the special case. let mut flags = ScopeFlags::Top; - if program.is_strict() { + if self.source_type.is_strict() || program.has_use_strict_directive() { flags |= ScopeFlags::StrictMode; } self.current_scope_id = self.scope.add_scope(None, self.current_node_id, flags); @@ -1589,7 +1589,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { self.enter_scope( { let mut flags = flags; - if func.is_strict() { + if func.has_use_strict_directive() { flags |= ScopeFlags::StrictMode; } flags