Skip to content

Commit

Permalink
fix(semantic): resolve function return type to function parent scope
Browse files Browse the repository at this point in the history
closes #6387
  • Loading branch information
Boshen committed Nov 12, 2024
1 parent 20fe3cd commit b576253
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,8 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.visit_formal_parameters(&func.params);
if let Some(return_type) = &func.return_type {
self.visit_ts_type_annotation(return_type);
// Resolve identifier references in return type.
self.resolve_references_for_current_scope();
}
if let Some(body) = &func.body {
self.visit_function_body(body);
Expand Down Expand Up @@ -1724,6 +1726,8 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {

if let Some(return_type) = &expr.return_type {
self.visit_ts_type_annotation(return_type);
// Resolve identifier references in return type.
self.resolve_references_for_current_scope();
}

self.visit_function_body(&expr.body);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
source: crates/oxc_semantic/tests/main.rs
input_file: crates/oxc_semantic/tests/fixtures/oxc/function/arrow_return_type.ts
snapshot_kind: text
---
[
{
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 1,
"node": "TSTypeAliasDeclaration",
"symbols": []
},
{
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 3,
"node": "TSTypeAliasDeclaration",
"symbols": []
}
],
"flags": "ScopeFlags(StrictMode | Function | Arrow)",
"id": 2,
"node": "ArrowFunctionExpression",
"symbols": [
{
"flags": "SymbolFlags(TypeAlias)",
"id": 1,
"name": "T",
"node": "TSTypeAliasDeclaration",
"references": []
}
]
}
],
"flags": "ScopeFlags(StrictMode | Top)",
"id": 0,
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(Export | TypeAlias)",
"id": 0,
"name": "T",
"node": "TSTypeAliasDeclaration",
"references": [
{
"flags": "ReferenceFlags(Type)",
"id": 0,
"name": "T",
"node_id": 13
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type T = void;

((): T => { // Return type `T` resolves to top level `T`, not `T` in function body.
type T = string;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
source: crates/oxc_semantic/tests/main.rs
input_file: crates/oxc_semantic/tests/fixtures/oxc/function/function_return_type.ts
snapshot_kind: text
---
[
{
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 1,
"node": "TSTypeAliasDeclaration",
"symbols": []
},
{
"children": [
{
"children": [],
"flags": "ScopeFlags(StrictMode)",
"id": 3,
"node": "TSTypeAliasDeclaration",
"symbols": []
}
],
"flags": "ScopeFlags(StrictMode | Function)",
"id": 2,
"node": "Function(<anonymous>)",
"symbols": [
{
"flags": "SymbolFlags(TypeAlias)",
"id": 1,
"name": "T",
"node": "TSTypeAliasDeclaration",
"references": []
}
]
}
],
"flags": "ScopeFlags(StrictMode | Top)",
"id": 0,
"node": "Program",
"symbols": [
{
"flags": "SymbolFlags(Export | TypeAlias)",
"id": 0,
"name": "T",
"node": "TSTypeAliasDeclaration",
"references": [
{
"flags": "ReferenceFlags(Type)",
"id": 0,
"name": "T",
"node_id": 13
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type T = void;

(function (): T { // Return type `T` resolves to top level `T`, not `T` in function body.
type T = string;
});

0 comments on commit b576253

Please sign in to comment.