Skip to content

Commit 3f50cef

Browse files
committed
refactor(ast): add AstKind for TSIndexSignature (#11966)
part of #11490
1 parent 03bce3f commit 3f50cef

File tree

11 files changed

+77
-45
lines changed

11 files changed

+77
-45
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

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

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

crates/oxc_ast/src/generated/ast_kind.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,35 @@ pub enum AstType {
171171
TSClassImplements = 154,
172172
TSInterfaceDeclaration = 155,
173173
TSPropertySignature = 156,
174-
TSCallSignatureDeclaration = 157,
175-
TSMethodSignature = 158,
176-
TSConstructSignatureDeclaration = 159,
177-
TSIndexSignatureName = 160,
178-
TSInterfaceHeritage = 161,
179-
TSTypePredicate = 162,
180-
TSModuleDeclaration = 163,
181-
TSModuleBlock = 164,
182-
TSTypeLiteral = 165,
183-
TSInferType = 166,
184-
TSTypeQuery = 167,
185-
TSImportType = 168,
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,
174+
TSIndexSignature = 157,
175+
TSCallSignatureDeclaration = 158,
176+
TSMethodSignature = 159,
177+
TSConstructSignatureDeclaration = 160,
178+
TSIndexSignatureName = 161,
179+
TSInterfaceHeritage = 162,
180+
TSTypePredicate = 163,
181+
TSModuleDeclaration = 164,
182+
TSModuleBlock = 165,
183+
TSTypeLiteral = 166,
184+
TSInferType = 167,
185+
TSTypeQuery = 168,
186+
TSImportType = 169,
187+
TSConstructorType = 170,
188+
TSMappedType = 171,
189+
TSTemplateLiteralType = 172,
190+
TSAsExpression = 173,
191+
TSSatisfiesExpression = 174,
192+
TSTypeAssertion = 175,
193+
TSImportEqualsDeclaration = 176,
194+
TSExternalModuleReference = 177,
195+
TSNonNullExpression = 178,
196+
Decorator = 179,
197+
TSExportAssignment = 180,
198+
TSNamespaceExportDeclaration = 181,
199+
TSInstantiationExpression = 182,
200+
JSDocNullableType = 183,
201+
JSDocNonNullableType = 184,
202+
JSDocUnknownType = 185,
202203
}
203204

204205
/// Untyped AST Node Kind
@@ -372,6 +373,7 @@ pub enum AstKind<'a> {
372373
TSClassImplements(&'a TSClassImplements<'a>) = AstType::TSClassImplements as u8,
373374
TSInterfaceDeclaration(&'a TSInterfaceDeclaration<'a>) = AstType::TSInterfaceDeclaration as u8,
374375
TSPropertySignature(&'a TSPropertySignature<'a>) = AstType::TSPropertySignature as u8,
376+
TSIndexSignature(&'a TSIndexSignature<'a>) = AstType::TSIndexSignature as u8,
375377
TSCallSignatureDeclaration(&'a TSCallSignatureDeclaration<'a>) =
376378
AstType::TSCallSignatureDeclaration as u8,
377379
TSMethodSignature(&'a TSMethodSignature<'a>) = AstType::TSMethodSignature as u8,
@@ -580,6 +582,7 @@ impl GetSpan for AstKind<'_> {
580582
Self::TSClassImplements(it) => it.span(),
581583
Self::TSInterfaceDeclaration(it) => it.span(),
582584
Self::TSPropertySignature(it) => it.span(),
585+
Self::TSIndexSignature(it) => it.span(),
583586
Self::TSCallSignatureDeclaration(it) => it.span(),
584587
Self::TSMethodSignature(it) => it.span(),
585588
Self::TSConstructSignatureDeclaration(it) => it.span(),
@@ -772,6 +775,7 @@ impl GetAddress for AstKind<'_> {
772775
Self::TSClassImplements(it) => Address::from_ptr(it),
773776
Self::TSInterfaceDeclaration(it) => Address::from_ptr(it),
774777
Self::TSPropertySignature(it) => Address::from_ptr(it),
778+
Self::TSIndexSignature(it) => Address::from_ptr(it),
775779
Self::TSCallSignatureDeclaration(it) => Address::from_ptr(it),
776780
Self::TSMethodSignature(it) => Address::from_ptr(it),
777781
Self::TSConstructSignatureDeclaration(it) => Address::from_ptr(it),
@@ -1592,6 +1596,11 @@ impl<'a> AstKind<'a> {
15921596
if let Self::TSPropertySignature(v) = self { Some(v) } else { None }
15931597
}
15941598

1599+
#[inline]
1600+
pub fn as_ts_index_signature(self) -> Option<&'a TSIndexSignature<'a>> {
1601+
if let Self::TSIndexSignature(v) = self { Some(v) } else { None }
1602+
}
1603+
15951604
#[inline]
15961605
pub fn as_ts_call_signature_declaration(self) -> Option<&'a TSCallSignatureDeclaration<'a>> {
15971606
if let Self::TSCallSignatureDeclaration(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
@@ -3694,10 +3694,12 @@ pub mod walk {
36943694

36953695
#[inline]
36963696
pub fn walk_ts_index_signature<'a, V: Visit<'a>>(visitor: &mut V, it: &TSIndexSignature<'a>) {
3697-
// No `AstKind` for this type
3697+
let kind = AstKind::TSIndexSignature(visitor.alloc(it));
3698+
visitor.enter_node(kind);
36983699
visitor.visit_span(&it.span);
36993700
visitor.visit_ts_index_signature_names(&it.parameters);
37003701
visitor.visit_ts_type_annotation(&it.type_annotation);
3702+
visitor.leave_node(kind);
37013703
}
37023704

37033705
#[inline]

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3897,10 +3897,12 @@ pub mod walk_mut {
38973897
visitor: &mut V,
38983898
it: &mut TSIndexSignature<'a>,
38993899
) {
3900-
// No `AstType` for this type
3900+
let kind = AstType::TSIndexSignature;
3901+
visitor.enter_node(kind);
39013902
visitor.visit_span(&mut it.span);
39023903
visitor.visit_ts_index_signature_names(&mut it.parameters);
39033904
visitor.visit_ts_type_annotation(&mut it.type_annotation);
3905+
visitor.leave_node(kind);
39043906
}
39053907

39063908
#[inline]

crates/oxc_formatter/src/generated/ast_nodes.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ pub enum AstNodes<'a> {
184184
TSClassImplements(&'a AstNode<'a, TSClassImplements<'a>>),
185185
TSInterfaceDeclaration(&'a AstNode<'a, TSInterfaceDeclaration<'a>>),
186186
TSPropertySignature(&'a AstNode<'a, TSPropertySignature<'a>>),
187+
TSIndexSignature(&'a AstNode<'a, TSIndexSignature<'a>>),
187188
TSCallSignatureDeclaration(&'a AstNode<'a, TSCallSignatureDeclaration<'a>>),
188189
TSMethodSignature(&'a AstNode<'a, TSMethodSignature<'a>>),
189190
TSConstructSignatureDeclaration(&'a AstNode<'a, TSConstructSignatureDeclaration<'a>>),
@@ -2469,6 +2470,7 @@ impl<'a> AstNodes<'a> {
24692470
Self::TSClassImplements(n) => n.span(),
24702471
Self::TSInterfaceDeclaration(n) => n.span(),
24712472
Self::TSPropertySignature(n) => n.span(),
2473+
Self::TSIndexSignature(n) => n.span(),
24722474
Self::TSCallSignatureDeclaration(n) => n.span(),
24732475
Self::TSMethodSignature(n) => n.span(),
24742476
Self::TSConstructSignatureDeclaration(n) => n.span(),
@@ -2660,6 +2662,7 @@ impl<'a> AstNodes<'a> {
26602662
Self::TSClassImplements(n) => n.parent,
26612663
Self::TSInterfaceDeclaration(n) => n.parent,
26622664
Self::TSPropertySignature(n) => n.parent,
2665+
Self::TSIndexSignature(n) => n.parent,
26632666
Self::TSCallSignatureDeclaration(n) => n.parent,
26642667
Self::TSMethodSignature(n) => n.parent,
26652668
Self::TSConstructSignatureDeclaration(n) => n.parent,
@@ -2851,6 +2854,7 @@ impl<'a> AstNodes<'a> {
28512854
Self::TSClassImplements(n) => SiblingNode::from(n.inner),
28522855
Self::TSInterfaceDeclaration(n) => SiblingNode::from(n.inner),
28532856
Self::TSPropertySignature(n) => SiblingNode::from(n.inner),
2857+
Self::TSIndexSignature(n) => SiblingNode::from(n.inner),
28542858
Self::TSCallSignatureDeclaration(n) => SiblingNode::from(n.inner),
28552859
Self::TSMethodSignature(n) => SiblingNode::from(n.inner),
28562860
Self::TSConstructSignatureDeclaration(n) => SiblingNode::from(n.inner),
@@ -3042,6 +3046,7 @@ impl<'a> AstNodes<'a> {
30423046
Self::TSClassImplements(_) => "TSClassImplements",
30433047
Self::TSInterfaceDeclaration(_) => "TSInterfaceDeclaration",
30443048
Self::TSPropertySignature(_) => "TSPropertySignature",
3049+
Self::TSIndexSignature(_) => "TSIndexSignature",
30453050
Self::TSCallSignatureDeclaration(_) => "TSCallSignatureDeclaration",
30463051
Self::TSMethodSignature(_) => "TSMethodSignature",
30473052
Self::TSConstructSignatureDeclaration(_) => "TSConstructSignatureDeclaration",
@@ -6787,9 +6792,12 @@ impl<'a> AstNode<'a, ClassElement<'a>> {
67876792
}))
67886793
}
67896794
ClassElement::TSIndexSignature(s) => {
6790-
panic!(
6791-
"No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`"
6792-
)
6795+
AstNodes::TSIndexSignature(self.allocator.alloc(AstNode {
6796+
inner: s.as_ref(),
6797+
parent,
6798+
allocator: self.allocator,
6799+
following_node: self.following_node,
6800+
}))
67936801
}
67946802
};
67956803
self.allocator.alloc(node)
@@ -9930,9 +9938,12 @@ impl<'a> AstNode<'a, TSSignature<'a>> {
99309938
let parent = self.parent;
99319939
let node = match self.inner {
99329940
TSSignature::TSIndexSignature(s) => {
9933-
panic!(
9934-
"No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`"
9935-
)
9941+
AstNodes::TSIndexSignature(self.allocator.alloc(AstNode {
9942+
inner: s.as_ref(),
9943+
parent,
9944+
allocator: self.allocator,
9945+
following_node: self.following_node,
9946+
}))
99369947
}
99379948
TSSignature::TSPropertySignature(s) => {
99389949
AstNodes::TSPropertySignature(self.allocator.alloc(AstNode {
@@ -9990,7 +10001,7 @@ impl<'a> AstNode<'a, TSIndexSignature<'a>> {
999010001
self.allocator.alloc(AstNode {
999110002
inner: &self.inner.parameters,
999210003
allocator: self.allocator,
9993-
parent: self.parent,
10004+
parent: self.allocator.alloc(AstNodes::TSIndexSignature(transmute_self(self))),
999410005
following_node,
999510006
})
999610007
}
@@ -10001,7 +10012,7 @@ impl<'a> AstNode<'a, TSIndexSignature<'a>> {
1000110012
self.allocator.alloc(AstNode {
1000210013
inner: self.inner.type_annotation.as_ref(),
1000310014
allocator: self.allocator,
10004-
parent: self.parent,
10015+
parent: self.allocator.alloc(AstNodes::TSIndexSignature(transmute_self(self))),
1000510016
following_node,
1000610017
})
1000710018
}

crates/oxc_formatter/src/generated/format.rs

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

25802580
impl<'a> Format<'a> for AstNode<'a, TSIndexSignature<'a>> {
25812581
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
2582-
self.write(f)
2582+
format_leading_comments(self.span).fmt(f)?;
2583+
let result = self.write(f);
2584+
format_trailing_comments(
2585+
&self.parent.as_sibling_node(),
2586+
&SiblingNode::from(self.inner),
2587+
self.following_node.as_ref(),
2588+
)
2589+
.fmt(f)?;
2590+
result
25832591
}
25842592
}
25852593

crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/index-signature.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati
3434
"flags": "ReferenceFlags(Type)",
3535
"id": 0,
3636
"name": "Bar",
37-
"node_id": 16
37+
"node_id": 18
3838
}
3939
]
4040
},
@@ -48,7 +48,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati
4848
"flags": "ReferenceFlags(Type)",
4949
"id": 1,
5050
"name": "Foo",
51-
"node_id": 25
51+
"node_id": 28
5252
}
5353
]
5454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
2323
"flags": "ReferenceFlags(Type)",
2424
"id": 1,
2525
"name": "I",
26-
"node_id": 20
26+
"node_id": 21
2727
}
2828
]
2929
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
3939
"flags": "ReferenceFlags(Type)",
4040
"id": 1,
4141
"name": "I",
42-
"node_id": 32
42+
"node_id": 33
4343
}
4444
]
4545
}

crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/signatures/index-sig.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio
3434
"flags": "ReferenceFlags(Type)",
3535
"id": 0,
3636
"name": "T",
37-
"node_id": 14
37+
"node_id": 15
3838
}
3939
]
4040
},

0 commit comments

Comments
 (0)