gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

Array destructuring - Ignoring some returned values - Error #127

Open adros opened 6 years ago

adros commented 6 years ago

When I use array destructuring with ignored values, grasp throws error. (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Ignoring_some_returned_values)

echo 'var [a, , b] = f()' | grasp "*"
Cannot read property 'type' of null

I have checked also flow parser. It has no problems with parsing this code. The 'ignored value' is represented by NULL in elements array:

{
    "type": "ArrayPattern",
    "loc": {
        "source": null,
        "start": {
            "line": 1,
            "column": 4
        },
        "end": {
            "line": 1,
            "column": 12
        }
    },
    "range": [
        4,
        12
    ],
    "elements": [
        {
            "type": "Identifier",
            "loc": {
                "source": null,
                "start": {
                    "line": 1,
                    "column": 5
                },
                "end": {
                    "line": 1,
                    "column": 6
                }
            },
            "range": [
                5,
                6
            ],
            "name": "a",
            "typeAnnotation": null,
            "optional": false
        },
        null,
        {
            "type": "Identifier",
            "loc": {
                "source": null,
                "start": {
                    "line": 1,
                    "column": 10
                },
                "end": {
                    "line": 1,
                    "column": 11
                }
            },
            "range": [
                10,
                11
            ],
            "name": "b",
            "typeAnnotation": null,
            "optional": false
        }
    ],
    "typeAnnotation": null
},
ainthek commented 5 years ago

Please fix this, it fails in manu other scenarios: echo 'const [, , , fieldName] = process.argv' | grasp -s 'program' Cannot read property 'type' of null

ainthek commented 5 years ago

Acorn also emits null , see example: $ echo 'const [, , , fieldName] = process.argv' | acorn { "type": "Program", "start": 0, "end": 39, "body": [ { "type": "VariableDeclaration", "start": 0, "end": 38, "declarations": [ { "type": "VariableDeclarator", "start": 6, "end": 38, "id": { "type": "ArrayPattern", "start": 6, "end": 23, "elements": [ null, null, null, { "type": "Identifier", "start": 13, "end": 22, "name": "fieldName" } ] }, "init": { "type": "MemberExpression", "start": 26, "end": 38, "object": { "type": "Identifier", "start": 26, "end": 33, "name": "process" }, "property": { "type": "Identifier", "start": 34, "end": 38, "name": "argv" }, "computed": false } } ], "kind": "const" } ], "sourceType": "script" }

adros commented 5 months ago

Works here: https://github.com/gratex/grasp/commit/babd2bda9ca733612ef9950925671897c66a4155