diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 23c92216c56..9c6b27de13c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2121,7 +2121,7 @@ namespace ts { : undefined; } - function getSingleInitializerOfVariableStatementOrPropertyDeclaration(node: Node): Expression | undefined { + export function getSingleInitializerOfVariableStatementOrPropertyDeclaration(node: Node): Expression | undefined { switch (node.kind) { case SyntaxKind.VariableStatement: const v = getSingleVariableOfVariableStatement(node); diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 35f44bb34c2..6f834512f0b 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -37,6 +37,10 @@ namespace ts.OutliningElementsCollector { addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out); } + if (isFunctionExpressionAssignedToVariable(n)) { + addOutliningForLeadingCommentsForNode(n.parent.parent.parent, sourceFile, cancellationToken, out); + } + const span = getOutliningSpanForNode(n, sourceFile); if (span) out.push(span); @@ -54,6 +58,14 @@ namespace ts.OutliningElementsCollector { } depthRemaining++; } + + function isFunctionExpressionAssignedToVariable(n: Node) { + if (!isFunctionExpression(n) && !isArrowFunction(n)) { + return false; + } + const ancestor = findAncestor(n, isVariableStatement); + return !!ancestor && getSingleInitializerOfVariableStatementOrPropertyDeclaration(ancestor) === n; + } } function addRegionOutliningSpans(sourceFile: SourceFile, out: Push): void { diff --git a/tests/cases/fourslash/getOutliningForBlockComments.ts b/tests/cases/fourslash/getOutliningForBlockComments.ts index 66838d0db75..cc0bbc3c310 100644 --- a/tests/cases/fourslash/getOutliningForBlockComments.ts +++ b/tests/cases/fourslash/getOutliningForBlockComments.ts @@ -99,6 +99,17 @@ //// return 1; //// }|] ////}|] +//// +////// Over a function expression assigned to a variable +//// [|/** +//// * Return a sum +//// * @param {Number} y +//// * @param {Number} z +//// * @returns {Number} the sum of y and z +//// */|] +//// const sum2 = (y, z) =>[| { +//// return y + z; +//// }|]; verify.outliningSpansInCurrentFile(test.ranges());