mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 21:01:52 +00:00
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:
@@ -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
10
test-toml-fix.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[global]
|
||||
inline_table = { q1 = 1 }
|
||||
|
||||
[[items]]
|
||||
q1 = 1
|
||||
q2 = 2
|
||||
|
||||
[[items]]
|
||||
q1 = 3
|
||||
q2 = 4
|
||||
@@ -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 }]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user