From 0f9d4c78d4e6b34d90585bc36f7a31d61ad5facb Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 30 Jun 2020 15:16:45 -0700 Subject: [PATCH] JSDoc uses same newlines as normal scanner (#39351) * JSDoc uses same newlines as normal scanner Previously, scanJsDocToken treated each newline character separately, so the sequence \r\n would be treated as two lines. This is unexpected, and not the way the normal scanner does it. This change makes the jsdoc scanner behave the same as the normal scanner. * fix lint in test --- src/compiler/scanner.ts | 6 +++++- src/testRunner/unittests/publicApi.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index ab2a37032ce..376e524f9c2 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -2352,8 +2352,12 @@ namespace ts { return token = SyntaxKind.WhitespaceTrivia; case CharacterCodes.at: return token = SyntaxKind.AtToken; - case CharacterCodes.lineFeed: case CharacterCodes.carriageReturn: + if (text.charCodeAt(pos) === CharacterCodes.lineFeed) { + pos++; + } + // falls through + case CharacterCodes.lineFeed: tokenFlags |= TokenFlags.PrecedingLineBreak; return token = SyntaxKind.NewLineTrivia; case CharacterCodes.asterisk: diff --git a/src/testRunner/unittests/publicApi.ts b/src/testRunner/unittests/publicApi.ts index 80df40e6d9b..7104662d033 100644 --- a/src/testRunner/unittests/publicApi.ts +++ b/src/testRunner/unittests/publicApi.ts @@ -53,6 +53,24 @@ describe("unittests:: Public APIs:: createPrivateIdentifier", () => { }); }); +describe("unittests:: Public APIs:: JSDoc newlines", () => { + it("are preserved verbatim", () => { + const testFilePath = "/file.ts"; + const testFileText = ` +/** +* @example +* Some\n * text\r\n * with newlines. +*/ +function test() {}`; + + const testSourceFile = ts.createSourceFile(testFilePath, testFileText, ts.ScriptTarget.Latest, /*setParentNodes*/ true); + const funcDec = testSourceFile.statements.find(ts.isFunctionDeclaration)!; + const tags = ts.getJSDocTags(funcDec); + assert.isDefined(tags[0].comment); + assert.equal(tags[0].comment, "Some\n text\r\n with newlines."); + }); +}); + describe("unittests:: Public APIs:: isPropertyName", () => { it("checks if a PrivateIdentifier is a valid property name", () => { const prop = ts.factory.createPrivateIdentifier("#foo");