From de862aad9db30a550dfc94f11e18ca35e726bb59 Mon Sep 17 00:00:00 2001 From: Song Gao Date: Fri, 20 Dec 2024 18:42:30 +0800 Subject: [PATCH] fix(binder): bind symbol for declare function --- crates/oxc_semantic/src/binder.rs | 4 +- .../class-deco-with-object-param.snap | 19 +++++++- .../declare-function.snap | 45 +++++++++++++++++++ .../function-declaration/declare-function.ts | 2 + .../function-declaration/overload.snap | 7 +-- .../type-declaration/literal-type3.snap | 10 ++++- 6 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.snap create mode 100644 crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.ts diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 9747af557a0de..4d6f5d2129bc4 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -161,7 +161,9 @@ impl<'a> Binder<'a> for Function<'a> { builder.current_node_id, ); ident.symbol_id.set(Some(symbol_id)); - } else if self.r#type == FunctionType::FunctionDeclaration { + } else if self.r#type == FunctionType::FunctionDeclaration + || self.r#type == FunctionType::TSDeclareFunction + { // The visitor is already inside the function scope, // retrieve the parent scope for the function id to bind to. 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 77c7a12285b72..d567c5567cab5 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 @@ -1,5 +1,6 @@ --- source: crates/oxc_semantic/tests/main.rs +assertion_line: 147 input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/decorators/class-deco-with-object-param.ts snapshot_kind: text --- @@ -14,7 +15,7 @@ snapshot_kind: text "symbols": [ { "flags": "SymbolFlags(FunctionScopedVariable)", - "id": 0, + "id": 1, "name": "param", "node": "BindingRestElement", "references": [] @@ -33,9 +34,23 @@ snapshot_kind: text "id": 0, "node": "Program", "symbols": [ + { + "flags": "SymbolFlags(BlockScopedVariable | Function)", + "id": 0, + "name": "deco", + "node": "Function(deco)", + "references": [ + { + "flags": "ReferenceFlags(Read)", + "id": 0, + "name": "deco", + "node_id": 19 + } + ] + }, { "flags": "SymbolFlags(Class)", - "id": 1, + "id": 2, "name": "Foo", "node": "Class(Foo)", "references": [] diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.snap new file mode 100644 index 0000000000000..5b7ff62b6b1bf --- /dev/null +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.snap @@ -0,0 +1,45 @@ +--- +source: crates/oxc_semantic/tests/main.rs +assertion_line: 147 +input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.ts +snapshot_kind: text +--- +[ + { + "children": [ + { + "children": [], + "flags": "ScopeFlags(StrictMode | Function)", + "id": 1, + "node": "Function(A)", + "symbols": [] + }, + { + "children": [], + "flags": "ScopeFlags(StrictMode | Function)", + "id": 2, + "node": "Function(B)", + "symbols": [] + } + ], + "flags": "ScopeFlags(StrictMode | Top)", + "id": 0, + "node": "Program", + "symbols": [ + { + "flags": "SymbolFlags(BlockScopedVariable | Function)", + "id": 0, + "name": "A", + "node": "Function(A)", + "references": [] + }, + { + "flags": "SymbolFlags(BlockScopedVariable | Function)", + "id": 1, + "name": "B", + "node": "Function(B)", + "references": [] + } + ] + } +] diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.ts b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.ts new file mode 100644 index 0000000000000..2ba1fdd15c198 --- /dev/null +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/declare-function.ts @@ -0,0 +1,2 @@ +declare function A(): void; +function B(): void { }; diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/overload.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/overload.snap index 15cfec50045d9..e075e1959cd03 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/overload.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/overload.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_semantic/tests/main.rs +assertion_line: 147 input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/functions/function-declaration/overload.ts snapshot_kind: text --- @@ -14,14 +15,14 @@ snapshot_kind: text "symbols": [ { "flags": "SymbolFlags(FunctionScopedVariable)", - "id": 0, + "id": 1, "name": "a", "node": "FormalParameter(a)", "references": [] }, { "flags": "SymbolFlags(FunctionScopedVariable)", - "id": 1, + "id": 2, "name": "b", "node": "FormalParameter(b)", "references": [] @@ -64,7 +65,7 @@ snapshot_kind: text "symbols": [ { "flags": "SymbolFlags(BlockScopedVariable | Function)", - "id": 2, + "id": 0, "name": "foo", "node": "Function(foo)", "references": [] diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/literal-type3.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/literal-type3.snap index c8401e0b70643..95963b8488441 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/literal-type3.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/literal-type3.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_semantic/tests/main.rs +assertion_line: 147 input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/type-declaration/literal-type3.ts snapshot_kind: text --- @@ -28,7 +29,7 @@ snapshot_kind: text "symbols": [ { "flags": "SymbolFlags(FunctionScopedVariable)", - "id": 2, + "id": 3, "name": "value", "node": "FormalParameter(value)", "references": [] @@ -67,6 +68,13 @@ snapshot_kind: text "node_id": 31 } ] + }, + { + "flags": "SymbolFlags(BlockScopedVariable | Function)", + "id": 2, + "name": "setAlignment", + "node": "Function(setAlignment)", + "references": [] } ] }