Skip to content

Commit d3fec7a

Browse files
authored
Merge pull request glayzzle#580 from glayzzle/fix-nested-list-assignment
fix: parse nested shorthand lists as lists
2 parents 39eee33 + 7eb6d40 commit d3fec7a

File tree

4 files changed

+393
-5
lines changed

4 files changed

+393
-5
lines changed

src/parser/expr.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ module.exports = {
489489
expr = this.read_scalar();
490490
if (expr.kind === "array" && expr.shortForm && this.token === "=") {
491491
// list assign
492-
const list = this.node("list")(expr.items, true);
492+
const list = this.convertToList(expr);
493493
if (expr.loc) list.loc = expr.loc;
494494
const right = this.next().read_expr();
495495
return result("assign", list, right, "=");
@@ -508,6 +508,27 @@ module.exports = {
508508
return expr;
509509
},
510510

511+
/**
512+
* Recursively convert nested array to nested list.
513+
*/
514+
convertToList: function (array) {
515+
const convertedItems = array.items.map((entry) => {
516+
if (
517+
entry.value &&
518+
entry.value.kind === "array" &&
519+
entry.value.shortForm
520+
) {
521+
entry.value = this.convertToList(entry.value);
522+
}
523+
return entry;
524+
});
525+
const node = this.node("list")(convertedItems, true);
526+
if (array.loc) node.loc = array.loc;
527+
if (array.leadingComments) node.leadingComments = array.leadingComments;
528+
if (array.trailingComments) node.trailingComments = array.trailingComments;
529+
return node;
530+
},
531+
511532
/**
512533
* Reads assignment
513534
* @param {*} left

test/snapshot/__snapshots__/acid.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -7540,7 +7540,7 @@ next:
75407540
},
75417541
},
75427542
"unpack": false,
7543-
"value": Array {
7543+
"value": List {
75447544
"items": Array [
75457545
Entry {
75467546
"byRef": false,
@@ -7633,7 +7633,7 @@ next:
76337633
},
76347634
},
76357635
],
7636-
"kind": "array",
7636+
"kind": "list",
76377637
"loc": Location {
76387638
"end": Position {
76397639
"column": 16,

0 commit comments

Comments
 (0)