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

Incorrect parse tree during several destructing assignment statements parsing #1950

Open
KvanTTT opened this Issue Sep 20, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@KvanTTT

KvanTTT commented Sep 20, 2018

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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment