Input
var [ a, , b ] = list
[ b, a ] = [ a, b ]
Expected
Parse tree should contain two statements:
{
"Type": "Program",
"Body": [
{
"Type": "VariableDeclaration",
"Declarations": [
{
"Type": "VariableDeclarator",
"Id": {
"Type": "ArrayPattern",
"Elements": [
{
"Type": "Identifier",
"Name": "a",
"Loc": {}
},
null,
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
}
],
"Loc": {}
},
"Init": {
"Type": "Identifier",
"Name": "list",
"Loc": {}
},
"Loc": {}
}
],
"Kind": "var",
"Loc": {}
},
{
"Type": "ExpressionStatement",
"Expression": {
"Left": {
"Type": "ArrayPattern",
"Elements": [
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
},
{
"Type": "Identifier",
"Name": "a",
"Loc": {}
}
],
"Loc": {}
},
"Right": {
"Type": "ArrayExpression",
"Elements": [
{
"Type": "Identifier",
"Name": "a",
"Loc": {}
},
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
}
],
"Loc": {}
},
"Loc": {}
},
"Loc": {}
}
],
"SourceType": "script",
"Loc": {}
}
Just add semicolon ;
to the end of the first line to get the correct output:
var [ a, , b ] = list;
[ b, a ] = [ a, b ]
Actual
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",
"Loc": {}
},
null,
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
}
],
"Loc": {}
},
"Init": {
"Left": {
"Type": "MemberExpression",
"Object": {
"Type": "Identifier",
"Name": "list",
"Loc": {}
},
"Property": {
"Type": "SequenceExpression",
"Expressions": [
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
},
{
"Type": "Identifier",
"Name": "a",
"Loc": {}
}
],
"Loc": {}
},
"Computed": true,
"Loc": {}
},
"Right": {
"Type": "ArrayExpression",
"Elements": [
{
"Type": "Identifier",
"Name": "a",
"Loc": {}
},
{
"Type": "Identifier",
"Name": "b",
"Loc": {}
}
],
"Loc": {}
},
"Loc": {}
},
"Loc": {}
}
],
"Kind": "var",
"Loc": {}
}
],
"SourceType": "script",
"Loc": {}
}