Skip to content

Commit

Permalink
refactor(js_semantic): use range start as binding identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Oct 18, 2023
1 parent 9e21f50 commit 21b87de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
43 changes: 22 additions & 21 deletions crates/biome_js_semantic/src/semantic_model/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub struct SemanticModelBuilder {
scope_range_by_start: FxHashMap<TextSize, BTreeSet<Interval<usize, usize>>>,
scope_hoisted_to_by_range: FxHashMap<TextSize, usize>,
bindings: Vec<SemanticModelBindingData>,
/// maps a binding range to its index inside SemanticModelBuilder::bindings vec
bindings_by_range: FxHashMap<TextRange, usize>,
/// maps a reference range to its bindings. usize points to SemanticModelBuilder::bindings vec
declared_at_by_range: FxHashMap<TextRange, usize>,
exported: FxHashSet<TextRange>,
/// maps a binding range start to its index inside SemanticModelBuilder::bindings vec
bindings_by_start: FxHashMap<TextSize, usize>,
/// maps a reference range start to its bindings. usize points to SemanticModelBuilder::bindings vec
declared_at_by_start: FxHashMap<TextSize, usize>,
exported: FxHashSet<TextSize>,
unresolved_references: Vec<SemanticModelUnresolvedReference>,
}

Expand All @@ -37,8 +37,8 @@ impl SemanticModelBuilder {
scope_range_by_start: FxHashMap::default(),
scope_hoisted_to_by_range: FxHashMap::default(),
bindings: vec![],
bindings_by_range: FxHashMap::default(),
declared_at_by_range: FxHashMap::default(),
bindings_by_start: FxHashMap::default(),
declared_at_by_start: FxHashMap::default(),
exported: FxHashSet::default(),
unresolved_references: Vec::new(),
}
Expand All @@ -48,7 +48,7 @@ impl SemanticModelBuilder {
pub fn push_node(&mut self, node: &JsSyntaxNode) {
use JsSyntaxKind::*;
match node.kind() {
// Acessible from bindings and references
// Accessible from bindings and references
JS_IDENTIFIER_BINDING
| TS_IDENTIFIER_BINDING
| JS_REFERENCE_IDENTIFIER
Expand All @@ -58,7 +58,7 @@ impl SemanticModelBuilder {
self.node_by_range.insert(node.text_range(), node.clone());
}

// Acessible from scopes, closures
// Accessible from scopes, closures
JS_MODULE
| JS_SCRIPT
| JS_FUNCTION_DECLARATION
Expand Down Expand Up @@ -155,7 +155,8 @@ impl SemanticModelBuilder {
range: name_range,
references: vec![],
});
self.bindings_by_range.insert(name_range, binding_id);
self.bindings_by_start
.insert(name_range.start(), binding_id);

let scope = self.scopes.get_mut(binding_scope_id).unwrap();

Expand All @@ -172,7 +173,7 @@ impl SemanticModelBuilder {
declared_at: declaration_at, //TODO change to binding_id like we do with scope_id
scope_id,
} => {
let binding_id = match self.bindings_by_range.entry(declaration_at) {
let binding_id = match self.bindings_by_start.entry(declaration_at.start()) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let id = self.bindings.len();
Expand All @@ -199,14 +200,14 @@ impl SemanticModelBuilder {
reference_id: reference_index,
});

self.declared_at_by_range.insert(range, binding_id);
self.declared_at_by_start.insert(range.start(), binding_id);
}
HoistedRead {
range,
declared_at: declaration_at,
scope_id,
} => {
let binding_id = self.bindings_by_range[&declaration_at];
let binding_id = self.bindings_by_start[&declaration_at.start()];
let binding = &mut self.bindings[binding_id];

let reference_index = binding.references.len();
Expand All @@ -222,14 +223,14 @@ impl SemanticModelBuilder {
reference_id: reference_index,
});

self.declared_at_by_range.insert(range, binding_id);
self.declared_at_by_start.insert(range.start(), binding_id);
}
Write {
range,
declared_at: declaration_at,
scope_id,
} => {
let binding_id = self.bindings_by_range[&declaration_at];
let binding_id = self.bindings_by_start[&declaration_at.start()];
let binding = &mut self.bindings[binding_id];

let reference_index = binding.references.len();
Expand All @@ -245,14 +246,14 @@ impl SemanticModelBuilder {
reference_id: reference_index,
});

self.declared_at_by_range.insert(range, binding_id);
self.declared_at_by_start.insert(range.start(), binding_id);
}
HoistedWrite {
range,
declared_at: declaration_at,
scope_id,
} => {
let binding_id = self.bindings_by_range[&declaration_at];
let binding_id = self.bindings_by_start[&declaration_at.start()];
let binding = &mut self.bindings[binding_id];

let reference_index = binding.references.len();
Expand All @@ -268,7 +269,7 @@ impl SemanticModelBuilder {
reference_id: reference_index,
});

