Skip to content

Commit 8e30c5f

Browse files
authored
refactor(ast): don't generate AstKind for ForStatementInit (#11617)
Part of #11490 - two lint rules needed change - formatter needed change (conformance went slightly up)
1 parent be5e0f7 commit 8e30c5f

File tree

12 files changed

+169
-153
lines changed

12 files changed

+169
-153
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ impl AstKind<'_> {
164164
Self::ForInStatement(_) => "ForInStatement".into(),
165165
Self::ForOfStatement(_) => "ForOfStatement".into(),
166166
Self::ForStatement(_) => "ForStatement".into(),
167-
Self::ForStatementInit(_) => "ForStatementInit".into(),
168167
Self::IfStatement(_) => "IfStatement".into(),
169168
Self::LabeledStatement(l) => format!("LabeledStatement({})", l.label.name).into(),
170169
Self::ReturnStatement(_) => "ReturnStatement".into(),

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 122 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -62,129 +62,128 @@ pub enum AstType {
6262
DoWhileStatement = 46,
6363
WhileStatement = 47,
6464
ForStatement = 48,
65-
ForStatementInit = 49,
66-
ForInStatement = 50,
67-
ForOfStatement = 51,
68-
ContinueStatement = 52,
69-
BreakStatement = 53,
70-
ReturnStatement = 54,
71-
WithStatement = 55,
72-
SwitchStatement = 56,
73-
SwitchCase = 57,
74-
LabeledStatement = 58,
75-
ThrowStatement = 59,
76-
TryStatement = 60,
77-
CatchClause = 61,
78-
CatchParameter = 62,
79-
DebuggerStatement = 63,
80-
AssignmentPattern = 64,
81-
ObjectPattern = 65,
82-
ArrayPattern = 66,
83-
BindingRestElement = 67,
84-
Function = 68,
85-
FormalParameters = 69,
86-
FormalParameter = 70,
87-
FunctionBody = 71,
88-
ArrowFunctionExpression = 72,
89-
YieldExpression = 73,
90-
Class = 74,
91-
ClassBody = 75,
92-
MethodDefinition = 76,
93-
PropertyDefinition = 77,
94-
PrivateIdentifier = 78,
95-
StaticBlock = 79,
96-
ModuleDeclaration = 80,
97-
ImportExpression = 81,
98-
ImportDeclaration = 82,
99-
ImportSpecifier = 83,
100-
ImportDefaultSpecifier = 84,
101-
ImportNamespaceSpecifier = 85,
102-
ExportNamedDeclaration = 86,
103-
ExportDefaultDeclaration = 87,
104-
ExportAllDeclaration = 88,
105-
ExportSpecifier = 89,
106-
V8IntrinsicExpression = 90,
107-
BooleanLiteral = 91,
108-
NullLiteral = 92,
109-
NumericLiteral = 93,
110-
StringLiteral = 94,
111-
BigIntLiteral = 95,
112-
RegExpLiteral = 96,
113-
JSXElement = 97,
114-
JSXOpeningElement = 98,
115-
JSXClosingElement = 99,
116-
JSXFragment = 100,
117-
JSXOpeningFragment = 101,
118-
JSXClosingFragment = 102,
119-
JSXNamespacedName = 103,
120-
JSXMemberExpression = 104,
121-
JSXExpressionContainer = 105,
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,
185-
JSDocNullableType = 169,
186-
JSDocNonNullableType = 170,
187-
JSDocUnknownType = 171,
65+
ForInStatement = 49,
66+
ForOfStatement = 50,
67+
ContinueStatement = 51,
68+
BreakStatement = 52,
69+
ReturnStatement = 53,
70+
WithStatement = 54,
71+
SwitchStatement = 55,
72+
SwitchCase = 56,
73+
LabeledStatement = 57,
74+
ThrowStatement = 58,
75+
TryStatement = 59,
76+
CatchClause = 60,
77+
CatchParameter = 61,
78+
DebuggerStatement = 62,
79+
AssignmentPattern = 63,
80+
ObjectPattern = 64,
81+
ArrayPattern = 65,
82+
BindingRestElement = 66,
83+
Function = 67,
84+
FormalParameters = 68,
85+
FormalParameter = 69,
86+
FunctionBody = 70,
87+
ArrowFunctionExpression = 71,
88+
YieldExpression = 72,
89+
Class = 73,
90+
ClassBody = 74,
91+
MethodDefinition = 75,
92+
PropertyDefinition = 76,
93+
PrivateIdentifier = 77,
94+
StaticBlock = 78,
95+
ModuleDeclaration = 79,
96+
ImportExpression = 80,
97+
ImportDeclaration = 81,
98+
ImportSpecifier = 82,
99+
ImportDefaultSpecifier = 83,
100+
ImportNamespaceSpecifier = 84,
101+
ExportNamedDeclaration = 85,
102+
ExportDefaultDeclaration = 86,
103+
ExportAllDeclaration = 87,
104+
ExportSpecifier = 88,
105+
V8IntrinsicExpression = 89,
106+
BooleanLiteral = 90,
107+
NullLiteral = 91,
108+
NumericLiteral = 92,
109+
StringLiteral = 93,
110+
BigIntLiteral = 94,
111+
RegExpLiteral = 95,
112+
JSXElement = 96,
113+
JSXOpeningElement = 97,
114+
JSXClosingElement = 98,
115+
JSXFragment = 99,
116+
JSXOpeningFragment = 100,
117+
JSXClosingFragment = 101,
118+
JSXNamespacedName = 102,
119+
JSXMemberExpression = 103,
120+
JSXExpressionContainer = 104,
121+
JSXEmptyExpression = 105,
122+
JSXAttribute = 106,
123+
JSXSpreadAttribute = 107,
124+
JSXIdentifier = 108,
125+
JSXSpreadChild = 109,
126+
JSXText = 110,
127+
TSThisParameter = 111,
128+
TSEnumDeclaration = 112,
129+
TSEnumBody = 113,
130+
TSEnumMember = 114,
131+
TSTypeAnnotation = 115,
132+
TSLiteralType = 116,
133+
TSConditionalType = 117,
134+
TSUnionType = 118,
135+
TSIntersectionType = 119,
136+
TSParenthesizedType = 120,
137+
TSIndexedAccessType = 121,
138+
TSNamedTupleMember = 122,
139+
TSAnyKeyword = 123,
140+
TSStringKeyword = 124,
141+
TSBooleanKeyword = 125,
142+
TSNumberKeyword = 126,
143+
TSNeverKeyword = 127,
144+
TSIntrinsicKeyword = 128,
145+
TSUnknownKeyword = 129,
146+
TSNullKeyword = 130,
147+
TSUndefinedKeyword = 131,
148+
TSVoidKeyword = 132,
149+
TSSymbolKeyword = 133,
150+
TSThisType = 134,
151+
TSObjectKeyword = 135,
152+
TSBigIntKeyword = 136,
153+
TSTypeReference = 137,
154+
TSTypeName = 138,
155+
TSQualifiedName = 139,
156+
TSTypeParameterInstantiation = 140,
157+
TSTypeParameter = 141,
158+
TSTypeParameterDeclaration = 142,
159+
TSTypeAliasDeclaration = 143,
160+
TSClassImplements = 144,
161+
TSInterfaceDeclaration = 145,
162+
TSPropertySignature = 146,
163+
TSMethodSignature = 147,
164+
TSConstructSignatureDeclaration = 148,
165+
TSInterfaceHeritage = 149,
166+
TSModuleDeclaration = 150,
167+
TSModuleBlock = 151,
168+
TSTypeLiteral = 152,
169+
TSInferType = 153,
170+
TSTypeQuery = 154,
171+
TSImportType = 155,
172+
TSMappedType = 156,
173+
TSTemplateLiteralType = 157,
174+
TSAsExpression = 158,
175+
TSSatisfiesExpression = 159,
176+
TSTypeAssertion = 160,
177+
TSImportEqualsDeclaration = 161,
178+
TSModuleReference = 162,
179+
TSExternalModuleReference = 163,
180+
TSNonNullExpression = 164,
181+
Decorator = 165,
182+
TSExportAssignment = 166,
183+
TSInstantiationExpression = 167,
184+
JSDocNullableType = 168,
185+
JSDocNonNullableType = 169,
186+
JSDocUnknownType = 170,
188187
}
189188

