Skip to content

Commit d6af9b7

Browse files
committed
Fix indentation preservation in JSDoc (microsoft#37717)
This fixes two bugs in the parseJSDocCommentWorker(). 1. The initial indent was calculated wrongly. It was set to the difference between the index of the last newline or beginning of file and the current start marker (position of /**). By calculating it this way, the newline character itself is counted as indentation character as well. The initial indent is used as margin for the whole comment. The margin contains the amount of characters to skip before the actual content or payload of a comment line. The algorithm does not skip non-whitespace characters at the beginning of the content, but it would strip away one whitespace character for indented content (which does matter, if there is e.g. a Markdown code block with indentation in the comment). 2. When reducing initial whitespace sequences of comment lines by the remaining margin the algorithm cut off one character too much. This might have been introduced to fix 1. It had a similar effect as 1.
1 parent 52dc9f2 commit d6af9b7

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/compiler/parser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6884,7 +6884,8 @@ namespace ts {
68846884
let state = JSDocState.SawAsterisk;
68856885
let margin: number | undefined;
68866886
// + 4 for leading '/** '
6887-
let indent = start - Math.max(content.lastIndexOf("\n", start), 0) + 4;
6887+
// + 1 because the last index of \n is always one index before the first character in the line and coincidentally, if there is no \n before start, it is -1, which is also one index before the first character
6888+
let indent = start - (content.lastIndexOf("\n", start) + 1) + 4;
68886889
function pushComment(text: string) {
68896890
if (!margin) {
68906891
margin = indent;
@@ -6940,7 +6941,7 @@ namespace ts {
69406941
comments.push(whitespace);
69416942
}
69426943
else if (margin !== undefined && indent + whitespace.length > margin) {
6943-
comments.push(whitespace.slice(margin - indent - 1));
6944+
comments.push(whitespace.slice(margin - indent));
69446945
}
69456946
indent += whitespace.length;
69466947
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///<reference path="fourslash.ts" />
2+
// @allowJs: true
3+
// @Filename: Foo.js
4+
5+
/////**
6+
//// * Does some stuff.
7+
//// * Second line.
8+
//// * Third line.
9+
//// */
10+
////function foo/**/(){}
11+
12+
goTo.marker();
13+
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///<reference path="fourslash.ts" />
2+
// @allowJs: true
3+
// @Filename: Foo.js
4+
5+
/////**
6+
//// Does some stuff.
7+
//// Second line.
8+
//// Third line.
9+
////*/
10+
////function foo/**/(){}
11+
12+
goTo.marker();
13+
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///<reference path="fourslash.ts" />
2+
// @allowJs: true
3+
// @Filename: Foo.js
4+
5+
/////**
6+
//// Does some stuff.
7+
//// Second line.
8+
//// Third line.
9+
////*/
10+
////function foo/**/(){}
11+
12+
goTo.marker();
13+
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");

0 commit comments

Comments
 (0)