From c1e853cc7d92ff489294b992827031ca3a42f9b7 Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 21 May 2024 11:36:15 +0200 Subject: [PATCH] fix: ignore invalid escape sequences in doc text --- packages/compiler/src/core/scanner.ts | 28 ++++++++++++++++++++++++++- packages/compiler/test/parser.test.ts | 24 +++++++++++------------ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/compiler/src/core/scanner.ts b/packages/compiler/src/core/scanner.ts index f245b4f742..d4acda5b92 100644 --- a/packages/compiler/src/core/scanner.ts +++ b/packages/compiler/src/core/scanner.ts @@ -1044,7 +1044,33 @@ export function createScanner( function getDocTextValue(): string { if (tokenFlags & TokenFlags.Escaped) { - return unescapeString(tokenPosition, position); + let start = tokenPosition; + const end = position; + + let result = ""; + let pos = start; + + while (pos < end) { + const ch = input.charCodeAt(pos); + if (ch !== CharCode.Backslash) { + pos++; + continue; + } + + result += input.substring(start, pos); + switch (input.charCodeAt(pos + 1)) { + case CharCode.At: + result += "@"; + break; + default: + result += input.substring(pos, pos + 2); + } + pos += 2; + start = pos; + } + + result += input.substring(start, pos); + return result; } else { return input.substring(tokenPosition, position); } diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index 94d802e49c..e9e551d70c 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -1045,18 +1045,18 @@ describe("compiler: parser", () => { strictEqual( docs[0].content[0].text, "This one has a `code span` and a code fence and it spreads over\n" + - "more than one line.\n\n" + - "```\n" + - "This is not a @tag because we're in a code fence.\n" + - " This is indented code.\n" + - "```\n\n" + - "```\n" + - "This code fence is glued\n" + - "to the stars\n" + - "```\n\n" + - "`This is not a @tag either because we're in a code span`.\n" + - "\n" + - "This is not a @tag because it is escaped." + "more than one line.\n\n" + + "```\n" + + "This is not a @tag because we're in a code fence.\n" + + " This is indented code.\n" + + "```\n\n" + + "```\n" + + "This code fence is glued\n" + + "to the stars\n" + + "```\n\n" + + "`This is not a @tag either because we're in a code span`.\n" + + "\n" + + "This is not a @tag because it is escaped." ); strictEqual(docs[0].tags.length, 6); const [xParam, yParam, tTemplate, uTemplate, returns, pretend] = docs[0].tags;