Skip to content

Commit

Permalink
feat(semantic): add get_node_by_span
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Dec 27, 2024
1 parent 04c7d38 commit 36aad06
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/oxc_semantic/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use hashbrown::HashMap;
use oxc_ast::AstKind;
use oxc_cfg::BlockNodeId;
use oxc_index::IndexVec;
use oxc_span::GetSpan;
use oxc_span::{GetSpan, Span};
use oxc_syntax::{
node::{NodeFlags, NodeId},
scope::ScopeId,
Expand Down Expand Up @@ -108,6 +109,7 @@ pub struct AstNodes<'a> {
nodes: IndexVec<NodeId, AstNode<'a>>,
/// `node` -> `parent`
parent_ids: IndexVec<NodeId, Option<NodeId>>,
node_by_span: HashMap<Span, AstNode<'a>>,
}

impl<'a> AstNodes<'a> {
Expand Down Expand Up @@ -194,6 +196,16 @@ impl<'a> AstNodes<'a> {
&mut self.nodes[node_id]
}

#[inline]
pub fn get_node_by_span(&self, span: Span) -> Option<&AstNode<'a>> {
self.node_by_span.get(&span)
}

#[inline]
pub fn get_node_by_span_mut(&mut self, span: Span) -> Option<&mut AstNode<'a>> {
self.node_by_span.get_mut(&span)
}

/// Get the root [`NodeId`]. This always points to a [`Program`] node.
///
/// Returns [`None`] if root node isn't set. This will never happen if you
Expand Down Expand Up @@ -255,6 +267,7 @@ impl<'a> AstNodes<'a> {
let node_id = self.parent_ids.push(Some(parent_node_id));
let node = AstNode::new(kind, scope_id, cfg_id, flags, node_id);
self.nodes.push(node);
self.node_by_span.insert(node.span(), node);
node_id
}

Expand All @@ -270,13 +283,15 @@ impl<'a> AstNodes<'a> {
self.root = Some(node_id);
let node = AstNode::new(kind, scope_id, cfg_id, flags, node_id);
self.nodes.push(node);
self.node_by_span.insert(node.span(), node);
node_id
}

/// Reserve space for at least `additional` more nodes.
pub fn reserve(&mut self, additional: usize) {
self.nodes.reserve(additional);
self.parent_ids.reserve(additional);
self.node_by_span.reserve(additional);
}
}

Expand Down

0 comments on commit 36aad06

Please sign in to comment.