Skip to content

Support for defaults when destructuring parameters #296

@tswaters

Description

@tswaters

The following code:

({a = 'b'}) => {}

When run through esprima returns:

{
    "type": "Program",
    "body": [
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "ArrowFunctionExpression",
                "id": null,
                "params": [
                    {
                        "type": "ObjectPattern",
                        "properties": [
                            {
                                "type": "Property",
                                "key": {
                                    "type": "Identifier",
                                    "name": "a"
                                },
                                "computed": false,
                                "value": {
                                    "type": "AssignmentPattern",
                                    "left": {
                                        "type": "Identifier",
                                        "name": "a"
                                    },
                                    "right": {
                                        "type": "Literal",
                                        "value": "b",
                                        "raw": "'b'"
                                    }
                                },
                                "kind": "init",
                                "method": false,
                                "shorthand": true
                            }
                        ]
                    }
                ],
                "defaults": [],
                "body": {
                    "type": "BlockStatement",
                    "body": []
                },
                "generator": false,
                "expression": false
            }
        }
    ],
    "sourceType": "script"
}

And when run back through escodegen, returns:

({a}) => {};

i.e., the value of a is not recognized and the AssignmentExpression is lost

..

I was playing around with this, and I found that adding expr.value as a third parameter to generatePropertyKey and modifying like so makes it work

-        result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
+        if (value.type === 'AssignmentPattern') {
+            result.push(this.AssignmentPattern(value, Precedence.Sequence, E_TTT));
+        }
+        else {
+            result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));
+        }

Thing is, I'm unsure if this could have other unintended consequences...the tests still pass, fwiw.

I can send along a PR with this change, and a simple object destructuring test... thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions