From 58789ba25c8f47a530f744e798e1f9d4265f9d68 Mon Sep 17 00:00:00 2001 From: Tomas Zijdemans Date: Thu, 3 Sep 2026 07:07:36 +0200 Subject: [PATCH] fix(jsonc): report unterminated strings as unexpected end of input --- jsonc/parse.ts | 7 +++++++ jsonc/parse_test.ts | 10 ++++++++++ 2 files changed, 17 insertions(+) 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,