Skip to content

apparent incorrect parse of new expression with a tagged template #334

Closed
@ltcmelo

Description

@ltcmelo

It seems that the parse of a new expression combined with a tagged template is incorrect. Consider the snippet below.

function f() {}
function i(template) { return f; }
new i`whatever`;

I expect this to be parsed as new_expression where the constructor is a member_expression consisting of identifier i and template_string whatever. However, this is being parsed as a call_expression with a new_expression and a template_string as children:

     call_expression    "new i`whatever`"
       new_expression    "new i"
         new    "new"
         identifier    "i"
       template_string    "`whatever`"
         `    "`"
         string_fragment    "whatever"
         `    "`"

When the new expression contains arguments, the incorrectness becomes worse as two call_expression are nested around the new_expression and template_string, with arguments of the new expression being parsed as the arguments of the outer call_expression:

function g(v) {}
function j(template) { return g; }
new j`whatever`(1);
     call_expression    "new j`whatever`(1)"
       call_expression    "new j`whatever`"
         new_expression    "new j"
           new    "new"
           identifier    "j"
         template_string    "`whatever`"
           `    "`"
           string_fragment    "whatever"
           `    "`"
       arguments    "(1)"
         (    "("
         number    "1"
         )    ")"

As a reference to what I consider the correct parse for these 2 snippets, I'm attaching a screenshot from AST explorer's parse of it.
Screen Shot 2024-08-13 at 14 28 52

Perhaps this can be fixed by replacing primary_expression for member_expression in the tree-sitter's grammar? (Link to the corresponding spec grammar.)

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