jquery / jquery/esprima

Incorrect parse tree during several destructing assignment statements parsing

Open
#1,950 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
7.1k
Forks
773
PR merge metrics
No merged PRs in 30d

Description

Steps to reproduce

Try to parse DestructuringAssignment.js file from examples dir of ANTLR JavaScript grammar or a simple code snippet from http://es6-features.org/#ArrayMatching

var [ a, , b ] = list
[ b, a ] = [ a, b ]
Expected output

Parse tree should contain two statements:

{
    "type": "Program",
    "body": [
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "ArrayPattern",
                        "elements": [
                            {
                                "type": "Identifier",
                                "name": "a"
                            },
                            null,
                            {
                                "type": "Identifier",
                                "name": "b"
                            }
                        ]
                    },
                    "init": {
                        "type": "Identifier",
                        "name": "list"
                    }
                }
            ],
            "kind": "var"
        },
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "AssignmentExpression",
                "operator": "=",
                "left": {
                    "type": "ArrayPattern",
                    "elements": [
                        {
                            "type": "Identifier",
                            "name": "b"
                        },
                        {
                            "type": "Identifier",
                            "name": "a"
                        }
                    ]
                },
                "right": {
                    "type": "ArrayExpression",
                    "elements": [
                        {
                            "type": "Identifier",
                            "name": "a"
                        },
                        {
                            "type": "Identifier",
                            "name": "b"
                        }
                    ]
                }
            }
        }
    ],
    "sourceType": "script"
}

Just add semicolon ; to the end of the first line to get the correct output:

var [ a, , b ] = list;
[ b, a ] = [ a, b ]
Actual output

Code parsed to the following parse tree with nonexistent MemberExpression and only a single declaration:

{
    "type": "Program",
    "body": [
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "ArrayPattern",
                        "elements": [
                            {
                                "type": "Identifier",
                                "name": "a"
                            },
                            null,
                            {
                                "type": "Identifier",
                                "name": "b"
                            }
                        ]
                    },
                    "init": {
                        "type": "AssignmentExpression",
                        "operator": "=",
                        "left": {
                            "type": "MemberExpression",
                            "computed": true,
                            "object": {
                                "type": "Identifier",
                                "name": "list"
                            },
                            "property": {
                                "type": "SequenceExpression",
                                "expressions": [
                                    {
                                        "type": "Identifier",
                                        "name": "b"
                                    },
                                    {
                                        "type": "Identifier",
                                        "name": "a"
                                    }
                                ]
                            }
                        },
                        "right": {
                            "type": "ArrayExpression",
                            "elements": [
                                {
                                    "type": "Identifier",
                                    "name": "a"
                                },
                                {
                                    "type": "Identifier",
                                    "name": "b"
                                }
                            ]
                        }
                    }
                }
            ],
            "kind": "var"
        }
    ],
    "sourceType": "script"
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by parsing examples/DestructuringAssignment.js or the minimal two-line snippet, comparing the resulting tree with the expected output. Investigate how the parser handles a destructuring assignment after a variable declaration without a semicolon; done means the tree contains two statements and no spurious MemberExpression.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.