Skip to content

Commit 5ca3d04

Browse files
committed
refactor(ast): add TSArrayType as AstKind (#11745)
1 parent abdbaa9 commit 5ca3d04

File tree

12 files changed

+88
-66
lines changed

12 files changed

+88
-66
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ impl AstKind<'_> {
331331
Self::TSUnknownKeyword(_) => "TSUnknownKeyword".into(),
332332
Self::TSInferType(_) => "TSInferType".into(),
333333
Self::TSTemplateLiteralType(_) => "TSTemplateLiteralType".into(),
334+
Self::TSArrayType(_) => "TSArrayType".into(),
334335

335336
Self::TSIndexedAccessType(_) => "TSIndexedAccessType".into(),
336337

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -134,58 +134,59 @@ pub enum AstType {
134134
TSUnionType = 118,
135135
TSIntersectionType = 119,
136136
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-
TSCallSignatureDeclaration = 147,
164-
TSMethodSignature = 148,
165-
TSConstructSignatureDeclaration = 149,
166-
TSIndexSignatureName = 150,
167-
TSInterfaceHeritage = 151,
168-
TSTypePredicate = 152,
169-
TSModuleDeclaration = 153,
170-
TSModuleBlock = 154,
171-
TSTypeLiteral = 155,
172-
TSInferType = 156,
173-
TSTypeQuery = 157,
174-
TSImportType = 158,
175-
TSMappedType = 159,
176-
TSTemplateLiteralType = 160,
177-
TSAsExpression = 161,
178-
TSSatisfiesExpression = 162,
179-
TSTypeAssertion = 163,
180-
TSImportEqualsDeclaration = 164,
181-
TSExternalModuleReference = 165,
182-
TSNonNullExpression = 166,
183-
Decorator = 167,
184-
TSExportAssignment = 168,
185-
TSInstantiationExpression = 169,
186-
JSDocNullableType = 170,
187-
JSDocNonNullableType = 171,
188-
JSDocUnknownType = 172,
137+
TSArrayType = 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+
TSCallSignatureDeclaration = 148,
165+
TSMethodSignature = 149,
166+
TSConstructSignatureDeclaration = 150,
167+
TSIndexSignatureName = 151,
168+
TSInterfaceHeritage = 152,
169+
TSTypePredicate = 153,
170+
TSModuleDeclaration = 154,
171+
TSModuleBlock = 155,
172+
TSTypeLiteral = 156,
173+
TSInferType = 157,
174+
TSTypeQuery = 158,
175+
TSImportType = 159,
176+
TSMappedType = 160,
177+
TSTemplateLiteralType = 161,
178+
TSAsExpression = 162,
179+
TSSatisfiesExpression = 163,
180+
TSTypeAssertion = 164,
181+
TSImportEqualsDeclaration = 165,
182+
TSExternalModuleReference = 166,
183+
TSNonNullExpression = 167,
184+
Decorator = 168,
185+
TSExportAssignment = 169,
186+
TSInstantiationExpression = 170,
187+
JSDocNullableType = 171,
188+
JSDocNonNullableType = 172,
189+
JSDocUnknownType = 173,
189190
}
190191

191192
/// Untyped AST Node Kind
@@ -320,6 +321,7 @@ pub enum AstKind<'a> {
320321
TSUnionType(&'a TSUnionType<'a>) = AstType::TSUnionType as u8,
321322
TSIntersectionType(&'a TSIntersectionType<'a>) = AstType::TSIntersectionType as u8,
322323
TSParenthesizedType(&'a TSParenthesizedType<'a>) = AstType::TSParenthesizedType as u8,
324+
TSArrayType(&'a TSArrayType<'a>) = AstType::TSArrayType as u8,
323325
TSIndexedAccessType(&'a TSIndexedAccessType<'a>) = AstType::TSIndexedAccessType as u8,
324326
TSNamedTupleMember(&'a TSNamedTupleMember<'a>) = AstType::TSNamedTupleMember as u8,
325327
TSAnyKeyword(&'a TSAnyKeyword) = AstType::TSAnyKeyword as u8,
@@ -517,6 +519,7 @@ impl GetSpan for AstKind<'_> {
517519
Self::TSUnionType(it) => it.span(),
518520
Self::TSIntersectionType(it) => it.span(),
519521
Self::TSParenthesizedType(it) => it.span(),
522+
Self::TSArrayType(it) => it.span(),
520523
Self::TSIndexedAccessType(it) => it.span(),
521524
Self::TSNamedTupleMember(it) => it.span(),
522525
Self::TSAnyKeyword(it) => it.span(),
@@ -1179,6 +1182,11 @@ impl<'a> AstKind<'a> {
11791182
if let Self::TSParenthesizedType(v) = self { Some(v) } else { None }
11801183
}
11811184

1185+
#[inline]
1186+
pub fn as_ts_array_type(self) -> Option<&'a TSArrayType<'a>> {
1187+
if let Self::TSArrayType(v) = self { Some(v) } else { None }
1188+
}
1189+
11821190
#[inline]
11831191
pub fn as_ts_indexed_access_type(self) -> Option<&'a TSIndexedAccessType<'a>> {
11841192
if let Self::TSIndexedAccessType(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
@@ -3339,9 +3339,11 @@ pub mod walk {
33393339

33403340
#[inline]
33413341
pub fn walk_ts_array_type<'a, V: Visit<'a>>(visitor: &mut V, it: &TSArrayType<'a>) {
3342-
// No `AstKind` for this type
3342+
let kind = AstKind::TSArrayType(visitor.alloc(it));
3343+
visitor.enter_node(kind);
33433344
visitor.visit_span(&it.span);
33443345
visitor.visit_ts_type(&it.element_type);
3346+
visitor.leave_node(kind);
33453347
}
33463348

33473349
#[inline]

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,9 +3506,11 @@ pub mod walk_mut {
35063506

35073507
#[inline]
35083508
pub fn walk_ts_array_type<'a, V: VisitMut<'a>>(visitor: &mut V, it: &mut TSArrayType<'a>) {
3509-
// No `AstType` for this type
3509+
let kind = AstType::TSArrayType;
3510+
visitor.enter_node(kind);
35103511
visitor.visit_span(&mut it.span);
35113512
visitor.visit_ts_type(&mut it.element_type);
3513+
visitor.leave_node(kind);
35123514
}
35133515

35143516
#[inline]

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub enum AstNodes<'a> {
148148
TSUnionType(&'a AstNode<'a, TSUnionType<'a>>),
149149
TSIntersectionType(&'a AstNode<'a, TSIntersectionType<'a>>),
150150
TSParenthesizedType(&'a AstNode<'a, TSParenthesizedType<'a>>),
151+
TSArrayType(&'a AstNode<'a, TSArrayType<'a>>),
151152
TSIndexedAccessType(&'a AstNode<'a, TSIndexedAccessType<'a>>),
152153
TSNamedTupleMember(&'a AstNode<'a, TSNamedTupleMember<'a>>),
153154
TSAnyKeyword(&'a AstNode<'a, TSAnyKeyword>),
@@ -327,6 +328,7 @@ impl<'a> AstNodes<'a> {
327328
Self::TSUnionType(n) => n.span(),
328329
Self::TSIntersectionType(n) => n.span(),
329330
Self::TSParenthesizedType(n) => n.span(),
331+
Self::TSArrayType(n) => n.span(),
330332
Self::TSIndexedAccessType(n) => n.span(),
331333
Self::TSNamedTupleMember(n) => n.span(),
332334
Self::TSAnyKeyword(n) => n.span(),
@@ -506,6 +508,7 @@ impl<'a> AstNodes<'a> {
506508
Self::TSUnionType(n) => n.parent,
507509
Self::TSIntersectionType(n) => n.parent,
508510
Self::TSParenthesizedType(n) => n.parent,
511+
Self::TSArrayType(n) => n.parent,
509512
Self::TSIndexedAccessType(n) => n.parent,
510513
Self::TSNamedTupleMember(n) => n.parent,
511514
Self::TSAnyKeyword(n) => n.parent,
@@ -685,6 +688,7 @@ impl<'a> AstNodes<'a> {
685688
Self::TSUnionType(_) => "TSUnionType",
686689
Self::TSIntersectionType(_) => "TSIntersectionType",
687690
Self::TSParenthesizedType(_) => "TSParenthesizedType",
691+
Self::TSArrayType(_) => "TSArrayType",
688692
Self::TSIndexedAccessType(_) => "TSIndexedAccessType",
689693
Self::TSNamedTupleMember(_) => "TSNamedTupleMember",
690694
Self::TSAnyKeyword(_) => "TSAnyKeyword",
@@ -5667,11 +5671,11 @@ impl<'a> AstNode<'a, TSType<'a>> {
56675671
parent,
56685672
allocator: self.allocator,
56695673
})),
5670-
TSType::TSArrayType(s) => {
5671-
panic!(
5672-
"No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`"
5673-
)
5674-
}
5674+
TSType::TSArrayType(s) => AstNodes::TSArrayType(self.allocator.alloc(AstNode {
5675+
inner: s.as_ref(),
5676+
parent,
5677+
allocator: self.allocator,
5678+
})),
56755679
TSType::TSConditionalType(s) => {
56765680
AstNodes::TSConditionalType(self.allocator.alloc(AstNode {
56775681
inner: s.as_ref(),
@@ -5938,7 +5942,7 @@ impl<'a> AstNode<'a, TSArrayType<'a>> {
59385942
self.allocator.alloc(AstNode {
59395943
inner: &self.inner.element_type,
59405944
allocator: self.allocator,
5941-
parent: self.parent,
5945+
parent: self.allocator.alloc(AstNodes::TSArrayType(transmute_self(self))),
59425946
})
59435947
}
59445948
}

crates/oxc_formatter/src/generated/format.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,10 @@ impl<'a> Format<'a> for AstNode<'a, TSTypeOperator<'a>> {
15161516

15171517
impl<'a> Format<'a> for AstNode<'a, TSArrayType<'a>> {
15181518
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
1519-
self.write(f)
1519+
format_leading_comments(self.span().start).fmt(f)?;
1520+
let result = self.write(f);
1521+
format_trailing_comments(self.span().end).fmt(f)?;
1522+
result
15201523
}
15211524
}
15221525

crates/oxc_linter/src/rules/typescript/array_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ impl Rule for ArrayType {
182182
&self.readonly.clone().unwrap_or_else(|| default_config.clone());
183183

184184
match node.kind() {
185+
AstKind::TSArrayType(ts_array_type) => {
186+
check(&ts_array_type.element_type, default_config, readonly_config, ctx);
187+
}
185188
AstKind::TSTypeAnnotation(ts_type_annotation) => {
186189
check(&ts_type_annotation.type_annotation, default_config, readonly_config, ctx);
187190
}

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-annotation/parameter-rest.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-annotation
4242
"flags": "ReferenceFlags(Type)",
4343
"id": 0,
4444
"name": "T",
45-
"node_id": 13
45+
"node_id": 14
4646
}
4747
]
4848
},

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/array-pattern.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2929
"flags": "ReferenceFlags(Type)",
3030
"id": 0,
3131
"name": "A",
32-
"node_id": 14
32+
"node_id": 15
3333
}
3434
]
3535
}

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/rest-element.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2929
"flags": "ReferenceFlags(Type)",
3030
"id": 0,
3131
"name": "A",
32-
"node_id": 13
32+
"node_id": 14
3333
}
3434
]
3535
}

0 commit comments

Comments
 (0)