Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn has_conditional_path_accept_throw(
// }
// _ => None,
// })
// .filter(|it| it.id() != to.id())
// .filter(|it| it.node_id() != to.node_id())
// .any(|it| {
// // TODO: it.may_throw()
// matches!(
Expand Down
16 changes: 9 additions & 7 deletions crates/oxc_semantic/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,18 @@ fn get_module_instance_state_for_alias_target<'a>(
}
}

let Some(node) = builder.nodes.ancestors(current_node_id).find(|node| {
matches!(
node.kind(),
AstKind::Program(_) | AstKind::TSModuleBlock(_) | AstKind::BlockStatement(_)
)
}) else {
let Some((node_id, node)) =
builder.nodes.ancestors_enumerated(current_node_id).find(|(_, node)| {
matches!(
node.kind(),
AstKind::Program(_) | AstKind::TSModuleBlock(_) | AstKind::BlockStatement(_)
)
})
else {
break;
};

current_node_id = node.id();
current_node_id = node_id;
current_block_stmts.clear();
// Didn't find the declaration whose name matches export specifier
// in the current block, so we need to check the parent block.
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,16 @@ pub fn check_binding_identifier(ident: &BindingIdentifier, ctx: &SemanticBuilder
};

let parent = ctx.nodes.parent_node(ctx.current_node_id);
let parent_id = parent.id();
let is_ok = match parent.kind() {
AstKind::Function(func) => matches!(func.r#type, FunctionType::TSDeclareFunction),
AstKind::FormalParameter(_) | AstKind::FormalParameterRest(_) => {
is_declare_function(&ctx.nodes.parent_kind(parent.id()))
is_declare_function(&ctx.nodes.parent_kind(parent_id))
}
AstKind::BindingRestElement(_) => {
let grand_parent = ctx.nodes.parent_node(parent.id());
is_declare_function(&ctx.nodes.parent_kind(grand_parent.id()))
let grand_parent = ctx.nodes.parent_node(parent_id);
let grand_parent_id = grand_parent.id();
is_declare_function(&ctx.nodes.parent_kind(grand_parent_id))
}
_ => false,
};
Expand Down
17 changes: 8 additions & 9 deletions crates/oxc_semantic/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use oxc_syntax::{node::NodeId, scope::ScopeId};
/// Semantic node contains all the semantic information about an ast node.
#[derive(Debug, Clone, Copy)]
pub struct AstNode<'a> {
id: NodeId,
/// A pointer to the ast node, which resides in the memory arena.
kind: AstKind<'a>,

Expand All @@ -19,14 +18,8 @@ pub struct AstNode<'a> {
}

impl<'a> AstNode<'a> {
pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId, id: NodeId) -> Self {
Self { id, kind, scope_id }
}

/// This node's unique identifier.
#[inline]
pub fn id(&self) -> NodeId {
self.id
pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId) -> Self {
Self { kind, scope_id }
}

/// Access the underlying struct from [`oxc_ast`].
Expand All @@ -35,6 +28,12 @@ impl<'a> AstNode<'a> {
self.kind
}

/// Node id assigned to this AST node.
#[inline]
pub fn id(&self) -> NodeId {
self.kind.node_id()
}

/// The scope in which this node was declared.
///
/// It is important to note that this is _not_ the scope created _by_ the
Expand Down
21 changes: 19 additions & 2 deletions crates/oxc_semantic/src/node/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl<'a> AstNodes<'a> {
self.nodes.iter()
}

/// Iterate over all [`AstNode`]s with their [`NodeId`].
pub fn iter_enumerated(&self) -> impl Iterator<Item = (NodeId, &AstNode<'a>)> + '_ {
self.nodes.iter_enumerated()
}

/// Returns the number of node in this AST.
#[inline]
pub fn len(&self) -> usize {
Expand Down Expand Up @@ -84,6 +89,18 @@ impl<'a> AstNodes<'a> {
self.ancestor_ids(node_id).map(|id| self.get_node(id))
}

/// Walk up the AST, iterating over each parent [`NodeId`] and [`AstNode`].
///
/// The first node produced by this iterator is the parent of `node_id`.
/// The last node will always be [`AstKind::Program`].
#[inline]
pub fn ancestors_enumerated(
&self,
node_id: NodeId,
) -> impl Iterator<Item = (NodeId, &AstNode<'a>)> + Clone + '_ {
self.ancestor_ids(node_id).map(|id| (id, self.get_node(id)))
}

/// Access the underlying struct from [`oxc_ast`].
#[inline]
pub fn kind(&self, node_id: NodeId) -> AstKind<'a> {
Expand Down Expand Up @@ -165,7 +182,7 @@ impl<'a> AstNodes<'a> {
) -> NodeId {
let node_id = self.parent_ids.push(parent_node_id);
kind.set_node_id(node_id);
let node = AstNode::new(kind, scope_id, node_id);
let node = AstNode::new(kind, scope_id);
self.nodes.push(node);
self.flags.push(flags);
#[cfg(feature = "cfg")]
Expand Down Expand Up @@ -194,7 +211,7 @@ impl<'a> AstNodes<'a> {
);
kind.set_node_id(NodeId::ROOT);
self.parent_ids.push(NodeId::ROOT);
self.nodes.push(AstNode::new(kind, scope_id, NodeId::ROOT));
self.nodes.push(AstNode::new(kind, scope_id));
self.flags.push(flags);
#[cfg(feature = "cfg")]
self.cfg_ids.push(cfg_id);
Expand Down
Loading