Skip to content

Commit 78c8b0f

Browse files
authored
fix(toml): nested table arrays (#6794)
1 parent cd1727d commit 78c8b0f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

toml/_parser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,16 @@ function deepAssignTableArray(
209209
return Object.assign(target, unflat(keys, [value]));
210210
}
211211
if (Array.isArray(currentValue)) {
212-
currentValue.push(value);
212+
if (table.keys.length === 1) {
213+
currentValue.push(value);
214+
} else {
215+
const last = currentValue.at(-1);
216+
deepAssign(last, {
217+
type: table.type,
218+
keys: table.keys.slice(1),
219+
value: table.value,
220+
});
221+
}
213222
return target;
214223
}
215224
if (isObject(currentValue)) {

toml/parse_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,20 @@ Deno.test({
857857
},
858858
});
859859

860+
Deno.test({
861+
name: "parse() handles nested table arrays",
862+
fn() {
863+
const content = `
864+
[[table]]
865+
foo = "foo"
866+
[[table.children]]
867+
bar = "bar"`;
868+
869+
const expected = { table: [{ foo: "foo", children: [{ bar: "bar" }] }] };
870+
assertEquals(parse(content), expected);
871+
},
872+
});
873+
860874
Deno.test({
861875
name: "parse() doesn't pollute prototype with __proto__",
862876
async fn() {

0 commit comments

Comments
 (0)