190189
/// Untyped AST Node Kind
@@ -244,7 +243,6 @@ pub enum AstKind<'a> {
244243
DoWhileStatement(&'a DoWhileStatement<'a>) = AstType::DoWhileStatement as u8,
245244
WhileStatement(&'a WhileStatement<'a>) = AstType::WhileStatement as u8,
246245
ForStatement(&'a ForStatement<'a>) = AstType::ForStatement as u8,
247-
ForStatementInit(&'a ForStatementInit<'a>) = AstType::ForStatementInit as u8,
248246
ForInStatement(&'a ForInStatement<'a>) = AstType::ForInStatement as u8,
249247
ForOfStatement(&'a ForOfStatement<'a>) = AstType::ForOfStatement as u8,
250248
ContinueStatement(&'a ContinueStatement<'a>) = AstType::ContinueStatement as u8,
@@ -442,7 +440,6 @@ impl GetSpan for AstKind<'_> {
442440
Self::DoWhileStatement(it) => it.span(),
443441
Self::WhileStatement(it) => it.span(),
444442
Self::ForStatement(it) => it.span(),
445-
Self::ForStatementInit(it) => it.span(),
446443
Self::ForInStatement(it) => it.span(),
447444
Self::ForOfStatement(it) => it.span(),
448445
Self::ContinueStatement(it) => it.span(),
@@ -815,11 +812,6 @@ impl<'a> AstKind<'a> {
815812
if let Self::ForStatement(v) = self { Some(v) } else { None }
816813
}
817814

