Skip to content

Commit

Permalink
Add almost testable comments to .scm files
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Jun 16, 2023
1 parent 0fe1b84 commit 38ee670
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
33 changes: 33 additions & 0 deletions queries/javascript.core.scm
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
;; import javascript.function.scm

;; `name` scope without `export`
(
(_
name: (_) @name
) @_.domain
(#not-parent-type? @_.domain export_statement)

;; We have special cases for these defined elsewhere
(#not-type?
@_.domain
variable_declarator
Expand All @@ -14,20 +17,33 @@
field_definition
)
)

;; `name` scope with `export`
(export_statement
(_
name: (_) @name
) @dummy

;; We have a special case for this one. Note we don't need to list the other
;; special cases from above because they can't be exported
(#not-type? @dummy variable_declarator)
) @_.domain

;; Special cases for `(let | const | var) foo = ...;` because the full statement
;; is actually a grandparent of the `name` node, so we want the domain to include
;; this full grandparent statement.
(
[
;;!! (const | let) foo = ...;
;;! --------------^^^-------
(lexical_declaration
(variable_declarator
name: (_) @name
)
)

;;!! var foo = ...;
;;! ----^^^-------
;; Note that we can't merge this with the variable declaration above because
;; of https://github.com/tree-sitter/tree-sitter/issues/1442#issuecomment-1584628651
(variable_declaration
Expand All @@ -37,24 +53,38 @@
)
] @_.domain
(#not-parent-type? @_.domain export_statement)

;; Handle multiple variable declarators in one statement, eg
;;!! (let | const | var) aaa = ..., ccc = ...;
;;! --------------------^^^--------^^^-------
(#allow-multiple! @name)
)

(
(export_statement
(_
;;!! export [default] (let | const | var) foo = ...;
;;! -------------------------------------^^^-------
(variable_declarator
name: (_) @name
)
)
) @_.domain

;; Handle multiple variable declarators in one statement, eg
;;!! var foo = ..., bar = ...;
;;! ----^^^--------^^^-------
(#allow-multiple! @name)
)

;;!! foo += ...;
;;! ^^^--------
(augmented_assignment_expression
left: (_) @name
) @_.domain

;;!! foo = ...;
;;! ^^^-------
(assignment_expression
left: (_) @name
) @_.domain
Expand All @@ -64,6 +94,9 @@
(formal_parameters)
] @name.iteration

;; Treat interior of all bodies as iteration scopes for `name`, eg
;;!! function foo() { }
;;! ***
(
(_
body: (_
Expand Down
28 changes: 27 additions & 1 deletion queries/javascript.jsx.scm
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@
;;!! <foo>bar</foo>
;;! ^^^^^^^^^^^^^^
;;! ###
;;! ***
(
(jsx_element) @xmlElement @_.interior @_.iteration
(#child-range! @_.interior 0 -1 true true)
(#child-range! @_.iteration 0 -1 true true)
)

;;!! <foo>bar</foo>
;;! ***
(
(jsx_element) @xmlStartTag.iteration @xmlEndTag.iteration @xmlBothTags.iteration
(#child-range! @xmlStartTag.iteration 0 -1 true true)
(#child-range! @xmlEndTag.iteration 0 -1 true true)
(#child-range! @xmlBothTags.iteration 0 -1 true true)
)

;;!! <foo>bar</foo>
;;! ^^^^^---------
(jsx_element
(jsx_opening_element) @xmlStartTag @xmlBothTags
(#allow-multiple! @xmlBothTags)
) @_.domain

;;!! <foo>bar</foo>
;;! --------^^^^^^
(jsx_element
(jsx_closing_element) @xmlEndTag @xmlBothTags
(#allow-multiple! @xmlBothTags)
) @_.domain

;;!! <foo/>
(jsx_self_closing_element) @xmlElement

;; JSX fragments, eg <>foo</>
;; ======== JSX fragments, eg <>foo</> ==========

;;!! <>foo</>
;;! ^^^^^^^^
;;! ###
;;! ***
(
(jsx_fragment) @xmlElement @_.interior @_.iteration
(#child-range! @_.interior 1 -3 true true)
(#child-range! @_.iteration 1 -3 true true)
)

;;!! <>foo</>
;;! ***
(
(jsx_fragment) @xmlStartTag.iteration @xmlEndTag.iteration @xmlBothTags.iteration
(#child-range! @xmlStartTag.iteration 1 -3 true true)
(#child-range! @xmlEndTag.iteration 1 -3 true true)
(#child-range! @xmlBothTags.iteration 1 -3 true true)
)

;;!! <>foo</>
;;! ^^------
(
(jsx_fragment) @xmlStartTag @xmlBothTags @_.domain
(#child-range! @xmlStartTag 0 1)
(#child-range! @xmlBothTags 0 1)
(#allow-multiple! @xmlBothTags)
)

;;!! <>foo</>
;;! -----^^^
(
(jsx_fragment) @xmlEndTag @xmlBothTags @_.domain
(#child-range! @xmlEndTag -3)
(#child-range! @xmlBothTags -3)
(#allow-multiple! @xmlBothTags)
)

;; Sets `name` to be empty range inside the fragment tag:
;;!! <>foo</>
;;! {} {}
;;! -- ---
(
(jsx_fragment
"<" @_.domain.start
Expand Down
15 changes: 10 additions & 5 deletions queries/javascript.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
;; Define this here because the `field_definition` node type doesn't exist
;; in typescript.
(
;; foo = () => {};
;; foo = function() {};
;; foo = function *() {};
;; (inside class bodies)
;;!! class Foo {
;;!! foo = () => {};
;;! ^^^^^^^^^^^^^^^
;;!! foo = function() {};
;;! ^^^^^^^^^^^^^^^^^^^^
;;!! foo = function *() {};
;;! ^^^^^^^^^^^^^^^^^^^^^^
;;!! }
(field_definition
property: (_) @functionName
value: [
Expand All @@ -28,7 +32,8 @@
)

(
;; foo = ...;
;;!! foo = ...;
;;! ^^^-------
(field_definition
property: (_) @name
) @name.domain.start
Expand Down
29 changes: 20 additions & 9 deletions queries/typescript.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@

;; import javascript.core.scm

;;!! function aaa(bbb?: Ccc = "ddd") {}
;;! ^^^--------------
(optional_parameter
(identifier) @name
) @_.domain

;;!! function aaa(bbb: Ccc = "ddd") {}
;;! ^^^-------------
(required_parameter
(identifier) @name
) @_.domain

;; Define these here because these node types don't exist in javascript.
(
[
;; foo(): void;
;; (in interface)
;; foo() {}
;; (in class)
;;!! class Foo { foo() {} }
;;! ^^^^^^^^
;;!! interface Foo { foo(): void; }
;;! ^^^^^^^^^^^^
(method_signature
name: (_) @functionName @name
)

;; abstract foo(): void;
;;!! class Foo { abstract foo(): void; }
;;! ^^^^^^^^^^^^^^^^^^^^^
(abstract_method_signature
name: (_) @functionName @name
)

;; [public | private | protected] foo = () => {};
;; [public | private | protected] foo = function() {};
;; [public | private | protected] foo = function *() {};
;;!! class Foo {
;;!! (public | private | protected) foo = () => {};
;;! ^^^^^^^^^^^^^^^
;;!! (public | private | protected) foo = function() {};
;;! ^^^^^^^^^^^^^^^^^^^^
;;!! (public | private | protected) foo = function *() {};
;;! ^^^^^^^^^^^^^^^^^^^^^^
;;!! }
(public_field_definition
name: (_) @functionName
value: [
Expand All @@ -49,7 +59,8 @@
)

(
;; [public | private | protected] foo = ...;
;;!! (public | private | protected) foo = ...;
;;! -------------------------------^^^-------
(public_field_definition
name: (_) @name
) @name.domain.start
Expand Down

0 comments on commit 38ee670

Please sign in to comment.