esprima icon indicating copy to clipboard operation
esprima copied to clipboard

Incorrect parse tree during several destructing assignment statements parsing

Open KvanTTT opened this issue 7 years ago • 0 comments

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"
}

KvanTTT avatar Sep 20 '18 09:09 KvanTTT