Fix TOML parser to handle inline tables and table arrays correctly (#20291)

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
Jarred Sumner
2025-06-09 18:52:30 -07:00
committed by GitHub
parent b642e36da2
commit 2801cb1f4a
3 changed files with 75 additions and 1 deletions

View File

@@ -326,6 +326,7 @@ pub const TOML = struct {
if (p.lexer.has_newline_before) {
is_single_line = false;
}
p.lexer.allow_double_bracket = true;
try p.lexer.expect(.t_close_brace);
return expr;
},
@@ -363,8 +364,8 @@ pub const TOML = struct {
if (p.lexer.has_newline_before) {
is_single_line = false;
}
try p.lexer.expect(.t_close_bracket);
p.lexer.allow_double_bracket = true;
try p.lexer.expect(.t_close_bracket);
return array_;
},
else => {

10
test-toml-fix.toml Normal file
View File

@@ -0,0 +1,10 @@
[global]
inline_table = { q1 = 1 }
[[items]]
q1 = 1
q2 = 2
[[items]]
q1 = 3
q2 = 4

View File

@@ -40,3 +40,66 @@ it("via dynamic import with type attribute", async () => {
it("empty via import statement", () => {
expect(emptyToml).toEqual({});
});
it("inline table followed by table array", () => {
const tomlContent = `
[global]
inline_table = { q1 = 1 }
[[items]]
q1 = 1
q2 = 2
[[items]]
q1 = 3
q2 = 4
`;
// Test via Bun's internal TOML parser
const Bun = globalThis.Bun;
const parsed = Bun.TOML.parse(tomlContent);
expect(parsed.global).toEqual({
inline_table: { q1: 1 },
});
expect(parsed.items).toEqual([
{ q1: 1, q2: 2 },
{ q1: 3, q2: 4 },
]);
});
it("array followed by table array", () => {
const tomlContent = `
[global]
array = [1, 2, 3]
[[items]]
q1 = 1
`;
const Bun = globalThis.Bun;
const parsed = Bun.TOML.parse(tomlContent);
expect(parsed.global).toEqual({
array: [1, 2, 3],
});
expect(parsed.items).toEqual([{ q1: 1 }]);
});
it("nested inline tables", () => {
const tomlContent = `
[global]
nested = { outer = { inner = 1 } }
[[items]]
q1 = 1
`;
const Bun = globalThis.Bun;
const parsed = Bun.TOML.parse(tomlContent);
expect(parsed.global).toEqual({
nested: { outer: { inner: 1 } },
});
expect(parsed.items).toEqual([{ q1: 1 }]);
});