diff --git a/crates/biome_js_semantic/src/events.rs b/crates/biome_js_semantic/src/events.rs index 501e8f3c5448..bf55ed9b546b 100644 --- a/crates/biome_js_semantic/src/events.rs +++ b/crates/biome_js_semantic/src/events.rs @@ -24,8 +24,8 @@ pub enum SemanticEvent { /// - Type parameters DeclarationFound { range: TextRange, - scope_id: usize, - hoisted_scope_id: Option, + scope_id: u32, + hoisted_scope_id: Option, }, /// Tracks where a symbol is read, but only if its declaration is before this reference. @@ -34,7 +34,7 @@ pub enum SemanticEvent { Read { range: TextRange, declared_at: TextRange, - scope_id: usize, + scope_id: u32, }, /// Tracks where a symbol is read, but only if its declaration was hoisted. @@ -43,7 +43,7 @@ pub enum SemanticEvent { HoistedRead { range: TextRange, declared_at: TextRange, - scope_id: usize, + scope_id: u32, }, /// Tracks where a symbol is written, but only if its declaration is before this reference. @@ -52,7 +52,7 @@ pub enum SemanticEvent { Write { range: TextRange, declared_at: TextRange, - scope_id: usize, + scope_id: u32, }, /// Tracks where a symbol is written, but only if its declaration was hoisted. @@ -62,7 +62,7 @@ pub enum SemanticEvent { HoistedWrite { range: TextRange, declared_at: TextRange, - scope_id: usize, + scope_id: u32, }, /// Tracks references that do no have any matching binding @@ -77,8 +77,8 @@ pub enum SemanticEvent { ScopeStarted { /// Scope range range: TextRange, - scope_id: usize, - parent_scope_id: Option, + scope_id: u32, + parent_scope_id: Option, is_closure: bool, }, @@ -89,7 +89,7 @@ pub enum SemanticEvent { ScopeEnded { /// Scope range range: TextRange, - scope_id: usize, + scope_id: u32, }, /// Tracks where a symbol is exported. @@ -144,15 +144,15 @@ impl SemanticEvent { /// } /// } /// ``` -#[derive(Default, Debug)] +#[derive(Debug, Default)] pub struct SemanticEventExtractor { /// Event queue stash: VecDeque, /// Stack of scopes scopes: Vec, /// Number of generated scopes - /// This allows assigning a unique scope id to every scope. - scope_count: usize, + /// This allows assigning a unique id to every scope. + scope_count: u32, /// At any point this is the set of available bindings and their range in the current scope bindings: FxHashMap, /// Type parameters bound in a `infer T` clause. @@ -270,7 +270,7 @@ enum ScopeHoisting { #[derive(Debug)] struct Scope { - scope_id: usize, + scope_id: u32, /// All bindings declared inside this scope bindings: Vec, /// References that still needs to be bound and will be solved at the end of the scope @@ -282,16 +282,6 @@ struct Scope { } impl SemanticEventExtractor { - pub fn new() -> Self { - Self { - stash: VecDeque::new(), - scopes: vec![], - scope_count: 0, - bindings: FxHashMap::default(), - infers: vec![], - } - } - /// See [SemanticEvent] for a more detailed description of which events [JsSyntaxNode] generates. #[inline] pub fn enter(&mut self, node: &JsSyntaxNode) { @@ -980,8 +970,8 @@ impl SemanticEventExtractor { /// /// This method when called inside the `f` scope will return /// the `f` scope index. - fn scope_index_to_hoist_declarations(&mut self, skip: usize) -> Option { - debug_assert!(self.scopes.len() > skip); + fn scope_index_to_hoist_declarations(&mut self, skip: u32) -> Option { + debug_assert!(self.scopes.len() > (skip as usize)); // We should at least have the global scope // that do not hoist debug_assert!(matches!( @@ -991,7 +981,7 @@ impl SemanticEventExtractor { self.scopes .iter() .rev() - .skip(skip) + .skip(skip as usize) .find(|scope| scope.hoisting == ScopeHoisting::DontHoistDeclarationsToParent) .map(|x| x.scope_id) .filter(|scope_id| self.current_scope_mut().scope_id != *scope_id) @@ -1000,7 +990,7 @@ impl SemanticEventExtractor { /// Push the binding `binding` into the hoisted scope if it exists, or into the current scope. fn push_binding( &mut self, - hoisted_scope_id: Option, + hoisted_scope_id: Option, binding_name: BindingName, binding_info: BindingInfo, ) { diff --git a/crates/biome_js_semantic/src/semantic_model/builder.rs b/crates/biome_js_semantic/src/semantic_model/builder.rs index 35d966b85a5e..dc384ffc8c4e 100644 --- a/crates/biome_js_semantic/src/semantic_model/builder.rs +++ b/crates/biome_js_semantic/src/semantic_model/builder.rs @@ -15,8 +15,8 @@ pub struct SemanticModelBuilder { globals: Vec, globals_by_name: FxHashMap>, scopes: Vec, - scope_range_by_start: FxHashMap>>, - scope_hoisted_to_by_range: FxHashMap, + scope_range_by_start: FxHashMap>>, + scope_hoisted_to_by_range: FxHashMap, bindings: Vec, /// maps a binding range start to its index inside SemanticModelBuilder::bindings vec bindings_by_start: FxHashMap, @@ -127,7 +127,7 @@ impl SemanticModelBuilder { is_closure, } => { // Scopes will be raised in order - debug_assert!(scope_id == self.scopes.len()); + debug_assert!((scope_id as usize) == self.scopes.len()); self.scopes.push(SemanticModelScopeData { range, @@ -141,7 +141,9 @@ impl SemanticModelBuilder { }); if let Some(parent_scope_id) = parent_scope_id { - self.scopes[parent_scope_id].children.push(scope_id); + self.scopes[parent_scope_id as usize] + .children + .push(scope_id); } let start = range.start(); @@ -164,7 +166,7 @@ impl SemanticModelBuilder { // SAFETY: this scope id is guaranteed to exist because they were generated by the // event extractor - debug_assert!(binding_scope_id < self.scopes.len()); + debug_assert!((binding_scope_id as usize) < self.scopes.len()); let binding_id = self.bindings.len(); self.bindings.push(SemanticModelBindingData { @@ -174,7 +176,7 @@ impl SemanticModelBuilder { }); self.bindings_by_start.insert(range.start(), binding_id); - let scope = &mut self.scopes[binding_scope_id]; + let scope = &mut self.scopes[binding_scope_id as usize]; scope.bindings.push(binding_id); // Handle bindings with a bogus name @@ -218,7 +220,7 @@ impl SemanticModelBuilder { ty: SemanticModelReferenceType::Read { hoisted: false }, }); - let scope = &mut self.scopes[scope_id]; + let scope = &mut self.scopes[scope_id as usize]; scope.read_references.push(SemanticModelScopeReference { binding_id, reference_id: reference_index, @@ -241,7 +243,7 @@ impl SemanticModelBuilder { ty: SemanticModelReferenceType::Read { hoisted: true }, }); - let scope = &mut self.scopes[scope_id]; + let scope = &mut self.scopes[scope_id as usize]; scope.read_references.push(SemanticModelScopeReference { binding_id, reference_id: reference_index, @@ -264,7 +266,7 @@ impl SemanticModelBuilder { ty: SemanticModelReferenceType::Write { hoisted: false }, }); - let scope = &mut self.scopes[scope_id]; + let scope = &mut self.scopes[scope_id as usize]; scope.read_references.push(SemanticModelScopeReference { binding_id, reference_id: reference_index, @@ -287,7 +289,7 @@ impl SemanticModelBuilder { ty: SemanticModelReferenceType::Write { hoisted: true }, }); - let scope = &mut self.scopes[scope_id]; + let scope = &mut self.scopes[scope_id as usize]; scope.read_references.push(SemanticModelScopeReference { binding_id, reference_id: reference_index, diff --git a/crates/biome_js_semantic/src/semantic_model/closure.rs b/crates/biome_js_semantic/src/semantic_model/closure.rs index 770add216204..06c4c2b7a267 100644 --- a/crates/biome_js_semantic/src/semantic_model/closure.rs +++ b/crates/biome_js_semantic/src/semantic_model/closure.rs @@ -120,7 +120,7 @@ impl Capture { pub struct AllCapturesIter { data: Rc, closure_range: TextRange, - scopes: Vec, + scopes: Vec, references: Vec, } @@ -143,7 +143,7 @@ impl Iterator for AllCapturesIter { } 'scopes: while let Some(scope_id) = self.scopes.pop() { - let scope = &self.data.scopes[scope_id]; + let scope = &self.data.scopes[scope_id as usize]; if scope.is_closure { continue 'scopes; @@ -167,7 +167,7 @@ impl FusedIterator for AllCapturesIter {} /// Iterate all immediate children closures of a specific closure pub struct ChildrenIter { data: Rc, - scopes: Vec, + scopes: Vec, } impl Iterator for ChildrenIter { @@ -175,7 +175,7 @@ impl Iterator for ChildrenIter { fn next(&mut self) -> Option { while let Some(scope_id) = self.scopes.pop() { - let scope = &self.data.scopes[scope_id]; + let scope = &self.data.scopes[scope_id as usize]; if scope.is_closure { return Some(Closure { data: self.data.clone(), @@ -196,7 +196,7 @@ impl FusedIterator for ChildrenIter {} /// Iterate all descendents closures of a specific closure pub struct DescendentsIter { data: Rc, - scopes: Vec, + scopes: Vec, } impl Iterator for DescendentsIter { @@ -204,7 +204,7 @@ impl Iterator for DescendentsIter { fn next(&mut self) -> Option { while let Some(scope_id) = self.scopes.pop() { - let scope = &self.data.scopes[scope_id]; + let scope = &self.data.scopes[scope_id as usize]; self.scopes.extend(scope.children.iter()); if scope.is_closure { return Some(Closure { @@ -225,7 +225,7 @@ impl FusedIterator for DescendentsIter {} #[derive(Clone)] pub struct Closure { data: Rc, - scope_id: usize, + scope_id: u32, closure_range: TextRange, } @@ -243,7 +243,7 @@ impl Closure { pub(super) fn from_scope( data: Rc, - scope_id: usize, + scope_id: u32, closure_range: &TextRange, ) -> Option { let node = &data.node_by_range[closure_range]; @@ -278,7 +278,7 @@ impl Closure { /// assert!(model.closure(function_f).all_captures(), &["a"]); /// ``` pub fn all_captures(&self) -> impl Iterator { - let scope = &self.data.scopes[self.scope_id]; + let scope = &self.data.scopes[self.scope_id as usize]; let scopes = scope.children.clone(); @@ -308,7 +308,7 @@ impl Closure { /// assert!(model.closure(function_f).children(), &["g"]); /// ``` pub fn children(&self) -> impl Iterator { - let scope = &self.data.scopes[self.scope_id]; + let scope = &self.data.scopes[self.scope_id as usize]; ChildrenIter { data: self.data.clone(), scopes: scope.children.clone(), diff --git a/crates/biome_js_semantic/src/semantic_model/model.rs b/crates/biome_js_semantic/src/semantic_model/model.rs index 90926e194be0..125ca0c6b04b 100644 --- a/crates/biome_js_semantic/src/semantic_model/model.rs +++ b/crates/biome_js_semantic/src/semantic_model/model.rs @@ -34,9 +34,9 @@ pub(crate) struct SemanticModelData { pub(crate) root: AnyJsRoot, // All scopes of this model pub(crate) scopes: Vec, - pub(crate) scope_by_range: rust_lapper::Lapper, + pub(crate) scope_by_range: rust_lapper::Lapper, // Maps the start of a node range to a scope id - pub(crate) scope_hoisted_to_by_range: FxHashMap, + pub(crate) scope_hoisted_to_by_range: FxHashMap, // Map to each by its range pub(crate) node_by_range: FxHashMap, // Maps any range start in the code to its bindings (usize points to bindings vec) @@ -68,7 +68,7 @@ impl SemanticModelData { binding.references.get(index.1 + 1) } - pub(crate) fn scope(&self, range: &TextRange) -> usize { + pub(crate) fn scope(&self, range: &TextRange) -> u32 { let start = range.start().into(); let end = range.end().into(); let scopes = self @@ -84,7 +84,7 @@ impl SemanticModelData { } } - fn scope_hoisted_to(&self, range: &TextRange) -> Option { + fn scope_hoisted_to(&self, range: &TextRange) -> Option { self.scope_hoisted_to_by_range.get(&range.start()).copied() } @@ -122,7 +122,7 @@ impl SemanticModel { pub fn scopes(&self) -> impl Iterator + '_ { self.data.scopes.iter().enumerate().map(|(id, _)| Scope { data: self.data.clone(), - id, + id: id as u32, }) } diff --git a/crates/biome_js_semantic/src/semantic_model/scope.rs b/crates/biome_js_semantic/src/semantic_model/scope.rs index 9052cd2aae61..6b462a336a31 100644 --- a/crates/biome_js_semantic/src/semantic_model/scope.rs +++ b/crates/biome_js_semantic/src/semantic_model/scope.rs @@ -9,9 +9,9 @@ pub(crate) struct SemanticModelScopeData { // The scope range pub(crate) range: TextRange, // The parent scope of this scope - pub(crate) parent: Option, + pub(crate) parent: Option, // All children scope of this scope - pub(crate) children: Vec, + pub(crate) children: Vec, // All bindings of this scope (points to SemanticModelData::bindings) pub(crate) bindings: Vec, // Map pointing to the [bindings] vec of each bindings by its name @@ -29,7 +29,7 @@ pub(crate) struct SemanticModelScopeData { #[derive(Clone, Debug)] pub struct Scope { pub(crate) data: Rc, - pub(crate) id: usize, + pub(crate) id: u32, } impl PartialEq for Scope { @@ -63,9 +63,9 @@ impl Scope { pub fn parent(&self) -> Option { // id will always be a valid scope because // it was created by [SemanticModel::scope] method. - debug_assert!(self.id < self.data.scopes.len()); + debug_assert!((self.id as usize) < self.data.scopes.len()); - let parent = self.data.scopes[self.id].parent?; + let parent = self.data.scopes[self.id as usize].parent?; Some(Scope { data: self.data.clone(), id: parent, @@ -85,7 +85,7 @@ impl Scope { /// Returns a binding by its name, like it appears on code. It **does /// not** returns bindings of parent scopes. pub fn get_binding(&self, name: impl AsRef) -> Option { - let data = &self.data.scopes[self.id]; + let data = &self.data.scopes[self.id as usize]; let name = name.as_ref(); let id = data.bindings_by_name.get(name)?; @@ -108,7 +108,7 @@ impl Scope { } pub fn range(&self) -> &TextRange { - &self.data.scopes[self.id].range + &self.data.scopes[self.id as usize].range } pub fn syntax(&self) -> &JsSyntaxNode { @@ -123,7 +123,7 @@ impl Scope { } } -/// Represents a refererence inside a scope. +/// Represents a reference inside a scope. #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub(crate) struct SemanticModelScopeReference { // Points to [SemanticModel]::bindings vec @@ -135,7 +135,7 @@ pub(crate) struct SemanticModelScopeReference { /// Iterate all descendents scopes of the specified scope in breadth-first order. pub struct ScopeDescendentsIter { data: Rc, - q: VecDeque, + q: VecDeque, } impl Iterator for ScopeDescendentsIter { @@ -143,7 +143,7 @@ impl Iterator for ScopeDescendentsIter { fn next(&mut self) -> Option { if let Some(id) = self.q.pop_front() { - let scope = &self.data.scopes[id]; + let scope = &self.data.scopes[id as usize]; self.q.extend(scope.children.iter()); Some(Scope { data: self.data.clone(), @@ -162,7 +162,7 @@ impl FusedIterator for ScopeDescendentsIter {} #[derive(Debug)] pub struct ScopeBindingsIter { data: Rc, - scope_id: usize, + scope_id: u32, binding_index: usize, } @@ -172,9 +172,9 @@ impl Iterator for ScopeBindingsIter { fn next(&mut self) -> Option { // scope_id will always be a valid scope because // it was created by [Scope::bindings] method. - debug_assert!(self.scope_id < self.data.scopes.len()); + debug_assert!((self.scope_id as usize) < self.data.scopes.len()); - let id = self.data.scopes[self.scope_id] + let id = self.data.scopes[self.scope_id as usize] .bindings .get(self.binding_index)?; @@ -191,9 +191,9 @@ impl ExactSizeIterator for ScopeBindingsIter { fn len(&self) -> usize { // scope_id will always be a valid scope because // it was created by [Scope::bindings] method. - debug_assert!(self.scope_id < self.data.scopes.len()); + debug_assert!((self.scope_id as usize) < self.data.scopes.len()); - self.data.scopes[self.scope_id].bindings.len() + self.data.scopes[self.scope_id as usize].bindings.len() } } diff --git a/crates/biome_js_semantic/src/tests/assertions.rs b/crates/biome_js_semantic/src/tests/assertions.rs index 96d82129f603..5969fc5ea0a5 100644 --- a/crates/biome_js_semantic/src/tests/assertions.rs +++ b/crates/biome_js_semantic/src/tests/assertions.rs @@ -124,7 +124,7 @@ pub fn assert(code: &str, test_name: &str) { // Extract semantic events and index by range let mut events_by_pos: FxHashMap> = FxHashMap::default(); - let mut scope_start_by_id: FxHashMap = FxHashMap::default(); + let mut scope_start_by_id: FxHashMap = FxHashMap::default(); for event in semantic_events(r.syntax()) { let pos = if let SemanticEvent::ScopeEnded { range, scope_id, .. @@ -199,13 +199,13 @@ struct DeclarationAssertion { #[derive(Clone, Debug)] struct ReadAssertion { range: TextRange, - declaration_asertion_name: String, + declaration_assertion_name: String, } #[derive(Clone, Debug)] struct WriteAssertion { range: TextRange, - declaration_asertion_name: String, + declaration_assertion_name: String, } #[derive(Clone, Debug)] @@ -278,7 +278,7 @@ impl SemanticAssertion { Some(SemanticAssertion::Read(ReadAssertion { range: token.parent().unwrap().text_range(), - declaration_asertion_name: symbol_name, + declaration_assertion_name: symbol_name, })) } else if assertion_text.starts_with("/*WRITE ") { let symbol_name = assertion_text @@ -290,7 +290,7 @@ impl SemanticAssertion { Some(SemanticAssertion::Write(WriteAssertion { range: token.parent().unwrap().text_range(), - declaration_asertion_name: symbol_name, + declaration_assertion_name: symbol_name, })) } else if assertion_text.contains("/*START") { let scope_name = assertion_text @@ -447,7 +447,7 @@ impl SemanticAssertions { code: &str, test_name: &str, events_by_pos: FxHashMap>, - scope_start: FxHashMap, + scope_start: FxHashMap, ) { // Check every declaration assertion is ok @@ -480,13 +480,13 @@ impl SemanticAssertions { for assertion in self.read_assertions.iter() { let decl = match self .declarations_assertions - .get(&assertion.declaration_asertion_name) + .get(&assertion.declaration_assertion_name) { Some(decl) => decl, None => { panic!( "No declaration found with name: {}", - assertion.declaration_asertion_name + assertion.declaration_assertion_name ); } }; @@ -545,13 +545,13 @@ impl SemanticAssertions { for assertion in self.write_assertions.iter() { let decl = match self .declarations_assertions - .get(&assertion.declaration_asertion_name) + .get(&assertion.declaration_assertion_name) { Some(decl) => decl, None => { panic!( "No declaration found with name: {}", - assertion.declaration_asertion_name + assertion.declaration_assertion_name ); } };