Skip to content

Commit 4855920

Browse files
author
Andy
authored
navigationBar/Tree: Better description for anonymous function (microsoft#27063)
1 parent b3dd471 commit 4855920

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/services/navigationBar.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,28 +631,50 @@ namespace ts.NavigationBar {
631631
}
632632

633633
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
634+
const { parent } = node;
634635
if (node.name && getFullWidth(node.name) > 0) {
635636
return declarationNameToString(node.name);
636637
}
637638
// See if it is a var initializer. If so, use the var name.
638-
else if (node.parent.kind === SyntaxKind.VariableDeclaration) {
639-
return declarationNameToString((node.parent as VariableDeclaration).name);
639+
else if (isVariableDeclaration(parent)) {
640+
return declarationNameToString(parent.name);
640641
}
641642
// See if it is of the form "<expr> = function(){...}". If so, use the text from the left-hand side.
642-
else if (node.parent.kind === SyntaxKind.BinaryExpression &&
643-
(node.parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken) {
644-
return nodeText((node.parent as BinaryExpression).left).replace(whiteSpaceRegex, "");
643+
else if (isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken) {
644+
return nodeText(parent.left).replace(whiteSpaceRegex, "");
645645
}
646646
// See if it is a property assignment, and if so use the property name
647-
else if (node.parent.kind === SyntaxKind.PropertyAssignment && (node.parent as PropertyAssignment).name) {
648-
return nodeText((node.parent as PropertyAssignment).name);
647+
else if (isPropertyAssignment(parent)) {
648+
return nodeText(parent.name);
649649
}
650650
// Default exports are named "default"
651651
else if (getModifierFlags(node) & ModifierFlags.Default) {
652652
return "default";
653653
}
654+
else if (isClassLike(node)) {
655+
return "<class>";
656+
}
657+
else if (isCallExpression(parent)) {
658+
const name = getCalledExpressionName(parent.expression);
659+
if (name !== undefined) {
660+
const args = mapDefined(parent.arguments, a => isStringLiteral(a) ? a.getText(curSourceFile) : undefined).join(", ");
661+
return `${name}(${args}) callback`;
662+
}
663+
}
664+
return "<function>";
665+
}
666+
667+
function getCalledExpressionName(expr: Expression): string | undefined {
668+
if (isIdentifier(expr)) {
669+
return expr.text;
670+
}
671+
else if (isPropertyAccessExpression(expr)) {
672+
const left = getCalledExpressionName(expr.expression);
673+
const right = expr.name.text;
674+
return left === undefined ? right : `${left}.${right}`;
675+
}
654676
else {
655-
return isClassLike(node) ? "<class>" : "<function>";
677+
return undefined;
656678
}
657679
}
658680

tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
//// // These will only show up as childItems.
1919
//// function z() {}
2020
//// console.log(function() {})
21+
//// describe("this", 'function', () => {});
22+
//// [].map(() => {});
2123
////})
2224
////(function classes() {
2325
//// // Classes show up in top-level regardless of whether they have names or inner declarations.
@@ -71,7 +73,15 @@ verify.navigationTree({
7173
"kind": "function",
7274
"childItems": [
7375
{
74-
"text": "<function>",
76+
"text": "console.log() callback",
77+
"kind": "function"
78+
},
79+
{
80+
"text": `describe("this", 'function') callback`,
81+
"kind": "function"
82+
},
83+
{
84+
"text": `map() callback`,
7585
"kind": "function"
7686
},
7787
{
@@ -185,7 +195,15 @@ verify.navigationBar([
185195
"kind": "function",
186196
"childItems": [
187197
{
188-
"text": "<function>",
198+
"text": "console.log() callback",
199+
"kind": "function"
200+
},
201+
{
202+
"text": `describe("this", 'function') callback`,
203+
"kind": "function"
204+
},
205+
{
206+
"text": `map() callback`,
189207
"kind": "function"
190208
},
191209
{

0 commit comments

Comments
 (0)