Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jackschu opened this issue Aug 25, 2024 · 0 comments
Open

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

jackschu opened this issue Aug 25, 2024 · 0 comments
Labels

Comments

@jackschu
Copy link
Contributor

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.

@jackschu jackschu added the bug label Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant