diff --git a/toml/_parser.ts b/toml/_parser.ts index 0107258f82f7..1981001bdd43 100644 --- a/toml/_parser.ts +++ b/toml/_parser.ts @@ -894,7 +894,7 @@ export function ParserFactory(parser: ParserComponent) { if (count > line.length) { count -= line.length + 1; } else { - return count; + break; } } return count; diff --git a/toml/parse_test.ts b/toml/parse_test.ts index 8fd88d1b1100..2e69caa80f2f 100644 --- a/toml/parse_test.ts +++ b/toml/parse_test.ts @@ -112,6 +112,14 @@ Violets are\\tblue"""`), TOMLParseError, "Invalid escape sequence: \\?", ); + assertThrows( + () => + parse(`""" +Roses are red +Violets are\\tblue`), + TOMLParseError, + "not closed", + ); }, }); @@ -146,6 +154,14 @@ Roses are red Violets are\\tblue'''`), "Roses are red\nViolets are\\tblue", ); + assertThrows( + () => + parse(`''' +Roses are red +Violets are\\tblue`), + TOMLParseError, + "not closed", + ); }, }); @@ -252,6 +268,7 @@ Deno.test({ assertEquals(parse("224_617.445_991_228"), 224_617.445_991_228); assertThrows(() => parse("")); assertThrows(() => parse("X")); + assertThrows(() => parse("e_+-")); }, }); @@ -352,6 +369,7 @@ Deno.test({ ]`), [1, 2], ); + assertThrows(() => parse("[1, 2, 3"), TOMLParseError, "not closed"); }, }); @@ -426,6 +444,7 @@ Deno.test({ fn() { const source = { foo: {}, + bar: null, }; Utils.deepAssignWithTable( @@ -446,6 +465,7 @@ Deno.test({ }, ], }, + bar: null, }, ); Utils.deepAssignWithTable( @@ -469,8 +489,37 @@ Deno.test({ }, ], }, + bar: null, }, ); + + assertThrows( + () => + Utils.deepAssignWithTable( + source, + { + type: "TableArray", + key: [], + value: { email: "sub@example.com" }, + }, + ), + Error, + "Unexpected key length", + ); + + assertThrows( + () => + Utils.deepAssignWithTable( + source, + { + type: "TableArray", + key: ["bar", "items"], + value: { email: "mail@example.com" }, + }, + ), + Error, + "Unexpected assign", + ); }, }); @@ -480,12 +529,25 @@ Deno.test({ assertThrows( () => parse("foo = 1\nbar ="), TOMLParseError, - "on line 2, column 5", + "line 2, column 5", ); assertThrows( () => parse("foo = 1\nbar = 'foo\nbaz=1"), TOMLParseError, "line 2, column 10", ); + assertThrows( + () => parse(""), + TOMLParseError, + "line 1, column 0", + ); + assertThrows( + () => + ParserFactory((_s) => { + throw "Custom parser"; + })(""), + TOMLParseError, + "[non-error thrown]", + ); }, }); diff --git a/toml/test.ts b/toml/test.ts index d910444029bf..2011910280c2 100644 --- a/toml/test.ts +++ b/toml/test.ts @@ -41,6 +41,7 @@ Deno.test({ literal3: "\\n\\t is 'literal'\\\n", literal4: 'Here are fifteen quotation marks: """""""""""""""', literal5: "Here are fifteen apostrophes: '''''''''''''''", + literal6: "'That,' she said, 'is still pointless.'", withApostrophe: "What if it's not?", withSemicolon: `const message = 'hello world';`, withHexNumberLiteral: @@ -522,7 +523,7 @@ Deno.test({ emptyArray: [], mixedArray1: [1, { b: 2 }], mixedArray2: [{ b: 2 }, 1], - nestedArray1: [[{ b: 1 }]], + nestedArray1: [[{ b: 1, date: new Date("2022-05-13") }]], nestedArray2: [[[{ b: 1 }]]], nestedArray3: [[], [{ b: 1 }]], deepNested: { @@ -534,7 +535,7 @@ Deno.test({ const expected = `emptyArray = [] mixedArray1 = [1,{b = 2}] mixedArray2 = [{b = 2},1] -nestedArray1 = [[{b = 1}]] +nestedArray1 = [[{b = 1,date = "2022-05-13T00:00:00.000"}]] nestedArray2 = [[[{b = 1}]]] nestedArray3 = [[],[{b = 1}]] @@ -762,3 +763,19 @@ Deno.test({ assertEquals(actual, expected); }, }); + +Deno.test({ + name: "stringify() throws on invalid value", + fn() { + assertThrows( + () => stringify({ a: [[null]] }), + Error, + "should never reach", + ); + assertThrows( + () => stringify({ a: [[undefined]] }), + Error, + "should never reach", + ); + }, +}); diff --git a/toml/testdata/string.toml b/toml/testdata/string.toml index d5d84afe4822..dec147320b78 100644 --- a/toml/testdata/string.toml +++ b/toml/testdata/string.toml @@ -48,6 +48,7 @@ literal3 = ''' ''' literal4 = '''Here are fifteen quotation marks: """""""""""""""''' literal5 = "Here are fifteen apostrophes: '''''''''''''''" +literal6 = ''''That,' she said, 'is still pointless.'''' withApostrophe = "What if it's not?" withSemicolon = "const message = 'hello world';"