Skip to content

Using of as identifier when declaring by let in for statement causes error #355

Open
@zyoshoka

Description

@zyoshoka

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

for (const of of []) break;
for (let of of []) break;
for (const of = '';;) break;
for (let of = '';;) break;

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

https://www.typescriptlang.org/play/?#code/GYewTgBAFAxiB2BnALhEw0YNoF0CUEARmAKYCGA1gNwCwAUKJFADYmrqYS4HHnX2NocJOwwBeCAHJJVKj1KVaDcNFaiIE6bPl8lQA

The output of tree-sitter parse is the following:

(program [0, 0] - [4, 0]
  (for_in_statement [0, 0] - [0, 27]
    left: (identifier [0, 11] - [0, 13])
    right: (array [0, 17] - [0, 19])
    body: (break_statement [0, 21] - [0, 27]))
  (for_in_statement [1, 0] - [1, 25]
    left: (identifier [1, 5] - [1, 8])
    right: (subscript_expression [1, 12] - [1, 17]
      object: (identifier [1, 12] - [1, 14])
      index: (identifier [1, 16] - [1, 16]))
    body: (break_statement [1, 19] - [1, 25]))
  (for_statement [2, 0] - [2, 28]
    initializer: (lexical_declaration [2, 5] - [2, 19]
      (variable_declarator [2, 11] - [2, 18]
        name: (identifier [2, 11] - [2, 13])
        value: (string [2, 16] - [2, 18])))
    condition: (empty_statement [2, 19] - [2, 20])
    body: (break_statement [2, 22] - [2, 28]))
  (for_statement [3, 0] - [3, 26]
    initializer: (assignment_expression [3, 5] - [3, 16]
      left: (identifier [3, 5] - [3, 8])
      (ERROR [3, 9] - [3, 11])
      right: (string [3, 14] - [3, 16]))
    condition: (empty_statement [3, 17] - [3, 18])
    body: (break_statement [3, 20] - [3, 26])))
/Users/username/test.js	Parse:    2.56 ms	    42 bytes/ms	(MISSING identifier [1, 16] - [1, 16])

Note that of is not a reserved word (https://tc39.es/ecma262/#prod-ReservedWord).

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