From dcb2528861fa2e266ce4345274151327e82df1fa Mon Sep 17 00:00:00 2001 From: Ali Rezvani <3788964+rzvxa@users.noreply.github.com> Date: Thu, 25 Apr 2024 04:13:49 +0330 Subject: [PATCH] fix(semantic): revert test code pushed to the main by accident. (#3085) I'm terribly sorry, I've reverted the wrong commit in #3084. --- .../src/rules/oxc/no_barrel_file.rs | 8 +++--- crates/oxc_semantic/src/node.rs | 27 +++++++------------ 2 files changed, 13 insertions(+), 22 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 eb7f58689..728b207db 100644 --- a/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs +++ b/crates/oxc_linter/src/rules/oxc/no_barrel_file.rs @@ -52,11 +52,11 @@ impl Rule for NoBarrelFile { fn run_once(&self, ctx: &LintContext<'_>) { let semantic = ctx.semantic(); let module_record = semantic.module_record(); - let root_id = semantic.nodes().root(); - if root_id == usize::MAX { + let Some(root) = semantic.nodes().root_node() else { + // Return early if the semantic's root node isn't set. + // It usually means we are running on an empty or invalid file. 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 582bab68f..edd53bca9 100644 --- a/crates/oxc_semantic/src/node.rs +++ b/crates/oxc_semantic/src/node.rs @@ -54,23 +54,13 @@ impl<'a> AstNode<'a> { } /// Untyped AST nodes flattened into an vec -#[derive(Debug)] +#[derive(Debug, Default)] pub struct AstNodes<'a> { - root: AstNodeId, + root: Option, nodes: IndexVec>, parent_ids: IndexVec>, } -impl<'a> Default for AstNodes<'a> { - fn default() -> Self { - Self { - root: AstNodeId::new(usize::MAX), - nodes: IndexVec::default(), - parent_ids: IndexVec::default(), - } - } -} - impl<'a> AstNodes<'a> { pub fn iter(&self) -> impl Iterator> + '_ { self.nodes.iter() @@ -111,7 +101,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 { + pub fn root(&self) -> Option { self.root } @@ -124,7 +114,7 @@ impl<'a> AstNodes<'a> { pub(super) unsafe fn set_root(&mut self, root: &AstNode<'a>) { match root.kind() { AstKind::Program(_) => { - self.root = root.id(); + self.root = Some(root.id()); } _ => unreachable!("Expected a `Program` node as the root of the tree."), } @@ -132,14 +122,14 @@ 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()) + pub fn root_node(&self) -> Option<&AstNode<'a>> { + self.root().map(|id| self.get_node(id)) } /// 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()) + pub fn root_node_mut(&mut self) -> Option<&mut AstNode<'a>> { + self.root().map(|id| self.get_node_mut(id)) } /// Walk up the AST, iterating over each parent node. @@ -151,6 +141,7 @@ impl<'a> AstNodes<'a> { std::iter::successors(Some(ast_node_id), |node_id| parent_ids[*node_id]) } + /// Adds an `AstNode` to the `AstNodes` tree and returns its `AstNodeId`. pub fn add_node(&mut self, node: AstNode<'a>, parent_id: Option) -> AstNodeId { let mut node = node; let ast_node_id = self.parent_ids.push(parent_id);