diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index 1c79e5d39b5ad..1de3d3844a979 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -394,6 +394,7 @@ impl AstKind<'_> { Self::TSArrayType(_) => "TSArrayType".into(), Self::TSOptionalType(_) => "TSOptionalType".into(), Self::TSTypeOperator(_) => "TSTypeOperator".into(), + Self::TSFunctionType(_) => "TSFunctionType".into(), Self::TSIndexedAccessType(_) => "TSIndexedAccessType".into(), diff --git a/crates/oxc_ast/src/generated/ast_kind.rs b/crates/oxc_ast/src/generated/ast_kind.rs index 1923308f596e1..47e3ab970a152 100644 --- a/crates/oxc_ast/src/generated/ast_kind.rs +++ b/crates/oxc_ast/src/generated/ast_kind.rs @@ -185,22 +185,23 @@ pub enum AstType { 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, + TSFunctionType = 171, + TSConstructorType = 172, + TSMappedType = 173, + TSTemplateLiteralType = 174, + TSAsExpression = 175, + TSSatisfiesExpression = 176, + TSTypeAssertion = 177, + TSImportEqualsDeclaration = 178, + TSExternalModuleReference = 179, + TSNonNullExpression = 180, + Decorator = 181, + TSExportAssignment = 182, + TSNamespaceExportDeclaration = 183, + TSInstantiationExpression = 184, + JSDocNullableType = 185, + JSDocNonNullableType = 186, + JSDocUnknownType = 187, } /// Untyped AST Node Kind @@ -391,6 +392,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, + TSFunctionType(&'a TSFunctionType<'a>) = AstType::TSFunctionType 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, @@ -599,6 +601,7 @@ impl GetSpan for AstKind<'_> { Self::TSInferType(it) => it.span(), Self::TSTypeQuery(it) => it.span(), Self::TSImportType(it) => it.span(), + Self::TSFunctionType(it) => it.span(), Self::TSConstructorType(it) => it.span(), Self::TSMappedType(it) => it.span(), Self::TSTemplateLiteralType(it) => it.span(), @@ -793,6 +796,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::TSFunctionType(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), @@ -1677,6 +1681,11 @@ impl<'a> AstKind<'a> { if let Self::TSImportType(v) = self { Some(v) } else { None } } + #[inline] + pub fn as_ts_function_type(self) -> Option<&'a TSFunctionType<'a>> { + if let Self::TSFunctionType(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 } diff --git a/crates/oxc_ast_visit/src/generated/visit.rs b/crates/oxc_ast_visit/src/generated/visit.rs index 389a4b81df28e..e744fe3dc4694 100644 --- a/crates/oxc_ast_visit/src/generated/visit.rs +++ b/crates/oxc_ast_visit/src/generated/visit.rs @@ -3943,7 +3943,8 @@ pub mod walk { #[inline] pub fn walk_ts_function_type<'a, V: Visit<'a>>(visitor: &mut V, it: &TSFunctionType<'a>) { - // No `AstKind` for this type + let kind = AstKind::TSFunctionType(visitor.alloc(it)); + visitor.enter_node(kind); visitor.enter_scope(ScopeFlags::empty(), &it.scope_id); visitor.visit_span(&it.span); if let Some(type_parameters) = &it.type_parameters { @@ -3955,6 +3956,7 @@ pub mod walk { visitor.visit_formal_parameters(&it.params); visitor.visit_ts_type_annotation(&it.return_type); visitor.leave_scope(); + 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 78ba74dae9ca2..c0b294eafa65f 100644 --- a/crates/oxc_ast_visit/src/generated/visit_mut.rs +++ b/crates/oxc_ast_visit/src/generated/visit_mut.rs @@ -4155,7 +4155,8 @@ pub mod walk_mut { visitor: &mut V, it: &mut TSFunctionType<'a>, ) { - // No `AstType` for this type + let kind = AstType::TSFunctionType; + visitor.enter_node(kind); visitor.enter_scope(ScopeFlags::empty(), &it.scope_id); visitor.visit_span(&mut it.span); if let Some(type_parameters) = &mut it.type_parameters { @@ -4167,6 +4168,7 @@ pub mod walk_mut { visitor.visit_formal_parameters(&mut it.params); visitor.visit_ts_type_annotation(&mut it.return_type); visitor.leave_scope(); + 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 35a9d32ffb3c9..f75b283cafc0e 100644 --- a/crates/oxc_formatter/src/generated/ast_nodes.rs +++ b/crates/oxc_formatter/src/generated/ast_nodes.rs @@ -198,6 +198,7 @@ pub enum AstNodes<'a> { TSInferType(&'a AstNode<'a, TSInferType<'a>>), TSTypeQuery(&'a AstNode<'a, TSTypeQuery<'a>>), TSImportType(&'a AstNode<'a, TSImportType<'a>>), + TSFunctionType(&'a AstNode<'a, TSFunctionType<'a>>), TSConstructorType(&'a AstNode<'a, TSConstructorType<'a>>), TSMappedType(&'a AstNode<'a, TSMappedType<'a>>), TSTemplateLiteralType(&'a AstNode<'a, TSTemplateLiteralType<'a>>), @@ -2486,6 +2487,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(n) => n.span(), Self::TSTypeQuery(n) => n.span(), Self::TSImportType(n) => n.span(), + Self::TSFunctionType(n) => n.span(), Self::TSConstructorType(n) => n.span(), Self::TSMappedType(n) => n.span(), Self::TSTemplateLiteralType(n) => n.span(), @@ -2679,6 +2681,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(n) => n.parent, Self::TSTypeQuery(n) => n.parent, Self::TSImportType(n) => n.parent, + Self::TSFunctionType(n) => n.parent, Self::TSConstructorType(n) => n.parent, Self::TSMappedType(n) => n.parent, Self::TSTemplateLiteralType(n) => n.parent, @@ -2872,6 +2875,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::TSFunctionType(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), @@ -3065,6 +3069,7 @@ impl<'a> AstNodes<'a> { Self::TSInferType(_) => "TSInferType", Self::TSTypeQuery(_) => "TSTypeQuery", Self::TSImportType(_) => "TSImportType", + Self::TSFunctionType(_) => "TSFunctionType", Self::TSConstructorType(_) => "TSConstructorType", Self::TSMappedType(_) => "TSMappedType", Self::TSTemplateLiteralType(_) => "TSTemplateLiteralType", @@ -10549,11 +10554,12 @@ impl<'a> AstNode<'a, TSType<'a>> { following_node: self.following_node, })) } - TSType::TSFunctionType(s) => { - panic!( - "No kind for current enum variant yet, please see `tasks/ast_tools/src/generators/ast_kind.rs`" - ) - } + TSType::TSFunctionType(s) => AstNodes::TSFunctionType(self.allocator.alloc(AstNode { + inner: s.as_ref(), + parent, + allocator: self.allocator, + following_node: self.following_node, + })), TSType::TSImportType(s) => AstNodes::TSImportType(self.allocator.alloc(AstNode { inner: s.as_ref(), parent, @@ -12948,7 +12954,7 @@ impl<'a> AstNode<'a, TSFunctionType<'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::TSFunctionType(transmute_self(self))), following_node, })) .as_ref() @@ -12961,7 +12967,7 @@ impl<'a> AstNode<'a, TSFunctionType<'a>> { .alloc(self.inner.this_param.as_ref().map(|inner| AstNode { inner: inner.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSFunctionType(transmute_self(self))), following_node, })) .as_ref() @@ -12973,7 +12979,7 @@ impl<'a> AstNode<'a, TSFunctionType<'a>> { self.allocator.alloc(AstNode { inner: self.inner.params.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSFunctionType(transmute_self(self))), following_node, }) } @@ -12984,7 +12990,7 @@ impl<'a> AstNode<'a, TSFunctionType<'a>> { self.allocator.alloc(AstNode { inner: self.inner.return_type.as_ref(), allocator: self.allocator, - parent: self.parent, + parent: self.allocator.alloc(AstNodes::TSFunctionType(transmute_self(self))), following_node, }) } diff --git a/crates/oxc_formatter/src/generated/format.rs b/crates/oxc_formatter/src/generated/format.rs index 47f33ef058d57..2ed84329708f4 100644 --- a/crates/oxc_formatter/src/generated/format.rs +++ b/crates/oxc_formatter/src/generated/format.rs @@ -2007,7 +2007,10 @@ impl<'a> Format<'a> for AstNode<'a, TSImportType<'a>> { impl<'a> Format<'a> for AstNode<'a, TSFunctionType<'a>> { fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - self.write(f) + self.format_leading_comments(f)?; + let result = self.write(f); + self.format_trailing_comments(f)?; + result } } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/class-deco-with-object-param.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/class-deco-with-object-param.snap index 659249869997f..d988e61c4df29 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/class-deco-with-object-param.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/class-deco-with-object-param.snap @@ -11,7 +11,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/clas "children": [], "flags": "ScopeFlags(StrictMode)", "id": 2, - "node": "TSTypeAnnotation", + "node": "TSFunctionType", "symbols": [] } ], @@ -50,7 +50,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/clas "flags": "ReferenceFlags(Read)", "id": 0, "name": "deco", - "node_id": 19 + "node_id": 20 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional3.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional3.snap index 2da8d352d5dc1..85559d205b2b4 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional3.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional3.snap @@ -13,7 +13,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 3, - "node": "TSConditionalType", + "node": "TSFunctionType", "symbols": [] } ], @@ -31,7 +31,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "I", - "node_id": 19 + "node_id": 20 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional5.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional5.snap index 11ee6799c0cbf..c1e5952c9b00b 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional5.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/conditional5.snap @@ -15,13 +15,13 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 4, - "node": "TSTypeAnnotation", + "node": "TSFunctionType", "symbols": [] } ], "flags": "ScopeFlags(StrictMode)", "id": 3, - "node": "TSConditionalType", + "node": "TSFunctionType", "symbols": [] } ], @@ -39,7 +39,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "I", - "node_id": 31 + "node_id": 33 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics1.snap index 0641a117ccb20..48cee58c0787a 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics1.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 3, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -36,7 +36,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "U", - "node_id": 15 + "node_id": 16 } ] } @@ -57,7 +57,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 18 + "node_id": 19 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics2.snap index b942d38cea31c..f930e133cfe27 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function-generics2.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 3, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [ { "flags": "SymbolFlags(TypeParameter)", @@ -30,7 +30,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "U", - "node_id": 15 + "node_id": 16 } ] } @@ -57,7 +57,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 18 + "node_id": 19 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function1.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function1.snap index 32765cd4ed7a1..7ffc2d0ad4c84 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function1.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function1.snap @@ -18,7 +18,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 3, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -42,13 +42,13 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "T", - "node_id": 12 + "node_id": 13 }, { "flags": "ReferenceFlags(Type)", "id": 1, "name": "T", - "node_id": 15 + "node_id": 16 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function2.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function2.snap index 6c6364e6d34a9..f27b4a456f81e 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function2.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/function2.snap @@ -11,7 +11,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 2, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -35,7 +35,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "arg", - "node_id": 14 + "node_id": 15 } ] }, diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/array-pattern.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/array-pattern.snap index 8f5482a8178c4..135a3e8134359 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/array-pattern.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/array-pattern.snap @@ -11,7 +11,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 2, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -29,7 +29,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 14 + "node_id": 15 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/object-pattern.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/object-pattern.snap index 5aa0994d4f373..c7503d5829524 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/object-pattern.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/object-pattern.snap @@ -11,7 +11,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 2, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -29,7 +29,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 19 + "node_id": 20 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/rest-element.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/rest-element.snap index 506d33ce06c3c..6bf1e6b0fd294 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/rest-element.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/function/params/rest-element.snap @@ -11,7 +11,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "children": [], "flags": "ScopeFlags(StrictMode)", "id": 2, - "node": "TSTypeAliasDeclaration", + "node": "TSFunctionType", "symbols": [] } ], @@ -29,7 +29,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaratio "flags": "ReferenceFlags(Type)", "id": 0, "name": "A", - "node_id": 13 + "node_id": 14 } ] } diff --git a/tasks/ast_tools/src/generators/ast_kind.rs b/tasks/ast_tools/src/generators/ast_kind.rs index bee3748ac8227..f14ab15d83e1c 100644 --- a/tasks/ast_tools/src/generators/ast_kind.rs +++ b/tasks/ast_tools/src/generators/ast_kind.rs @@ -32,7 +32,6 @@ use super::define_generator; /// const STRUCTS_BLACK_LIST: &[&str] = &[ "BindingPattern", - "TSFunctionType", // This one should not be removed "Span", ];