From 9f6784ab3c02f149d5124855b228f82d14331dfd Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sun, 29 Jun 2025 17:24:10 +0000 Subject: [PATCH] refactor(ast): add `AstKind` for `TSInterfaceBody` (#11967) part of #11490 --- crates/oxc_ast/src/ast_kind_impl.rs | 1 + crates/oxc_ast/src/generated/ast_kind.rs | 69 +++++++++++-------- crates/oxc_ast_visit/src/generated/visit.rs | 4 +- .../oxc_ast_visit/src/generated/visit_mut.rs | 4 +- .../oxc_formatter/src/generated/ast_nodes.rs | 7 +- crates/oxc_formatter/src/generated/format.rs | 10 ++- .../ts/exports/named/interface-heritage.snap | 2 +- .../interfaces/property-with-type-import.snap | 8 +-- .../class/declaration/implements-generic.snap | 4 +- .../class/declaration/implements.snap | 2 +- .../export/equals3-type.snap | 2 +- .../jsx/namespaced-attribute.snap | 4 +- .../type-declaration/interface-heritage1.snap | 2 +- .../type-declaration/interface2.snap | 2 +- .../interface/body-reference.snap | 2 +- .../interface/extends-reference.snap | 4 +- tasks/ast_tools/src/generators/ast_kind.rs | 1 - 17 files changed, 77 insertions(+), 51 deletions(-) diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index cb934915d15b9..b9111dc1da00b 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -405,6 +405,7 @@ impl AstKind<'_> { Self::TSConstructSignatureDeclaration(_) => "TSConstructSignatureDeclaration".into(), Self::TSExportAssignment(_) => "TSExportAssignment".into(), Self::TSConstructorType(_) => "TSConstructorType".into(), + Self::TSInterfaceBody(_) => "TSInterfaceBody".into(), Self::TSIndexSignature(_) => "TSIndexSignature".into(), Self::V8IntrinsicExpression(_) => "V8IntrinsicExpression".into(), diff --git a/crates/oxc_ast/src/generated/ast_kind.rs b/crates/oxc_ast/src/generated/ast_kind.rs index 9c7d716dd22ea..e770da8ce352d 100644 --- a/crates/oxc_ast/src/generated/ast_kind.rs +++ b/crates/oxc_ast/src/generated/ast_kind.rs @@ -170,36 +170,37 @@ pub enum AstType { TSTypeAliasDeclaration = 153, TSClassImplements = 154, TSInterfaceDeclaration = 155, - TSPropertySignature = 156, - TSIndexSignature = 157, - TSCallSignatureDeclaration = 158, - TSMethodSignature = 159, - TSConstructSignatureDeclaration = 160, - TSIndexSignatureName = 161, - TSInterfaceHeritage = 162, - TSTypePredicate = 163, - TSModuleDeclaration = 164, - TSModuleBlock = 165, - TSTypeLiteral = 166, - TSInferType = 167, - TSTypeQuery = 168, - TSImportType = 169, - TSConstructorType = 170, - TSMappedType = 171, - TSTemplateLiteralType = 172, - TSAsExpression = 173, - TSSatisfiesExpression = 174, - TSTypeAssertion = 175, - TSImportEqualsDeclaration = 176, - TSExternalModuleReference = 177, - TSNonNullExpression = 178, - Decorator = 179, - TSExportAssignment = 180, - TSNamespaceExportDeclaration = 181, - TSInstantiationExpression = 182, - JSDocNullableType = 183, - JSDocNonNullableType = 184, - JSDocUnknownType = 185, + TSInterfaceBody = 156, + TSPropertySignature = 157, + TSIndexSignature = 158, + TSCallSignatureDeclaration = 159, + TSMethodSignature = 160, + TSConstructSignatureDeclaration = 161, + TSIndexSignatureName = 162, + TSInterfaceHeritage = 163, + TSTypePredicate = 164, + TSModuleDeclaration = 165, + TSModuleBlock = 166, + TSTypeLiteral = 167, + TSInferType = 168, + TSTypeQuery = 169, + TSImportType = 170, + TSConstructorType = 171, + TSMappedType = 172, + TSTemplateLiteralType = 173, + TSAsExpression = 174, + TSSatisfiesExpression = 175, + TSTypeAssertion = 176, + TSImportEqualsDeclaration = 177, + TSExternalModuleReference = 178, + TSNonNullExpression = 179, + Decorator = 180, + TSExportAssignment = 181, + TSNamespaceExportDeclaration = 182, + TSInstantiationExpression = 183, + JSDocNullableType = 184, + JSDocNonNullableType = 185, + JSDocUnknownType = 186, } /// Untyped AST Node Kind @@ -372,6 +373,7 @@ pub enum AstKind<'a> { TSTypeAliasDeclaration(&'a TSTypeAliasDeclaration<'a>) = AstType::TSTypeAliasDeclaration as u8, TSClassImplements(&'a TSClassImplements<'a>) = AstType::TSClassImplements as u8, TSInterfaceDeclaration(&'a TSInterfaceDeclaration<'a>) = AstType::TSInterfaceDeclaration as u8, + TSInterfaceBody(&'a TSInterfaceBody<'a>) = AstType::TSInterfaceBody as u8, TSPropertySignature(&'a TSPropertySignature<'a>) = AstType::TSPropertySignature as u8, TSIndexSignature(&'a TSIndexSignature<'a>) = AstType::TSIndexSignature as u8, TSCallSignatureDeclaration(&'a TSCallSignatureDeclaration<'a>) = @@ -581,6 +583,7 @@ impl GetSpan for AstKind<'_> { Self::TSTypeAliasDeclaration(it) => it.span(), Self::TSClassImplements(it) => it.span(), Self::TSInterfaceDeclaration(it) => it.span(), + Self::TSInterfaceBody(it) => it.span(), Self::TSPropertySignature(it) => it.span(), Self::TSIndexSignature(it) => it.span(), Self::TSCallSignatureDeclaration(it) => it.span(), @@ -774,6 +777,7 @@ impl GetAddress for AstKind<'_> { Self::TSTypeAliasDeclaration(it) => Address::from_ptr(it), Self::TSClassImplements(it) => Address::from_ptr(it), Self::TSInterfaceDeclaration(it) => Address::from_ptr(it), + Self::TSInterfaceBody(it) => Address::from_ptr(it), Self::TSPropertySignature(it) => Address::from_ptr(it), Self::TSIndexSignature(it) => Address::from_ptr(it), Self::TSCallSignatureDeclaration(it) => Address::from_ptr(it), @@ -1591,6 +1595,11 @@ impl<'a> AstKind<'a> { if let Self::TSInterfaceDeclaration(v) = self { Some(v) } else { None } } + #[inline] + pub fn as_ts_interface_body(self) -> Option<&'a TSInterfaceBody<'a>> { + if let Self::TSInterfaceBody(v) = self { Some(v) } else { None } + } + #[inline] pub fn as_ts_property_signature(self) -> Option<&'a TSPropertySignature<'a>> { if let Self::TSPropertySignature(v) = self { Some(v) } else { None } diff --git a/crates/oxc_ast_visit/src/generated/visit.rs b/crates/oxc_ast_visit/src/generated/visit.rs index 32f76ec62e410..ebed981503b26 100644 --- a/crates/oxc_ast_visit/src/generated/visit.rs +++ b/crates/oxc_ast_visit/src/generated/visit.rs @@ -3656,9 +3656,11 @@ pub mod walk { #[inline] pub fn walk_ts_interface_body<'a, V: Visit<'a>>(visitor: &mut V, it: &TSInterfaceBody<'a>) { - // No `AstKind` for this type + let kind = AstKind::TSInterfaceBody(visitor.alloc(it)); + visitor.enter_node(kind); visitor.visit_span(&it.span); visitor.visit_ts_signatures(&it.body); + visitor.leave_node(kind); } #[inline] diff --git a/crates/oxc_ast_visit/src/generated/visit_mut.rs b/crates/oxc_ast_visit/src/generated/visit_mut.rs index 0dad4da1d823a..92d78ef820129 100644 --- a/crates/oxc_ast_visit/src/generated/visit_mut.rs +++ b/crates/oxc_ast_visit/src/generated/visit_mut.rs @@ -3856,9 +3856,11 @@ pub mod walk_mut { visitor: &mut V, it: &mut TSInterfaceBody<'a>, ) { - // No `AstType` for this type + let kind = AstType::TSInterfaceBody; + visitor.enter_node(kind); visitor.visit_span(&mut it.span); visitor.visit_ts_signatures(&mut it.body); + visitor.leave_node(kind); } #[inline] diff --git a/crates/oxc_formatter/src/generated/ast_nodes.rs b/crates/oxc_formatter/src/generated/ast_nodes.rs index 5db6543ba95ac..84a3a91e266fe 100644 --- a/crates/oxc_formatter/src/generated/ast_nodes.rs +++ b/crates/oxc_formatter/src/generated/ast_nodes.rs @@ -183,6 +183,7 @@ pub enum AstNodes<'a> { TSTypeAliasDeclaration(&'a AstNode<'a, TSTypeAliasDeclaration<'a>>), TSClassImplements(&'a AstNode<'a, TSClassImplements<'a>>), TSInterfaceDeclaration(&'a AstNode<'a, TSInterfaceDeclaration<'a>>), + TSInterfaceBody(&'a AstNode<'a, TSInterfaceBody<'a>>), TSPropertySignature(&'a AstNode<'a, TSPropertySignature<'a>>), TSIndexSignature(&'a AstNode<'a, TSIndexSignature<'a>>), TSCallSignatureDeclaration(&'a AstNode<'a, TSCallSignatureDeclaration<'a>>), @@ -2469,6 +2470,7 @@ impl<'a> AstNodes<'a> { Self::TSTypeAliasDeclaration(n) => n.span(), Self::TSClassImplements(n) => n.span(), Self::TSInterfaceDeclaration(n) => n.span(), + Self::TSInterfaceBody(n) => n.span(), Self::TSPropertySignature(n) => n.span(), Self::TSIndexSignature(n) => n.span(), Self::TSCallSignatureDeclaration(n) => n.span(), @@ -2661,6 +2663,7 @@ impl<'a> AstNodes<'a> { Self::TSTypeAliasDeclaration(n) => n.parent, Self::TSClassImplements(n) => n.parent, Self::TSInterfaceDeclaration(n) => n.parent, + Self::TSInterfaceBody(n) => n.parent, Self::TSPropertySignature(n) => n.parent, Self::TSIndexSignature(n) => n.parent, Self::TSCallSignatureDeclaration(n) => n.parent, @@ -2853,6 +2856,7 @@ impl<'a> AstNodes<'a> { Self::TSTypeAliasDeclaration(n) => SiblingNode::from(n.inner), Self::TSClassImplements(n) => SiblingNode::from(n.inner), Self::TSInterfaceDeclaration(n) => SiblingNode::from(n.inner), + Self::TSInterfaceBody(n) => SiblingNode::from(n.inner), Self::TSPropertySignature(n) => SiblingNode::from(n.inner), Self::TSIndexSignature(n) => SiblingNode::from(n.inner), Self::TSCallSignatureDeclaration(n) => SiblingNode::from(n.inner), @@ -3045,6 +3049,7 @@ impl<'a> AstNodes<'a> { Self::TSTypeAliasDeclaration(_) => "TSTypeAliasDeclaration", Self::TSClassImplements(_) => "TSClassImplements", Self::TSInterfaceDeclaration(_) => "TSInterfaceDeclaration", + Self::TSInterfaceBody(_) => "TSInterfaceBody", Self::TSPropertySignature(_) => "TSPropertySignature", Self::TSIndexSignature(_) => "TSIndexSignature", Self::TSCallSignatureDeclaration(_) => "TSCallSignatureDeclaration", @@ -9879,7 +9884,7 @@ impl<'a> AstNode<'a, TSInterfaceBody<'a>> { self.allocator.alloc(AstNode { inner: &self.inner.body, allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSInterfaceBody(transmute_self(self))), following_node, }) } diff --git a/crates/oxc_formatter/src/generated/format.rs b/crates/oxc_formatter/src/generated/format.rs index 7bd6e2fa388e5..d06acc8ed1fe3 100644 --- a/crates/oxc_formatter/src/generated/format.rs +++ b/crates/oxc_formatter/src/generated/format.rs @@ -2553,7 +2553,15 @@ impl<'a> Format<'a> for AstNode<'a, TSInterfaceDeclaration<'a>> { impl<'a> Format<'a> for AstNode<'a, TSInterfaceBody<'a>> { fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - self.write(f) + format_leading_comments(self.span).fmt(f)?; + let result = self.write(f); + format_trailing_comments( + &self.parent.as_sibling_node(), + &SiblingNode::from(self.inner), + self.following_node.as_ref(), + ) + .fmt(f)?; + result } } diff --git a/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/named/interface-heritage.snap b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/named/interface-heritage.snap index 70dd01211cac4..3048bafec6daa 100644 --- a/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/named/interface-heritage.snap +++ b/crates/oxc_semantic/tests/fixtures/oxc/ts/exports/named/interface-heritage.snap @@ -27,7 +27,7 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/ts/exports/named/interface-he "flags": "ReferenceFlags(Read)", "id": 1, "name": "forwardRef", - "node_id": 19 + "node_id": 20 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-type-import.snap b/crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-type-import.snap index 4c4fbac7d2fed..a0244ff0865de 100644 --- a/crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-type-import.snap +++ b/crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-type-import.snap @@ -41,13 +41,13 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-t "flags": "ReferenceFlags(Type)", "id": 0, "name": "X", - "node_id": 15 + "node_id": 16 }, { "flags": "ReferenceFlags(Type)", "id": 2, "name": "X", - "node_id": 27 + "node_id": 29 } ] }, @@ -61,13 +61,13 @@ input_file: crates/oxc_semantic/tests/fixtures/oxc/ts/interfaces/property-with-t "flags": "ReferenceFlags(Type)", "id": 1, "name": "B", - "node_id": 19 + "node_id": 20 }, { "flags": "ReferenceFlags(Type)", "id": 3, "name": "B", - "node_id": 32 + "node_id": 34 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements-generic.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements-generic.snap index 1cb550334a1e8..2b234d01fa091 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements-generic.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements-generic.snap @@ -49,7 +49,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 13 + "node_id": 14 } ] }, @@ -63,7 +63,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 17 + "node_id": 18 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements.snap index c2bd40f0537e1..8532dc48ca56c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declaration/implements.snap @@ -34,7 +34,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/class/declarati "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 7 + "node_id": 8 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/export/equals3-type.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/export/equals3-type.snap index 34e0a81687101..fe72be1781564 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/export/equals3-type.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/export/equals3-type.snap @@ -27,7 +27,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/export/equals3- "flags": "ReferenceFlags(Type)", "id": 0, "name": "Foo", - "node_id": 11 + "node_id": 12 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced-attribute.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced-attribute.snap index 8123299e4c56b..5a885ad027e22 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced-attribute.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced-attribute.snap @@ -28,7 +28,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced- "flags": "ReferenceFlags(Read)", "id": 3, "name": "props", - "node_id": 51 + "node_id": 52 } ] } @@ -70,7 +70,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/jsx/namespaced- "flags": "ReferenceFlags(Type)", "id": 2, "name": "FooProps", - "node_id": 43 + "node_id": 44 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface-heritage1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface-heritage1.snap index 215e226b21971..03c490ed20433 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface-heritage1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface-heritage1.snap @@ -34,7 +34,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "Parent", - "node_id": 6 + "node_id": 7 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface2.snap index aea84ad68053e..64e97f59377ee 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/interface2.snap @@ -34,7 +34,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 7 + "node_id": 8 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/body-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/body-reference.snap index f16b521d33db3..f50b89545430c 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/body-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/body-reference.snap @@ -21,7 +21,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "T", - "node_id": 12 + "node_id": 13 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/extends-reference.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/extends-reference.snap index dc3e1e384245e..af13c8dcb14df 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/extends-reference.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/type-parameters/interface/extends-reference.snap @@ -36,7 +36,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 16 + "node_id": 17 } ] } @@ -57,7 +57,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 12 + "node_id": 13 } ] }, diff --git a/tasks/ast_tools/src/generators/ast_kind.rs b/tasks/ast_tools/src/generators/ast_kind.rs index 7595de1f95d09..c79dbafd70cbe 100644 --- a/tasks/ast_tools/src/generators/ast_kind.rs +++ b/tasks/ast_tools/src/generators/ast_kind.rs @@ -32,7 +32,6 @@ const STRUCTS_BLACK_LIST: &[&str] = &[ "AssignmentTargetPropertyProperty", "BindingPattern", "BindingProperty", - "TSInterfaceBody", "TSFunctionType", "Span", ];