From 03bce3f39b1a8f9d4546e1ef2abf3577dddf6273 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sun, 29 Jun 2025 16:28:09 +0000 Subject: [PATCH] refactor(ast): add `AstKind` for `TSConstructorType` (#11965) part of #11490 --- crates/oxc_ast/src/ast_kind_impl.rs | 1 + crates/oxc_ast/src/generated/ast_kind.rs | 39 ++++++++++++------- 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 | 20 +++++++--- crates/oxc_formatter/src/generated/format.rs | 10 ++++- .../function/constructor-generics1.snap | 4 +- .../function/constructor-generics2.snap | 4 +- .../function/constructor.snap | 4 +- tasks/ast_tools/src/generators/ast_kind.rs | 1 - 10 files changed, 60 insertions(+), 31 deletions(-) diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index c968039bf3a3d..5b33913ee2eef 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -404,6 +404,7 @@ impl AstKind<'_> { Self::TSMappedType(_) => "TSMappedType".into(), Self::TSConstructSignatureDeclaration(_) => "TSConstructSignatureDeclaration".into(), Self::TSExportAssignment(_) => "TSExportAssignment".into(), + Self::TSConstructorType(_) => "TSConstructorType".into(), Self::V8IntrinsicExpression(_) => "V8IntrinsicExpression".into(), Self::JSDocNullableType(_) => "JSDocNullableType".into(), diff --git a/crates/oxc_ast/src/generated/ast_kind.rs b/crates/oxc_ast/src/generated/ast_kind.rs index 0ef867b3e80fe..efad0c9f2611e 100644 --- a/crates/oxc_ast/src/generated/ast_kind.rs +++ b/crates/oxc_ast/src/generated/ast_kind.rs @@ -183,21 +183,22 @@ pub enum AstType { TSInferType = 166, TSTypeQuery = 167, TSImportType = 168, - TSMappedType = 169, - TSTemplateLiteralType = 170, - TSAsExpression = 171, - TSSatisfiesExpression = 172, - TSTypeAssertion = 173, - TSImportEqualsDeclaration = 174, - TSExternalModuleReference = 175, - TSNonNullExpression = 176, - Decorator = 177, - TSExportAssignment = 178, - TSNamespaceExportDeclaration = 179, - TSInstantiationExpression = 180, - JSDocNullableType = 181, - JSDocNonNullableType = 182, - JSDocUnknownType = 183, + TSConstructorType = 169, + TSMappedType = 170, + TSTemplateLiteralType = 171, + TSAsExpression = 172, + TSSatisfiesExpression = 173, + TSTypeAssertion = 174, + TSImportEqualsDeclaration = 175, + TSExternalModuleReference = 176, + TSNonNullExpression = 177, + Decorator = 178, + TSExportAssignment = 179, + TSNamespaceExportDeclaration = 180, + TSInstantiationExpression = 181, + JSDocNullableType = 182, + JSDocNonNullableType = 183, + JSDocUnknownType = 184, } /// Untyped AST Node Kind @@ -385,6 +386,7 @@ pub enum AstKind<'a> { TSInferType(&'a TSInferType<'a>) = AstType::TSInferType as u8, TSTypeQuery(&'a TSTypeQuery<'a>) = AstType::TSTypeQuery as u8, TSImportType(&'a TSImportType<'a>) = AstType::TSImportType as u8, + TSConstructorType(&'a TSConstructorType<'a>) = AstType::TSConstructorType as u8, TSMappedType(&'a TSMappedType<'a>) = AstType::TSMappedType as u8, TSTemplateLiteralType(&'a TSTemplateLiteralType<'a>) = AstType::TSTemplateLiteralType as u8, TSAsExpression(&'a TSAsExpression<'a>) = AstType::TSAsExpression as u8, @@ -590,6 +592,7 @@ impl GetSpan for AstKind<'_> { Self::TSInferType(it) => it.span(), Self::TSTypeQuery(it) => it.span(), Self::TSImportType(it) => it.span(), + Self::TSConstructorType(it) => it.span(), Self::TSMappedType(it) => it.span(), Self::TSTemplateLiteralType(it) => it.span(), Self::TSAsExpression(it) => it.span(), @@ -781,6 +784,7 @@ impl GetAddress for AstKind<'_> { Self::TSInferType(it) => Address::from_ptr(it), Self::TSTypeQuery(it) => Address::from_ptr(it), Self::TSImportType(it) => Address::from_ptr(it), + Self::TSConstructorType(it) => Address::from_ptr(it), Self::TSMappedType(it) => Address::from_ptr(it), Self::TSTemplateLiteralType(it) => Address::from_ptr(it), Self::TSAsExpression(it) => Address::from_ptr(it), @@ -1650,6 +1654,11 @@ impl<'a> AstKind<'a> { if let Self::TSImportType(v) = self { Some(v) } else { None } } + #[inline] + pub fn as_ts_constructor_type(self) -> Option<&'a TSConstructorType<'a>> { + if let Self::TSConstructorType(v) = self { Some(v) } else { None } + } + #[inline] pub fn as_ts_mapped_type(self) -> Option<&'a TSMappedType<'a>> { if let Self::TSMappedType(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 c6ac5a6fae7a1..cb7dec71ee71a 100644 --- a/crates/oxc_ast_visit/src/generated/visit.rs +++ b/crates/oxc_ast_visit/src/generated/visit.rs @@ -3954,13 +3954,15 @@ pub mod walk { #[inline] pub fn walk_ts_constructor_type<'a, V: Visit<'a>>(visitor: &mut V, it: &TSConstructorType<'a>) { - // No `AstKind` for this type + let kind = AstKind::TSConstructorType(visitor.alloc(it)); + visitor.enter_node(kind); visitor.visit_span(&it.span); if let Some(type_parameters) = &it.type_parameters { visitor.visit_ts_type_parameter_declaration(type_parameters); } visitor.visit_formal_parameters(&it.params); visitor.visit_ts_type_annotation(&it.return_type); + 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 92401e13d5c01..c4b8077aabec6 100644 --- a/crates/oxc_ast_visit/src/generated/visit_mut.rs +++ b/crates/oxc_ast_visit/src/generated/visit_mut.rs @@ -4169,13 +4169,15 @@ pub mod walk_mut { visitor: &mut V, it: &mut TSConstructorType<'a>, ) { - // No `AstType` for this type + let kind = AstType::TSConstructorType; + visitor.enter_node(kind); visitor.visit_span(&mut it.span); if let Some(type_parameters) = &mut it.type_parameters { visitor.visit_ts_type_parameter_declaration(type_parameters); } visitor.visit_formal_parameters(&mut it.params); visitor.visit_ts_type_annotation(&mut it.return_type); + 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 e55290b8be557..88e166220aa42 100644 --- a/crates/oxc_formatter/src/generated/ast_nodes.rs +++ b/crates/oxc_formatter/src/generated/ast_nodes.rs @@ -196,6 +196,7 @@ pub enum AstNodes<'a> { TSInferType(&'a AstNode<'a, TSInferType<'a>>), TSTypeQuery(&'a AstNode<'a, TSTypeQuery<'a>>), TSImportType(&'a AstNode<'a, TSImportType<'a>>), + TSConstructorType(&'a AstNode<'a, TSConstructorType<'a>>), TSMappedType(&'a AstNode<'a, TSMappedType<'a>>), TSTemplateLiteralType(&'a AstNode<'a, TSTemplateLiteralType<'a>>), TSAsExpression(&'a AstNode<'a, TSAsExpression<'a>>), @@ -2480,6 +2481,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(n) => n.span(), Self::TSTypeQuery(n) => n.span(), Self::TSImportType(n) => n.span(), + Self::TSConstructorType(n) => n.span(), Self::TSMappedType(n) => n.span(), Self::TSTemplateLiteralType(n) => n.span(), Self::TSAsExpression(n) => n.span(), @@ -2670,6 +2672,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(n) => n.parent, Self::TSTypeQuery(n) => n.parent, Self::TSImportType(n) => n.parent, + Self::TSConstructorType(n) => n.parent, Self::TSMappedType(n) => n.parent, Self::TSTemplateLiteralType(n) => n.parent, Self::TSAsExpression(n) => n.parent, @@ -2860,6 +2863,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(n) => SiblingNode::from(n.inner), Self::TSTypeQuery(n) => SiblingNode::from(n.inner), Self::TSImportType(n) => SiblingNode::from(n.inner), + Self::TSConstructorType(n) => SiblingNode::from(n.inner), Self::TSMappedType(n) => SiblingNode::from(n.inner), Self::TSTemplateLiteralType(n) => SiblingNode::from(n.inner), Self::TSAsExpression(n) => SiblingNode::from(n.inner), @@ -3050,6 +3054,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(_) => "TSInferType", Self::TSTypeQuery(_) => "TSTypeQuery", Self::TSImportType(_) => "TSImportType", + Self::TSConstructorType(_) => "TSConstructorType", Self::TSMappedType(_) => "TSMappedType", Self::TSTemplateLiteralType(_) => "TSTemplateLiteralType", Self::TSAsExpression(_) => "TSAsExpression", @@ -8928,9 +8933,12 @@ impl<'a> AstNode<'a, TSType<'a>> { })) } TSType::TSConstructorType(s) => { - panic!( - "No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`" - ) + AstNodes::TSConstructorType(self.allocator.alloc(AstNode { + inner: s.as_ref(), + parent, + allocator: self.allocator, + following_node: self.following_node, + })) } TSType::TSFunctionType(s) => { panic!( @@ -10753,7 +10761,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> { .alloc(self.inner.type_parameters.as_ref().map(|inner| AstNode { inner: inner.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))), following_node, })) .as_ref() @@ -10765,7 +10773,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> { self.allocator.alloc(AstNode { inner: self.inner.params.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))), following_node, }) } @@ -10776,7 +10784,7 @@ impl<'a> AstNode<'a, TSConstructorType<'a>> { self.allocator.alloc(AstNode { inner: self.inner.return_type.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSConstructorType(transmute_self(self))), following_node, }) } diff --git a/crates/oxc_formatter/src/generated/format.rs b/crates/oxc_formatter/src/generated/format.rs index a666fef383637..9dc06fe3f1e65 100644 --- a/crates/oxc_formatter/src/generated/format.rs +++ b/crates/oxc_formatter/src/generated/format.rs @@ -2783,7 +2783,15 @@ impl<'a> Format<'a> for AstNode<'a, TSFunctionType<'a>> { impl<'a> Format<'a> for AstNode<'a, TSConstructorType<'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/typescript-eslint/type-declaration/function/constructor-generics1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics1.snap index a0d79c5a45b7c..0c407dee0510f 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics1.snap @@ -28,7 +28,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "U", - "node_id": 16 + "node_id": 17 } ] } @@ -49,7 +49,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 20 + "node_id": 21 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics2.snap index ccd8573bc53c0..212c81bcadbc3 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor-generics2.snap @@ -28,7 +28,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "U", - "node_id": 16 + "node_id": 17 } ] } @@ -49,7 +49,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 20 + "node_id": 21 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor.snap index 10b6bd055bb78..b5a61c7ab82c7 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/constructor.snap @@ -34,13 +34,13 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "T", - "node_id": 13 + "node_id": 14 }, { "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 17 + "node_id": 18 } ] }, diff --git a/tasks/ast_tools/src/generators/ast_kind.rs b/tasks/ast_tools/src/generators/ast_kind.rs index 016dd7403f00e..4a3c7f599e195 100644 --- a/tasks/ast_tools/src/generators/ast_kind.rs +++ b/tasks/ast_tools/src/generators/ast_kind.rs @@ -35,7 +35,6 @@ const STRUCTS_BLACK_LIST: &[&str] = &[ "TSInterfaceBody", "TSIndexSignature", "TSFunctionType", - "TSConstructorType", "Span", ];