From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Danny Freeman via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#65234: [PATCH] Fix jsx font-lock in older tree-sitter-js grammars Date: Fri, 11 Aug 2023 17:18:32 -0400 Message-ID: <87jzu1p8kt.fsf@dfreeman.email> Reply-To: Danny Freeman Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18810"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Vincenzo Pupillo , Daniel Colascione To: 65234@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 11 23:35:20 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qUZmx-0004dS-Ij for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 11 Aug 2023 23:35:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qUZmj-0002ct-Fp; Fri, 11 Aug 2023 17:35:07 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qUZmg-0002cf-Em for bug-gnu-emacs@gnu.org; Fri, 11 Aug 2023 17:35:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qUZmg-0001Az-6n for bug-gnu-emacs@gnu.org; Fri, 11 Aug 2023 17:35:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qUZmf-0000z6-Py for bug-gnu-emacs@gnu.org; Fri, 11 Aug 2023 17:35:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Danny Freeman Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 11 Aug 2023 21:35:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65234 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16917896773703 (code B ref -1); Fri, 11 Aug 2023 21:35:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Aug 2023 21:34:37 +0000 Original-Received: from localhost ([127.0.0.1]:48191 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUZmF-0000xZ-Of for submit@debbugs.gnu.org; Fri, 11 Aug 2023 17:34:37 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:55404) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qUZmA-0000wz-HA for submit@debbugs.gnu.org; Fri, 11 Aug 2023 17:34:34 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qUZm5-0002Zt-2y for bug-gnu-emacs@gnu.org; Fri, 11 Aug 2023 17:34:25 -0400 Original-Received: from out-121.mta1.migadu.com ([95.215.58.121]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qUZm2-00019S-0a for bug-gnu-emacs@gnu.org; Fri, 11 Aug 2023 17:34:24 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dfreeman.email; s=key1; t=1691789655; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=erxApkNBrKmITHtMOPH3F2CjN8ORL6TDnT+z3v2krIE=; b=PZBZoZy7V9DkeBgtD/w6uEm6G5x8j5OILbX813RdNroE90U/KKEzjZx+b9yEphPgZdmd8R NaC7qNVUuZHCj0Qkb6uYkIXiDeRaX5ill1TTfMEt4sES4gDaA1o3IjxBwORzqRWYfRY7Io /hZUU09ck5zDQsC5WgcNlWeOUG1S1h8= X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=95.215.58.121; envelope-from=danny@dfreeman.email; helo=out-121.mta1.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:267230 Archived-At: --=-=-= Content-Type: text/plain I have a patch to fix a bug I found in js-ts-mode. Description of Problem: A new function was wrtiten to account for a breaking change in the tree-sitter-javascript grammar, see [0] This function returns some tree-sitter queries used by the js-ts-mode font lock settings. We attempted to check for the breaking change by running a query (treesit-query-capture 'javascript '((member_expression) @capture)) which would in theory fail when an old grammar is being used. The problem is that this query does not fail on old grammars, so the backwards incompatible font-lock queries are always used. When font-locking a JSX file, the queries that look like (jsx_opening_element [(member_expression) ... throw errors and font locking fails (only on jsx elements). Solution: Instead of fixing the bb1f97b compatibility check with something like (treesit-query-capture 'javascript '((jsx_opening_element (member_expression) @capture))) I have opted re-write the font lock rules that capture the same nodes by the "name:" field instead of by the specific node names (where the backwards incompatible change took place). The possible nodes that can be in the "name" field: After [1]: "identifier" "member_expression" "jsx_namespace_name" Before [2]: "identifier" "nested_identifier" "jsx_namespace_name" "jsx_namespace_name" is an additional node captured by these rules. As an example, in the JSX expression "Foo:bar" is a node of type "jsx_namespace_name" and would be captured with @font-lock-function-call-face, where before this change it was not captured or highlighted at all. If there is a reason this should not be captured and highlighted, a new query can be added in to capture these nodes first with @default so they are not highlighted like before. Some thoughts: These backwards incompatible grammar changes are rough to deal with, but I'm glad they are taken into account. I use nixos, and the current stable version 23.05 packages the older version of the JS grammar for emacs which is how I noticed this. I wish the developers of the js grammar were more careful about backwards compatible changes. The fact that they were renaming a node without much reason didn't seem to bother any of the maintainers. --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Fix-jsx-font-lock-in-older-tree-sitter-js-grammars.patch >From ab7e8c1e7120fa129840e4951803542509bee116 Mon Sep 17 00:00:00 2001 From: dannyfreeman Date: Fri, 11 Aug 2023 16:43:58 -0400 Subject: [PATCH] Fix jsx font-lock in older tree-sitter-js grammars * lisp/progmodes/js.el (js--treesit-font-lock-settings): Use queries that are backwards compatible with tree-sitter-javascript bb1f97b * list/progmodes/js.el (-jsx--treesit-font-lock-compatibility-bb1f97b): deleted unused function --- lisp/progmodes/js.el | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index c583b6f6191..9d2990e7bc9 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3501,35 +3501,6 @@ js--treesit-operators "&&" "||" "!") "JavaScript operators for tree-sitter font-locking.") -(defun js-jsx--treesit-font-lock-compatibility-bb1f97b () - "Font lock rules helper, to handle different releases of tree-sitter-javascript. -Check if a node type is available, then return the right font lock rules." - ;; handle commit bb1f97b - (condition-case nil - (progn (treesit-query-capture 'javascript '((member_expression) @capture)) - '((jsx_opening_element - [(member_expression (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_closing_element - [(member_expression (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_self_closing_element - [(member_expression (identifier)) (identifier)] - @font-lock-function-call-face))) - (error '((jsx_opening_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_closing_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face) - - (jsx_self_closing_element - [(nested_identifier (identifier)) (identifier)] - @font-lock-function-call-face))))) - (defvar js--treesit-font-lock-settings (treesit-font-lock-rules @@ -3639,8 +3610,10 @@ js--treesit-font-lock-settings :language 'javascript :feature 'jsx - (append (js-jsx--treesit-font-lock-compatibility-bb1f97b) - '((jsx_attribute (property_identifier) @font-lock-constant-face))) + '((jsx_opening_element name: (_) @font-lock-function-call-face) + (jsx_closing_element name: (_) @font-lock-function-call-face) + (jsx_self_closing_element name: (_) @font-lock-function-call-face) + (jsx_attribute (property_identifier) @font-lock-constant-face)) :language 'javascript :feature 'number -- 2.40.1 --=-=-= Content-Type: text/plain Sources [0]: https://github.com/tree-sitter/tree-sitter-javascript/commit/bb1f97b643b77fc1f082d621bf533b4b14cf0c30 [1]: https://github.com/tree-sitter/tree-sitter-javascript/blob/bb1f97b643b77fc1f082d621bf533b4b14cf0c30/grammar.js#L637-L641 [2]: https://github.com/tree-sitter/tree-sitter-javascript/blob/5720b249490b3c17245ba772f6be4a43edb4e3b7/grammar.js#L635-L639 Thank you, -- Danny Freeman --=-=-=--