Skip to content

Commit

Permalink
[language-(java|type)script]: Update Tree-sitter parsers to 0.23.0…
Browse files Browse the repository at this point in the history
…and modify query files in response to all non–backwards-compatible changes.
  • Loading branch information
savetheclocktower committed Sep 9, 2024
1 parent fbdf18a commit 03ba902
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parser: 'tree-sitter-javascript'
injectionRegex: '^(js|javascript|JS|JAVASCRIPT)$'

treeSitter:
parserSource: 'github:tree-sitter/tree-sitter-javascript#f1e5a09b8d02f8209a68249c93f0ad647b228e6e'
parserSource: 'github:tree-sitter/tree-sitter-javascript#v0.23.0'
grammar: 'tree-sitter/tree-sitter-javascript.wasm'
highlightsQuery: 'tree-sitter/highlights.scm'
localsQuery: 'tree-sitter/locals.scm'
Expand Down
28 changes: 17 additions & 11 deletions packages/language-javascript/grammars/tree-sitter/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
; MISC
; ====

; In the JSX construct `<FOO.Bar>`, `FOO` should not be marked as
; `constant.other.js`. Block off identifiers within complex JSX tag names early
; to prevent this.
; In the JSX construct `<Foo.Bar>`, `Foo.Bar` is treated as a
; `member_expression`. We don't want the ordinary rules for member expressions
; to apply, so we block them off.
;
; TODO: If we wanted to give these segments individual scopes, we'd do that
; here — replacing the `@_IGNORE_`s with scope names.

(jsx_opening_element
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))

(jsx_closing_element
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))

(jsx_self_closing_element
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))


Expand Down Expand Up @@ -98,7 +104,7 @@
(assignment_expression
left: (member_expression
property: (property_identifier) @variable.other.assignment.property.js)
right: [(arrow_function) (function)] @_IGNORE_
right: [(arrow_function) (function_expression)] @_IGNORE_
(#set! isFunctionProperty true))

; The "bar" in `foo.bar = true`.
Expand Down Expand Up @@ -279,7 +285,7 @@

; Named function expressions:
; the "foo" in `let bar = function foo () {`
(function
(function_expression
name: (identifier) @entity.name.function.definition.js)

; Function definitions:
Expand Down Expand Up @@ -313,27 +319,27 @@
left: (member_expression
property: (property_identifier) @entity.name.function.definition.js
(#set! capture.final true))
right: [(arrow_function) (function)])
right: [(arrow_function) (function_expression)])

; Function variable assignment:
; The "foo" in `let foo = function () {`
(variable_declarator
name: (identifier) @entity.name.function.definition.js
value: [(function) (arrow_function)])
value: [(function_expression) (arrow_function)])

; Function variable reassignment:
; The "foo" in `foo = function () {`
(assignment_expression
left: (identifier) @function
right: [(function) (arrow_function)])
right: [(function_expression) (arrow_function)])

; Object key-value pair function:
; The "foo" in `{ foo: function () {} }`
(pair
key: (property_identifier) @entity.name.function.method.definition.js
value: [(function) (arrow_function)])
value: [(function_expression) (arrow_function)])

(function "function" @storage.type.function.js)
(function_expression "function" @storage.type.function.js)
(function_declaration "function" @storage.type.function.js)

(generator_function "function" @storage.type.function.js)
Expand Down Expand Up @@ -1010,7 +1016,7 @@
(#set! adjust.endAt lastChild.startPosition)
(#set! capture.final true))

(function
(function_expression
body: (statement_block) @meta.block.function.js
(#set! adjust.startAt firstChild.endPosition)
(#set! adjust.endAt lastChild.startPosition)
Expand Down
10 changes: 5 additions & 5 deletions packages/language-javascript/grammars/tree-sitter/tags.scm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(comment)* @doc
.
[
(function
(function_expression
name: (identifier) @name)
(function_declaration
name: (identifier) @name)
Expand All @@ -47,7 +47,7 @@
(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)]) @definition.function)
value: [(arrow_function) (function_expression)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)
Expand All @@ -58,7 +58,7 @@
(variable_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)]) @definition.function)
value: [(arrow_function) (function_expression)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)
Expand All @@ -69,12 +69,12 @@
(member_expression
property: (property_identifier) @name)
]
right: [(arrow_function) (function)]
right: [(arrow_function) (function_expression)]
) @definition.function

