Skip to content

Commit 6cd27a3

Browse files
author
Andy
authored
Support doc comment template at function expression (microsoft#25050)
1 parent 4c326b2 commit 6cd27a3

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

src/services/jsDoc.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ namespace ts.JsDoc {
263263
return { newText: singleLineResult, caretOffset: 3 };
264264
}
265265

266-
const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
267-
const lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
268-
269-
// replace non-whitespace characters in prefix with spaces.
270-
const indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).replace(/\S/i, () => " ");
266+
const indentationStr = getIndentationStringAtPosition(sourceFile, position);
271267

272268
// A doc comment consists of the following
273269
// * The opening comment line
@@ -276,8 +272,7 @@ namespace ts.JsDoc {
276272
// * TODO: other tags.
277273
// * the closing comment line
278274
// * if the caret was directly in front of the object, then we add an extra line and indentation.
279-
const preamble = "/**" + newLine +
280-
indentationStr + " * ";
275+
const preamble = "/**" + newLine + indentationStr + " * ";
281276
const result =
282277
preamble + newLine +
283278
parameterDocComments(parameters, hasJavaScriptFileExtension(sourceFile.fileName), indentationStr, newLine) +
@@ -287,6 +282,14 @@ namespace ts.JsDoc {
287282
return { newText: result, caretOffset: preamble.length };
288283
}
289284

285+
function getIndentationStringAtPosition(sourceFile: SourceFile, position: number): string {
286+
const { text } = sourceFile;
287+
const lineStart = getLineStartPositionForPosition(position, sourceFile);
288+
let pos = lineStart;
289+
for (; pos <= position && isWhiteSpaceSingleLine(text.charCodeAt(pos)); pos++);
290+
return text.slice(lineStart, pos);
291+
}
292+
290293
function parameterDocComments(parameters: ReadonlyArray<ParameterDeclaration>, isJavaScriptFile: boolean, indentationStr: string, newLine: string): string {
291294
return parameters.map(({ name, dotDotDotToken }, i) => {
292295
const paramName = name.kind === SyntaxKind.Identifier ? name.text : "param" + i;
@@ -303,6 +306,7 @@ namespace ts.JsDoc {
303306
for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) {
304307
switch (commentOwner.kind) {
305308
case SyntaxKind.FunctionDeclaration:
309+
case SyntaxKind.FunctionExpression:
306310
case SyntaxKind.MethodDeclaration:
307311
case SyntaxKind.Constructor:
308312
case SyntaxKind.MethodSignature:

tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const multiLineOffset = 12;
99
//// foo();
1010
//// /*2*/foo(a);
1111
//// /*3*/foo(a, b);
12-
//// /*4*/ foo(a, {x: string}, [c]);
12+
//// /*4*/foo(a, {x: string}, [c]);
1313
//// /*5*/foo(a?, b?, ...args) {
1414
//// }
1515
////}
@@ -43,7 +43,8 @@ verify.docCommentTemplateAt("4", multiLineOffset,
4343
* @param a
4444
* @param param1
4545
* @param param2
46-
*/`);
46+
*/
47+
`);
4748

4849
verify.docCommentTemplateAt("5", multiLineOffset,
4950
`/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*above*/
4+
////const x = /*next*/ function f(p) {}
5+
6+
for (const marker of test.markerNames()) {
7+
verify.docCommentTemplateAt(marker, 8,
8+
`/**
9+
*
10+
* @param p
11+
*/`);
12+
}

tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ const multiLineOffset = 12;
1212
//// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { }
1313
////}
1414

15-
verify.docCommentTemplateAt("0", singleLineOffset,
16-
"/** */");
15+
verify.docCommentTemplateAt("0", singleLineOffset, "/** */");
1716

1817
verify.docCommentTemplateAt("1", multiLineOffset,
1918
`/**
2019
*
2120
* @param x
2221
* @param y
2322
* @param z
24-
*/`);
23+
*/`);

tests/cases/fourslash/docCommentTemplateRegex.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// <reference path='fourslash.ts' />
22

3-
// @Filename: regex.ts
43
////var regex = /*0*///*1*/asdf/*2*/ /*3*///*4*/;
54

65
for (const marker of test.markers()) {

0 commit comments

Comments
 (0)