grasp icon indicating copy to clipboard operation
grasp copied to clipboard

Array destructuring - Ignoring some returned values - Error

Open adros opened this issue 5 years ago • 2 comments

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

adros avatar Jul 25 '18 14:07 adros

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

ainthek avatar Aug 21 '19 13:08 ainthek

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

ainthek avatar Aug 21 '19 13:08 ainthek