Skip to content

Commit f1d4086

Browse files
committed
refactor(ast): remove AstKind for ModuleDeclaration (#12022)
- Part of #11490 Similar to how we handled member expressions, I've added a new `enum` that narrows `AstKind` down to just things that are `ModuleDeclaration`s.
1 parent 754c05a commit f1d4086

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+265
-240
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,30 @@ impl<'a> AstKind<'a> {
1818
pub fn is_declaration(self) -> bool {
1919
matches!(self, Self::Function(func) if func.is_declaration())
2020
|| matches!(self, Self::Class(class) if class.is_declaration())
21-
|| matches!(self, Self::ModuleDeclaration(_) | Self::TSEnumDeclaration(_) | Self::TSModuleDeclaration(_)
21+
|| matches!(self, Self::TSEnumDeclaration(_) | Self::TSModuleDeclaration(_)
2222
| Self::VariableDeclaration(_) | Self::TSInterfaceDeclaration(_)
2323
| Self::TSTypeAliasDeclaration(_) | Self::TSImportEqualsDeclaration(_) | Self::PropertyDefinition(_)
24-
)
24+
) || self.is_module_declaration()
25+
}
26+
27+
pub fn is_module_declaration(self) -> bool {
28+
self.as_module_declaration_kind().is_some()
29+
}
30+
31+
pub fn as_module_declaration_kind(&self) -> Option<ModuleDeclarationKind<'a>> {
32+
match self {
33+
Self::ImportDeclaration(decl) => Some(ModuleDeclarationKind::Import(decl)),
34+
Self::ExportAllDeclaration(decl) => Some(ModuleDeclarationKind::ExportAll(decl)),
35+
Self::ExportNamedDeclaration(decl) => Some(ModuleDeclarationKind::ExportNamed(decl)),
36+
Self::ExportDefaultDeclaration(decl) => {
37+
Some(ModuleDeclarationKind::ExportDefault(decl))
38+
}
39+
Self::TSExportAssignment(decl) => Some(ModuleDeclarationKind::TSExportAssignment(decl)),
40+
Self::TSNamespaceExportDeclaration(decl) => {
41+
Some(ModuleDeclarationKind::TSNamespaceExport(decl))
42+
}
43+
_ => None,
44+
}
2545
}
2646

2747
#[rustfmt::skip]
@@ -309,7 +329,6 @@ impl AstKind<'_> {
309329

310330
Self::Decorator(_) => "Decorator".into(),
311331

312-
Self::ModuleDeclaration(_) => "ModuleDeclaration".into(),
313332
Self::ImportDeclaration(_) => "ImportDeclaration".into(),
314333
Self::ImportSpecifier(i) => format!("ImportSpecifier({})", i.local.name).into(),
315334
Self::ExportSpecifier(e) => format!("ExportSpecifier({})", e.local.name()).into(),
@@ -505,3 +524,39 @@ impl GetSpan for MemberExpressionKind<'_> {
505524
}
506525
}
507526
}
527+
528+
pub enum ModuleDeclarationKind<'a> {
529+
Import(&'a ImportDeclaration<'a>),
530+
ExportAll(&'a ExportAllDeclaration<'a>),
531+
ExportNamed(&'a ExportNamedDeclaration<'a>),
532+
ExportDefault(&'a ExportDefaultDeclaration<'a>),
533+
TSExportAssignment(&'a TSExportAssignment<'a>),
534+
TSNamespaceExport(&'a TSNamespaceExportDeclaration<'a>),
535+
}
536+
537+
impl ModuleDeclarationKind<'_> {
538+
/// Returns whether this module declaration is an `export` declaration.
539+
pub fn is_export(&self) -> bool {
540+
matches!(
541+
self,
542+
Self::ExportAll(_)
543+
| Self::ExportNamed(_)
544+
| Self::ExportDefault(_)
545+
| Self::TSExportAssignment(_)
546+
| Self::TSNamespaceExport(_)
547+
)
548+
}
549+
}
550+
551+
impl GetSpan for ModuleDeclarationKind<'_> {
552+
fn span(&self) -> Span {
553+
match self {
554+
Self::Import(decl) => decl.span,
555+
Self::ExportAll(decl) => decl.span,
556+
Self::ExportNamed(decl) => decl.span,
557+
Self::ExportDefault(decl) => decl.span,
558+
Self::TSExportAssignment(decl) => decl.span,
559+
Self::TSNamespaceExport(decl) => decl.span,
560+
}
561+
}
562+
}

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 104 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -99,111 +99,110 @@ pub enum AstType {
9999
PropertyDefinition = 82,
100100
PrivateIdentifier = 83,
101101
StaticBlock = 84,
102-
ModuleDeclaration = 85,
103-
AccessorProperty = 86,
104-
ImportExpression = 87,
105-
ImportDeclaration = 88,
106-
ImportSpecifier = 89,
107-
ImportDefaultSpecifier = 90,
108-
ImportNamespaceSpecifier = 91,
109-
WithClause = 92,
110-
ImportAttribute = 93,
111-
ExportNamedDeclaration = 94,
112-
ExportDefaultDeclaration = 95,
113-
ExportAllDeclaration = 96,
114-
ExportSpecifier = 97,
115-
V8IntrinsicExpression = 98,
116-
BooleanLiteral = 99,
117-
NullLiteral = 100,
118-
NumericLiteral = 101,
119-
StringLiteral = 102,
120-
BigIntLiteral = 103,
121-
RegExpLiteral = 104,
122-
JSXElement = 105,
123-
JSXOpeningElement = 106,
124-
JSXClosingElement = 107,
125-
JSXFragment = 108,
126-
JSXOpeningFragment = 109,
127-
JSXClosingFragment = 110,
128-
JSXNamespacedName = 111,
129-
JSXMemberExpression = 112,
130-
JSXExpressionContainer = 113,
131-
JSXEmptyExpression = 114,
132-
JSXAttribute = 115,
133-
JSXSpreadAttribute = 116,
134-
JSXIdentifier = 117,
135-
JSXSpreadChild = 118,
136-
JSXText = 119,
137-
TSThisParameter = 120,
138-
TSEnumDeclaration = 121,
139-
TSEnumBody = 122,
140-
TSEnumMember = 123,
141-
TSTypeAnnotation = 124,
142-
TSLiteralType = 125,
143-
TSConditionalType = 126,
144-
TSUnionType = 127,
145-
TSIntersectionType = 128,
146-
TSParenthesizedType = 129,
147-
TSTypeOperator = 130,
148-
TSArrayType = 131,
149-
TSIndexedAccessType = 132,
150-
TSTupleType = 133,
151-
TSNamedTupleMember = 134,
152-
TSOptionalType = 135,
153-
TSRestType = 136,
154-
TSAnyKeyword = 137,
155-
TSStringKeyword = 138,
156-
TSBooleanKeyword = 139,
157-
TSNumberKeyword = 140,
158-
TSNeverKeyword = 141,
159-
TSIntrinsicKeyword = 142,
160-
TSUnknownKeyword = 143,
161-
TSNullKeyword = 144,
162-
TSUndefinedKeyword = 145,
163-
TSVoidKeyword = 146,
164-
TSSymbolKeyword = 147,
165-
TSThisType = 148,
166-
TSObjectKeyword = 149,
167-
TSBigIntKeyword = 150,
168-
TSTypeReference = 151,
169-
TSQualifiedName = 152,
170-
TSTypeParameterInstantiation = 153,
171-
TSTypeParameter = 154,
172-
TSTypeParameterDeclaration = 155,
173-
TSTypeAliasDeclaration = 156,
174-
TSClassImplements = 157,
175-
TSInterfaceDeclaration = 158,
176-
TSInterfaceBody = 159,
177-
TSPropertySignature = 160,
178-
TSIndexSignature = 161,
179-
TSCallSignatureDeclaration = 162,
180-
TSMethodSignature = 163,
181-
TSConstructSignatureDeclaration = 164,
182-
TSIndexSignatureName = 165,
183-
TSInterfaceHeritage = 166,
184-
TSTypePredicate = 167,
185-
TSModuleDeclaration = 168,
186-
TSModuleBlock = 169,
187-
TSTypeLiteral = 170,
188-
TSInferType = 171,
189-
TSTypeQuery = 172,
190-
TSImportType = 173,
191-
TSConstructorType = 174,
192-
TSMappedType = 175,
193-
TSTemplateLiteralType = 176,
194-
TSAsExpression = 177,
195-
TSSatisfiesExpression = 178,
196-
TSTypeAssertion = 179,
197-
TSImportEqualsDeclaration = 180,
198-
TSExternalModuleReference = 181,
199-
TSNonNullExpression = 182,
200-
Decorator = 183,
201-
TSExportAssignment = 184,
202-
TSNamespaceExportDeclaration = 185,
203-
TSInstantiationExpression = 186,
204-
JSDocNullableType = 187,
205-
JSDocNonNullableType = 188,
206-
JSDocUnknownType = 189,
102+
AccessorProperty = 85,
103+
ImportExpression = 86,
104+
ImportDeclaration = 87,
105+
ImportSpecifier = 88,
106+
ImportDefaultSpecifier = 89,
107+
ImportNamespaceSpecifier = 90,
108+
WithClause = 91,
109+
ImportAttribute = 92,
110+
ExportNamedDeclaration = 93,
111+
ExportDefaultDeclaration = 94,
112+
ExportAllDeclaration = 95,
113+
ExportSpecifier = 96,
114+
V8IntrinsicExpression = 97,
115+
BooleanLiteral = 98,
116+
NullLiteral = 99,
117+
NumericLiteral = 100,
118+
StringLiteral = 101,
119+
BigIntLiteral = 102,
120+
RegExpLiteral = 103,
121+
JSXElement = 104,
122+
JSXOpeningElement = 105,
123+
JSXClosingElement = 106,
124+
JSXFragment = 107,
125+
JSXOpeningFragment = 108,
126+
JSXClosingFragment = 109,
127+
JSXNamespacedName = 110,
128+
JSXMemberExpression = 111,
129+
JSXExpressionContainer = 112,
130+
JSXEmptyExpression = 113,
131+
JSXAttribute = 114,
132+
JSXSpreadAttribute = 115,
133+
JSXIdentifier = 116,
134+
JSXSpreadChild = 117,
135+
JSXText = 118,
136+
TSThisParameter = 119,
137+
TSEnumDeclaration = 120,
138+
TSEnumBody = 121,
139+
TSEnumMember = 122,
140+
TSTypeAnnotation = 123,
141+
TSLiteralType = 124,
142+
TSConditionalType = 125,
143+
TSUnionType = 126,
144+
TSIntersectionType = 127,
145+
TSParenthesizedType = 128,
146+
TSTypeOperator = 129,
147+
TSArrayType = 130,
148+
TSIndexedAccessType = 131,
149+
TSTupleType = 132,
150+
TSNamedTupleMember = 133,
151+
TSOptionalType = 134,
152+
TSRestType = 135,
153+
TSAnyKeyword = 136,
154+
TSStringKeyword = 137,
155+
TSBooleanKeyword = 138,
156+
TSNumberKeyword = 139,
157+
TSNeverKeyword = 140,
158+
TSIntrinsicKeyword = 141,
159+
TSUnknownKeyword = 142,
160+
TSNullKeyword = 143,
161+
TSUndefinedKeyword = 144,
162+
TSVoidKeyword = 145,
163+
TSSymbolKeyword = 146,
164+
TSThisType = 147,
165+
TSObjectKeyword = 148,
166+
TSBigIntKeyword = 149,
167+
TSTypeReference = 150,
168+
TSQualifiedName = 151,
169+
TSTypeParameterInstantiation = 152,
170+
TSTypeParameter = 153,
171+
TSTypeParameterDeclaration = 154,
172+
TSTypeAliasDeclaration = 155,
173+
TSClassImplements = 156,
174+
TSInterfaceDeclaration = 157,
175+
TSInterfaceBody = 158,
176+
TSPropertySignature = 159,
177+
TSIndexSignature = 160,
178+
TSCallSignatureDeclaration = 161,
179+
TSMethodSignature = 162,
180+
TSConstructSignatureDeclaration = 163,
181+
TSIndexSignatureName = 164,
182+
TSInterfaceHeritage = 165,
183+
TSTypePredicate = 166,
184+
TSModuleDeclaration = 167,
185+
TSModuleBlock = 168,
186+
TSTypeLiteral = 169,
187+
TSInferType = 170,
188+
TSTypeQuery = 171,
189+
TSImportType = 172,
190+
TSConstructorType = 173,
191+
TSMappedType = 174,
192+
TSTemplateLiteralType = 175,
193+
TSAsExpression = 176,
194+
TSSatisfiesExpression = 177,
195+
TSTypeAssertion = 178,
196+
TSImportEqualsDeclaration = 179,
197+
TSExternalModuleReference = 180,
198+
TSNonNullExpression = 181,
199+
Decorator = 182,
200+
TSExportAssignment = 183,
201+
TSNamespaceExportDeclaration = 184,
202+
TSInstantiationExpression = 185,
203+
JSDocNullableType = 186,
204+
JSDocNonNullableType = 187,
205+
JSDocUnknownType = 188,
207206
}
208207

