Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jsonc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,20 @@ class JsoncParser {
// '"\\\\\\""' => '\\"'
// '"\\\\\\\\"' => '\\\\'
let shouldEscapeNext = false;
let hasEndOfString = false;
i++;
for (; i < this.#length; i++) { // read until find `"`
if (this.#text[i] === '"' && !shouldEscapeNext) {
hasEndOfString = true;
break;
}
shouldEscapeNext = this.#text[i] === "\\" && !shouldEscapeNext;
}
if (!hasEndOfString) {
throw new SyntaxError(
"Cannot parse JSONC: unexpected end of JSONC input",
);
}
yield {
type: "String",
sourceText: this.#text.substring(startIndex, i + 1),
Expand Down
10 changes: 10 additions & 0 deletions jsonc/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ Deno.test({
SyntaxError,
"Cannot parse JSONC: unexpected end of JSONC input",
);
assertInvalidParse(
`"abc`,
SyntaxError,
"Cannot parse JSONC: unexpected end of JSONC input",
);
assertInvalidParse(
`{"a": "b`,
SyntaxError,
"Cannot parse JSONC: unexpected end of JSONC input",
);
assertInvalidParse(
`[]100`,
SyntaxError,
Expand Down
Loading