From fd2d28df0261b6b574cc415e9d47b02df21371eb Mon Sep 17 00:00:00 2001 From: Tingan Ho Date: Mon, 22 Feb 2016 12:38:14 +0800 Subject: [PATCH] Fixes new implementation --- src/services/navigationBar.ts | 23 +++++++++++++------ ...ionBarItemsInsideMethodsAndConstructors.ts | 7 +++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 44a71141694..b537a56e856 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -157,8 +157,12 @@ namespace ts.NavigationBar { topLevelNodes.push(node); forEach((node).members, (node) => { if (node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.Constructor) { - if ((node).body) { - topLevelNodes.push(node); + type FunctionLikeMember = MethodDeclaration | ConstructorDeclaration; + if ((node).body) { + // We do not include methods that does not have child functions in it, because of duplications. + if (hasNonAnonymousFunctionDeclarations(((node).body).statements)) { + topLevelNodes.push(node); + } addTopLevelNodes(((node).body).statements, topLevelNodes); } } @@ -186,14 +190,19 @@ namespace ts.NavigationBar { } } + function hasNonAnonymousFunctionDeclarations(nodes: NodeArray) { + if (forEach(nodes, s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text))) { + return true; + } + } + function isTopLevelFunctionDeclaration(functionDeclaration: FunctionLikeDeclaration) { if (functionDeclaration.kind === SyntaxKind.FunctionDeclaration) { // A function declaration is 'top level' if it contains any function declarations // within it. if (functionDeclaration.body && functionDeclaration.body.kind === SyntaxKind.Block) { // Proper function declarations can only have identifier names - if (forEach((functionDeclaration.body).statements, - s => s.kind === SyntaxKind.FunctionDeclaration && !isEmpty((s).name.text))) { + if (hasNonAnonymousFunctionDeclarations((functionDeclaration.body).statements)) { return true; } @@ -390,13 +399,13 @@ namespace ts.NavigationBar { case SyntaxKind.MethodDeclaration: case SyntaxKind.Constructor: - return createMemberFunctionLikeItem(node); + return createMemberFunctionLikeItem(node); case SyntaxKind.EnumDeclaration: return createEnumItem(node); case SyntaxKind.InterfaceDeclaration: - return createIterfaceItem(node); + return createInterfaceItem(node); case SyntaxKind.ModuleDeclaration: return createModuleItem(node); @@ -540,7 +549,7 @@ namespace ts.NavigationBar { getIndent(node)); } - function createIterfaceItem(node: InterfaceDeclaration): ts.NavigationBarItem { + function createInterfaceItem(node: InterfaceDeclaration): ts.NavigationBarItem { let childItems = getItemsWorker(sortNodes(removeDynamicallyNamedProperties(node)), createChildItem); return getNavigationBarItem( node.name.text, diff --git a/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts b/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts index 538e864d10c..4dcca43af35 100644 --- a/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts +++ b/tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts @@ -13,6 +13,7 @@ //// {| "itemName": "LocalEnumMemberInConstructor", "kind": "property", "parentName": "LocalEnumInConstructor"|}LocalEnumMemberInConstructor, //// } //// } +//// //// method() { //// {| "itemName": "LocalFunctionInMethod", "kind": "function", "parentName": "foo"|}function LocalFunctionInMethod() { //// {| "itemName": "LocalFunctionInLocalFunctionInMethod", "kind": "function", "parentName": "bar"|}function LocalFunctionInLocalFunctionInMethod() { @@ -27,6 +28,10 @@ //// {| "itemName": "LocalEnumMemberInMethod", "kind": "property", "parentName": "foo"|}LocalEnumMemberInMethod, //// } //// } +//// +//// emptyMethod() { // Non child functions method should not be duplicated +//// +//// } ////} test.markers().forEach((marker) => { @@ -34,4 +39,4 @@ test.markers().forEach((marker) => { }); // no other items -verify.getScriptLexicalStructureListCount(12); +verify.getScriptLexicalStructureListCount(17);