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
This commit is contained in:
Nathan Shively-Sanders
2020-06-30 15:16:45 -07:00
committed by GitHub
parent e2c5a90cc3
commit 0f9d4c78d4
2 changed files with 23 additions and 1 deletions

View File

@@ -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:

View File

@@ -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");