818-
#[inline]
819-
pub fn as_for_statement_init(self) -> Option<&'a ForStatementInit<'a>> {
820-
if let Self::ForStatementInit(v) = self { Some(v) } else { None }
821-
}
822-
823815
#[inline]
824816
pub fn as_for_in_statement(self) -> Option<&'a ForInStatement<'a>> {
825817
if let Self::ForInStatement(v) = self { Some(v) } else { None }

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,13 +2161,11 @@ pub mod walk {
21612161

21622162
#[inline]
21632163
pub fn walk_for_statement_init<'a, V: Visit<'a>>(visitor: &mut V, it: &ForStatementInit<'a>) {
2164-
let kind = AstKind::ForStatementInit(visitor.alloc(it));
2165-
visitor.enter_node(kind);
2164+
// No `AstKind` for this type
21662165
match it {
21672166
ForStatementInit::VariableDeclaration(it) => visitor.visit_variable_declaration(it),
21682167
match_expression!(ForStatementInit) => visitor.visit_expression(it.to_expression()),
21692168
}
2170-
visitor.leave_node(kind);
21712169
}
21722170

21732171
#[inline]

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,13 +2204,11 @@ pub mod walk_mut {
22042204
visitor: &mut V,
22052205
it: &mut ForStatementInit<'a>,
22062206
) {
2207-
let kind = AstType::ForStatementInit;
2208-
visitor.enter_node(kind);
2207+
// No `AstType` for this type
22092208
match it {
22102209
ForStatementInit::VariableDeclaration(it) => visitor.visit_variable_declaration(it),
22112210
match_expression!(ForStatementInit) => visitor.visit_expression(it.to_expression_mut()),
22122211
}
2213-
visitor.leave_node(kind);
22142212
}
22152213

22162214
#[inline]

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub enum AstNodes<'a> {
7676
DoWhileStatement(&'a AstNode<'a, DoWhileStatement<'a>>),
7777
WhileStatement(&'a AstNode<'a, WhileStatement<'a>>),
7878
ForStatement(&'a AstNode<'a, ForStatement<'a>>),
79-
ForStatementInit(&'a AstNode<'a, ForStatementInit<'a>>),
8079
ForInStatement(&'a AstNode<'a, ForInStatement<'a>>),
8180
ForOfStatement(&'a AstNode<'a, ForOfStatement<'a>>),
8281
ContinueStatement(&'a AstNode<'a, ContinueStatement<'a>>),
@@ -254,7 +253,6 @@ impl<'a> AstNodes<'a> {
254253
Self::DoWhileStatement(n) => n.span(),
255254
Self::WhileStatement(n) => n.span(),
256255
Self::ForStatement(n) => n.span(),
257-
Self::ForStatementInit(n) => n.span(),
258256
Self::ForInStatement(n) => n.span(),
259257
Self::ForOfStatement(n) => n.span(),
260258
Self::ContinueStatement(n) => n.span(),
@@ -432,7 +430,6 @@ impl<'a> AstNodes<'a> {
432430
Self::DoWhileStatement(n) => n.parent,
433431
Self::WhileStatement(n) => n.parent,
434432
Self::ForStatement(n) => n.parent,
435-
Self::ForStatementInit(n) => n.parent,
436433
Self::ForInStatement(n) => n.parent,
437434
Self::ForOfStatement(n) => n.parent,
438435
Self::ContinueStatement(n) => n.parent,
@@ -610,7 +607,6 @@ impl<'a> AstNodes<'a> {
610607
Self::DoWhileStatement(_) => "DoWhileStatement",
611608
Self::WhileStatement(_) => "WhileStatement",
612609
Self::ForStatement(_) => "ForStatement",
613-
Self::ForStatementInit(_) => "ForStatementInit",
614610
Self::ForInStatement(_) => "ForInStatement",
615611
Self::ForOfStatement(_) => "ForOfStatement",
616612
Self::ContinueStatement(_) => "ContinueStatement",
@@ -2874,7 +2870,7 @@ impl<'a> AstNode<'a, ForStatement<'a>> {
28742870
impl<'a> AstNode<'a, ForStatementInit<'a>> {
28752871
#[inline]
28762872
pub fn as_ast_nodes(&self) -> &AstNodes<'a> {
2877-
let parent = self.allocator.alloc(AstNodes::ForStatementInit(transmute_self(self)));
2873+
let parent = self.parent;
28782874
let node = match self.inner {
28792875
ForStatementInit::VariableDeclaration(s) => {
28802876
AstNodes::VariableDeclaration(self.allocator.alloc(AstNode {

crates/oxc_formatter/src/generated/format_write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ForStatementInit<'a>> {
472472
#[inline]
473473
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
474474
let allocator = self.allocator;
475-
let parent = allocator.alloc(AstNodes::ForStatementInit(transmute_self(self)));
475+
let parent = self.parent;
476476
match self.inner {
477477
ForStatementInit::VariableDeclaration(inner) => {
478478
allocator.alloc(AstNode::<VariableDeclaration> { inner, parent, allocator }).fmt(f)

crates/oxc_formatter/src/write/variable_declaration.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use super::FormatWrite;
2020
impl<'a> FormatWrite<'a> for AstNode<'a, VariableDeclaration<'a>> {
2121
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
2222
let semicolon = match self.parent {
23-
AstNodes::ForStatementInit(_) | AstNodes::ExportNamedDeclaration(_) => false,
23+
AstNodes::ExportNamedDeclaration(_) => false,
24+
AstNodes::ForStatement(stmt) => {
25+
stmt.init().is_some_and(|init| init.span() != self.span())
26+
}
2427
// TODO: It would be better if there is a AstNodes which is `left` of `ForInStatement` and `ForOfStatement`.
2528
AstNodes::ForInStatement(stmt) => stmt.left().span() != self.span(),
2629
AstNodes::ForOfStatement(stmt) => stmt.left().span() != self.span(),

0 commit comments

Comments
 (0)