From c7145d4ed8fe1a67b9f293d1932509370eea65a9 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Wed, 17 Jul 2024 16:37:52 -0700 Subject: [PATCH] fixes bug rendering backslashes when present as last character in doc comment line --- .../changes/fix-get-doc-2024-6-17-16-36-18.md | 7 +++++++ packages/compiler/src/core/scanner.ts | 7 +++++-- packages/compiler/test/parser.test.ts | 20 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 .chronus/changes/fix-get-doc-2024-6-17-16-36-18.md diff --git a/.chronus/changes/fix-get-doc-2024-6-17-16-36-18.md b/.chronus/changes/fix-get-doc-2024-6-17-16-36-18.md new file mode 100644 index 00000000000..d9dd756e6b4 --- /dev/null +++ b/.chronus/changes/fix-get-doc-2024-6-17-16-36-18.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fixes a bug where ending a non-terminal line in a multi-line comment with a backslash caused the next star to show up in the parsed doc string. \ No newline at end of file diff --git a/packages/compiler/src/core/scanner.ts b/packages/compiler/src/core/scanner.ts index e327ec59805..1b0bed8c32c 100644 --- a/packages/compiler/src/core/scanner.ts +++ b/packages/compiler/src/core/scanner.ts @@ -661,8 +661,11 @@ export function createScanner( return next(Token.NewLine); case CharCode.Backslash: - tokenFlags |= TokenFlags.Escaped; - return position === endPosition - 1 ? next(Token.DocText) : next(Token.DocText, 2); + if (lookAhead(1) === CharCode.At) { + tokenFlags |= TokenFlags.Escaped; + return next(Token.DocText, 2); + } + return next(Token.DocText); case CharCode.Space: case CharCode.Tab: diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index e70bd01501e..72136e299d0 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -1037,7 +1037,7 @@ describe("compiler: parser", () => { *\`\`\` * * \`This is not a @tag either because we're in a code span\`. - * + * * This is not a \\@tag because it is escaped. * * @param x the param @@ -1105,6 +1105,24 @@ describe("compiler: parser", () => { strictEqual(pretend.content[0].text, "this an unknown tag"); }, ], + [ + ` + /** + * Lines that end with \\ + * don't create an extra star. + */ + model M {} + `, + (script) => { + const docs = script.statements[0].docs; + strictEqual(docs?.length, 1); + strictEqual(docs[0].content.length, 1); + strictEqual( + docs[0].content[0].text, + "Lines that end with \\\ndon't create an extra star." + ); + }, + ], ], { docs: true } );