Skip to content

Commit 38ee670

Browse files
committed
Add almost testable comments to .scm files
1 parent 0fe1b84 commit 38ee670

File tree

4 files changed

+90
-15
lines changed

4 files changed

+90
-15
lines changed

queries/javascript.core.scm

+33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
;; import javascript.function.scm
22

3+
;; `name` scope without `export`
34
(
45
(_
56
name: (_) @name
67
) @_.domain
78
(#not-parent-type? @_.domain export_statement)
9+
10+
;; We have special cases for these defined elsewhere
811
(#not-type?
912
@_.domain
1013
variable_declarator
@@ -14,20 +17,33 @@
1417
field_definition
1518
)
1619
)
20+
21+
;; `name` scope with `export`
1722
(export_statement
1823
(_
1924
name: (_) @name
2025
) @dummy
26+
27+
;; We have a special case for this one. Note we don't need to list the other
28+
;; special cases from above because they can't be exported
2129
(#not-type? @dummy variable_declarator)
2230
) @_.domain
2331

32+
;; Special cases for `(let | const | var) foo = ...;` because the full statement
33+
;; is actually a grandparent of the `name` node, so we want the domain to include
34+
;; this full grandparent statement.
2435
(
2536
[
37+
;;!! (const | let) foo = ...;
38+
;;! --------------^^^-------
2639
(lexical_declaration
2740
(variable_declarator
2841
name: (_) @name
2942
)
3043
)
44+
45+
;;!! var foo = ...;
46+
;;! ----^^^-------
3147
;; Note that we can't merge this with the variable declaration above because
3248
;; of https://github.com/tree-sitter/tree-sitter/issues/1442#issuecomment-1584628651
3349
(variable_declaration
@@ -37,24 +53,38 @@
3753
)
3854
] @_.domain
3955
(#not-parent-type? @_.domain export_statement)
56+
57+
;; Handle multiple variable declarators in one statement, eg
58+
;;!! (let | const | var) aaa = ..., ccc = ...;
59+
;;! --------------------^^^--------^^^-------
4060
(#allow-multiple! @name)
4161
)
4262

4363
(
4464
(export_statement
4565
(_
66+
;;!! export [default] (let | const | var) foo = ...;
67+
;;! -------------------------------------^^^-------
4668
(variable_declarator
4769
name: (_) @name
4870
)
4971
)
5072
) @_.domain
73+
74+
;; Handle multiple variable declarators in one statement, eg
75+
;;!! var foo = ..., bar = ...;
76+
;;! ----^^^--------^^^-------
5177
(#allow-multiple! @name)
5278
)
5379

80+
;;!! foo += ...;
81+
;;! ^^^--------
5482
(augmented_assignment_expression
5583
left: (_) @name
5684
) @_.domain
5785

86+
;;!! foo = ...;
87+
;;! ^^^-------
5888
(assignment_expression
5989
left: (_) @name
6090
) @_.domain
@@ -64,6 +94,9 @@
6494
(formal_parameters)
6595
] @name.iteration
6696

97+
;; Treat interior of all bodies as iteration scopes for `name`, eg
98+
;;!! function foo() { }
99+
;;! ***
67100
(
68101
(_
69102
body: (_

queries/javascript.jsx.scm

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,82 @@
1+
;;!! <foo>bar</foo>
2+
;;! ^^^^^^^^^^^^^^
3+
;;! ###
4+
;;! ***
15
(
26
(jsx_element) @xmlElement @_.interior @_.iteration
37
(#child-range! @_.interior 0 -1 true true)
48
(#child-range! @_.iteration 0 -1 true true)
59
)
610

11+
;;!! <foo>bar</foo>
12+
;;! ***
713
(
814
(jsx_element) @xmlStartTag.iteration @xmlEndTag.iteration @xmlBothTags.iteration
915
(#child-range! @xmlStartTag.iteration 0 -1 true true)
1016
(#child-range! @xmlEndTag.iteration 0 -1 true true)
1117
(#child-range! @xmlBothTags.iteration 0 -1 true true)
1218
)
1319

20+
;;!! <foo>bar</foo>
21+
;;! ^^^^^---------
1422
(jsx_element
1523
(jsx_opening_element) @xmlStartTag @xmlBothTags
1624
(#allow-multiple! @xmlBothTags)
1725
) @_.domain
1826

27+
;;!! <foo>bar</foo>
28+
;;! --------^^^^^^
1929
(jsx_element
2030
(jsx_closing_element) @xmlEndTag @xmlBothTags
2131
(#allow-multiple! @xmlBothTags)
2232
) @_.domain
2333

34+
;;!! <foo/>
2435
(jsx_self_closing_element) @xmlElement
2536

26-
;; JSX fragments, eg <>foo</>
37+
;; ======== JSX fragments, eg <>foo</> ==========
38+
39+
;;!! <>foo</>
40+
;;! ^^^^^^^^
41+
;;! ###
42+
;;! ***
2743
(
2844
(jsx_fragment) @xmlElement @_.interior @_.iteration
2945
(#child-range! @_.interior 1 -3 true true)
3046
(#child-range! @_.iteration 1 -3 true true)
3147
)
3248

49+
;;!! <>foo</>
50+
;;! ***
3351
(
3452
(jsx_fragment) @xmlStartTag.iteration @xmlEndTag.iteration @xmlBothTags.iteration
3553
(#child-range! @xmlStartTag.iteration 1 -3 true true)
3654
(#child-range! @xmlEndTag.iteration 1 -3 true true)
3755
(#child-range! @xmlBothTags.iteration 1 -3 true true)
3856
)
3957

58+
;;!! <>foo</>
59+
;;! ^^------
4060
(
4161
(jsx_fragment) @xmlStartTag @xmlBothTags @_.domain
4262
(#child-range! @xmlStartTag 0 1)
4363
(#child-range! @xmlBothTags 0 1)
4464
(#allow-multiple! @xmlBothTags)
4565
)
4666

67+
;;!! <>foo</>
68+
;;! -----^^^
4769
(
4870
(jsx_fragment) @xmlEndTag @xmlBothTags @_.domain
4971
(#child-range! @xmlEndTag -3)
5072
(#child-range! @xmlBothTags -3)
5173
(#allow-multiple! @xmlBothTags)
5274
)
5375

76+
;; Sets `name` to be empty range inside the fragment tag:
77+
;;!! <>foo</>
78+
;;! {} {}
79+
;;! -- ---
5480
(
5581
(jsx_fragment
5682
"<" @_.domain.start

queries/javascript.scm

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
;; Define this here because the `field_definition` node type doesn't exist
88
;; in typescript.
99
(
10-
;; foo = () => {};
11-
;; foo = function() {};
12-
;; foo = function *() {};
13-
;; (inside class bodies)
10+
;;!! class Foo {
11+
;;!! foo = () => {};
12+
;;! ^^^^^^^^^^^^^^^
13+
;;!! foo = function() {};
14+
;;! ^^^^^^^^^^^^^^^^^^^^
15+
;;!! foo = function *() {};
16+
;;! ^^^^^^^^^^^^^^^^^^^^^^
17+
;;!! }
1418
(field_definition
1519
property: (_) @functionName
1620
value: [
@@ -28,7 +32,8 @@
2832
)
2933

3034
(
31-
;; foo = ...;
35+
;;!! foo = ...;
36+
;;! ^^^-------
3237
(field_definition
3338
property: (_) @name
3439
) @name.domain.start

queries/typescript.scm

+20-9
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,43 @@
44

55
;; import javascript.core.scm
66

7+
;;!! function aaa(bbb?: Ccc = "ddd") {}
8+
;;! ^^^--------------
79
(optional_parameter
810
(identifier) @name
911
) @_.domain
1012

13+
;;!! function aaa(bbb: Ccc = "ddd") {}
14+
;;! ^^^-------------
1115
(required_parameter
1216
(identifier) @name
1317
) @_.domain
1418

1519
;; Define these here because these node types don't exist in javascript.
1620
(
1721
[
18-
;; foo(): void;
19-
;; (in interface)
20-
;; foo() {}
21-
;; (in class)
22+
;;!! class Foo { foo() {} }
23+
;;! ^^^^^^^^
24+
;;!! interface Foo { foo(): void; }
25+
;;! ^^^^^^^^^^^^
2226
(method_signature
2327
name: (_) @functionName @name
2428
)
2529

26-
;; abstract foo(): void;
30+
;;!! class Foo { abstract foo(): void; }
31+
;;! ^^^^^^^^^^^^^^^^^^^^^
2732
(abstract_method_signature
2833
name: (_) @functionName @name
2934
)
3035

31-
;; [public | private | protected] foo = () => {};
32-
;; [public | private | protected] foo = function() {};
33-
;; [public | private | protected] foo = function *() {};
36+
;;!! class Foo {
37+
;;!! (public | private | protected) foo = () => {};
38+
;;! ^^^^^^^^^^^^^^^
39+
;;!! (public | private | protected) foo = function() {};
40+
;;! ^^^^^^^^^^^^^^^^^^^^
41+
;;!! (public | private | protected) foo = function *() {};
42+
;;! ^^^^^^^^^^^^^^^^^^^^^^
43+
;;!! }
3444
(public_field_definition
3545
name: (_) @functionName
3646
value: [
@@ -49,7 +59,8 @@
4959
)
5060

5161
(
52-
;; [public | private | protected] foo = ...;
62+
;;!! (public | private | protected) foo = ...;
63+
;;! -------------------------------^^^-------
5364
(public_field_definition
5465
name: (_) @name
5566
) @name.domain.start

0 commit comments

Comments
 (0)