-
Notifications
You must be signed in to change notification settings - Fork 109
/
stack-graphs.tsg
5917 lines (4676 loc) · 147 KB
/
stack-graphs.tsg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ########################################################################################
;
; ######## ## ## ######## ######## ###### ###### ######## #### ######## ########
; ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
; ## #### ## ## ## ## ## ## ## ## ## ## ##
; ## ## ######## ###### ###### ## ######## ## ######## ##
; ## ## ## ## ## ## ## ## ## ## ##
; ## ## ## ## ## ## ## ## ## ## ## ## ##
; ## ## ## ######## ###### ###### ## ## #### ## ##
;
; ########################################################################################
; Global Variables
; ^^^^^^^^^^^^^^^^
global FILE_PATH
global ROOT_NODE
global JUMP_TO_SCOPE_NODE
; Attribute Shorthands
; ^^^^^^^^^^^^^^^^^^^^
attribute node_definition = node => type = "pop_symbol", node_symbol = node, is_definition
attribute node_reference = node => type = "push_symbol", node_symbol = node, is_reference
attribute pop_node = node => type = "pop_symbol", node_symbol = node
attribute pop_scoped_node = node => type = "pop_scoped_symbol", node_symbol = node
attribute pop_scoped_symbol = symbol => type = "pop_scoped_symbol", symbol = symbol
attribute pop_symbol = symbol => type = "pop_symbol", symbol = symbol
attribute push_node = node => type = "push_symbol", node_symbol = node
attribute push_scoped_node = node => type = "push_scoped_symbol", node_symbol = node
attribute push_scoped_symbol = symbol => type = "push_scoped_symbol", symbol = symbol
attribute push_symbol = symbol => type = "push_symbol", symbol = symbol
attribute scoped_node_definition = node => type = "pop_scoped_symbol", node_symbol = node, is_definition
attribute scoped_node_reference = node => type = "push_scoped_symbol", node_symbol = node, is_reference
attribute symbol_definition = symbol => type = "pop_symbol", symbol = symbol, is_definition
attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, is_reference
attribute node_symbol = node => symbol = (source-text node), source_node = node
; Node Name & Symbol Conventions
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; @node.{expr,type,mod}_def = STRING
; A definition node, pop node with a name value.
;
; @node.{expr,type,mod}_ref = STRING
; A reference node, a push node with a name value.
;
; @node.{expr,type,mod}_{def,ref}.ns = "%T" | "%E" | "%M"
; Internal symbol indicating the namespace of an identifier.
;
; @node.{expr,type,ns}_{def,ref}.typeof = ":"
; Internal symbol indicating the type of an identifier.
;
; @node.callable = "->"
; Internal symbol indicating a callable.
;
; @node.member = "."
; Internal symbol indicating a named member (e.g., a field or method).
;
; @node.ctor = "<new>"
; Internal symbol indicating a constructor.
;
; @node.type_{abs,app} = "<>"
; Internal scoped symbol indicating type parameters & arguments.
;
; @node.indexable = "[]"
; Internal symbol indicating an indexable (i.e., array access).
;
; @node.{type,expr}_export = "%T" | "%E"
; Internal node for namespace guards for default and = exports.
; comments can appear almost everywhere, so we simply make sure all
; possible nodes are defined on comments
(comment)@comment {
node @comment.alias_type
node @comment.aliased_type
node @comment.applied_type
node @comment.arg
node @comment.arg_index
node @comment.args
node @comment.async_type
node @comment.await_type
node @comment.callable
node @comment.coargs
node @comment.cocallable
node @comment.comp_index
node @comment.coparam
node @comment.coparams
node @comment.cotype
node @comment.default_export
node @comment.defs
node @comment.exports
node @comment.expr_def
node @comment.expr_def__ns
node @comment.expr_ref
node @comment.expr_ref__ns
node @comment.lexical_defs
node @comment.lexical_scope
node @comment.member
node @comment.mod_def
node @comment.mod_def__ns
node @comment.mod_ref
node @comment.mod_ref__ns
node @comment.param
node @comment.params
node @comment.return_type
node @comment.static_members
node @comment.static_type
node @comment.type
node @comment.type_def
node @comment.type_def__ns
node @comment.type_members
node @comment.type_ref
node @comment.type_ref__ns
node @comment.var_defs
}
; ######
; # # ##### #### #### ##### ## # # ####
; # # # # # # # # # # # # ## ## #
; ###### # # # # # # # # # # ## # ####
; # ##### # # # ### ##### ###### # # #
; # # # # # # # # # # # # # # #
; # # # #### #### # # # # # # ####
;
; ########################################################
;; Attributes defined on programs
;
; out .lexical_scope
; Lexical scope.
;
; in .defs
; Lexical and variable definitions.
;
; in .exports
; Exported definitions.
;
; out .mod_def
; Module definition
(program)@prog {
node @prog.defs
node @prog.exports
node @prog.lexical_scope
node @prog.mod_def
node @prog.mod_def__ns
}
(program)@prog {
; propagate lexical scope
edge @prog.lexical_scope -> ROOT_NODE
; expose definitions
edge @prog.lexical_scope -> @prog.defs
; module definition in ROOT_NODE scope
edge ROOT_NODE -> @prog.mod_def__ns
;
attr (@prog.mod_def__ns) pop_symbol = "%M"
edge @prog.mod_def__ns -> @prog.mod_def
;
let mod_name = (path-filestem FILE_PATH)
let mod_path = (path-normalize (path-join (path-dir FILE_PATH) mod_name))
attr (@prog.mod_def) symbol_definition = mod_path, source_node = @prog
; expose exports via module definition
edge @prog.mod_def -> @prog.exports
; if this is an index.ts, also add a definition for the directory of this file
scan mod_name {
"index" {
node mod_index_def
edge @prog.mod_def__ns -> mod_index_def
let mod_dir = (path-normalize (path-dir FILE_PATH))
attr (mod_index_def) symbol_definition = mod_dir, source_node = @prog
edge mod_index_def -> @prog.exports
}
}
}
(program
(_)@stmt
)@prog {
; propagate lexical scope
edge @stmt.lexical_scope -> @prog.lexical_scope
; expose lexical and variable declarations
edge @prog.defs -> @stmt.lexical_defs
edge @prog.defs -> @stmt.var_defs
; exports are visible via module definition
edge @prog.exports -> @stmt.exports
}
(program [(import_statement) (export_statement)]* @imexs)@prog {
if (is-empty @imexs) {
; expose global definitions in ROOT_NODE
edge ROOT_NODE -> @prog.defs
}
}
;; hashbang
(hash_bang_line)@hashbang {
}
;; Comments
(comment)@comment {
}
; #####
; # # ##### ## ##### ###### # # ###### # # ##### ####
; # # # # # # ## ## # ## # # #
; ##### # # # # ##### # ## # ##### # # # # ####
; # # ###### # # # # # # # # # #
; # # # # # # # # # # # ## # # #
; ##### # # # # ###### # # ###### # # # ####
;
; ###################################################################
;; Attributes defined on statements
;
; in .lexical_scope
; Lexical scope.
;
; out .lexical_defs
; Lexical definitions, that are block scoped
;
; out .var_defs
; Variable definitions, that are function scoped
;
; out .exports
; Exported definitions.
;
; out .default_export
; Nameless (but namespaced) exports for use in default and = exports.
;
; out .return_type
; Type of contained return statement.
[
(abstract_class_declaration)
(ambient_declaration)
(break_statement)
(catch_clause)
(class_declaration)
(continue_statement)
(debugger_statement)
(do_statement)
(else_clause)
(empty_statement)
(enum_declaration)
(export_statement)
(expression_statement)
(finally_clause)
(for_in_statement)
(for_statement)
(function_declaration)
(function_signature)
(generator_function_declaration)
(hash_bang_line)
(if_statement)
(import_statement)
(interface_declaration)
(internal_module)
(labeled_statement)
(lexical_declaration)
(module)
(return_statement)
(statement_block)
(switch_body)
(switch_case)
(switch_default)
(switch_statement)
(throw_statement)
(try_statement)
(type_alias_declaration)
(variable_declaration)
(while_statement)
(with_statement)
]@stmt {
node @stmt.default_export
node @stmt.exports
node @stmt.lexical_defs
node @stmt.lexical_scope
node @stmt.return_type
node @stmt.var_defs
}
;; Imports & Exports (The Many Faced God)
;
; TypeScript has many import / export forms. Below is a condensed presentation that can be used as a reference
; for the sections below where the actual rules are defined. One has to be careful with the queries to ensure
; that cases do not overlap, even if their syntactic forms (partially) do. This must be done by adding negative
; matches to some queries. This overview should make it easier to spot where negative are required.
;
;;;; Exports
;
; export_statement :=
; (export_statement "*" source:(_)@from) ; export * from MODULE ; re-export everything from MODULE
; | (export_statement (namespace_export (identifier)@ns) source:(_)@from) ; export * as NAMESPACE from MODULE ; re-export MODULE as NAMESPACE
; | (export_statement "default" value:(_)@value) ; export default EXPRESSION ; export EXPRESSION as default
; | (export_statement "default" declaration:(_)@decl) ; export default DECLARATION ; export DECLARATION as default
; | (export_statement ^"default" declaration:(_)@decl) ; export DECL ; export DECLARATION
; | (export_statement "type"? (export_clause)@clause source:(_)@from) ; export (type) CLAUSE from MODULE ; re-export (type) identifiers from MODULE
; | (export_statement "type"? (export_clause)@clause !source ) ; export (type) CLAUSE ; export (type) identifiers
; | (export_statement "=" . (expression)@value) ; export = EXPRESSION ; export EXPRESSION as exports object
; | (export_statement "as" "namespace" . (_)@ns) ; export as namespace NAMESPACE ; export this module as global NAMESPACE
;
; export_clause :=
; (export_clause (export_specifier)*) ; { EXPORT_SPECIFIER* }
;
; export_specifier :=
; (export_specifier name:(_) alias:(_)) ; NAME
; | (export_specifier name:(_) !alias ) ; NAME as ALIAS
;
;;;; Imports
;
; import_statement :=
; ; note that the first case overlaps with the two after it, but the behaviour is orthogonal
; (import_statement "type"? (import_clause (identifier)@name ) source:(_)@from) ; import (type) NAME from MODULE ; import default (type) from MODULE as NAME
; | (import_statement "type"? (import_clause (namespace_import (identifier)@ns)) source:(_)@from) ; import (type) * as NAMESPACE from MODULE ; import all (type) identifiers from MODULE in NAMESPACE
; | (import_statement "type"? (import_clause (named_imports)@named_imports ) source:(_)@from) ; import (type) NAMED_IMPORTS from MODULE ; import named (type) identifiers from MODULE
; | (import_statement "type"? ^(import_clause) source:(_)@from) ; import (type) MODULE ; do not import any identifiers from MODULE
; | (import_statement "type"? (import_require_clause (identifier)@name source:(_)@from)) ; import (type) NAME = require(MODULE) ; import exports object (type) as NAME from MODULE
;
; named_imports :=
; (named_imports (import_specifier)*) ; { IMPORT_SPECIFIER* }
;
; import_specifier :=
; (import_specifier name:(_) alias:(_)) ; NAME
; (import_specifier name:(_) !alias ) ; NAME as ALIAS
;
; Note that the grammar productions for imports and exports are factored slightly
; different. The statements above are inlined such that corresponding import and
; export statements all live in the top-level list. Be mindful that (export_clause)
; corresponds to (import_clause (named_imports)) and not to (import_cluase)!
;
; The presence of a "type" modifier does not fundamentally change the way a certain
; export/import variant works, only which identifiers are included in the import/export.
; Therefore, we do not handle that as different export/export variants, but deal with that
; in the rules for the various clauses, which already determine the identifiers involved.
;; Module References
[
(export_statement source:(_)@from )
(import_statement source:(_)@from )
(import_statement (import_require_clause source:(_)@from))
]@import_stmt {
node @from.mod_ref
node @from.mod_ref__ns
scan (replace (source-text @from) "[\"\']" "") {
"^/.*$" {
; absolute paths are not supported
}
"^(\.|\.\.)/.*$" {
; relative paths
; module reference
let mod_name = (path-normalize (path-join (path-dir FILE_PATH) $0))
attr (@from.mod_ref) symbol_reference = mod_name, source_node = @import_stmt
edge @from.mod_ref -> @from.mod_ref__ns
;
attr (@from.mod_ref__ns) push_symbol = "%M"
edge @from.mod_ref__ns -> ROOT_NODE
}
"^.+$" {
; non-relative paths
; FIXME
; relative to baseUrl, ambient module declaration, path mapping
}
}
}
;; Named Clauses
[
(export_statement "type"? (export_clause )@clause)
(import_statement "type"? (import_clause (named_imports)@clause))
] {
node @clause.defs
node @clause.exports
node @clause.lexical_defs
node @clause.lexical_scope
}
;;;; Types
[
(export_statement "type"? (export_clause (export_specifier name:(_)@name))@clause)
(import_statement "type"? (import_clause (named_imports (import_specifier name:(_)@name))@clause))
] {
node @name.type_def
node @name.type_def__ns
node @name.type_ref
node @name.type_ref__ns
; type reference
attr (@name.type_ref) node_reference = @name
edge @name.type_ref -> @name.type_ref__ns
;
attr (@name.type_ref__ns) push_symbol = "%T"
edge @name.type_ref__ns -> @clause.lexical_scope
}
[
(export_statement "type"? (export_clause (export_specifier name:(_)@name !alias))@clause)
(import_statement "type"? (import_clause (named_imports (import_specifier name:(_)@name !alias))@clause))
] {
; type definition
edge @clause.defs -> @name.type_def__ns ; FIXME defs, lexical_defs?
;
attr (@name.type_def__ns) pop_symbol = "%T"
edge @name.type_def__ns -> @name.type_def
;
attr (@name.type_def) node_definition = @name
edge @name.type_def -> @name.type_ref
}
[
(export_statement "type"? (export_clause (export_specifier name:(_)@name alias:(_)@alias))@clause)
(import_statement "type"? (import_clause (named_imports (import_specifier name:(_)@name alias:(_)@alias))@clause))
] {
node @alias.type_def
node @alias.type_def__ns
node @alias.type_ref
; type definition
edge @clause.defs -> @alias.type_def__ns
;
attr (@alias.type_def__ns) pop_symbol = "%T"
edge @alias.type_def__ns -> @alias.type_def
;
attr (@alias.type_def) node_definition = @alias
edge @alias.type_def -> @name.type_ref
}
;;;; Expressions
[
(export_statement "type"?@is_type (export_clause (export_specifier name:(_)@name))@clause)
(import_statement "type"?@is_type (import_clause (named_imports (import_specifier name:(_)@name))@clause))
] {
if none @is_type {
node @name.expr_def
node @name.expr_def__ns
node @name.expr_ref
node @name.expr_ref__ns
; expr reference
attr (@name.expr_ref) node_reference = @name
edge @name.expr_ref -> @name.expr_ref__ns
;
attr (@name.expr_ref__ns) push_symbol = "%E"
edge @name.expr_ref__ns -> @clause.lexical_scope
}
}
[
(export_statement "type"?@is_type (export_clause (export_specifier name:(_)@name !alias))@clause)
(import_statement "type"?@is_type (import_clause (named_imports (import_specifier name:(_)@name !alias))@clause))
] {
if none @is_type {
; expr definition
edge @clause.defs -> @name.expr_def__ns ; FIXME defs, lexical_defs?
;
attr (@name.expr_def__ns) pop_symbol = "%E"
edge @name.expr_def__ns -> @name.expr_def
;
attr (@name.expr_def) node_definition = @name
edge @name.expr_def -> @name.expr_ref
}
}
[
(export_statement "type"?@is_type (export_clause (export_specifier name:(_)@name alias:(_)@alias))@clause)
(import_statement "type"?@is_type (import_clause (named_imports (import_specifier name:(_)@name alias:(_)@alias))@clause))
] {
if none @is_type {
node @alias.expr_def
node @alias.expr_def__ns
node @alias.expr_ref
; expr definition
edge @clause.defs -> @alias.expr_def__ns
;
attr (@alias.expr_def__ns) pop_symbol = "%E"
edge @alias.expr_def__ns -> @alias.expr_def
;
attr (@alias.expr_def) node_definition = @alias
edge @alias.expr_def -> @name.expr_ref
}
}
;; Exports
; export * from from MODULE
;
; (export_statement
; "*" source:(string)@name
; )@export_stmt
(export_statement
"*" source:(_)@name
)@export_stmt {
; export everything from source module
edge @export_stmt.exports -> @name.mod_ref
}
; export CLAUSE from MODULE
; export type CLAUSE from MODULE
;
; (export_statement
; "type"? (export_clause)@clause source:(_)@name
; )@export_stmt
(export_statement
"type"? (export_clause)@clause source:(_)@name
)@export_stmt {
; propagate lexical scope
edge @clause.lexical_scope -> @name.mod_ref
; propagate exports
edge @export_stmt.exports -> @clause.defs
}
; export CLAUSE
; export type CLAUSE
;
; (export_statement
; "type"?
; (export_clause)@clause
; )@export_stmt
(export_statement
"type"?
(export_clause)@clause
!source
)@export_stmt {
; propagate lexical scope
edge @clause.lexical_scope -> @export_stmt.lexical_scope
; propagate exports
edge @export_stmt.exports -> @clause.defs
}
; export DECL
;
; (export_statement
; ^"default"
; declaration:(_)@decl
; )@export_stmt
(export_statement
"default"?@is_default
declaration:(_)@decl
)@export_stmt {
if none @is_default {
; propagate lexical scope
edge @decl.lexical_scope -> @export_stmt.lexical_scope
; expose lexical definitions
edge @export_stmt.lexical_defs -> @decl.lexical_defs
; expose variable definitions
edge @export_stmt.var_defs -> @decl.var_defs
; export the definitions
edge @export_stmt.exports -> @decl.lexical_defs
edge @export_stmt.exports -> @decl.var_defs
}
}
; export default DECL
;
; (export_statement
; "default"
; declaration:(_)@decl
; )@export_stmt
; export default EXPR
;
; (export_statement
; "default"
; value:(_)@expr
; )@export_stmt
; export = NAME
;
; (export_statement
; "="
; (expression)@expr
; )@export_stmt
; NOTE Default exports are modeled as variables named "default", because the grammar
; treats "default" as a regular identifier instead of a keyword. This approach
; ensures renaming imports such as `import { default as X } from ...` work correctly.
;
; Default exports are generally expressions. However, if the export is specified as
; an identifier, the type with that name is also exported.
(export_statement
["default" "="]
)@export_stmt {
node @export_stmt.default_def
node @export_stmt.default_expr_def
node @export_stmt.default_expr_def__ns_pop
node @export_stmt.default_expr_def__ns_push
node @export_stmt.default_type_def
node @export_stmt.default_type_def__ns_pop
node @export_stmt.default_type_def__ns_push
; export as default expr
edge @export_stmt.exports -> @export_stmt.default_expr_def__ns_pop
;
attr (@export_stmt.default_expr_def__ns_pop) pop_symbol = "%E"
edge @export_stmt.default_expr_def__ns_pop -> @export_stmt.default_expr_def
;
;; @export_stmt.default_expr_def pop_symbol set below for "default" and "="
edge @export_stmt.default_expr_def -> @export_stmt.default_expr_def__ns_push
;
attr (@export_stmt.default_expr_def__ns_push) push_symbol = "%E"
edge @export_stmt.default_expr_def__ns_push -> @export_stmt.default_def
; export as default type
edge @export_stmt.exports -> @export_stmt.default_type_def__ns_pop
;
attr (@export_stmt.default_type_def__ns_pop) pop_symbol = "%T"
edge @export_stmt.default_type_def__ns_pop -> @export_stmt.default_type_def
;
;; @export_stmt.default_expr_def pop_symbol set below for "default" and "="
edge @export_stmt.default_type_def -> @export_stmt.default_type_def__ns_push
;
attr (@export_stmt.default_type_def__ns_push) push_symbol = "%T"
edge @export_stmt.default_type_def__ns_push -> @export_stmt.default_def
}
(export_statement
"default"
)@export_stmt {
attr (@export_stmt.default_expr_def) symbol_definition = "default", source_node = @export_stmt
attr (@export_stmt.default_type_def) symbol_definition = "default", source_node = @export_stmt
}
(export_statement
"="
)@export_stmt {
attr (@export_stmt.default_expr_def) symbol_definition = "<exports>", source_node = @export_stmt
attr (@export_stmt.default_type_def) symbol_definition = "<exports>", source_node = @export_stmt
}
(export_statement
"default"
declaration:(_)@decl
)@export_stmt {
; propagate lexical scope
edge @decl.lexical_scope -> @export_stmt.lexical_scope
; expose lexical definitions
edge @export_stmt.lexical_defs -> @decl.lexical_defs
; expose variable definitions
edge @export_stmt.var_defs -> @decl.var_defs
; export as default
edge @export_stmt.default_def -> @decl.default_export
}
[
(export_statement "default" value:(_)@expr)@export_stmt
(export_statement "=" . (_)@expr)@export_stmt
] {
node @export_stmt.default_expr__ns
node @export_stmt.default_expr__typeof
; propagate lexical scope
edge @expr.lexical_scope -> @export_stmt.lexical_scope
; expose lexical definitions
edge @export_stmt.lexical_defs -> @expr.lexical_defs
; expose variable definitions
edge @export_stmt.var_defs -> @expr.var_defs
; export expression as default
edge @export_stmt.default_def -> @export_stmt.default_expr__ns
;
attr (@export_stmt.default_expr__ns) pop_symbol = "%E"
edge @export_stmt.default_expr__ns -> @export_stmt.default_expr__typeof
;
attr (@export_stmt.default_expr__typeof) pop_symbol = ":"
edge @export_stmt.default_expr__typeof -> @expr.type
}
; NOTE an identifier in a default export can also be a type reference
[
(export_statement "default" value:(identifier)@name)@export_stmt
(export_statement "=" . (identifier)@name)@export_stmt
] {
node @export_stmt.default_type__ns
node @name.type_ref
node @name.type_ref__ns
; export type as default
edge @export_stmt.default_def -> @export_stmt.default_type__ns
;
attr (@export_stmt.default_type__ns) pop_symbol = "%T"
edge @export_stmt.default_type__ns -> @name.type_ref
;
attr (@name.type_ref) node_reference = @name
edge @name.type_ref -> @name.type_ref__ns
;
attr (@name.type_ref__ns) push_symbol = "%T"
edge @name.type_ref__ns -> @export_stmt.lexical_scope
}
; export * as namespace NAME
; (export_statement
; (namespace_export (identifier)@ns)
; source:(_)@from
; )
(export_statement
(namespace_export (identifier)@ns)
source:(_)@name
)@export_stmt {
; namespace definitions are specified together with (module) and (internal_module)
; export definitions
edge @export_stmt.exports -> @name.expr_def__ns
edge @export_stmt.exports -> @name.type_def__ns
; connect definitions to exports
edge @export_stmt.expr_member__ns -> @name.mod_ref
edge @export_stmt.type_member__ns -> @name.mod_ref
}
; export as namespace NAME
;
; (export_statement
; "as"
; "namespace"
; (identifier)@name
; )@export_stmt
(program (export_statement
"as" "namespace" . (_)@name
)@export_stmt)@program {
; namespace definitions are specified together with (module) and (internal_module)
; make definitions global
edge ROOT_NODE -> @name.expr_def__ns
edge ROOT_NODE -> @name.type_def__ns
; connect definitions to exports
edge @export_stmt.expr_member__ns -> @program.exports
edge @export_stmt.type_member__ns -> @program.exports
}
;; Imports
; import MODULE
;
; (import_statement
; ^(import_clause)
; source:(_)@from
; )@import_stmt
(import_statement
"type"?
(import_clause)?@has_clause
source:(_)@from
)@import_stmt {
if none @has_clause {
; do nothing, imported for side-effect only
}
}
; import (type) NAMED_IMPORTS from MODULE
(import_statement
"type"?
(import_clause (named_imports)@clause)
source:(_)@from
)@import_stmt {
; use module for lexical scope of import clause
edge @clause.lexical_scope -> @from.mod_ref
; expose imported definitions
edge @import_stmt.lexical_defs -> @clause.defs
}
; import (type) NAME from MODULE
; import (type) NAME = require(MODULE)
[
(import_statement "type"? (import_clause (identifier)) source:(_)@from)@import_stmt
(import_statement "type"? (import_require_clause (identifier) source:(_)@from))@import_stmt
] {
node @import_stmt.default_ref
; connect default refererence to module
edge @import_stmt.default_ref -> @from.mod_ref
}
;;;; Types
[
(import_statement "type"? (import_clause (identifier)@name))@import_stmt
(import_statement "type"? (import_require_clause (identifier)@name))@import_stmt
] {
node @import_stmt.type_def
node @import_stmt.type_def__ns
node @import_stmt.default_type_ref
node @import_stmt.default_type_ref__ns
; type definition
edge @import_stmt.lexical_defs -> @import_stmt.type_def__ns
;
attr (@import_stmt.type_def__ns) pop_symbol = "%T"
edge @import_stmt.type_def__ns -> @import_stmt.type_def
;
attr (@import_stmt.type_def) node_definition = @name
edge @import_stmt.type_def -> @import_stmt.default_type_ref
;
;; @import_stmt.default_type_ref pop_symbol set below for "default" and "="
edge @import_stmt.default_type_ref -> @import_stmt.default_type_ref__ns
;
attr (@import_stmt.default_type_ref__ns) push_symbol = "%T"
edge @import_stmt.default_type_ref__ns -> @import_stmt.default_ref
}
(import_statement "type"? (import_clause (identifier)))@import_stmt {
attr (@import_stmt.default_type_ref) symbol_reference = "default", source_node = @import_stmt
}
(import_statement "type"? (import_require_clause (identifier)))@import_stmt {
attr (@import_stmt.default_type_ref) symbol_reference = "<exports>", source_node = @import_stmt
}
;;;; Expressions
[
(import_statement "type"?@is_type (import_clause (identifier)@name))@import_stmt
(import_statement "type"?@is_type (import_require_clause (identifier)@name))@import_stmt
] {
if none @is_type {
node @import_stmt.expr_def
node @import_stmt.expr_def__ns
node @import_stmt.default_expr_ref
node @import_stmt.default_expr_ref__ns
; expr definition
edge @import_stmt.lexical_defs -> @import_stmt.expr_def__ns
;
attr (@import_stmt.expr_def__ns) pop_symbol = "%E"
edge @import_stmt.expr_def__ns -> @import_stmt.expr_def
;
attr (@import_stmt.expr_def) node_definition = @name
edge @import_stmt.expr_def -> @import_stmt.default_expr_ref
;
;; @import_stmt.default_expr_ref pop_symbol set below for "default" and "="
edge @import_stmt.default_expr_ref -> @import_stmt.default_expr_ref__ns
;
attr (@import_stmt.default_expr_ref__ns) push_symbol = "%E"
edge @import_stmt.default_expr_ref__ns -> @import_stmt.default_ref
}
}
(import_statement "type"?@is_type (import_clause (identifier)))@import_stmt {
if none @is_type {
attr (@import_stmt.default_expr_ref) symbol_reference = "default", source_node = @import_stmt
}
}
(import_statement "type"?@is_type (import_require_clause (identifier)))@import_stmt {
if none @is_type {
attr (@import_stmt.default_expr_ref) symbol_reference = "<exports>", source_node = @import_stmt
}
}
; import (type) * as NAMESPACE from MODULE
(import_statement
"type"?
(import_clause (namespace_import (identifier)@name))
source:(_)@from
)@import_stmt {
; namespace definitions are specified together with (module) and (internal_module)
; connect definitions to import
edge @import_stmt.expr_member__ns -> @from.mod_ref
edge @import_stmt.type_member__ns -> @from.mod_ref
}
;; Import Alias
; import ID = ID | NESTED_ID
; (import_alias
; (identifier)
; [(identifier) (nested_identifier)]
; ) {}
;; Debugger
; debugger;
; debugger
(debugger_statement)@debugger_stmt {
}
;; Expression
; 1;
(expression_statement (_)@expr)@expr_stmt {
; propagate lexical scope
edge @expr.lexical_scope -> @expr_stmt.lexical_scope
}
;; Declaration
;; Variable Declarations
; var foo;
; var x = 1;
; var x, y = {}, z;
; let baz = bar;
; const quux = baz;
(variable_declaration (variable_declarator)@declor)@decl {
; propagate lexical scope
edge @declor.lexical_scope -> @decl.lexical_scope
; local definitions
edge @decl.var_defs -> @declor.defs
; default export
edge @decl.default_export -> @declor.default_export
}
(lexical_declaration (variable_declarator)@declor)@decl {
; propagate lexical scope
edge @declor.lexical_scope -> @decl.lexical_scope
; local definitions
edge @decl.lexical_defs -> @declor.defs
; default export
edge @decl.default_export -> @declor.default_export
}
;; Variable Declarator
(variable_declarator)@decl {
node @decl.default_export
node @decl.defs
node @decl.lexical_scope
}
(variable_declarator
name:(_)@pat
)@decl {
node @decl.expr_export
node @decl.type_export
; propagate lexical scope
edge @pat.lexical_scope -> @decl.lexical_scope
; local definitions from pattern
edge @decl.defs -> @pat.defs
; default export
edge @decl.default_export -> @decl.expr_export
;
attr (@decl.expr_export) pop_symbol = "%E"
}
(variable_declarator
name:(_)@pat