Replies: 5 comments 8 replies
-
1 - SyntaxNode
|
Beta Was this translation helpful? Give feedback.
-
2 -
|
Beta Was this translation helpful? Give feedback.
-
3 - Lifting parent to Option worldRust unfortunately is not a complete functional language, so we have no easy way to lift the parent function to Option. And even if we could, there is no easy way to turn lifted function into "postfixed operators", because Rust do not have a pipeline operator. In this case my proposal is to create a extension method to pub trait AstNodeParent {
type Language: Language;
fn parent<T: AstNode<Language = Self::Language> + Sized>(&self) -> Option<T>;
}
impl<TAstNode, TLanguage> AstNodeParent for Option<TAstNode>
where
TAstNode: AstNode<Language = TLanguage>,
TLanguage: Language,
{
type Language = TLanguage;
fn parent<T: AstNode<Language = Self::Language> + Sized>(&self) -> Option<T> {
self.as_ref()?.syntax().parent().and_then(T::cast)
}
} This brings the solution to: fn is_identifier_declared_with_var(binding: JsSyntaxNode) -> bool {
binding
.cast::<JsIdentifierBinding>()
.parent::<JsVariableDeclarator>()
.parent::<JsVariableDeclaratorList>()
.parent::<JsVariableDeclaration>()
.map(|declaration| declaration.is_var())
.unwrap_or(false)
} |
Beta Was this translation helpful? Give feedback.
-
4 - using the
|
Beta Was this translation helpful? Give feedback.
-
PR for this discussion: #2601 |
Beta Was this translation helpful? Give feedback.
-
Today we don't have a very nice way to navigate the CST upwards. Of course, we have
SyntaxNode::parent
but then I need to work on the untyped tree.tools/crates/rome_rowan/src/cursor/node.rs
Line 160 in 3d5b0f0
All the examples below are navigating from a binding identifier to its declaration statement and checking if the
var
keyword was used:My first option is to navigate the untyped tree and ignore checking anything along the way.
But if we don't wanna check anything, we may as well do:
If I do need to check anything along the way the chain gets much bigger and with a lot of boring repetitive code. One of the reasons is that
AstNode
does not have a parent method. So I need to jump into the untyped tree again.tools/crates/rome_rowan/src/ast/mod.rs
Line 25 in 3d5b0f0
Any functional programmer at this point is laughing at me for not using "do notations" or any other monadic comprehension. So we can use the "try" operator:
One annoying thing here is that we can't have one chain because
cast
is an associated function. Another very annoying thing is the almost double "?" operator. This we can alleviate with:Any other variations were cosmetic changes from these possibilities.
Now, below are my suggestions on how to improve this navigation, split into four different threads, so it becomes easy to discuss each specific point.
1 - SyntaxNode
cast
extension method;2 -
AstNode
needs aparent
function;3 - Lifting
parent
toOption<>
world;4 - using the
extension-trait
crate.I am a strong advocate for implementing the first three, whereas the fourth is more like just sugar on top of it.
Beta Was this translation helpful? Give feedback.
All reactions