Skip to content

Commit 03bce3f

Browse files
committed
refactor(ast): add AstKind for TSConstructorType (#11965)
part of #11490
1 parent 46b59d8 commit 03bce3f

File tree

10 files changed

+60
-31
lines changed

10 files changed

+60
-31
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ impl AstKind<'_> {
404404
Self::TSMappedType(_) => "TSMappedType".into(),
405405
Self::TSConstructSignatureDeclaration(_) => "TSConstructSignatureDeclaration".into(),
406406
Self::TSExportAssignment(_) => "TSExportAssignment".into(),
407+
Self::TSConstructorType(_) => "TSConstructorType".into(),
407408
Self::V8IntrinsicExpression(_) => "V8IntrinsicExpression".into(),
408409

409410
Self::JSDocNullableType(_) => "JSDocNullableType".into(),

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,22 @@ pub enum AstType {
183183
TSInferType = 166,
184184
TSTypeQuery = 167,
185185
TSImportType = 168,
186-
TSMappedType = 169,
187-
TSTemplateLiteralType = 170,
188-
TSAsExpression = 171,
189-
TSSatisfiesExpression = 172,
190-
TSTypeAssertion = 173,
191-
TSImportEqualsDeclaration = 174,
192-
TSExternalModuleReference = 175,
193-
TSNonNullExpression = 176,
194-
Decorator = 177,
195-
TSExportAssignment = 178,
196-
TSNamespaceExportDeclaration = 179,
197-
TSInstantiationExpression = 180,
198-
JSDocNullableType = 181,
199-
JSDocNonNullableType = 182,
200-
JSDocUnknownType = 183,
186+
TSConstructorType = 169,
187+
TSMappedType = 170,
188+
TSTemplateLiteralType = 171,
189+
TSAsExpression = 172,
190+
TSSatisfiesExpression = 173,
191+
TSTypeAssertion = 174,
192+
TSImportEqualsDeclaration = 175,
193+
TSExternalModuleReference = 176,
194+
TSNonNullExpression = 177,
195+
Decorator = 178,
196+
TSExportAssignment = 179,
197+
TSNamespaceExportDeclaration = 180,
198+
TSInstantiationExpression = 181,
199+
JSDocNullableType = 182,
200+
JSDocNonNullableType = 183,
201+
JSDocUnknownType = 184,
201202
}
202203

203204
/// Untyped AST Node Kind
@@ -385,6 +386,7 @@ pub enum AstKind<'a> {
385386
TSInferType(&'a TSInferType<'a>) = AstType::TSInferType as u8,
386387
TSTypeQuery(&'a TSTypeQuery<'a>) = AstType::TSTypeQuery as u8,
387388
TSImportType(&'a TSImportType<'a>) = AstType::TSImportType as u8,
389+
TSConstructorType(&'a TSConstructorType<'a>) = AstType::TSConstructorType as u8,
388390
TSMappedType(&'a TSMappedType<'a>) = AstType::TSMappedType as u8,
389391
TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>) = AstType::TSTemplateLiteralType as u8,
390392
TSAsExpression(&'a TSAsExpression<'a>) = AstType::TSAsExpression as u8,
@@ -590,6 +592,7 @@ impl GetSpan for AstKind<'_> {
590592
Self::TSInferType(it) => it.span(),
591593
Self::TSTypeQuery(it) => it.span(),
592594
Self::TSImportType(it) => it.span(),
595+
Self::TSConstructorType(it) => it.span(),
593596
Self::TSMappedType(it) => it.span(),
594597
Self::TSTemplateLiteralType(it) => it.span(),
595598
Self::TSAsExpression(it) => it.span(),
@@ -781,6 +784,7 @@ impl GetAddress for AstKind<'_> {
781784
Self::TSInferType(it) => Address::from_ptr(it),
782785
Self::TSTypeQuery(it) => Address::from_ptr(it),
783786
Self::TSImportType(it) => Address::from_ptr(it),
787+
Self::TSConstructorType(it) => Address::from_ptr(it),
784788
Self::TSMappedType(it) => Address::from_ptr(it),
785789
Self::TSTemplateLiteralType(it) => Address::from_ptr(it),
786790
Self::TSAsExpression(it) => Address::from_ptr(it),
@@ -1650,6 +1654,11 @@ impl<'a> AstKind<'a> {
16501654
if let Self::TSImportType(v) = self { Some(v) } else { None }
16511655
}
16521656

