From 8d17ab36c3d901efbdb901d865c09c973de39649 Mon Sep 17 00:00:00 2001 From: Ali Rezvani <3788964+rzvxa@users.noreply.github.com> Date: Wed, 24 Apr 2024 20:40:59 +0330 Subject: [PATCH] fix(semantic): allow `root_node` to be empty for empty trees. (#3084) related to #3082, #3030 and #3069 --- crates/oxc_linter/src/rules/oxc/no_barrel_file.rs | 6 +++++- crates/oxc_semantic/src/node.rs | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) 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()) }