Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 60214a5

Browse files
author
Hendrik van Antwerpen
committed
Fix ambient module declarations
1 parent 36274e1 commit 60214a5

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,16 +2154,23 @@ if none @is_async {
21542154

21552155
;; Ambient Declaration
21562156

2157+
;; We expose the ambient declarations via the statement's .lexical_defs and .var_defs.
2158+
;; Technically this means that the declarations could be scoped in an enclosing block,
2159+
;; or hidden behind a module name.
2160+
;; However, ambient declarations are only allowed at the top-level of script files, so
2161+
;; these cases cannot occur. Therefore, we have not introduced a separate .global_defs
2162+
;; that propagates all the way to the top.
2163+
21572164
(ambient_declaration
21582165
(declaration)@decl
21592166
)@amb_decl {
21602167
; propagate lexical scope
21612168
edge @decl.lexical_scope -> @amb_decl.lexical_scope
21622169

2163-
; lexical definitions are for ROOT_NODE
2170+
; expose lexical definitions
21642171
edge @amb_decl.lexical_defs -> @decl.lexical_defs
21652172

2166-
; variable definitions are for ROOT_NODE
2173+
; expose variable definitions
21672174
edge @amb_decl.var_defs -> @decl.var_defs
21682175
}
21692176

@@ -2174,11 +2181,11 @@ if none @is_async {
21742181
; propagate lexical scope
21752182
edge @stmt_blk.lexical_scope -> @amb_decl.lexical_scope
21762183

2177-
; lexical definitions are for ROOT_NODE
2178-
edge ROOT_NODE -> @stmt_blk.lexical_defs
2184+
; expose lexical definitions
2185+
edge @amb_decl.lexical_defs -> @stmt_blk.lexical_defs
21792186

2180-
; variable definitions are for ROOT_NODE
2181-
edge ROOT_NODE -> @stmt_blk.var_defs
2187+
; expose variable definitions
2188+
edge @amb_decl.var_defs -> @stmt_blk.var_defs
21822189
}
21832190

21842191
; FIXME what is this? tsc doesn't recognize this syntax
@@ -2369,22 +2376,30 @@ if none @is_async {
23692376
(ambient_declaration
23702377
(module name:(string)@name body:(_)@body)
23712378
)@amb_decl {
2372-
node @name.mod_def
2373-
node @name.mod_def__ns
2374-
23752379
; propagate lexical scope
23762380
edge @body.lexical_scope -> @amb_decl.lexical_scope
23772381

2378-
; global definition
2379-
edge ROOT_NODE -> @name.mod_def__ns
2382+
; module definition
2383+
let mod_path = (replace (source-text @name) "[\"\']" "")
23802384
;
2381-
attr (@name.mod_def__ns) pop_symbol = "%M"
2382-
edge @name.mod_def__ns -> @name.mod_def
2385+
node mod_def__ns
2386+
attr (mod_def__ns) pop_symbol = "%NonRelM"
2387+
edge @amb_decl.lexical_defs -> mod_def__ns
23832388
;
2384-
attr (@name.mod_def) symbol_definition = (replace (source-text @name) "[\"\']" ""), source_node = @name
2385-
2386-
; exports are reachable via module definition
2387-
edge @name.mod_def -> @body.exports
2389+
var mod_scope = mod_def__ns
2390+
scan mod_path {
2391+
"([^/]+)/?" {
2392+
node mod_def
2393+
attr (mod_def) pop_symbol = $1
2394+
edge mod_scope -> mod_def
2395+
;
2396+
set mod_scope = mod_def
2397+
}
2398+
}
2399+
; make the last one a definition
2400+
attr (mod_scope) is_definition, source_node = @name
2401+
; expose exports via module definition
2402+
edge mod_scope -> @body.exports
23882403
}
23892404

23902405

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* --- path: ./index.ts --- */
2+
import { foo } from "@my/lib";
3+
// ^ defined: 8
4+
5+
/* --- path: ./mod.ts --- */
6+
7+
declare module "@my/lib" {
8+
export const foo = 42;
9+
}

0 commit comments

Comments
 (0)