diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index d4bb1f73585..26a5f098967 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -47,10 +47,8 @@ import { getSyntacticModifierFlags, getTextOfIdentifierOrLiteral, getTextOfNode, - hasDynamicName, hasJSDocNodes, Identifier, - idText, ImportClause, InterfaceDeclaration, InternalSymbolName, @@ -62,8 +60,10 @@ import { isCallExpression, isClassDeclaration, isClassLike, + isComputedPropertyName, isDeclaration, isElementAccessExpression, + isEntityNameExpression, isExportAssignment, isExpression, isExternalModule, @@ -73,6 +73,7 @@ import { isJSDocTypeAlias, isModuleBlock, isModuleDeclaration, + isNumericLiteral, isObjectLiteralExpression, isParameterPropertyDeclaration, isPrivateIdentifier, @@ -82,6 +83,7 @@ import { isPropertyNameLiteral, isStatic, isStringLiteralLike, + isStringOrNumericLiteralLike, isToken, isVariableDeclaration, lastOrUndefined, @@ -306,19 +308,15 @@ function addNodeWithRecursiveInitializer(node: VariableDeclaration | PropertyAss } } -/** - * Historically, we've elided dynamic names from the nav tree (including late bound names), - * but included certain "well known" symbol names. While we no longer distinguish those well-known - * symbols from other unique symbols, we do the below to retain those members in the nav tree. - */ function hasNavigationBarName(node: Declaration) { - return !hasDynamicName(node) || - ( - node.kind !== SyntaxKind.BinaryExpression && - isPropertyAccessExpression(node.name.expression) && - isIdentifier(node.name.expression.expression) && - idText(node.name.expression.expression) === "Symbol" - ); + const name = getNameOfDeclaration(node); + if (name === undefined) return false; + + if (isComputedPropertyName(name)) { + const expression = name.expression; + return isEntityNameExpression(expression) || isNumericLiteral(expression) || isStringOrNumericLiteralLike(expression); + } + return !!name; } /** Look for navigation bar items in node's subtree, adding them to the current `parent`. */ diff --git a/tests/cases/fourslash/navigationBarItemsComputedNames.ts b/tests/cases/fourslash/navigationBarItemsComputedNames.ts new file mode 100644 index 00000000000..dcf5cdc8ce7 --- /dev/null +++ b/tests/cases/fourslash/navigationBarItemsComputedNames.ts @@ -0,0 +1,67 @@ +/// + +////const enum E { +//// A = 'A', +////} +////const a = ''; +//// +////class C { +//// [a]() { +//// return 1; +//// } +//// +//// [E.A]() { +//// return 1; +//// } +//// +//// [1]() { +//// return 1; +//// }, +//// +//// ["foo"]() { +//// return 1; +//// }, +////} + +verify.navigationTree({ + text: "", + kind: "script", + childItems: [ + { + text: "a", + kind: "const" + }, + { + text: "C", + kind: "class", + childItems: [ + { + text: "[a]", + kind: "method" + }, + { + text: "[E.A]", + kind: "method" + }, + { + text: "[1]", + kind: "method" + }, + { + text: "[\"foo\"]", + kind: "method" + } + ], + }, + { + text: "E", + kind: "enum", + childItems: [ + { + text: "A", + kind: "enum member" + }, + ] + } + ] +});