-
Notifications
You must be signed in to change notification settings - Fork 666
feature(rome_js_semantic): semantic model built from semantic events #2776
Conversation
Deploying with Cloudflare Pages
|
ef6df0e
to
901d072
Compare
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
crates/rome_js_semantic/src/model.rs
Outdated
pub struct SemanticModel { | ||
data: Arc<SemanticModelData>, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should document this public object and explain the contents of data
, because SemanticModelData
is private at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
crates/rome_js_semantic/src/model.rs
Outdated
type Item = Binding; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
debug_assert!(self.id < self.data.scopes.len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a string that explains the reason of this debug assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
crates/rome_js_semantic/src/model.rs
Outdated
fn next(&mut self) -> Option<Self::Item> { | ||
debug_assert!(self.id < self.data.scopes.len()); | ||
|
||
let result = match self.data.scopes[self.id].bindings.get(self.binding_index) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The debug assertion will disappear in production, but self.data.scopes[self.id]
will stay. Is this operation safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I pushed an explanation of why.
} | ||
|
||
/// Provides all information regarding a specific scope. | ||
/// Allows navigation to parent and children scope and binding information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Allows navigation to parent and children scope and binding information. | |
/// Allows navigation to parent and children scopes and binding information. |
} | ||
|
||
impl Scope { | ||
/// Return this scope parent. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Return this scope parent. | |
/// Return the parent this scope. |
} | ||
} | ||
|
||
/// Provides all information regarding to a specific binding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Provides all information regarding to a specific binding. | |
/// Provides information of a specific binding. |
This PR creates the semantic model and allows navigation and querying the model.
The idea is to enable rules from #2743 to be implemented.
This is the code example:
The interesting part are:
scope navigation
iterate scope bindings
get scope binding by name
get declaration of a reference
bindings
We do not allocate when saving bindings names, which is fine. But to enable this the event now needs to carry the SyntaxTokenText, which brings a green node with it.
This was not my initial idea.
I am fine with this for now, but if in the end no other solution appear, I will change SemanticEvents to return SyntaxNode, SynaxtToken instead.
SemanticModelData
The "heart" of the semantic model is inside the struct
SemanticModelData
. This struct lives behind anArc
that is shared with all semantic structs.rust-lapper
To allow any node/token to have a
scope
function, I am using interval trees (https://en.wikipedia.org/wiki/Interval_tree). In this case, using the crate https://github.com/sstadick/rust-lapper.