209208
/// Untyped AST Node Kind
@@ -303,7 +302,6 @@ pub enum AstKind<'a> {
303302
PropertyDefinition(&'a PropertyDefinition<'a>) = AstType::PropertyDefinition as u8,
304303
PrivateIdentifier(&'a PrivateIdentifier<'a>) = AstType::PrivateIdentifier as u8,
305304
StaticBlock(&'a StaticBlock<'a>) = AstType::StaticBlock as u8,
306-
ModuleDeclaration(&'a ModuleDeclaration<'a>) = AstType::ModuleDeclaration as u8,
307305
AccessorProperty(&'a AccessorProperty<'a>) = AstType::AccessorProperty as u8,
308306
ImportExpression(&'a ImportExpression<'a>) = AstType::ImportExpression as u8,
309307
ImportDeclaration(&'a ImportDeclaration<'a>) = AstType::ImportDeclaration as u8,
@@ -520,7 +518,6 @@ impl GetSpan for AstKind<'_> {
520518
Self::PropertyDefinition(it) => it.span(),
521519
Self::PrivateIdentifier(it) => it.span(),
522520
Self::StaticBlock(it) => it.span(),
523-
Self::ModuleDeclaration(it) => it.span(),
524521
Self::AccessorProperty(it) => it.span(),
525522
Self::ImportExpression(it) => it.span(),
526523
Self::ImportDeclaration(it) => it.span(),
@@ -717,7 +714,6 @@ impl GetAddress for AstKind<'_> {
717714
Self::PropertyDefinition(it) => Address::from_ptr(it),
718715
Self::PrivateIdentifier(it) => Address::from_ptr(it),
719716
Self::StaticBlock(it) => Address::from_ptr(it),
720-
Self::ModuleDeclaration(it) => it.address(),
721717
Self::AccessorProperty(it) => Address::from_ptr(it),
722718
Self::ImportExpression(it) => Address::from_ptr(it),
723719
Self::ImportDeclaration(it) => Address::from_ptr(it),
@@ -1256,11 +1252,6 @@ impl<'a> AstKind<'a> {
12561252
if let Self::StaticBlock(v) = self { Some(v) } else { None }
12571253
}
12581254