self.declared_at_by_range.insert(range, binding_id);
self.declared_at_by_start.insert(range.start(), binding_id);
}
UnresolvedReference { is_read, range } => {
let ty = if is_read {
Expand Down Expand Up @@ -307,7 +308,7 @@ impl SemanticModelBuilder {
}
}
Exported { range } => {
self.exported.insert(range);
self.exported.insert(range.start());
}
}
}
Expand All @@ -327,8 +328,8 @@ impl SemanticModelBuilder {
scope_hoisted_to_by_range: self.scope_hoisted_to_by_range,
node_by_range: self.node_by_range,
bindings: self.bindings,
bindings_by_range: self.bindings_by_range,
declared_at_by_range: self.declared_at_by_range,
bindings_by_start: self.bindings_by_start,
declared_at_by_start: self.declared_at_by_start,
exported: self.exported,
unresolved_references: self.unresolved_references,
globals: self.globals,
Expand Down
16 changes: 8 additions & 8 deletions crates/biome_js_semantic/src/semantic_model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ pub(crate) struct SemanticModelData {
pub(crate) scope_hoisted_to_by_range: FxHashMap<TextSize, usize>,
// Map to each by its range
pub(crate) node_by_range: FxHashMap<TextRange, JsSyntaxNode>,
// Maps any range in the code to its bindings (usize points to bindings vec)
pub(crate) declared_at_by_range: FxHashMap<TextRange, usize>,
// Maps any range start in the code to its bindings (usize points to bindings vec)
pub(crate) declared_at_by_start: FxHashMap<TextSize, usize>,
// List of all the declarations
pub(crate) bindings: Vec<SemanticModelBindingData>,
// Index bindings by range
pub(crate) bindings_by_range: FxHashMap<TextRange, usize>,
// Index bindings by range start
pub(crate) bindings_by_start: FxHashMap<TextSize, usize>,
// All bindings that were exported
pub(crate) exported: FxHashSet<TextRange>,
pub(crate) exported: FxHashSet<TextSize>,
/// All references that could not be resolved
pub(crate) unresolved_references: Vec<SemanticModelUnresolvedReference>,
/// All globals references
Expand Down Expand Up @@ -89,7 +89,7 @@ impl SemanticModelData {
}

pub fn is_exported(&self, range: TextRange) -> bool {
self.exported.contains(&range)
self.exported.contains(&range.start())
}
}

Expand Down Expand Up @@ -210,7 +210,7 @@ impl SemanticModel {
pub fn binding(&self, reference: &impl HasDeclarationAstNode) -> Option<Binding> {
let reference = reference.node();
let range = reference.syntax().text_range();
let id = *self.data.declared_at_by_range.get(&range)?;
let id = *self.data.declared_at_by_start.get(&range.start())?;
Some(Binding {
data: self.data.clone(),
index: id.into(),
Expand Down Expand Up @@ -331,7 +331,7 @@ impl SemanticModel {

pub fn as_binding(&self, binding: &impl IsBindingAstNode) -> Binding {
let range = binding.syntax().text_range();
let id = &self.data.bindings_by_range[&range];
let id = &self.data.bindings_by_start[&range.start()];
Binding {
data: self.data.clone(),
index: (*id).into(),
Expand Down

0 comments on commit 21b87de

Please sign in to comment.