Skip to content

Commit 0f9d4c7

Browse files
authored
JSDoc uses same newlines as normal scanner (microsoft#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
1 parent e2c5a90 commit 0f9d4c7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/compiler/scanner.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2352,8 +2352,12 @@ namespace ts {
23522352
return token = SyntaxKind.WhitespaceTrivia;
23532353
case CharacterCodes.at:
23542354
return token = SyntaxKind.AtToken;
2355-
case CharacterCodes.lineFeed:
23562355
case CharacterCodes.carriageReturn:
2356+
if (text.charCodeAt(pos) === CharacterCodes.lineFeed) {
2357+
pos++;
2358+
}
2359+
// falls through
2360+
case CharacterCodes.lineFeed:
23572361
tokenFlags |= TokenFlags.PrecedingLineBreak;
23582362
return token = SyntaxKind.NewLineTrivia;
23592363
case CharacterCodes.asterisk:

src/testRunner/unittests/publicApi.ts

+18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ describe("unittests:: Public APIs:: createPrivateIdentifier", () => {
5353
});
5454
});
5555

56+
describe("unittests:: Public APIs:: JSDoc newlines", () => {
57+
it("are preserved verbatim", () => {
58+
const testFilePath = "/file.ts";
59+
const testFileText = `
60+
/**
61+
* @example
62+
* Some\n * text\r\n * with newlines.
63+
*/
64+
function test() {}`;
65+
66+
const testSourceFile = ts.createSourceFile(testFilePath, testFileText, ts.ScriptTarget.Latest, /*setParentNodes*/ true);
67+
const funcDec = testSourceFile.statements.find(ts.isFunctionDeclaration)!;
68+
const tags = ts.getJSDocTags(funcDec);
69+
assert.isDefined(tags[0].comment);
70+
assert.equal(tags[0].comment, "Some\n text\r\n with newlines.");
71+
});
72+
});
73+
5674
describe("unittests:: Public APIs:: isPropertyName", () => {
5775
it("checks if a PrivateIdentifier is a valid property name", () => {
5876
const prop = ts.factory.createPrivateIdentifier("#foo");

0 commit comments

Comments
 (0)