1657+
#[inline]
1658+
pub fn as_ts_constructor_type(self) -> Option<&'a TSConstructorType<'a>> {
1659+
if let Self::TSConstructorType(v) = self { Some(v) } else { None }
1660+
}
1661+
16531662
#[inline]
16541663
pub fn as_ts_mapped_type(self) -> Option<&'a TSMappedType<'a>> {
16551664
if let Self::TSMappedType(v) = self { Some(v) } else { None }

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3954,13 +3954,15 @@ pub mod walk {
39543954

39553955
#[inline]
39563956
pub fn walk_ts_constructor_type<'a, V: Visit<'a>>(visitor: &mut V, it: &TSConstructorType<'a>) {
3957-
// No `AstKind` for this type
3957+
let kind = AstKind::TSConstructorType(visitor.alloc(it));
3958+
visitor.enter_node(kind);
39583959
visitor.visit_span(&it.span);
39593960
if let Some(type_parameters) = &it.type_parameters {
39603961
visitor.visit_ts_type_parameter_declaration(type_parameters);
39613962
}
39623963
visitor.visit_formal_parameters(&it.params);
39633964
visitor.visit_ts_type_annotation(&it.return_type);
3965+
visitor.leave_node(kind);
39643966
}
39653967

39663968
#[inline]

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4169,13 +4169,15 @@ pub mod walk_mut {
41694169
visitor: &mut V,
41704170
it: &mut TSConstructorType<'a>,
41714171
) {
4172-
// No `AstType` for this type
4172+
let kind = AstType::TSConstructorType;
4173+
visitor.enter_node(kind);
41734174
visitor.visit_span(&mut it.span);
41744175
if let Some(type_parameters) = &mut it.type_parameters {
41754176
visitor.visit_ts_type_parameter_declaration(type_parameters);
41764177
}
41774178
visitor.visit_formal_parameters(&mut it.params);
41784179
visitor.visit_ts_type_annotation(&mut it.return_type);
4180+
visitor.leave_node(kind);
41794181
}
41804182

41814183
#[inline]

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub enum AstNodes<'a> {
196196
TSInferType(&'a AstNode<'a, TSInferType<'a>>),
197197
TSTypeQuery(&'a AstNode<'a, TSTypeQuery<'a>>),
198198
TSImportType(&'a AstNode<'a, TSImportType<'a>>),
199+
TSConstructorType(&'a AstNode<'a, TSConstructorType<'a>>),
199200
TSMappedType(&'a AstNode<'a, TSMappedType<'a>>),
200201
TSTemplateLiteralType(&'a AstNode<'a, TSTemplateLiteralType<'a>>),
201202
TSAsExpression(&'a AstNode<'a, TSAsExpression<'a>>),
@@ -2480,6 +2481,7 @@ impl<'a> AstNodes<'a> {
24802481
Self::TSInferType(n) => n.span(),
24812482
Self::TSTypeQuery(n) => n.span(),
24822483
Self::TSImportType(n) => n.span(),
2484+
Self::TSConstructorType(n) => n.span(),
24832485
Self::TSMappedType(n) => n.span(),
24842486
Self::TSTemplateLiteralType(n) => n.span(),
24852487
Self::TSAsExpression(n) => n.span(),
@@ -2670,6 +2672,7 @@ impl<'a> AstNodes<'a> {
26702672
Self::TSInferType(n) => n.parent,
26712673
Self::TSTypeQuery(n) => n.parent,
26722674
Self::TSImportType(n) => n.parent,
2675+
Self::TSConstructorType(n) => n.parent,
26732676
Self::TSMappedType(n) => n.parent,
26742677
Self::TSTemplateLiteralType(n) => n.parent,
26752678
Self::TSAsExpression(n) => n.parent,
@@ -2860,6 +2863,7 @@ impl<'a> AstNodes<'a> {
28602863
Self::TSInferType(n) => SiblingNode::from(n.inner),
28612864
Self::TSTypeQuery(n) => SiblingNode::from(n.inner),
28622865
Self::TSImportType(n) => SiblingNode::from(n.inner),
2866+
Self::TSConstructorType(n) => SiblingNode::from(n.inner),
28632867
Self::TSMappedType(n) => SiblingNode::from(n.inner),
28642868
Self::TSTemplateLiteralType(n) => SiblingNode::from(n.inner),
28652869
Self::TSAsExpression(n) => SiblingNode::from(n.inner),
@@ -3050,6 +3054,7 @@ impl<'a> AstNodes<'a> {
30503054
Self::TSInferType(_) => "TSInferType",
30513055
Self::TSTypeQuery(_) => "TSTypeQuery",
30523056
Self::TSImportType(_) => "TSImportType",
3057+
Self::TSConstructorType(_) => "TSConstructorType",
30533058
Self::TSMappedType(_) => "TSMappedType",
30543059
Self::TSTemplateLiteralType(_) => "TSTemplateLiteralType",
30553060
Self::TSAsExpression(_) => "TSAsExpression",
@@ -8928,9 +8933,12 @@ impl<'a> AstNode<'a, TSType<'a>> {
89288933
}))
89298934
}
89308935
TSType::TSConstructorType(s) => {
8931-
panic!(
8932-
"No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`"
8933-
)
8936+
AstNodes::TSConstructorType(self.allocator.alloc(AstNode {
8937+
inner: s.as_ref(),
8938+
parent,
8939+
allocator: self.allocator,
8940+
following_node: self.following_node,
8941+
}))
89348942
}
89358943
TSType::TSFunctionType(s) => {
89368944
panic!(
@@ -10753,7 +10761,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> {
1075310761
.alloc(self.inner.type_parameters.as_ref().map(|inner| AstNode {
1075410762
inner: inner.as_ref(),
1075510763
allocator: self.allocator,
10756-
parent: self.parent,
10764+
parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))),
1075710765
following_node,
1075810766
}))
1075910767
.as_ref()
@@ -10765,7 +10773,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> {
1076510773
self.allocator.alloc(AstNode {
1076610774
inner: self.inner.params.as_ref(),
1076710775
allocator: self.allocator,
10768-
parent: self.parent,
10776+
parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))),
1076910777
following_node,
1077010778
})
1077110779
}
@@ -10776,7 +10784,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> {
1077610784
self.allocator.alloc(AstNode {
1077710785
inner: self.inner.return_type.as_ref(),
1077810786
allocator: self.allocator,
10779-
parent: self.parent,
10787+
parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))),
1078010788
following_node,
1078110789
})
1078210790
}

crates/oxc_formatter/src/generated/format.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,15 @@ impl<'a> Format<'a> for AstNode<'a, TSFunctionType<'a>> {
27832783

27842784
impl<'a> Format<'a> for AstNode<'a, TSConstructorType<'a>> {
27852785
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
2786-
self.write(f)
2786+
format_leading_comments(self.span).fmt(f)?;
2787+
let result = self.write(f);
2788+
format_trailing_comments(
2789+
&self.parent.as_sibling_node(),
2790+
&SiblingNode::from(self.inner),
2791+
self.following_node.as_ref(),
2792+
)
2793+
.fmt(f)?;
2794+
result
27872795
}
27882796
}
27892797

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics1.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2828
"flags": "ReferenceFlags(Type)",
2929
"id": 0,
3030
"name": "U",
31-
"node_id": 16
31+
"node_id": 17
3232
}
3333
]
3434
}
@@ -49,7 +49,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
4949
"flags": "ReferenceFlags(Type)",
5050
"id": 1,
5151
"name": "T",
52-
"node_id": 20
52+
"node_id": 21
5353
}
5454
]
5555
},

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics2.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2828
"flags": "ReferenceFlags(Type)",
2929
"id": 0,
3030
"name": "U",
31-
"node_id": 16
31+
"node_id": 17
3232
}
3333
]
3434
}
@@ -49,7 +49,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
4949
"flags": "ReferenceFlags(Type)",
5050
"id": 1,
5151
"name": "T",
52-
"node_id": 20
52+
"node_id": 21
5353
}
5454
]
5555
},

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
3434
"flags": "ReferenceFlags(Type)",
3535
"id": 0,
3636
"name": "T",
37-
"node_id": 13
37+
"node_id": 14
3838
},
3939
{
4040
"flags": "ReferenceFlags(Type)",
4141
"id": 1,
4242
"name": "T",
43-
"node_id": 17
43+
"node_id": 18
4444
}
4545
]
4646
},

tasks/ast_tools/src/generators/ast_kind.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const STRUCTS_BLACK_LIST: &[&str] = &[
3535
"TSInterfaceBody",
3636
"TSIndexSignature",
3737
"TSFunctionType",
38-
"TSConstructorType",
3938
"Span",
4039
];
4140

0 commit comments

Comments
 (0)