Support doc comment template at function expression (#25050)

This commit is contained in:
Andy
2018-06-25 11:40:45 -07:00
committed by GitHub
parent 4c326b2b6c
commit 6cd27a3217
5 changed files with 28 additions and 13 deletions

View File

@@ -263,11 +263,7 @@ namespace ts.JsDoc {
return { newText: singleLineResult, caretOffset: 3 };
}
const posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
const lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
// replace non-whitespace characters in prefix with spaces.
const indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).replace(/\S/i, () => " ");
const indentationStr = getIndentationStringAtPosition(sourceFile, position);
// A doc comment consists of the following
// * The opening comment line
@@ -276,8 +272,7 @@ namespace ts.JsDoc {
// * TODO: other tags.
// * the closing comment line
// * if the caret was directly in front of the object, then we add an extra line and indentation.
const preamble = "/**" + newLine +
indentationStr + " * ";
const preamble = "/**" + newLine + indentationStr + " * ";
const result =
preamble + newLine +
parameterDocComments(parameters, hasJavaScriptFileExtension(sourceFile.fileName), indentationStr, newLine) +
@@ -287,6 +282,14 @@ namespace ts.JsDoc {
return { newText: result, caretOffset: preamble.length };
}
function getIndentationStringAtPosition(sourceFile: SourceFile, position: number): string {
const { text } = sourceFile;
const lineStart = getLineStartPositionForPosition(position, sourceFile);
let pos = lineStart;
for (; pos <= position && isWhiteSpaceSingleLine(text.charCodeAt(pos)); pos++);
return text.slice(lineStart, pos);
}
function parameterDocComments(parameters: ReadonlyArray<ParameterDeclaration>, isJavaScriptFile: boolean, indentationStr: string, newLine: string): string {
return parameters.map(({ name, dotDotDotToken }, i) => {
const paramName = name.kind === SyntaxKind.Identifier ? name.text : "param" + i;
@@ -303,6 +306,7 @@ namespace ts.JsDoc {
for (let commentOwner = tokenAtPos; commentOwner; commentOwner = commentOwner.parent) {
switch (commentOwner.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.Constructor:
case SyntaxKind.MethodSignature:

View File

@@ -9,7 +9,7 @@ const multiLineOffset = 12;
//// foo();
//// /*2*/foo(a);
//// /*3*/foo(a, b);
//// /*4*/ foo(a, {x: string}, [c]);
//// /*4*/foo(a, {x: string}, [c]);
//// /*5*/foo(a?, b?, ...args) {
//// }
////}
@@ -43,7 +43,8 @@ verify.docCommentTemplateAt("4", multiLineOffset,
* @param a
* @param param1
* @param param2
*/`);
*/
`);
verify.docCommentTemplateAt("5", multiLineOffset,
`/**

View File

@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
/////*above*/
////const x = /*next*/ function f(p) {}
for (const marker of test.markerNames()) {
verify.docCommentTemplateAt(marker, 8,
`/**
*
* @param p
*/`);
}

View File

@@ -12,8 +12,7 @@ const multiLineOffset = 12;
//// [1 + 2 + 3 + Math.rand()](x: number, y: string, z = true) { }
////}
verify.docCommentTemplateAt("0", singleLineOffset,
"/** */");
verify.docCommentTemplateAt("0", singleLineOffset, "/** */");
verify.docCommentTemplateAt("1", multiLineOffset,
`/**
@@ -21,4 +20,4 @@ verify.docCommentTemplateAt("1", multiLineOffset,
* @param x
* @param y
* @param z
*/`);
*/`);

View File

@@ -1,6 +1,5 @@
/// <reference path='fourslash.ts' />
// @Filename: regex.ts
////var regex = /*0*///*1*/asdf/*2*/ /*3*///*4*/;
for (const marker of test.markers()) {