Skip to content

Commit d89d00d

Browse files
committed
refactor(semantic): split node.rs into node/mod.rs and node/nodes.rs
1 parent d00410d commit d89d00d

File tree

2 files changed

+78
-69
lines changed

2 files changed

+78
-69
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
mod nodes;
2+
3+
pub use nodes::AstNodes;
4+
5+
use oxc_allocator::{Address, GetAddress};
6+
use oxc_ast::AstKind;
7+
#[cfg(feature = "cfg")]
8+
use oxc_cfg::BlockNodeId;
9+
use oxc_span::{GetSpan, Span};
10+
use oxc_syntax::{node::NodeId, scope::ScopeId};
11+
12+
/// Semantic node contains all the semantic information about an ast node.
13+
#[derive(Debug, Clone, Copy)]
14+
pub struct AstNode<'a> {
15+
id: NodeId,
16+
/// A pointer to the ast node, which resides in the `bumpalo` memory arena.
17+
kind: AstKind<'a>,
18+
19+
/// Associated Scope (initialized by binding)
20+
scope_id: ScopeId,
21+
}
22+
23+
impl<'a> AstNode<'a> {
24+
#[inline]
25+
#[cfg(feature = "cfg")]
26+
pub(crate) fn new(
27+
kind: AstKind<'a>,
28+
scope_id: ScopeId,
29+
_cfg_id: BlockNodeId,
30+
id: NodeId,
31+
) -> Self {
32+
Self { id, kind, scope_id }
33+
}
34+
35+
#[cfg(not(feature = "cfg"))]
36+
pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId, _cfg_id: (), id: NodeId) -> Self {
37+
Self { id, kind, scope_id }
38+
}
39+
40+
/// This node's unique identifier.
41+
#[inline]
42+
pub fn id(&self) -> NodeId {
43+
self.id
44+
}
45+
46+
/// Access the underlying struct from [`oxc_ast`].
47+
#[inline]
48+
pub fn kind(&self) -> AstKind<'a> {
49+
self.kind
50+
}
51+
52+
/// The scope in which this node was declared.
53+
///
54+
/// It is important to note that this is _not_ the scope created _by_ the
55+
/// node. For example, given a function declaration, this is the scope where
56+
/// the function is declared, not the scope created by its body.
57+
#[inline]
58+
pub fn scope_id(&self) -> ScopeId {
59+
self.scope_id
60+
}
61+
}
62+
63+
impl GetSpan for AstNode<'_> {
64+
#[inline]
65+
fn span(&self) -> Span {
66+
self.kind.span()
67+
}
68+
}
69+
70+
impl GetAddress for AstNode<'_> {
71+
#[inline]
72+
fn address(&self) -> Address {
73+
self.kind.address()
74+
}
75+
}

crates/oxc_semantic/src/node.rs renamed to crates/oxc_semantic/src/node/nodes.rs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,17 @@
11
use std::iter::FusedIterator;
22

3-
use oxc_allocator::{Address, GetAddress};
43
use oxc_ast::{AstKind, AstType, ast::Program};
54
#[cfg(feature = "cfg")]
65
use oxc_cfg::BlockNodeId;
76
use oxc_index::{IndexSlice, IndexVec};
8-
use oxc_span::{GetSpan, Span};
97
use oxc_syntax::{
108
node::{NodeFlags, NodeId},
119
scope::ScopeId,
1210
};
1311

12+
use super::AstNode;
1413
use crate::ast_types_bitset::AstTypesBitset;
1514

16-
/// Semantic node contains all the semantic information about an ast node.
17-
#[derive(Debug, Clone, Copy)]
18-
pub struct AstNode<'a> {
19-
id: NodeId,
20-
/// A pointer to the ast node, which resides in the `bumpalo` memory arena.
21-
kind: AstKind<'a>,
22-
23-
/// Associated Scope (initialized by binding)
24-
scope_id: ScopeId,
25-
}
26-
27-
impl<'a> AstNode<'a> {
28-
#[inline]
29-
#[cfg(feature = "cfg")]
30-
pub(crate) fn new(
31-
kind: AstKind<'a>,
32-
scope_id: ScopeId,
33-
_cfg_id: BlockNodeId,
34-
id: NodeId,
35-
) -> Self {
36-
Self { id, kind, scope_id }
37-
}
38-
39-
#[cfg(not(feature = "cfg"))]
40-
pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId, _cfg_id: (), id: NodeId) -> Self {
41-
Self { id, kind, scope_id }
42-
}
43-
44-
/// This node's unique identifier.
45-
#[inline]
46-
pub fn id(&self) -> NodeId {
47-
self.id
48-
}
49-
50-
/// Access the underlying struct from [`oxc_ast`].
51-
#[inline]
52-
pub fn kind(&self) -> AstKind<'a> {
53-
self.kind
54-
}
55-
56-
/// The scope in which this node was declared.
57-
///
58-
/// It is important to note that this is _not_ the scope created _by_ the
59-
/// node. For example, given a function declaration, this is the scope where
60-
/// the function is declared, not the scope created by its body.
61-
#[inline]
62-
pub fn scope_id(&self) -> ScopeId {
63-
self.scope_id
64-
}
65-
}
66-
67-
impl GetSpan for AstNode<'_> {
68-
#[inline]
69-
fn span(&self) -> Span {
70-
self.kind.span()
71-
}
72-
}
73-
74-
impl GetAddress for AstNode<'_> {
75-
#[inline]
76-
fn address(&self) -> Address {
77-
self.kind.address()
78-
}
79-
}
80-
8115
/// Untyped AST nodes flattened into an vec
8216
#[derive(Debug, Default)]
8317
pub struct AstNodes<'a> {
@@ -146,7 +80,7 @@ impl<'a> AstNodes<'a> {
14680
/// Access the underlying struct from [`oxc_ast`].
14781
#[inline]
14882
pub fn kind(&self, node_id: NodeId) -> AstKind<'a> {
149-
self.nodes[node_id].kind
83+
self.nodes[node_id].kind()
15084
}
15185

15286
/// Get id of this node's parent.
@@ -200,7 +134,7 @@ impl<'a> AstNodes<'a> {
200134
#[inline]
201135
pub fn program(&self) -> &'a Program<'a> {
202136
if let Some(node) = self.nodes.first()
203-
&& let AstKind::Program(program) = node.kind
137+
&& let AstKind::Program(program) = node.kind()
204138
{
205139
return program;
206140
}

0 commit comments

Comments
 (0)