1259-
#[inline]
1260-
pub fn as_module_declaration(self) -> Option<&'a ModuleDeclaration<'a>> {
1261-
if let Self::ModuleDeclaration(v) = self { Some(v) } else { None }
1262-
}
1263-
12641255
#[inline]
12651256
pub fn as_accessor_property(self) -> Option<&'a AccessorProperty<'a>> {
12661257
if let Self::AccessorProperty(v) = self { Some(v) } else { None }

crates/oxc_ast/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub use crate::{
7676
ast::comment::{Comment, CommentContent, CommentKind, CommentPosition},
7777
ast_builder_impl::{AstBuilder, NONE},
7878
ast_kind::{AstKind, AstType},
79-
ast_kind_impl::MemberExpressionKind,
79+
ast_kind_impl::{MemberExpressionKind, ModuleDeclarationKind},
8080
trivia::{CommentsRange, comments_range, has_comments_between},
8181
};
8282

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,8 +2638,7 @@ pub mod walk {
26382638
}
26392639

26402640
pub fn walk_module_declaration<'a, V: Visit<'a>>(visitor: &mut V, it: &ModuleDeclaration<'a>) {
2641-
let kind = AstKind::ModuleDeclaration(visitor.alloc(it));
2642-
visitor.enter_node(kind);
2641+
// No `AstKind` for this type
26432642
match it {
26442643
ModuleDeclaration::ImportDeclaration(it) => visitor.visit_import_declaration(it),
26452644
ModuleDeclaration::ExportAllDeclaration(it) => visitor.visit_export_all_declaration(it),
@@ -2654,7 +2653,6 @@ pub mod walk {
26542653
visitor.visit_ts_namespace_export_declaration(it)
26552654
}
26562655
}
2657-
visitor.leave_node(kind);
26582656
}
26592657

26602658
#[inline]

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,8 +2733,7 @@ pub mod walk_mut {
27332733
visitor: &mut V,
27342734
it: &mut ModuleDeclaration<'a>,
27352735
) {
2736-
let kind = AstType::ModuleDeclaration;
2737-
visitor.enter_node(kind);
2736+
// No `AstType` for this type
27382737
match it {
27392738
ModuleDeclaration::ImportDeclaration(it) => visitor.visit_import_declaration(it),
27402739
ModuleDeclaration::ExportAllDeclaration(it) => visitor.visit_export_all_declaration(it),
@@ -2749,7 +2748,6 @@ pub mod walk_mut {
27492748
visitor.visit_ts_namespace_export_declaration(it)
27502749
}
27512750
}
2752-
visitor.leave_node(kind);
27532751
}
27542752

27552753
#[inline]

0 commit comments

Comments
 (0)