@@ -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 :
0 commit comments