Skip to content

Bug: escape sequences are too restricted for tagged templates #337

Open
@jackschu

Description

@jackschu

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 (apparent incorrect parse of new expression with a tagged template #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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions