Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(semantic): simplify inherit scope flags from parent scope #4664

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,9 @@ impl<'a> SemanticBuilder<'a> {

impl<'a> Visit<'a> for SemanticBuilder<'a> {
// NB: Not called for `Program`
fn enter_scope(&mut self, flags: ScopeFlags, scope_id: &Cell<Option<ScopeId>>) {
fn enter_scope(&mut self, mut flags: ScopeFlags, scope_id: &Cell<Option<ScopeId>>) {
let parent_scope_id = self.current_scope_id;

let mut flags = flags;
if !flags.is_strict_mode() && self.current_node_flags.has_class() {
// NOTE A class definition is always strict mode code.
flags |= ScopeFlags::StrictMode;
Expand Down
23 changes: 7 additions & 16 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,18 @@ impl ScopeTree {
&mut self.flags[scope_id]
}

pub fn get_new_scope_flags(&self, flags: ScopeFlags, parent_scope_id: ScopeId) -> ScopeFlags {
let mut strict_mode = self.root_flags().is_strict_mode();
let parent_scope_flags = self.get_flags(parent_scope_id);

// Inherit strict mode for functions
pub fn get_new_scope_flags(
&self,
mut flags: ScopeFlags,
parent_scope_id: ScopeId,
) -> ScopeFlags {
// https://tc39.es/ecma262/#sec-strict-mode-code
if !strict_mode
&& (parent_scope_flags.is_function() || parent_scope_flags.is_ts_module_block())
&& parent_scope_flags.is_strict_mode()
{
strict_mode = true;
}
let parent_scope_flags = self.get_flags(parent_scope_id);
flags |= parent_scope_flags & ScopeFlags::StrictMode;

// inherit flags for non-function scopes
let mut flags = flags;
if !flags.contains(ScopeFlags::Function) {
flags |= parent_scope_flags & ScopeFlags::Modifiers;
};

if strict_mode {
flags |= ScopeFlags::StrictMode;
}

flags
Expand Down