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

Commit 85e67b5

Browse files
author
Hendrik van Antwerpen
committed
Add explicit nodes for globals to ensure export as namespace works even in the presence of other exports
1 parent 2769b5e commit 85e67b5

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
104104
node @comment.expr_def__ns
105105
node @comment.expr_ref
106106
node @comment.expr_ref__ns
107+
node @comment.global_defs
107108
node @comment.lexical_defs
108109
node @comment.lexical_scope
109110
node @comment.member
@@ -223,6 +224,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
223224
(program)@prog {
224225
node @prog.defs
225226
node @prog.exports
227+
node @prog.globals
226228
node @prog.lexical_scope
227229
}
228230

@@ -258,6 +260,9 @@ attribute node_symbol = node => symbol = (source-text node), source_n
258260
set proj_scope = proj_def
259261
}
260262

263+
; expose globals
264+
edge proj_scope -> @prog.globals
265+
261266
var mod_scope = proj_scope
262267
if (not (is-empty @imexs)) {
263268
; module definition
@@ -285,6 +290,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
285290
; expose exports via module definition
286291
edge mod_scope -> @prog.exports
287292
} else {
293+
; expose definitions via project scope
288294
edge mod_scope -> @prog.defs
289295
}
290296
}
@@ -358,6 +364,9 @@ attribute node_symbol = node => symbol = (source-text node), source_n
358364

359365
; exports are visible via module definition
360366
edge @prog.exports -> @stmt.exports
367+
368+
; globals are visible in the project scope
369+
edge @prog.globals -> @stmt.global_defs
361370
}
362371

363372

@@ -447,6 +456,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
447456
]@stmt {
448457
node @stmt.default_export
449458
node @stmt.exports
459+
node @stmt.global_defs
450460
node @stmt.lexical_defs
451461
node @stmt.lexical_scope
452462
node @stmt.return_type
@@ -921,16 +931,16 @@ if none @is_default {
921931

922932
(program (export_statement
923933
"as" "namespace" . (_)@name
924-
)@export_stmt)@program {
934+
)@export_stmt)@prog {
925935
; namespace definitions are specified together with (module) and (internal_module)
926936

927937
; make definitions global
928-
edge ROOT_NODE -> @name.expr_def__ns
929-
edge ROOT_NODE -> @name.type_def__ns
938+
edge @prog.globals -> @name.expr_def__ns
939+
edge @prog.globals -> @name.type_def__ns
930940

931941
; connect definitions to exports
932-
edge @export_stmt.expr_member__ns -> @program.exports
933-
edge @export_stmt.type_member__ns -> @program.exports
942+
edge @export_stmt.expr_member__ns -> @prog.exports
943+
edge @export_stmt.type_member__ns -> @prog.exports
934944
}
935945

936946
;; Imports
@@ -2147,10 +2157,11 @@ if none @is_async {
21472157

21482158
(statement_identifier)@stmt_identifier {
21492159
; FIXME remove when we bump tree-sitter-typescript version
2150-
node @stmt_identifier.lexical_scope
2160+
node @stmt_identifier.global_defs
21512161
node @stmt_identifier.lexical_defs
2152-
node @stmt_identifier.var_defs
2162+
node @stmt_identifier.lexical_scope
21532163
node @stmt_identifier.return_type
2164+
node @stmt_identifier.var_defs
21542165
}
21552166

21562167
; FIXME match body: when we bump tree-sitter-typescript version
@@ -2172,24 +2183,17 @@ if none @is_async {
21722183

21732184
;; Ambient Declaration
21742185

2175-
;; We expose the ambient declarations via the statement's .lexical_defs and .var_defs.
2176-
;; Technically this means that the declarations could be scoped in an enclosing block,
2177-
;; or hidden behind a module name.
2178-
;; However, ambient declarations are only allowed at the top-level of script files, so
2179-
;; these cases cannot occur. Therefore, we have not introduced a separate .global_defs
2180-
;; that propagates all the way to the top.
2181-
21822186
(ambient_declaration
21832187
(declaration)@decl
21842188
)@amb_decl {
21852189
; propagate lexical scope
21862190
edge @decl.lexical_scope -> @amb_decl.lexical_scope
21872191

21882192
; expose lexical definitions
2189-
edge @amb_decl.lexical_defs -> @decl.lexical_defs
2193+
edge @amb_decl.global_defs -> @decl.lexical_defs
21902194

21912195
; expose variable definitions
2192-
edge @amb_decl.var_defs -> @decl.var_defs
2196+
edge @amb_decl.global_defs -> @decl.var_defs
21932197
}
21942198

21952199
(ambient_declaration
@@ -2200,10 +2204,10 @@ if none @is_async {
22002204
edge @stmt_blk.lexical_scope -> @amb_decl.lexical_scope
22012205

22022206
; expose lexical definitions
2203-
edge @amb_decl.lexical_defs -> @stmt_blk.lexical_defs
2207+
edge @amb_decl.global_defs -> @stmt_blk.lexical_defs
22042208

22052209
; expose variable definitions
2206-
edge @amb_decl.var_defs -> @stmt_blk.var_defs
2210+
edge @amb_decl.global_defs -> @stmt_blk.var_defs
22072211
}
22082212

22092213
; FIXME what is this? tsc doesn't recognize this syntax

0 commit comments

Comments
 (0)