Skip to content

Commit c868ee3

Browse files
committed
refactor(semantic): rename AstNodeIdAncestorsIter and add comments (#12136)
Follow-on after #12123. Pure refactor. * Rename `AstNodeIdParentIter` to `AstNodeIdAncestorsIter`, as that better describes what it is. * Add a `new` method. * Add comments.
1 parent 7103527 commit c868ee3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

crates/oxc_semantic/src/node.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a> AstNodes<'a> {
128128
/// The last node will always be [`AstKind::Program`].
129129
#[inline]
130130
pub fn ancestor_ids(&self, node_id: NodeId) -> impl Iterator<Item = NodeId> + Clone + '_ {
131-
AstNodeIdParentIter { next_node_id: Some(node_id), nodes: self }
131+
AstNodeIdAncestorsIter::new(node_id, self)
132132
}
133133

134134
/// Walk up the AST, iterating over each parent [`AstKind`].
@@ -252,17 +252,27 @@ impl<'a, 'n> IntoIterator for &'n AstNodes<'a> {
252252
}
253253
}
254254

255+
/// Iterator over ancestors of an AST node, starting with the node itself.
256+
///
257+
/// Yields `NodeId` of each AST node. The last node yielded is `Program`.
255258
#[derive(Debug, Clone)]
256-
pub struct AstNodeIdParentIter<'s, 'a> {
259+
pub struct AstNodeIdAncestorsIter<'s, 'a> {
257260
next_node_id: Option<NodeId>,
258261
nodes: &'s AstNodes<'a>,
259262
}
260263

261-
impl Iterator for AstNodeIdParentIter<'_, '_> {
264+
impl<'s, 'a> AstNodeIdAncestorsIter<'s, 'a> {
265+
fn new(node_id: NodeId, nodes: &'s AstNodes<'a>) -> Self {
266+
Self { next_node_id: Some(node_id), nodes }
267+
}
268+
}
269+
270+
impl Iterator for AstNodeIdAncestorsIter<'_, '_> {
262271
type Item = NodeId;
263272

264273
fn next(&mut self) -> Option<Self::Item> {
265274
if let Some(node_id) = self.next_node_id {
275+
// `Program`'s parent is itself, so next node is `None` if this node is `Program`
266276
self.next_node_id =
267277
if node_id == NodeId::ROOT { None } else { Some(self.nodes.parent_ids[node_id]) };
268278
Some(node_id)
@@ -272,4 +282,4 @@ impl Iterator for AstNodeIdParentIter<'_, '_> {
272282
}
273283
}
274284

275-
impl FusedIterator for AstNodeIdParentIter<'_, '_> {}
285+
impl FusedIterator for AstNodeIdAncestorsIter<'_, '_> {}

0 commit comments

Comments
 (0)