diff --git a/jsonc/parse.ts b/jsonc/parse.ts index 608b65b1503c..21a94e41e2e1 100644 --- a/jsonc/parse.ts +++ b/jsonc/parse.ts @@ -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), diff --git a/jsonc/parse_test.ts b/jsonc/parse_test.ts index 6d66def312b7..38d8ed8f9080 100644 --- a/jsonc/parse_test.ts +++ b/jsonc/parse_test.ts @@ -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,