tree-sitter-typescript icon indicating copy to clipboard operation
tree-sitter-typescript copied to clipboard

bug: querying template substitutions stopping at first capture

Open TylerLeonhardt opened this issue 4 months ago • 2 comments

Did you check existing issues?

  • [x] I have read all the tree-sitter docs if it relates to using the parser
  • [x] I have searched the existing issues of tree-sitter-typescript

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

Using this code:

const me = 'me';
const you = 'you';
const a = `Hello ${me} and ${you} and ${you} again`;

and this query:

(template_string (template_substitution)* @sub) @tagged_template

I am expecting that tagged_template would have 3 captures with name as sub but instead it has only 1.

Easily reproducible in the Playground:

Image

Notice only the first template parameter is highlighted.

Steps To Reproduce/Bad Parse Tree

const me = 'me';
const you = 'you';
const a = `Hello ${me} and ${you} and ${you} again`;

Which gives this parse tree:

program [0, 0] - [3, 0]
  lexical_declaration [0, 0] - [0, 16]
    variable_declarator [0, 6] - [0, 15]
      name: identifier [0, 6] - [0, 8]
      value: string [0, 11] - [0, 15]
        string_fragment [0, 12] - [0, 14]
  lexical_declaration [1, 0] - [1, 18]
    variable_declarator [1, 6] - [1, 17]
      name: identifier [1, 6] - [1, 9]
      value: string [1, 12] - [1, 17]
        string_fragment [1, 13] - [1, 16]
  lexical_declaration [2, 0] - [2, 52]
    variable_declarator [2, 6] - [2, 51]
      name: identifier [2, 6] - [2, 7]
      value: template_string [2, 10] - [2, 51]
        string_fragment [2, 11] - [2, 17]
        template_substitution [2, 17] - [2, 22]
          identifier [2, 19] - [2, 21]
        string_fragment [2, 22] - [2, 27]
        template_substitution [2, 27] - [2, 33]
          identifier [2, 29] - [2, 32]
        string_fragment [2, 33] - [2, 38]
        template_substitution [2, 38] - [2, 44]
          identifier [2, 40] - [2, 43]
        string_fragment [2, 44] - [2, 50]

and this query:

(template_string (template_substitution)* @sub) @tagged_template

only captures the first template_substitution

Expected Behavior/Parse Tree

The query result should contain all of the template_substitutions

Repro

const me = 'me';
const you = 'you';
const a = `Hello ${me} and ${you} and ${you} again`;

TylerLeonhardt avatar Aug 04 '25 20:08 TylerLeonhardt

Workaround that is gross:

(template_string (_)* @sub) @tagged_template

and filter out in post.

TylerLeonhardt avatar Aug 05 '25 18:08 TylerLeonhardt

If this is indeed a bug (I'm not qualified to judge) then I suspect it's something that should be filed to the tree-sitter tool itself, since this repository is just defining the typescript grammar and not the mechanics of how queries are evaluated.

wetneb avatar Aug 05 '25 18:08 wetneb