From 34970d8a692f9532af2aff146558f81e94c1cd8f Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Mon, 31 Dec 2018 15:10:22 -0800 Subject: [PATCH 1/3] give jsdoc outline span before func exp assigned to var --- src/services/outliningElementsCollector.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index 5e3ab31c46a..3f4d345be8c 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,12 @@ namespace ts.OutliningElementsCollector { } depthRemaining++; } + + function isFunctionExpressionAssignedToVariable(n: Node) { + return (isFunctionExpression(n) || isArrowFunction(n)) && + n.parent && n.parent.parent && n.parent.parent.parent && + isVariableStatement(n.parent.parent.parent); + } } function addRegionOutliningSpans(sourceFile: SourceFile, out: Push): void { From bb2f3001917ae61caa2c0a98ac25bea84df0aa9a Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Mon, 31 Dec 2018 15:15:47 -0800 Subject: [PATCH 2/3] Add test --- tests/cases/fourslash/getOutliningForBlockComments.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) 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()); From 048d04684bd79722c83c42ccf2530e491cde54a9 Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Mon, 31 Dec 2018 15:56:54 -0800 Subject: [PATCH 3/3] use existing util functions --- src/compiler/utilities.ts | 2 +- src/services/outliningElementsCollector.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) 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 3f4d345be8c..c6080277fc7 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -60,9 +60,11 @@ namespace ts.OutliningElementsCollector { } function isFunctionExpressionAssignedToVariable(n: Node) { - return (isFunctionExpression(n) || isArrowFunction(n)) && - n.parent && n.parent.parent && n.parent.parent.parent && - isVariableStatement(n.parent.parent.parent); + if (!isFunctionExpression(n) && !isArrowFunction(n)) { + return false; + } + const ancestor = findAncestor(n, isVariableStatement); + return !!ancestor && getSingleInitializerOfVariableStatementOrPropertyDeclaration(ancestor) === n; } }