From 824e79a5f1226eee9bd04bdda98f9c5b78835b88 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Sun, 21 Jul 2024 13:55:49 +0200 Subject: [PATCH] Signal error on wrong typescript dialect * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode-wrong-dialect-error): New error. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--check-dialect): Helper function. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules, tsx-ts-mode--font-lock-compatibility-bb1f97b, tsx-ts-mode--font-lock-compatibility-function-expression, tsx-ts-mode--font-lock-compatibility-function-expression, typescript-ts-mode--font-lock-settings): Use the new helper. (cherry picked from commit 7a059ed88a13d660cc5eb20f5fa6a2903ed23247) --- lisp/progmodes/typescript-ts-mode.el | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 3606a139d50..93a871e55d5 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -80,6 +80,15 @@ table) "Syntax table for `typescript-ts-mode'.") +(define-error 'typescript-ts-mode-wrong-dialect-error + "Wrong typescript dialect" + 'error) + +(defun typescript-ts-mode--check-dialect (dialect) + (unless (or (eq dialect 'typescript) (eq dialect 'tsx)) + (signal 'typescript-ts-mode-wrong-dialect-error + (list "Unsupported dialect for typescript-ts-mode suplied" dialect)))) + (defun tsx-ts-mode--indent-compatibility-b893426 () "Indent rules helper, to handle different releases of tree-sitter-tsx. Check if a node type is available, then return the right indent rules." @@ -106,6 +115,7 @@ declarations, accounting for the length of keyword (var, let, or const)." (defun typescript-ts-mode--indent-rules (language) "Rules used for indentation. Argument LANGUAGE is either `typescript' or `tsx'." + (typescript-ts-mode--check-dialect language) `((,language ((parent-is "program") column-0 0) ((node-is "}") parent-bol 0) @@ -188,6 +198,7 @@ Argument LANGUAGE is either `typescript' or `tsx'." ;; Warning: treesitter-query-capture says both node types are valid, ;; but then raises an error if the wrong node type is used. So it is ;; important to check with the new node type (member_expression) + (typescript-ts-mode--check-dialect language) (condition-case nil (progn (treesit-query-capture language '((jsx_opening_element (member_expression) @capture))) '((jsx_opening_element @@ -219,6 +230,7 @@ Argument LANGUAGE is either `typescript' or `tsx'." LANGUAGE can be `typescript' or `tsx'. Starting from version 0.20.4 of the typescript/tsx grammar, `function' becomes `function_expression'." + (typescript-ts-mode--check-dialect language) (condition-case nil (progn (treesit-query-capture language '((function_expression) @cap)) ;; New version of the grammar @@ -230,6 +242,7 @@ typescript/tsx grammar, `function' becomes `function_expression'." (defun typescript-ts-mode--font-lock-settings (language) "Tree-sitter font-lock settings. Argument LANGUAGE is either `typescript' or `tsx'." + (typescript-ts-mode--check-dialect language) (let ((func-exp (tsx-ts-mode--font-lock-compatibility-function-expression language))) (treesit-font-lock-rules :language language -- 2.39.2