diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ae13b11389..b68d14ed5e 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -6884,7 +6884,8 @@ namespace ts { let state = JSDocState.SawAsterisk; let margin: number | undefined; // + 4 for leading '/** ' - let indent = start - Math.max(content.lastIndexOf("\n", start), 0) + 4; + // + 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 + let indent = start - (content.lastIndexOf("\n", start) + 1) + 4; function pushComment(text: string) { if (!margin) { margin = indent; @@ -6940,7 +6941,7 @@ namespace ts { comments.push(whitespace); } else if (margin !== undefined && indent + whitespace.length > margin) { - comments.push(whitespace.slice(margin - indent - 1)); + comments.push(whitespace.slice(margin - indent)); } indent += whitespace.length; break; diff --git a/tests/cases/fourslash/jsDocIndentationPreservation1.ts b/tests/cases/fourslash/jsDocIndentationPreservation1.ts new file mode 100644 index 0000000000..1e454ac4be --- /dev/null +++ b/tests/cases/fourslash/jsDocIndentationPreservation1.ts @@ -0,0 +1,13 @@ +/// +// @allowJs: true +// @Filename: Foo.js + +/////** +//// * Does some stuff. +//// * Second line. +//// * Third line. +//// */ +////function foo/**/(){} + +goTo.marker(); +verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line."); diff --git a/tests/cases/fourslash/jsDocIndentationPreservation2.ts b/tests/cases/fourslash/jsDocIndentationPreservation2.ts new file mode 100644 index 0000000000..e832bc1270 --- /dev/null +++ b/tests/cases/fourslash/jsDocIndentationPreservation2.ts @@ -0,0 +1,13 @@ +/// +// @allowJs: true +// @Filename: Foo.js + +/////** +//// Does some stuff. +//// Second line. +//// Third line. +////*/ +////function foo/**/(){} + +goTo.marker(); +verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line."); diff --git a/tests/cases/fourslash/jsDocIndentationPreservation3.ts b/tests/cases/fourslash/jsDocIndentationPreservation3.ts new file mode 100644 index 0000000000..117ea0dee4 --- /dev/null +++ b/tests/cases/fourslash/jsDocIndentationPreservation3.ts @@ -0,0 +1,13 @@ +/// +// @allowJs: true +// @Filename: Foo.js + +/////** +//// Does some stuff. +//// Second line. +//// Third line. +////*/ +////function foo/**/(){} + +goTo.marker(); +verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");