From b05f2bf1a3ebbd0a1967c63d5cd6852d396f4bad Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 23 Jan 2015 17:08:30 -0800 Subject: [PATCH 1/2] handle binding patterns correctly when getting script lexical structure --- src/services/navigationBar.ts | 2 +- .../cases/fourslash/scriptLexicalStructureBindingPatterns.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 9051ee80399..5ffcc6e1b6a 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -51,7 +51,7 @@ module ts.NavigationBar { forEach((node).elements, visit); break; case SyntaxKind.VariableDeclaration: - if (isBindingPattern(node)) { + if (isBindingPattern((node).name)) { visit((node).name); break; } diff --git a/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts b/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts new file mode 100644 index 00000000000..b3141de5720 --- /dev/null +++ b/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts @@ -0,0 +1,4 @@ +/// +////var foo, {} +////var bar, [] +verify.getScriptLexicalStructureListCount(3); // global (1) + variable declarations (2) From f4ca318c39ca4181fb331c2b03980524359c0572 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sat, 24 Jan 2015 00:17:55 -0800 Subject: [PATCH 2/2] add binding elements from variable declaration into script lexical structure --- src/services/navigationBar.ts | 36 ++++++++++++++----- .../scriptLexicalStructureBindingPatterns.ts | 19 +++++++++- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 5ffcc6e1b6a..423ee5e58a6 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -50,6 +50,7 @@ module ts.NavigationBar { case SyntaxKind.ArrayBindingPattern: forEach((node).elements, visit); break; + case SyntaxKind.BindingElement: case SyntaxKind.VariableDeclaration: if (isBindingPattern((node).name)) { visit((node).name); @@ -262,17 +263,34 @@ module ts.NavigationBar { return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.functionElement); case SyntaxKind.VariableDeclaration: - if (isBindingPattern((node).name)) { - break; - } - if (isConst(node)) { - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.constElement); - } - else if (isLet(node)) { - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.letElement); + case SyntaxKind.BindingElement: + var variableDeclarationNode: Node; + var name: Node; + + if (node.kind === SyntaxKind.BindingElement) { + name = (node).name; + variableDeclarationNode = node; + // binding elements are added only for variable declarations + // bubble up to the containing variable declaration + while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) { + variableDeclarationNode = variableDeclarationNode.parent; + } + Debug.assert(variableDeclarationNode !== undefined); } else { - return createItem(node, getTextOfNode((node).name), ts.ScriptElementKind.variableElement); + Debug.assert(!isBindingPattern((node).name)); + variableDeclarationNode = node; + name = (node).name; + } + + if (isConst(variableDeclarationNode)) { + return createItem(node, getTextOfNode(name), ts.ScriptElementKind.constElement); + } + else if (isLet(variableDeclarationNode)) { + return createItem(node, getTextOfNode(name), ts.ScriptElementKind.letElement); + } + else { + return createItem(node, getTextOfNode(name), ts.ScriptElementKind.variableElement); } case SyntaxKind.Constructor: diff --git a/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts b/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts index b3141de5720..2b363bfad3b 100644 --- a/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts +++ b/tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts @@ -1,4 +1,21 @@ /// +////'use strict' ////var foo, {} ////var bar, [] -verify.getScriptLexicalStructureListCount(3); // global (1) + variable declarations (2) +////let foo1, {a, b} +////const bar1, [c, d] +////var {e, x: [f, g]} = {a:1, x:[]}; + +verify.getScriptLexicalStructureListCount(12); // global (1) + variable declarations (4) + binding patterns (7) +verify.getScriptLexicalStructureListContains("foo", "var"); +verify.getScriptLexicalStructureListContains("bar", "var"); +verify.getScriptLexicalStructureListContains("foo1", "let") +verify.getScriptLexicalStructureListContains("a", "let"); +verify.getScriptLexicalStructureListContains("b", "let"); +verify.getScriptLexicalStructureListContains("bar1", "const"); +verify.getScriptLexicalStructureListContains("c", "const"); +verify.getScriptLexicalStructureListContains("d", "const"); +verify.getScriptLexicalStructureListContains("e", "var"); +verify.getScriptLexicalStructureListContains("f", "var"); +verify.getScriptLexicalStructureListContains("g", "var"); +