Skip to content

Commit de29bdd

Browse files
authored
Add almost testable comments to .scm files (#1535)
See #1524 (comment) ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent 95f605b commit de29bdd

File tree

5 files changed

+116
-41
lines changed

5 files changed

+116
-41
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.function.scm

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
;; Anonymous functions
22
[
3-
;; function() {}
3+
;;!! function() {}
44
(function
55
!name
66
)
77

8-
;; function *() {}
8+
;;!! function *() {}
99
(generator_function
1010
!name
1111
)
1212

13-
;; () => {}
13+
;;!! () => {}
1414
(arrow_function)
1515
] @anonymousFunction
1616

1717
;; If we export an anonymous function as default, it semantically feels like a
1818
;; named function.
1919
(export_statement
2020
[
21-
;; export default function() {}
21+
;;!! export default function() {}
2222
(function
2323
!name
2424
)
2525

26-
;; export default function *() {}
26+
;;!! export default function *() {}
2727
(generator_function
2828
!name
2929
)
3030

31-
;; export default () => {}
31+
;;!! export default () => {}
3232
(arrow_function)
3333
]
3434
) @namedFunction
3535

3636
;; Named functions without export
3737
(
3838
[
39-
;; function foo() {}
39+
;;!! function foo() {}
4040
(function_declaration
4141
name: (_) @functionName
4242
)
4343

44-
;; function *foo() {}
44+
;;!! function *foo() {}
4545
(generator_function_declaration
4646
name: (_) @functionName
4747
)
4848

49-
;; (let | const) foo = () => {}
50-
;; (let | const) foo = function() {}
51-
;; (let | const) foo = function *() {}
49+
;;!! (let | const) foo = () => {}
50+
;;!! (let | const) foo = function() {}
51+
;;!! (let | const) foo = function *() {}
5252
(lexical_declaration
5353
(variable_declarator
5454
name: (_) @functionName
@@ -64,9 +64,9 @@
6464
)
6565
)
6666

67-
;; var foo = () => {}
68-
;; var foo = function() {}
69-
;; var foo = function *() {}
67+
;;!! var foo = () => {}
68+
;;!! var foo = function() {}
69+
;;!! var foo = function *() {}
7070
;; Note that we can't merge this with the variable declaration above because
7171
;; of https://github.com/tree-sitter/tree-sitter/issues/1442#issuecomment-1584628651
7272
(variable_declaration
@@ -90,19 +90,19 @@
9090
;; Exported named functions
9191
(export_statement
9292
[
93-
;; export [default] function foo() {}
93+
;;!! export [default] function foo() {}
9494
(function_declaration
9595
name: (_) @functionName
9696
)
9797

98-
;; export [default] function *foo() {}
98+
;;!! export [default] function *foo() {}
9999
(generator_function_declaration
100100
name: (_) @functionName
101101
)
102102

103-
;; export [default] (let | const | var) foo = () => {}
104-
;; export [default] (let | const | var) foo = function() {}
105-
;; export [default] (let | const | var) foo = function *() {}
103+
;;!! export [default] (let | const | var) foo = () => {}
104+
;;!! export [default] (let | const | var) foo = function() {}
105+
;;!! export [default] (let | const | var) foo = function *() {}
106106
(_
107107
(variable_declarator
108108
name: (_) @functionName
@@ -125,25 +125,25 @@
125125
;; We also don't handle function declarations that only exist in Javascript;
126126
;; see javascript.scm.
127127
[
128-
;; (function foo() {})
128+
;;!! (function foo() {})
129129
(function
130130
name: (_) @functionName
131131
)
132132

133-
;; (function *foo() {})
133+
;;!! (function *foo() {})
134134
(generator_function
135135
name: (_) @functionName
136136
)
137137

138-
;; foo() {}
139-
;; (in class bodies)
138+
;;!! class Foo { foo() {} }
139+
;;! ^^^^^^^^
140140
(method_definition
141141
name: (_) @functionName
142142
)
143143

144-
;; foo = () => {};
145-
;; foo = function() {};
146-
;; foo = function *() {};
144+
;;!! foo = () => {};
145+
;;!! foo = function() {};
146+
;;!! foo = function *() {};
147147
(assignment_expression
148148
left: (_) @functionName
149149
right: [

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

0 commit comments

Comments
 (0)