Skip to main content

oxc_syntax/
node.rs

1//! AST Node ID and flags.
2
3use bitflags::bitflags;
4
5use oxc_allocator::{Allocator, CloneIn, CloneInSemanticIds, Dummy};
6use oxc_ast_macros::ast;
7use oxc_index::define_nonmax_u32_index_type;
8
9use crate::semantic_id::SemanticId;
10
11define_nonmax_u32_index_type! {
12    /// AST Node ID
13    #[ast]
14    #[clone_in(semantic_id)]
15    #[content_eq(skip)]
16    #[estree(skip)]
17    pub struct NodeId;
18}
19
20impl NodeId {
21    /// Mock node id.
22    ///
23    /// This is used for synthetically-created AST nodes, among other things.
24    pub const DUMMY: Self = NodeId::new(0);
25
26    /// Node id of the Program node.
27    pub const ROOT: Self = NodeId::new(0);
28}
29
30impl Default for NodeId {
31    #[inline]
32    fn default() -> Self {
33        Self::DUMMY
34    }
35}
36
37impl<'a> Dummy<'a> for NodeId {
38    #[inline]
39    fn dummy(_: &'a Allocator) -> Self {
40        Self::DUMMY
41    }
42}
43
44impl<'alloc> CloneIn<'alloc> for NodeId {
45    type Cloned = Self;
46
47    #[expect(clippy::inline_always)]
48    #[inline(always)] // Because this method only delegates
49    fn clone_in_impl(&self, with_semantic_ids: CloneInSemanticIds, _: &'alloc Allocator) -> Self {
50        self.clone_id(with_semantic_ids)
51    }
52}
53
54impl SemanticId for NodeId {}
55
56bitflags! {
57    /// Contains additional information about an AST node.
58    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
59    pub struct NodeFlags: u8 {
60        /// Set if the Node has a JSDoc comment attached
61        const JSDoc     = 1 << 0;
62    }
63}
64
65impl NodeFlags {
66    /// Returns `true` if this node has a JSDoc comment attached to it.
67    #[inline]
68    pub fn has_jsdoc(self) -> bool {
69        self.contains(Self::JSDoc)
70    }
71}