Handle typescript ts grammar breaking change for function_expression

Starting from version 0.20.4 of the typescript/tsx grammar, "function"
becomes "function_expression". The right expression is used depending
on the grammar version.

* lisp/progmodes/typescript-ts-mode.el
(tsx-ts-mode--font-lock-compatibility-function-expression):
New function (bug#69024).
(typescript-ts-mode--font-lock-settings): Use it.

Copyright-paperwork-exempt: yes
This commit is contained in:
Loïc Lemaître 2024-02-11 05:00:38 +02:00 committed by Dmitry Gutov
parent 717d8c4285
commit 9f9da26e0d

View file

@ -199,9 +199,23 @@ Argument LANGUAGE is either `typescript' or `tsx'."
[(nested_identifier (identifier)) (identifier)]
@typescript-ts-jsx-tag-face)))))
(defun tsx-ts-mode--font-lock-compatibility-function-expression (language)
"Handle tree-sitter grammar breaking change for `function' expression.
LANGUAGE can be `typescript' or `tsx'. Starting from version 0.20.4 of the
typescript/tsx grammar, `function' becomes `function_expression'."
(condition-case nil
(progn (treesit-query-capture language '((function_expression) @cap))
;; New version of the grammar
'function_expression)
(treesit-query-error
;; Old version of the grammar
'function)))
(defun typescript-ts-mode--font-lock-settings (language)
"Tree-sitter font-lock settings.
Argument LANGUAGE is either `typescript' or `tsx'."
(let ((func-exp (tsx-ts-mode--font-lock-compatibility-function-expression language)))
(treesit-font-lock-rules
:language language
:feature 'comment
@ -228,7 +242,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
:language language
:override t ;; for functions assigned to variables
:feature 'declaration
`((function
`((,func-exp
name: (identifier) @font-lock-function-name-face)
(function_declaration
name: (identifier) @font-lock-function-name-face)
@ -244,7 +258,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
(variable_declarator
name: (identifier) @font-lock-function-name-face
value: [(function) (arrow_function)])
value: [(,func-exp) (arrow_function)])
(variable_declarator
name: (identifier) @font-lock-variable-name-face)
@ -264,7 +278,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
name: (array_pattern
(identifier)
(identifier) @font-lock-function-name-face)
value: (array (number) (function)))
value: (array (number) (,func-exp)))
(catch_clause
parameter: (identifier) @font-lock-variable-name-face)
@ -323,11 +337,11 @@ Argument LANGUAGE is either `typescript' or `tsx'."
:language language
:feature 'expression
'((assignment_expression
`((assignment_expression
left: [(identifier) @font-lock-function-name-face
(member_expression
property: (property_identifier) @font-lock-function-name-face)]
right: [(function) (arrow_function)]))
right: [(,func-exp) (arrow_function)]))
:language language
:feature 'function
@ -375,7 +389,7 @@ Argument LANGUAGE is either `typescript' or `tsx'."
:language language
:feature 'escape-sequence
:override t
'((escape_sequence) @font-lock-escape-face)))
'((escape_sequence) @font-lock-escape-face))))
;;;###autoload
(define-derived-mode typescript-ts-base-mode prog-mode "TypeScript"