1- #![ expect( missing_docs) ] // FIXME
1+ //! Implementation details for AST node kinds
2+ //!
3+ //! This module provides methods and utilities for working with [`AstKind`],
4+ //! including type checking, conversions, and tree traversal helpers.
25
36use oxc_allocator:: { Address , GetAddress } ;
47use oxc_span:: { Atom , GetSpan } ;
58
69use super :: { AstKind , ast:: * } ;
710
811impl < ' a > AstKind < ' a > {
12+ /// Check if this AST node is a statement
13+ ///
14+ /// Returns `true` for all statement types including iteration statements,
15+ /// control flow statements, and declaration statements.
916 #[ rustfmt:: skip]
1017 pub fn is_statement ( self ) -> bool {
1118 self . is_iteration_statement ( )
@@ -16,6 +23,10 @@ impl<'a> AstKind<'a> {
1623 | Self :: IfStatement ( _) | Self :: VariableDeclaration ( _) | Self :: ExportDefaultDeclaration ( _) )
1724 }
1825
26+ /// Check if this AST node is a declaration
27+ ///
28+ /// Returns `true` for function declarations, class declarations,
29+ /// variable declarations, TypeScript declarations, and module declarations.
1930 #[ rustfmt:: skip]
2031 pub fn is_declaration ( self ) -> bool {
2132 matches ! ( self , Self :: Function ( func) if func. is_declaration( ) )
@@ -26,10 +37,17 @@ impl<'a> AstKind<'a> {
2637 ) || self . is_module_declaration ( )
2738 }
2839
40+ /// Check if this AST node is a module declaration
41+ ///
42+ /// Returns `true` for import/export declarations.
2943 pub fn is_module_declaration ( self ) -> bool {
3044 self . as_module_declaration_kind ( ) . is_some ( )
3145 }
3246
47+ /// Attempt to convert this AST node to a module declaration kind
48+ ///
49+ /// Returns `Some(ModuleDeclarationKind)` if this is a module declaration,
50+ /// `None` otherwise.
3351 pub fn as_module_declaration_kind ( & self ) -> Option < ModuleDeclarationKind < ' a > > {
3452 match self {
3553 Self :: ImportDeclaration ( decl) => Some ( ModuleDeclarationKind :: Import ( decl) ) ,
@@ -46,19 +64,30 @@ impl<'a> AstKind<'a> {
4664 }
4765 }
4866
67+ /// Check if this AST node is an iteration statement
68+ ///
69+ /// Returns `true` for do-while, while, for-in, for-of, and for statements.
4970 #[ rustfmt:: skip]
5071 pub fn is_iteration_statement ( self ) -> bool {
5172 matches ! ( self , Self :: DoWhileStatement ( _) | Self :: WhileStatement ( _) | Self :: ForInStatement ( _)
5273 | Self :: ForOfStatement ( _) | Self :: ForStatement ( _) )
5374 }
5475
76+ /// Check if this AST node is any kind of identifier
77+ ///
78+ /// Returns `true` for binding identifiers, identifier references,
79+ /// and label identifiers.
5580 #[ rustfmt:: skip]
5681 pub fn is_identifier ( self ) -> bool {
5782 matches ! ( self , Self :: BindingIdentifier ( _)
5883 | Self :: IdentifierReference ( _)
5984 | Self :: LabelIdentifier ( _) )
6085 }
6186
87+ /// Check if this AST node is a TypeScript type
88+ ///
89+ /// Returns `true` for all TypeScript type nodes including keywords,
90+ /// type references, unions, intersections, etc.
6291 #[ rustfmt:: skip]
6392 pub fn is_type ( self ) -> bool {
6493 matches ! ( self , Self :: TSAnyKeyword ( _) | Self :: TSBigIntKeyword ( _) | Self :: TSBooleanKeyword ( _) | Self :: TSIntrinsicKeyword ( _)
@@ -69,6 +98,10 @@ impl<'a> AstKind<'a> {
6998 | Self :: TSTypeLiteral ( _) | Self :: TSTypeReference ( _) | Self :: TSUnionType ( _) )
7099 }
71100
101+ /// Check if this AST node is a literal
102+ ///
103+ /// Returns `true` for numeric, string, boolean, null, bigint,
104+ /// regexp, and template literals.
72105 pub fn is_literal ( self ) -> bool {
73106 matches ! (
74107 self ,
@@ -82,10 +115,17 @@ impl<'a> AstKind<'a> {
82115 )
83116 }
84117
118+ /// Check if this AST node is function-like
119+ ///
120+ /// Returns `true` for function expressions/declarations and arrow functions.
85121 pub fn is_function_like ( self ) -> bool {
86122 matches ! ( self , Self :: Function ( _) | Self :: ArrowFunctionExpression ( _) )
87123 }
88124
125+ /// Get the name of an identifier node
126+ ///
127+ /// Returns the identifier name if this is any kind of identifier node,
128+ /// `None` otherwise.
89129 pub fn identifier_name ( self ) -> Option < Atom < ' a > > {
90130 match self {
91131 Self :: BindingIdentifier ( ident) => Some ( ident. name ) ,
@@ -96,6 +136,9 @@ impl<'a> AstKind<'a> {
96136 }
97137 }
98138
139+ /// Check if this is an identifier reference with a specific name
140+ ///
141+ /// Returns `true` if this is an `IdentifierReference` with the given name.
99142 pub fn is_specific_id_reference ( & self , name : & str ) -> bool {
100143 match self {
101144 Self :: IdentifierReference ( ident) => ident. name == name,
@@ -125,10 +168,17 @@ impl<'a> AstKind<'a> {
125168 }
126169 }
127170
171+ /// Check if this AST node is a property key
172+ ///
173+ /// Returns `true` for identifier names and private identifiers used as property keys.
128174 pub fn is_property_key ( & self ) -> bool {
129175 self . as_property_key_kind ( ) . is_some ( )
130176 }
131177
178+ /// Attempt to convert this AST node to a property key kind
179+ ///
180+ /// Returns `Some(PropertyKeyKind)` if this is a property key,
181+ /// `None` otherwise.
132182 pub fn as_property_key_kind ( & self ) -> Option < PropertyKeyKind < ' a > > {
133183 match self {
134184 Self :: IdentifierName ( ident) => Some ( PropertyKeyKind :: Static ( ident) ) ,
@@ -137,6 +187,9 @@ impl<'a> AstKind<'a> {
137187 }
138188 }
139189
190+ /// Create an `AstKind` from an expression
191+ ///
192+ /// Converts any expression type to its corresponding `AstKind` variant.
140193 pub fn from_expression ( e : & ' a Expression < ' a > ) -> Self {
141194 match e {
142195 Expression :: BooleanLiteral ( e) => Self :: BooleanLiteral ( e) ,
@@ -649,12 +702,21 @@ impl GetAddress for MemberExpressionKind<'_> {
649702 }
650703}
651704
705+ /// Module declaration types
706+ ///
707+ /// Represents different kinds of module import and export declarations.
652708pub enum ModuleDeclarationKind < ' a > {
709+ /// An import declaration like `import foo from 'bar'`
653710 Import ( & ' a ImportDeclaration < ' a > ) ,
711+ /// An export all declaration like `export * from 'foo'`
654712 ExportAll ( & ' a ExportAllDeclaration < ' a > ) ,
713+ /// A named export declaration like `export { foo, bar }`
655714 ExportNamed ( & ' a ExportNamedDeclaration < ' a > ) ,
715+ /// A default export declaration like `export default foo`
656716 ExportDefault ( & ' a ExportDefaultDeclaration < ' a > ) ,
717+ /// A TypeScript export assignment like `export = foo`
657718 TSExportAssignment ( & ' a TSExportAssignment < ' a > ) ,
719+ /// A TypeScript namespace export like `export as namespace foo`
658720 TSNamespaceExport ( & ' a TSNamespaceExportDeclaration < ' a > ) ,
659721}
660722
@@ -685,6 +747,9 @@ impl GetSpan for ModuleDeclarationKind<'_> {
685747 }
686748}
687749
750+ /// Property key types
751+ ///
752+ /// Represents different kinds of property keys in objects and classes.
688753pub enum PropertyKeyKind < ' a > {
689754 /// A static identifier property key, like `a` in `{ a: 1 }`.
690755 Static ( & ' a IdentifierName < ' a > ) ,
0 commit comments