Skip to content

Commit

Permalink
add binding elements from variable declaration into script lexical st…
Browse files Browse the repository at this point in the history
…ructure
  • Loading branch information
vladima committed Jan 24, 2015
1 parent b05f2bf commit f4ca318
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
32 changes: 25 additions & 7 deletions src/services/navigationBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module ts.NavigationBar {
case SyntaxKind.ArrayBindingPattern:
forEach((<BindingPattern>node).elements, visit);
break;
case SyntaxKind.BindingElement:
case SyntaxKind.VariableDeclaration:
if (isBindingPattern((<VariableDeclaration>node).name)) {
visit((<VariableDeclaration>node).name);
Expand Down Expand Up @@ -262,17 +263,34 @@ module ts.NavigationBar {
return createItem(node, getTextOfNode((<FunctionLikeDeclaration>node).name), ts.ScriptElementKind.functionElement);

case SyntaxKind.VariableDeclaration:
if (isBindingPattern((<VariableDeclaration>node).name)) {
break;
case SyntaxKind.BindingElement:
var variableDeclarationNode: Node;
var name: Node;

if (node.kind === SyntaxKind.BindingElement) {
name = (<BindingElement>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);

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Jan 29, 2015

Contributor

How does this assert not crash for an assignment pattern?

}
if (isConst(node)) {
return createItem(node, getTextOfNode((<VariableDeclaration>node).name), ts.ScriptElementKind.constElement);
else {
Debug.assert(!isBindingPattern((<VariableDeclaration>node).name));
variableDeclarationNode = node;
name = (<VariableDeclaration>node).name;
}

if (isConst(variableDeclarationNode)) {
return createItem(node, getTextOfNode(name), ts.ScriptElementKind.constElement);
}
else if (isLet(node)) {
return createItem(node, getTextOfNode((<VariableDeclaration>node).name), ts.ScriptElementKind.letElement);
else if (isLet(variableDeclarationNode)) {
return createItem(node, getTextOfNode(name), ts.ScriptElementKind.letElement);
}
else {
return createItem(node, getTextOfNode((<VariableDeclaration>node).name), ts.ScriptElementKind.variableElement);
return createItem(node, getTextOfNode(name), ts.ScriptElementKind.variableElement);
}

case SyntaxKind.Constructor:
Expand Down
19 changes: 18 additions & 1 deletion tests/cases/fourslash/scriptLexicalStructureBindingPatterns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
/// <reference path='fourslash.ts'/>
////'use strict'

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Jan 29, 2015

Contributor

I'm curious, why did you add this?

////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");

0 comments on commit f4ca318

Please sign in to comment.