(pair
key: (property_identifier) @name
value: [(arrow_function) (function)]) @definition.function
value: [(arrow_function) (function_expression)]) @definition.function

(
(call_expression
Expand Down
Binary file not shown.
14 changes: 7 additions & 7 deletions packages/language-typescript/grammars/common/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@

; Named function expressions:
; the "foo" in `let bar = function foo () {`
(function
(function_expression
name: (identifier) @entity.name.function.definition._LANG_)

; Function definitions:
Expand Down Expand Up @@ -697,28 +697,28 @@
left: (member_expression
property: (property_identifier) @entity.name.function.definition._LANG_
(#set! capture.final true))
right: [(arrow_function) (function)])
right: [(arrow_function) (function_expression)])

; Function variable assignment:
; The "foo" in `let foo = function () {`
(variable_declarator
name: (identifier) @entity.name.function.definition._LANG_
value: [(function) (arrow_function)])
value: [(function_expression) (arrow_function)])

; Function variable reassignment:
; The "foo" in `foo = function () {`
(assignment_expression
left: (identifier) @function
right: [(function) (arrow_function)])
right: [(function_expression) (arrow_function)])

; Object key-value pair function:
; The "foo" in `{ foo: function () {} }`
(pair
key: (property_identifier) @entity.name.function.method.definition._LANG_
value: [(function) (arrow_function)])
value: [(function_expression) (arrow_function)])

; Function is `storage.type` because it's a core language construct.
(function "function" @storage.type.function._LANG_)
(function_expression "function" @storage.type.function._LANG_)
(function_declaration "function" @storage.type.function._LANG_)

