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

Bug: escape sequences are too restricted for tagged templates

Open jackschu opened this issue 1 year ago • 0 comments

The following piece of code is valid but it is parsed incorrectly:

foo`\x`

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript:

The output of tree-sitter parse is the following:

program [0, 0] - [1, 0]
  expression_statement [0, 0] - [0, 7]
    call_expression [0, 0] - [0, 7]
      function: identifier [0, 0] - [0, 3]
      arguments: template_string [0, 3] - [0, 7]
        ERROR [0, 4] - [0, 6]
          ERROR [0, 4] - [0, 6]

Note that this is roughly correct for

`\x`

Because MDN in their discussion of non-tagged template strings states

non-well-formed escape sequence is a syntax error

But it also states

Tagged templates enable the embedding of arbitrary string content, where escape sequences may follow a different syntax. ... Therefore, the syntax restriction of well-formed escape sequences is removed from tagged templates.

So the first example should be allowed, despite it containing a typically malformed escape sequence


I'd put up a PR myself but I'm not sure what direction to take this. Options I'd consider are:

  1. reworking tagged templates at large to not become a call_expression node, this is not how theyre represented by ESTree (they're their own kind of expression (src)) and have caused issues elsewhere (#334)
  2. relaxing the conditions on escape sequence for any template string
  3. creating a new syntax node for tagged template string, relaxing the escape sequences

what do you think @amaanq or @maxbrunsfeld , also I've been working on this project a good bit, is github the best way to communicate? Totally OK if that's what works for yall, but wondering if theres a better channel.

jackschu avatar Aug 25 '24 19:08 jackschu