Skip to content

Commit d41fb13

Browse files
authored
refactor(ast): get jsx types out of AstKind exceptions (#11535)
This is my (not so confident) attempt to do #11490 for all jsx types. 1. removed all jsx types from STRUCTS_BLACK_LIST and ENUMS_WHITE_LIST 2. regenerated ast code 3. added missing arms to AstKind::debug_name 4. removed now broken is_jsx function, because it was unused 5. fixed all compile errors 6. fixed all test errors 7. regenerated snapshots (different node ids and decreased prettier conformance) All tests are green 🥳🎉
1 parent 3cabde2 commit d41fb13

35 files changed

+231
-253
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ impl<'a> AstKind<'a> {
7474
}
7575
}
7676

77-
pub fn is_jsx(self) -> bool {
78-
matches!(
79-
self,
80-
Self::JSXElement(_)
81-
| Self::JSXOpeningElement(_)
82-
| Self::JSXElementName(_)
83-
| Self::JSXFragment(_)
84-
| Self::JSXAttributeItem(_)
85-
| Self::JSXText(_)
86-
| Self::JSXExpressionContainer(_)
87-
)
88-
}
89-
9077
pub fn is_specific_id_reference(&self, name: &str) -> bool {
9178
match self {
9279
Self::IdentifierReference(ident) => ident.name == name,
@@ -306,16 +293,18 @@ impl AstKind<'_> {
306293
Self::ExportAllDeclaration(_) => "ExportAllDeclaration".into(),
307294
Self::JSXOpeningElement(_) => "JSXOpeningElement".into(),
308295
Self::JSXClosingElement(_) => "JSXClosingElement".into(),
309-
Self::JSXElementName(n) => format!("JSXElementName({n})").into(),
310296
Self::JSXElement(_) => "JSXElement".into(),
311297
Self::JSXFragment(_) => "JSXFragment".into(),
312-
Self::JSXAttributeItem(_) => "JSXAttributeItem".into(),
298+
Self::JSXOpeningFragment(_) => "JSXOpeningFragment".into(),
299+
Self::JSXClosingFragment(_) => "JSXClosingFragment".into(),
300+
Self::JSXEmptyExpression(_) => "JSXEmptyExpression".into(),
301+
Self::JSXSpreadChild(_) => "JSXSpreadChild".into(),
302+
Self::JSXAttribute(_) => "JSXAttribute".into(),
313303
Self::JSXSpreadAttribute(_) => "JSXSpreadAttribute".into(),
314304
Self::JSXText(_) => "JSXText".into(),
315305
Self::JSXExpressionContainer(_) => "JSXExpressionContainer".into(),
316306
Self::JSXIdentifier(id) => format!("JSXIdentifier({id})").into(),
317307
Self::JSXMemberExpression(_) => "JSXMemberExpression".into(),
318-
Self::JSXMemberExpressionObject(_) => "JSXMemberExpressionObject".into(),
319308
Self::JSXNamespacedName(_) => "JSXNamespacedName".into(),
320309

321310
Self::TSModuleBlock(_) => "TSModuleBlock".into(),

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 95 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -114,72 +114,74 @@ pub enum AstType {
114114
JSXOpeningElement = 98,
115115
JSXClosingElement = 99,
116116
JSXFragment = 100,
117-
JSXElementName = 101,
118-
JSXNamespacedName = 102,
119-
JSXMemberExpression = 103,
120-
JSXMemberExpressionObject = 104,
117+
JSXOpeningFragment = 101,
118+
JSXClosingFragment = 102,
119+
JSXNamespacedName = 103,
120+
JSXMemberExpression = 104,
121121
JSXExpressionContainer = 105,
122-
JSXAttributeItem = 106,
123-
JSXSpreadAttribute = 107,
124-
JSXIdentifier = 108,
125-
JSXText = 109,
126-
TSThisParameter = 110,
127-
TSEnumDeclaration = 111,
128-
TSEnumBody = 112,
129-
TSEnumMember = 113,
130-
TSTypeAnnotation = 114,
131-
TSLiteralType = 115,
132-
TSConditionalType = 116,
133-
TSUnionType = 117,
134-
TSIntersectionType = 118,
135-
TSParenthesizedType = 119,
136-
TSIndexedAccessType = 120,
137-
TSNamedTupleMember = 121,
138-
TSAnyKeyword = 122,
139-
TSStringKeyword = 123,
140-
TSBooleanKeyword = 124,
141-
TSNumberKeyword = 125,
142-
TSNeverKeyword = 126,
143-
TSIntrinsicKeyword = 127,
144-
TSUnknownKeyword = 128,
145-
TSNullKeyword = 129,
146-
TSUndefinedKeyword = 130,
147-
TSVoidKeyword = 131,
148-
TSSymbolKeyword = 132,
149-
TSThisType = 133,
150-
TSObjectKeyword = 134,
151-
TSBigIntKeyword = 135,
152-
TSTypeReference = 136,
153-
TSTypeName = 137,
154-
TSQualifiedName = 138,
155-
TSTypeParameterInstantiation = 139,
156-
TSTypeParameter = 140,
157-
TSTypeParameterDeclaration = 141,
158-
TSTypeAliasDeclaration = 142,
159-
TSClassImplements = 143,
160-
TSInterfaceDeclaration = 144,
161-
TSPropertySignature = 145,
162-
TSMethodSignature = 146,
163-
TSConstructSignatureDeclaration = 147,
164-
TSInterfaceHeritage = 148,
165-
TSModuleDeclaration = 149,
166-
TSModuleBlock = 150,
167-
TSTypeLiteral = 151,
168-
TSInferType = 152,
169-
TSTypeQuery = 153,
170-
TSImportType = 154,
171-
TSMappedType = 155,
172-
TSTemplateLiteralType = 156,
173-
TSAsExpression = 157,
174-
TSSatisfiesExpression = 158,
175-
TSTypeAssertion = 159,
176-
TSImportEqualsDeclaration = 160,
177-
TSModuleReference = 161,
178-
TSExternalModuleReference = 162,
179-
TSNonNullExpression = 163,
180-
Decorator = 164,
181-
TSExportAssignment = 165,
182-
TSInstantiationExpression = 166,
122+
JSXEmptyExpression = 106,
123+
JSXAttribute = 107,
124+
JSXSpreadAttribute = 108,
125+
JSXIdentifier = 109,
126+
JSXSpreadChild = 110,
127+
JSXText = 111,
128+
TSThisParameter = 112,
129+
TSEnumDeclaration = 113,
130+
TSEnumBody = 114,
131+
TSEnumMember = 115,
132+
TSTypeAnnotation = 116,
133+
TSLiteralType = 117,
134+
TSConditionalType = 118,
135+
TSUnionType = 119,
136+
TSIntersectionType = 120,
137+
TSParenthesizedType = 121,
138+
TSIndexedAccessType = 122,
139+
TSNamedTupleMember = 123,
140+
TSAnyKeyword = 124,
141+
TSStringKeyword = 125,
142+
TSBooleanKeyword = 126,
143+
TSNumberKeyword = 127,
144+
TSNeverKeyword = 128,
145+
TSIntrinsicKeyword = 129,
146+
TSUnknownKeyword = 130,
147+
TSNullKeyword = 131,
148+
TSUndefinedKeyword = 132,
149+
TSVoidKeyword = 133,
150+
TSSymbolKeyword = 134,
151+
TSThisType = 135,
152+
TSObjectKeyword = 136,
153+
TSBigIntKeyword = 137,
154+
TSTypeReference = 138,
155+
TSTypeName = 139,
156+
TSQualifiedName = 140,
157+
TSTypeParameterInstantiation = 141,
158+
TSTypeParameter = 142,
159+
TSTypeParameterDeclaration = 143,
160+
TSTypeAliasDeclaration = 144,
161+
TSClassImplements = 145,
162+
TSInterfaceDeclaration = 146,
163+
TSPropertySignature = 147,
164+
TSMethodSignature = 148,
165+
TSConstructSignatureDeclaration = 149,
166+
TSInterfaceHeritage = 150,
167+
TSModuleDeclaration = 151,
168+
TSModuleBlock = 152,
169+
TSTypeLiteral = 153,
170+
TSInferType = 154,
171+
TSTypeQuery = 155,
172+
TSImportType = 156,
173+
TSMappedType = 157,
174+
TSTemplateLiteralType = 158,
175+
TSAsExpression = 159,
176+
TSSatisfiesExpression = 160,
177+
TSTypeAssertion = 161,
178+
TSImportEqualsDeclaration = 162,
179+
TSModuleReference = 163,
180+
TSExternalModuleReference = 164,
181+
TSNonNullExpression = 165,
182+
Decorator = 166,
183+
TSExportAssignment = 167,
184+
TSInstantiationExpression = 168,
183185
}
184186

185187
/// Untyped AST Node Kind
@@ -294,15 +296,16 @@ pub enum AstKind<'a> {
294296
JSXOpeningElement(&'a JSXOpeningElement<'a>) = AstType::JSXOpeningElement as u8,
295297
JSXClosingElement(&'a JSXClosingElement<'a>) = AstType::JSXClosingElement as u8,
296298
JSXFragment(&'a JSXFragment<'a>) = AstType::JSXFragment as u8,
297-
JSXElementName(&'a JSXElementName<'a>) = AstType::JSXElementName as u8,
299+
JSXOpeningFragment(&'a JSXOpeningFragment) = AstType::JSXOpeningFragment as u8,
300+
JSXClosingFragment(&'a JSXClosingFragment) = AstType::JSXClosingFragment as u8,
298301
JSXNamespacedName(&'a JSXNamespacedName<'a>) = AstType::JSXNamespacedName as u8,
299302
JSXMemberExpression(&'a JSXMemberExpression<'a>) = AstType::JSXMemberExpression as u8,
300-
JSXMemberExpressionObject(&'a JSXMemberExpressionObject<'a>) =
301-
AstType::JSXMemberExpressionObject as u8,
302303
JSXExpressionContainer(&'a JSXExpressionContainer<'a>) = AstType::JSXExpressionContainer as u8,
303-
JSXAttributeItem(&'a JSXAttributeItem<'a>) = AstType::JSXAttributeItem as u8,
304+
JSXEmptyExpression(&'a JSXEmptyExpression) = AstType::JSXEmptyExpression as u8,
305+
JSXAttribute(&'a JSXAttribute<'a>) = AstType::JSXAttribute as u8,
304306
JSXSpreadAttribute(&'a JSXSpreadAttribute<'a>) = AstType::JSXSpreadAttribute as u8,
305307
JSXIdentifier(&'a JSXIdentifier<'a>) = AstType::JSXIdentifier as u8,
308+
JSXSpreadChild(&'a JSXSpreadChild<'a>) = AstType::JSXSpreadChild as u8,
306309
JSXText(&'a JSXText<'a>) = AstType::JSXText as u8,
307310
TSThisParameter(&'a TSThisParameter<'a>) = AstType::TSThisParameter as u8,
308311
TSEnumDeclaration(&'a TSEnumDeclaration<'a>) = AstType::TSEnumDeclaration as u8,
@@ -485,14 +488,16 @@ impl GetSpan for AstKind<'_> {
485488
Self::JSXOpeningElement(it) => it.span(),
486489
Self::JSXClosingElement(it) => it.span(),
487490
Self::JSXFragment(it) => it.span(),
488-
Self::JSXElementName(it) => it.span(),
491+
Self::JSXOpeningFragment(it) => it.span(),
492+
Self::JSXClosingFragment(it) => it.span(),
489493
Self::JSXNamespacedName(it) => it.span(),
490494
Self::JSXMemberExpression(it) => it.span(),
491-
Self::JSXMemberExpressionObject(it) => it.span(),
492495
Self::JSXExpressionContainer(it) => it.span(),
493-
Self::JSXAttributeItem(it) => it.span(),
496+
Self::JSXEmptyExpression(it) => it.span(),
497+
Self::JSXAttribute(it) => it.span(),
494498
Self::JSXSpreadAttribute(it) => it.span(),
495499
Self::JSXIdentifier(it) => it.span(),
500+
Self::JSXSpreadChild(it) => it.span(),
496501
Self::JSXText(it) => it.span(),
497502
Self::TSThisParameter(it) => it.span(),
498503
Self::TSEnumDeclaration(it) => it.span(),
@@ -1062,8 +1067,13 @@ impl<'a> AstKind<'a> {
10621067
}
10631068

10641069
#[inline]
1065-
pub fn as_jsx_element_name(self) -> Option<&'a JSXElementName<'a>> {
1066-
if let Self::JSXElementName(v) = self { Some(v) } else { None }
1070+
pub fn as_jsx_opening_fragment(self) -> Option<&'a JSXOpeningFragment> {
1071+
if let Self::JSXOpeningFragment(v) = self { Some(v) } else { None }
1072+
}
1073+
1074+
#[inline]
1075+
pub fn as_jsx_closing_fragment(self) -> Option<&'a JSXClosingFragment> {
1076+
if let Self::JSXClosingFragment(v) = self { Some(v) } else { None }
10671077
}
10681078

10691079
#[inline]
@@ -1077,18 +1087,18 @@ impl<'a> AstKind<'a> {
10771087
}
10781088

10791089
#[inline]
1080-
pub fn as_jsx_member_expression_object(self) -> Option<&'a JSXMemberExpressionObject<'a>> {
1081-
if let Self::JSXMemberExpressionObject(v) = self { Some(v) } else { None }
1090+
pub fn as_jsx_expression_container(self) -> Option<&'a JSXExpressionContainer<'a>> {
1091+
if let Self::JSXExpressionContainer(v) = self { Some(v) } else { None }
10821092
}
10831093

10841094
#[inline]
1085-
pub fn as_jsx_expression_container(self) -> Option<&'a JSXExpressionContainer<'a>> {
1086-
if let Self::JSXExpressionContainer(v) = self { Some(v) } else { None }
1095+
pub fn as_jsx_empty_expression(self) -> Option<&'a JSXEmptyExpression> {
1096+
if let Self::JSXEmptyExpression(v) = self { Some(v) } else { None }
10871097
}
10881098

10891099
#[inline]
1090-
pub fn as_jsx_attribute_item(self) -> Option<&'a JSXAttributeItem<'a>> {
1091-
if let Self::JSXAttributeItem(v) = self { Some(v) } else { None }
1100+
pub fn as_jsx_attribute(self) -> Option<&'a JSXAttribute<'a>> {
1101+
if let Self::JSXAttribute(v) = self { Some(v) } else { None }
10921102
}
10931103

10941104
#[inline]
@@ -1101,6 +1111,11 @@ impl<'a> AstKind<'a> {
11011111
if let Self::JSXIdentifier(v) = self { Some(v) } else { None }
11021112
}
11031113

1114+
#[inline]
1115+
pub fn as_jsx_spread_child(self) -> Option<&'a JSXSpreadChild<'a>> {
1116+
if let Self::JSXSpreadChild(v) = self { Some(v) } else { None }
1117+
}
1118+
11041119
#[inline]
11051120
pub fn as_jsx_text(self) -> Option<&'a JSXText<'a>> {
11061121
if let Self::JSXText(v) = self { Some(v) } else { None }

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,28 +2969,30 @@ pub mod walk {
29692969

29702970
#[inline]
29712971
pub fn walk_jsx_opening_fragment<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXOpeningFragment) {
2972-
// No `AstKind` for this type
2972+
let kind = AstKind::JSXOpeningFragment(visitor.alloc(it));
2973+
visitor.enter_node(kind);
29732974
visitor.visit_span(&it.span);
2975+
visitor.leave_node(kind);
29742976
}
29752977

29762978
#[inline]
29772979
pub fn walk_jsx_closing_fragment<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXClosingFragment) {
2978-
// No `AstKind` for this type
2980+
let kind = AstKind::JSXClosingFragment(visitor.alloc(it));
2981+
visitor.enter_node(kind);
29792982
visitor.visit_span(&it.span);
2983+
visitor.leave_node(kind);
29802984
}
29812985

29822986
#[inline]
29832987
pub fn walk_jsx_element_name<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXElementName<'a>) {
2984-
let kind = AstKind::JSXElementName(visitor.alloc(it));
2985-
visitor.enter_node(kind);
2988+
// No `AstKind` for this type
29862989
match it {
29872990
JSXElementName::Identifier(it) => visitor.visit_jsx_identifier(it),
29882991
JSXElementName::IdentifierReference(it) => visitor.visit_identifier_reference(it),
29892992
JSXElementName::NamespacedName(it) => visitor.visit_jsx_namespaced_name(it),
29902993
JSXElementName::MemberExpression(it) => visitor.visit_jsx_member_expression(it),
29912994
JSXElementName::ThisExpression(it) => visitor.visit_this_expression(it),
29922995
}
2993-
visitor.leave_node(kind);
29942996
}
29952997

29962998
#[inline]
@@ -3021,8 +3023,7 @@ pub mod walk {
30213023
visitor: &mut V,
30223024
it: &JSXMemberExpressionObject<'a>,
30233025
) {
3024-
let kind = AstKind::JSXMemberExpressionObject(visitor.alloc(it));
3025-
visitor.enter_node(kind);
3026+
// No `AstKind` for this type
30263027
match it {
30273028
JSXMemberExpressionObject::IdentifierReference(it) => {
30283029
visitor.visit_identifier_reference(it)
@@ -3032,7 +3033,6 @@ pub mod walk {
30323033
}
30333034
JSXMemberExpressionObject::ThisExpression(it) => visitor.visit_this_expression(it),
30343035
}
3035-
visitor.leave_node(kind);
30363036
}
30373037

30383038
#[inline]
@@ -3058,29 +3058,31 @@ pub mod walk {
30583058

30593059
#[inline]
30603060
pub fn walk_jsx_empty_expression<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXEmptyExpression) {
3061-
// No `AstKind` for this type
3061+
let kind = AstKind::JSXEmptyExpression(visitor.alloc(it));
3062+
visitor.enter_node(kind);
30623063
visitor.visit_span(&it.span);
3064+
visitor.leave_node(kind);
30633065
}
30643066

30653067
#[inline]
30663068
pub fn walk_jsx_attribute_item<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXAttributeItem<'a>) {
3067-
let kind = AstKind::JSXAttributeItem(visitor.alloc(it));
3068-
visitor.enter_node(kind);
3069+
// No `AstKind` for this type
30693070
match it {
30703071
JSXAttributeItem::Attribute(it) => visitor.visit_jsx_attribute(it),
30713072
JSXAttributeItem::SpreadAttribute(it) => visitor.visit_jsx_spread_attribute(it),
30723073
}
3073-
visitor.leave_node(kind);
30743074
}
30753075

30763076
#[inline]
30773077
pub fn walk_jsx_attribute<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXAttribute<'a>) {
3078-
// No `AstKind` for this type
3078+
let kind = AstKind::JSXAttribute(visitor.alloc(it));
3079+
visitor.enter_node(kind);
30793080
visitor.visit_span(&it.span);
30803081
visitor.visit_jsx_attribute_name(&it.name);
30813082
if let Some(value) = &it.value {
30823083
visitor.visit_jsx_attribute_value(value);
30833084
}
3085+
visitor.leave_node(kind);
30843086
}
30853087

30863088
#[inline]
@@ -3139,9 +3141,11 @@ pub mod walk {
31393141

31403142
#[inline]
31413143
pub fn walk_jsx_spread_child<'a, V: Visit<'a>>(visitor: &mut V, it: &JSXSpreadChild<'a>) {
3142-
// No `AstKind` for this type
3144+
let kind = AstKind::JSXSpreadChild(visitor.alloc(it));
3145+
visitor.enter_node(kind);
31433146
visitor.visit_span(&it.span);
31443147
visitor.visit_expression(&it.expression);
3148+
visitor.leave_node(kind);
31453149
}
31463150

31473151
#[inline]

0 commit comments

Comments
 (0)