From f39f42cd899050d256a85904d7bf3abf02eea1ab Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 30 Jun 2020 13:49:58 -0700 Subject: [PATCH 1/2] 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. --- 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 ab2a37032ced2..376e524f9c2d5 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 80df40e6d9b4e..9740020b93270 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, 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"); From 63745e421b7d6ae2abe037abea2a93d9d51cc855 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 30 Jun 2020 14:41:39 -0700 Subject: [PATCH 2/2] fix lint in test --- src/testRunner/unittests/publicApi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/testRunner/unittests/publicApi.ts b/src/testRunner/unittests/publicApi.ts index 9740020b93270..7104662d033ef 100644 --- a/src/testRunner/unittests/publicApi.ts +++ b/src/testRunner/unittests/publicApi.ts @@ -63,7 +63,7 @@ describe("unittests:: Public APIs:: JSDoc newlines", () => { */ function test() {}`; - const testSourceFile = ts.createSourceFile(testFilePath, testFileText, ts.ScriptTarget.Latest, true); + 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);