From 3e32a6131bfdb26c34dfc0faeac5939e3499f7c7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 13 May 2016 08:26:41 -0700 Subject: [PATCH 1/4] Include declaration expressions (class expressions and function expressions) in named declarations --- src/services/services.ts | 20 ++++++++++++++----- .../cases/fourslash/declarationExpressions.ts | 19 ++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/cases/fourslash/declarationExpressions.ts diff --git a/src/services/services.ts b/src/services/services.ts index b2f7eb4bdbaaf..6132f65ca6108 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -904,6 +904,7 @@ namespace ts { function visit(node: Node): void { switch (node.kind) { case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: const functionDeclaration = node; @@ -930,6 +931,7 @@ namespace ts { break; case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.TypeAliasDeclaration: case SyntaxKind.EnumDeclaration: @@ -967,11 +969,15 @@ namespace ts { } // fall through case SyntaxKind.VariableDeclaration: - case SyntaxKind.BindingElement: - if (isBindingPattern((node).name)) { - forEachChild((node).name, visit); + case SyntaxKind.BindingElement: { + const decl = node; + if (isBindingPattern(decl.name)) { + forEachChild(decl.name, visit); break; } + if (decl.initializer) + visit(decl.initializer); + } case SyntaxKind.EnumMember: case SyntaxKind.PropertyDeclaration: case SyntaxKind.PropertySignature: @@ -2770,7 +2776,9 @@ namespace ts { /* @internal */ export function getNodeKind(node: Node): string { switch (node.kind) { case SyntaxKind.ModuleDeclaration: return ScriptElementKind.moduleElement; - case SyntaxKind.ClassDeclaration: return ScriptElementKind.classElement; + case SyntaxKind.ClassDeclaration: + case SyntaxKind.ClassExpression: + return ScriptElementKind.classElement; case SyntaxKind.InterfaceDeclaration: return ScriptElementKind.interfaceElement; case SyntaxKind.TypeAliasDeclaration: return ScriptElementKind.typeElement; case SyntaxKind.EnumDeclaration: return ScriptElementKind.enumElement; @@ -2780,7 +2788,9 @@ namespace ts { : isLet(node) ? ScriptElementKind.letElement : ScriptElementKind.variableElement; - case SyntaxKind.FunctionDeclaration: return ScriptElementKind.functionElement; + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + return ScriptElementKind.functionElement; case SyntaxKind.GetAccessor: return ScriptElementKind.memberGetAccessorElement; case SyntaxKind.SetAccessor: return ScriptElementKind.memberSetAccessorElement; case SyntaxKind.MethodDeclaration: diff --git a/tests/cases/fourslash/declarationExpressions.ts b/tests/cases/fourslash/declarationExpressions.ts new file mode 100644 index 0000000000000..764dab1521e28 --- /dev/null +++ b/tests/cases/fourslash/declarationExpressions.ts @@ -0,0 +1,19 @@ +/// + +////class A {} +////const B = class C { +//// public x; +////}; +////function D() {} +////const E = function F() {} + +// Search for properties defined in the constructor, but not other constructor paramters +var searchValue = "search"; +verify.navigationItemsListContains("A", "class", "A", "exact"); +verify.navigationItemsListContains("B", "const", "B", "exact"); +verify.navigationItemsListContains("C", "class", "C", "exact"); +verify.navigationItemsListContains("x", "property", "x", "exact"); + +verify.navigationItemsListContains("D", "function", "D", "exact"); +verify.navigationItemsListContains("E", "const", "E", "exact"); +verify.navigationItemsListContains("F", "function", "F", "exact") From 81a43f0c7e1453ad89ca448dca791262357d8c42 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 13 May 2016 08:36:51 -0700 Subject: [PATCH 2/4] Look for declaration expressions in nested expressions --- src/services/services.ts | 16 +++------------- tests/cases/fourslash/declarationExpressions.ts | 4 ++++ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 6132f65ca6108..578e0166defee 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -946,22 +946,9 @@ namespace ts { case SyntaxKind.SetAccessor: case SyntaxKind.TypeLiteral: addDeclaration(node); - // fall through - case SyntaxKind.Constructor: - case SyntaxKind.VariableStatement: - case SyntaxKind.VariableDeclarationList: - case SyntaxKind.ObjectBindingPattern: - case SyntaxKind.ArrayBindingPattern: - case SyntaxKind.ModuleBlock: forEachChild(node, visit); break; - case SyntaxKind.Block: - if (isFunctionBlock(node)) { - forEachChild(node, visit); - } - break; - case SyntaxKind.Parameter: // Only consider parameter properties if (!(node.flags & NodeFlags.ParameterPropertyModifier)) { @@ -1014,6 +1001,9 @@ namespace ts { } } break; + + default: + forEachChild(node, visit); } } } diff --git a/tests/cases/fourslash/declarationExpressions.ts b/tests/cases/fourslash/declarationExpressions.ts index 764dab1521e28..a7a08e92e599a 100644 --- a/tests/cases/fourslash/declarationExpressions.ts +++ b/tests/cases/fourslash/declarationExpressions.ts @@ -6,6 +6,8 @@ ////}; ////function D() {} ////const E = function F() {} +////console.log(function inner() {}) + // Search for properties defined in the constructor, but not other constructor paramters var searchValue = "search"; @@ -17,3 +19,5 @@ verify.navigationItemsListContains("x", "property", "x", "exact"); verify.navigationItemsListContains("D", "function", "D", "exact"); verify.navigationItemsListContains("E", "const", "E", "exact"); verify.navigationItemsListContains("F", "function", "F", "exact") + +verify.navigationItemsListContains("inner", "function", "inner", "exact"); From 67beb002996754275d16fdcb69a9a0c9fa77f71d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 13 May 2016 10:23:38 -0700 Subject: [PATCH 3/4] Improve test --- .../cases/fourslash/declarationExpressions.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/cases/fourslash/declarationExpressions.ts b/tests/cases/fourslash/declarationExpressions.ts index a7a08e92e599a..22d623c557331 100644 --- a/tests/cases/fourslash/declarationExpressions.ts +++ b/tests/cases/fourslash/declarationExpressions.ts @@ -7,17 +7,23 @@ ////function D() {} ////const E = function F() {} ////console.log(function inner() {}) +////String(function fun() { class cls { public prop; } })) +function navExact(name: string, kind: string) { + verify.navigationItemsListContains(name, kind, name, "exact"); +} -// Search for properties defined in the constructor, but not other constructor paramters -var searchValue = "search"; -verify.navigationItemsListContains("A", "class", "A", "exact"); -verify.navigationItemsListContains("B", "const", "B", "exact"); -verify.navigationItemsListContains("C", "class", "C", "exact"); -verify.navigationItemsListContains("x", "property", "x", "exact"); +navExact("A", "class"); +navExact("B", "const"); +navExact("C", "class"); +navExact("x", "property"); -verify.navigationItemsListContains("D", "function", "D", "exact"); -verify.navigationItemsListContains("E", "const", "E", "exact"); -verify.navigationItemsListContains("F", "function", "F", "exact") +navExact("D", "function"); +navExact("E", "const"); +navExact("F", "function") -verify.navigationItemsListContains("inner", "function", "inner", "exact"); +navExact("inner", "function"); + +navExact("fun", "function"); +navExact("cls", "class"); +navExact("prop", "property"); From b91d14fd6cc0d4be8611451813ee7ecbea85f713 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 16 May 2016 06:41:39 -0700 Subject: [PATCH 4/4] Test expressions with no name --- tests/cases/fourslash/declarationExpressions.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/cases/fourslash/declarationExpressions.ts b/tests/cases/fourslash/declarationExpressions.ts index 22d623c557331..7f2a64d8ecfb6 100644 --- a/tests/cases/fourslash/declarationExpressions.ts +++ b/tests/cases/fourslash/declarationExpressions.ts @@ -6,8 +6,9 @@ ////}; ////function D() {} ////const E = function F() {} -////console.log(function inner() {}) -////String(function fun() { class cls { public prop; } })) +////console.log(function() {}, class {}); // Expression with no name should have no effect. +////console.log(function inner() {}); +////String(function fun() { class cls { public prop; } })); function navExact(name: string, kind: string) { verify.navigationItemsListContains(name, kind, name, "exact");