diff --git a/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs b/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs index b648756e4..eb7f58689 100644 --- a/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs +++ b/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs @@ -52,7 +52,11 @@ impl Rule for NoBarrelFile { fn run_once(&self, ctx: &LintContext<'_>) { let semantic = ctx.semantic(); let module_record = semantic.module_record(); - let Some(root) = ctx.nodes().iter().next() else { return }; + let root_id = semantic.nodes().root(); + if root_id == usize::MAX { + return; + } + let root = semantic.nodes().get_node(root_id); let AstKind::Program(program) = root.kind() else { unreachable!() }; diff --git a/crates/oxc_semantic/src/node.rs b/crates/oxc_semantic/src/node.rs index 3c9cac77a..582bab68f 100644 --- a/crates/oxc_semantic/src/node.rs +++ b/crates/oxc_semantic/src/node.rs @@ -64,7 +64,7 @@ pub struct AstNodes<'a> { impl<'a> Default for AstNodes<'a> { fn default() -> Self { Self { - root: AstNodeId::new(0), + root: AstNodeId::new(usize::MAX), nodes: IndexVec::default(), parent_ids: IndexVec::default(), } @@ -110,6 +110,7 @@ impl<'a> AstNodes<'a> { } /// Get the root `AstNodeId`, It is always pointing to a `Program`. + /// Returns `None` if root node isn't set. pub fn root(&self) -> AstNodeId { self.root } @@ -130,11 +131,13 @@ impl<'a> AstNodes<'a> { } /// Get the root node as immutable reference, It is always guaranteed to be a `Program`. + /// Returns `None` if root node isn't set. pub fn root_node(&self) -> &AstNode<'a> { self.get_node(self.root()) } /// Get the root node as mutable reference, It is always guaranteed to be a `Program`. + /// Returns `None` if root node isn't set. pub fn root_node_mut(&mut self) -> &mut AstNode<'a> { self.get_node_mut(self.root()) }