(generator_function "function" @storage.type.function._LANG_)
Expand Down Expand Up @@ -985,7 +985,7 @@
body: (statement_block) @meta.block.function._LANG_
(#set! capture.final true))

(function
(function_expression
body: (statement_block) @meta.block.function._LANG_
(#set! capture.final true))

Expand Down
4 changes: 2 additions & 2 deletions packages/language-typescript/grammars/common/tags.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: (identifier) @name
(#set! symbol.tag "function")) @definition.function

(function
(function_expression
name: (identifier) @name
(#set! symbol.tag "function")) @definition.function

Expand Down Expand Up @@ -36,7 +36,7 @@
(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function)]) @definition.function)
value: [(arrow_function) (function_expression)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ comments:
start: '// '

treeSitter:
parserSource: 'github:tree-sitter/tree-sitter-typescript#v0.23.0'
languageSegment: 'ts.tsx'
grammar: 'tree-sitter-tsx/tree-sitter-tsx.wasm'
highlightsQuery: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ comments:
start: '// '

treeSitter:
parserSource: 'github:tree-sitter/tree-sitter-typescript#v0.23.0'
languageSegment: 'ts'
grammar: 'tree-sitter-typescript/tree-sitter-typescript.wasm'
highlightsQuery: 'common/highlights.scm'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
(#set! fold.endAt lastChild.previousSibling.endPosition))

((jsx_self_closing_element) @fold
; Exclude both the slash and angle bracket `/>` from the fold.
(#set! fold.endAt lastChild.previousSibling.startPosition))
; Exclude the slash and angle bracket `/>` from the fold.
(#set! fold.endAt lastChild.startPosition))
Original file line number Diff line number Diff line change
@@ -1,69 +1,64 @@
; MISC
; ====

; In the JSX construct `<FOO.Bar>`, `FOO` should not be marked as
; `constant.other.ts.tsx`. Block off identifiers within complex JSX tag names
; early to prevent this.
; In the JSX construct `<Foo.Bar>`, `Foo.Bar` is treated as a
; `member_expression`. We don't want the ordinary rules for member expressions
; to apply, so we block them off.
;
; TODO: If we wanted to give these segments individual scopes, we'd do that
; here — replacing the `@_IGNORE_`s with scope names.

(jsx_opening_element
(nested_identifier
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))

(jsx_closing_element
(nested_identifier
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))

(jsx_self_closing_element
(nested_identifier
(member_expression
(identifier) @_IGNORE_
(property_identifier) @_IGNORE_
(#set! capture.final)))

; JSX
; ===

[
(jsx_self_closing_element)
(jsx_opening_element)
(jsx_closing_element)
] @meta.tag.ts.tsx

; The "Foo" in `<Foo />`.
(jsx_self_closing_element
name: (_) @entity.name.tag.ts.tsx
) @meta.tag.ts.tsx
name: (_) @entity.name.tag.ts.tsx)

; The "Foo" in `<Foo>`.
(jsx_opening_element
name: (identifier) @entity.name.tag.ts.tsx) @meta.tag.ts.tsx
name: (identifier) @entity.name.tag.ts.tsx)

; The "Foo.Bar" in `<Foo.Bar>`.
(jsx_opening_element
name: (nested_identifier) @entity.name.tag.ts.tsx) @meta.tag.ts.tsx
name: (member_expression) @entity.name.tag.ts.tsx)

; The "Foo" in `</Foo>`.
(jsx_closing_element
name: (identifier) @entity.name.tag.ts.tsx) @meta.tag.ts.tsx
name: (identifier) @entity.name.tag.ts.tsx)

; The "Foo.Bar" in `</Foo.Bar>`.
(jsx_closing_element
name: (nested_identifier) @entity.name.tag.ts.tsx) @meta.tag.ts.tsx
name: (member_expression) @entity.name.tag.ts.tsx)

; The "bar" in `<Foo bar={true} />`.
(jsx_attribute
(property_identifier) @entity.other.attribute-name.ts.tsx)

; The empty tag used as a shorthand for a fragment: `<>`.
((jsx_fragment) @meta.tag.fragment.ts.tsx
(#set! adjust.endAfterFirstMatchOf "^<>"))

; The closing fragment tag: `</>`.
((jsx_fragment) @meta.tag.fragment.ts.tsx
(#set! adjust.startBeforeFirstMatchOf "</>$"))

; (jsx_fragment)

; The slashes in closing tags should not be interpreted as math operators.
(jsx_self_closing_element "/" @punctuation.definition.tag.end.ts.tsx
(#set! capture.final))
(jsx_closing_element "/" @punctuation.definition.tag.end.ts.tsx
(#set! capture.final))

; All JSX expressions/interpolations within braces.
((jsx_expression) @meta.embedded.block.ts.tsx
(#match? @meta.embedded.block.ts.tsx "\\n")
Expand All @@ -74,31 +69,16 @@
(jsx_opening_element
"<" @punctuation.definition.tag.begin.ts.tsx
">" @punctuation.definition.tag.end.ts.tsx)

;
(jsx_closing_element
"<" @punctuation.definition.tag.begin.ts.tsx
">" @punctuation.definition.tag.end.ts.tsx)

(jsx_fragment
"<" @punctuation.definition.tag.begin.ts.tsx
"</" @punctuation.definition.tag.begin.ts.tsx
">" @punctuation.definition.tag.end.ts.tsx)

(jsx_fragment
"/" @punctuation.definition.tag.end.ts.tsx
(#set! capture.final))

(jsx_self_closing_element
"<" @punctuation.definition.tag.begin.ts.tsx
"/>" @punctuation.definition.tag.end.ts.tsx
(#set! capture.final))

((jsx_self_closing_element
; The "/>" in `<Foo />`, extended to cover both anonymous nodes at once.
"/") @punctuation.definition.tag.end.ts.tsx
(#set! adjust.startAt lastChild.previousSibling.startPosition)
(#set! adjust.endAt lastChild.endPosition)
(#set! capture.final true))



; META
; ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
; point, so the usual heuristic won't work. Instead we set `indent.force` and
; use `test.lastTextOnRow` to ensure that the dedent fires exactly once while
; typing.
((jsx_self_closing_element ">" @dedent)
((jsx_self_closing_element "/>" @dedent)
(#is-not? test.startsOnSameRowAs parent.firstChild.startPosition)
(#is? test.lastTextOnRow)
(#set! indent.force true))
Binary file not shown.
Binary file not shown.

0 comments on commit 03ba902

Please sign in to comment.