Skip to content

Parser fails on variable declarator + nested object pattern + assignment #243

@lyonbot

Description

@lyonbot

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

let { user: { uid, username } = {}, meta: { updatedAt } = {} } = response

However tree-sitter-typescript can parse it correctly 😕


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

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

The parsed AST by babel / acorn:

https://astexplorer.net/#/gist/8eee3082c2e1b974d23232a366026caa/latest

The output of tree-sitter parse is the following:

(program [0, 0] - [1, 0]
  (lexical_declaration [0, 0] - [0, 60]
    (variable_declarator [0, 4] - [0, 34]
      name: (object_pattern [0, 4] - [0, 29]
        (pair_pattern [0, 6] - [0, 29]
          key: (property_identifier [0, 6] - [0, 10])
          value: (object_pattern [0, 12] - [0, 29]
            (shorthand_property_identifier_pattern [0, 14] - [0, 17])
            (shorthand_property_identifier_pattern [0, 19] - [0, 27]))))
      value: (object [0, 32] - [0, 34]))
    (ERROR [0, 36] - [0, 41]
      (identifier [0, 36] - [0, 40]))
    (variable_declarator [0, 42] - [0, 60]
      name: (object_pattern [0, 42] - [0, 55]
        (shorthand_property_identifier_pattern [0, 44] - [0, 53]))
      value: (object [0, 58] - [0, 60])))
  (ERROR [0, 61] - [0, 64])
  (expression_statement [0, 65] - [0, 73]
    (identifier [0, 65] - [0, 73])))
1.js	1 ms	(MISSING "}" [0, 29] - [0, 29])

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