Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
semantic model improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Leite committed Nov 22, 2022
1 parent b5d530b commit c660de3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 42 deletions.
48 changes: 11 additions & 37 deletions crates/rome_js_semantic/src/semantic_model/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Binding {
})
}
let first = self.data.bindings[self.id]
.references.get(0)
.references.first()
.map(|_| {
Reference {
data: self.data.clone(),
Expand All @@ -87,56 +87,30 @@ impl Binding {

/// Returns an iterator to all reads references of this binding.
pub fn all_reads(&self) -> AllBindingReadReferencesIter {
fn succ_all_reads(current: &Reference) -> Option<Reference> {
let &id = &current.data
.bindings[current.binding_id]
.references
.iter()
.skip(current.id + 1)
.position(|x| x.is_read())?;
Some(Reference {
data: current.data.clone(),
binding_id: current.binding_id,
id: current.id + id,
})
}
let first = self.data.bindings[self.id]
.references.get(0)
.map(|_| {
let first = self.data
.position_next_reference_read(self.id, 0)
.map(|pos| {
Reference {
data: self.data.clone(),
binding_id: self.id,
id: 0,
id: pos,
}
});
std::iter::successors(first, succ_all_reads)
std::iter::successors(first, Reference::find_next_read)
}

/// Returns an iterator to all write references of this binding.
pub fn all_writes(&self) -> AllBindingWriteReferencesIter {
fn succ_all_writes(current: &Reference) -> Option<Reference> {
let &id = &current.data
.bindings[current.binding_id]
.references
.iter()
.skip(current.id + 1)
.position(|x| x.is_write())?;
Some(Reference {
data: current.data.clone(),
binding_id: current.binding_id,
id: current.id + id,
})
}
let first = self.data.bindings[self.id]
.references.get(0)
.map(|_| {
let first = self.data
.position_next_reference_write(self.id, 0)
.map(|pos| {
Reference {
data: self.data.clone(),
binding_id: self.id,
id: 0,
id: pos,
}
});
std::iter::successors(first, succ_all_writes)
std::iter::successors(first, Reference::find_next_write)
}
}

Expand Down
19 changes: 15 additions & 4 deletions crates/rome_js_semantic/src/semantic_model/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ impl SemanticModelBuilder {
range: range.clone(),
references: vec![],
});
dbg!(range);
self.bindings_by_range.insert(range, binding_id);

let scope = self.scopes
Expand All @@ -133,11 +132,23 @@ impl SemanticModelBuilder {
declared_at: declaration_at, //TODO change to binding_id like we do with scope_id
scope_id,
} => {
dbg!(declaration_at);
let binding_id = self.bindings_by_range[&declaration_at];
let binding_id = match self.bindings_by_range.entry(declaration_at) {
Entry::Occupied(entry) => {
*entry.get()
},
Entry::Vacant(entry) => {
let id = self.bindings.len();
self.bindings.push(SemanticModelBindingData {
id,
range,
references: vec![]
});
*entry.insert(id)
},
};
let binding = &mut self.bindings[binding_id];

let reference_id = binding.references.len();

binding.references.push(SemanticModelReference {
range,
ty: SemanticModelReferenceType::Read { hoisted: false }
Expand Down
24 changes: 24 additions & 0 deletions crates/rome_js_semantic/src/semantic_model/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ pub struct Reference {
}

impl Reference {
pub(crate) fn find_next_read(&self) -> Option<Reference> {
self.data
.position_next_reference_read(self.binding_id, self.id + 1)
.map(|pos| {
Reference {
data: self.data.clone(),
binding_id: self.binding_id,
id: pos,
}
})
}

pub(crate) fn find_next_write(&self) -> Option<Reference> {
self.data
.position_next_reference_write(self.binding_id, self.id + 1)
.map(|pos| {
Reference {
data: self.data.clone(),
binding_id: self.binding_id,
id: pos,
}
})
}

pub fn range(&self) -> &TextRange {
let reference = &self.data
.bindings[self.binding_id]
Expand Down
22 changes: 21 additions & 1 deletion crates/rome_js_semantic/src/semantic_model/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ impl SemanticModelData {
pub fn is_exported(&self, range: TextRange) -> bool {
self.exported.contains(&range)
}

pub fn position_next_reference_read(&self, binding_id: usize, skip_references: usize) -> Option<usize> {
let (id, _) = self.bindings[binding_id]
.references
.iter()
.enumerate()
.skip(skip_references)
.find(|(_, reference)| reference.is_read())?;
Some(id)
}

pub fn position_next_reference_write(&self, binding_id: usize, skip_references: usize) -> Option<usize> {
let (id, _) = self.bindings[binding_id]
.references
.iter()
.enumerate()
.skip(skip_references)
.find(|(_, reference)| reference.is_write())?;
Some(id)
}
}

impl PartialEq for SemanticModelData {
Expand Down Expand Up @@ -203,7 +223,7 @@ impl SemanticModel {
.globals.get(global_id)
.and_then(|global| {
global.references.get(id)
}).map(|reference| {
}).map(|_| {
GlobalReference {
data: current.data.clone(),
global_id,
Expand Down
1 change: 1 addition & 0 deletions crates/rome_js_semantic/src/tests/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ assert_semantics! {
// Functions
assert_semantics! {
ok_declaration_function, ";function/*START A*/ f(a/*#a*//*@A*/) {/*START B*/ let b/*#b*//*@B*/ = 1; }",
ok_declaration_self_invocation, ";(function f/*#F*/() {})();",
ok_declaration_arrow_function, ";(/*START A*/ a/*#a*//*@A*/) => {/*START B*/ let b/*#b*//*@B*/ = 1; }",
}

Expand Down

0 comments on commit c660de3

Please sign in to comment.