GNU bug report logs - #65234
[PATCH] Fix jsx font-lock in older tree-sitter-js grammars

Previous Next

Package: emacs;

Reported by: Danny Freeman <danny <at> dfreeman.email>

Date: Fri, 11 Aug 2023 21:35:01 UTC

Severity: normal

Tags: patch

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#65234: closed ([PATCH] Fix jsx font-lock in older
 tree-sitter-js grammars)
Date: Thu, 17 Aug 2023 08:08:01 +0000
[Message part 1 (text/plain, inline)]
Your message dated Thu, 17 Aug 2023 11:06:45 +0300
with message-id <83zg2q5bze.fsf <at> gnu.org>
and subject line Re: bug#65234: [PATCH] Fix jsx font-lock in older tree-sitter-js grammars
has caused the debbugs.gnu.org bug report #65234,
regarding [PATCH] Fix jsx font-lock in older tree-sitter-js grammars
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
65234: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=65234
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Danny Freeman <danny <at> dfreeman.email>
To: bug-gnu-emacs <at> gnu.org
Cc: Vincenzo Pupillo <v.pupillo <at> gmail.com>,
 Daniel Colascione <dancol <at> dancol.org>
Subject: [PATCH] Fix jsx font-lock in older tree-sitter-js grammars
Date: Fri, 11 Aug 2023 17:18:32 -0400
[Message part 3 (text/plain, inline)]
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/>

"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.

[0001-Fix-jsx-font-lock-in-older-tree-sitter-js-grammars.patch (text/patch, attachment)]
[Message part 5 (text/plain, inline)]
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
[Message part 6 (message/rfc822, inline)]
From: Eli Zaretskii <eliz <at> gnu.org>
To: Dmitry Gutov <dmitry <at> gutov.dev>
Cc: casouri <at> gmail.com, 65234-done <at> debbugs.gnu.org, danny <at> dfreeman.email,
 v.pupillo <at> gmail.com, theo <at> thornhill.no, dancol <at> dancol.org
Subject: Re: bug#65234: [PATCH] Fix jsx font-lock in older tree-sitter-js
 grammars
Date: Thu, 17 Aug 2023 11:06:45 +0300
> Date: Wed, 16 Aug 2023 05:04:10 +0300
> Cc: 65234 <at> debbugs.gnu.org, v.pupillo <at> gmail.com, dancol <at> dancol.org
> From: Dmitry Gutov <dmitry <at> gutov.dev>
> 
> On 12/08/2023 08:41, Eli Zaretskii wrote:
> > Yuan, Theo: any comments on this?  Is this safe enough to go into the
> > emacs-29 branch?
> 
> It looks good to me (and works okay too). I also saw the same error in 
> my testing.
> 
> Especially since Emacs 29.2 is not quite imminent, I think it's a good 
> idea to install this: queries are simpler and thus more reliable. And 
> the fixed-fix, of course.

Thanks, installed on the emacs-29 branch, and closing the bug.


This bug report was last modified 1 year and 337 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.