Skip to content

Commit bb70e9e

Browse files
committed
Merge pull request #1346 from Microsoft/destructuring
Destructuring
2 parents cd2016c + 7bc35b3 commit bb70e9e

File tree

45 files changed

+2578
-1072
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2578
-1072
lines changed

src/compiler/binder.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,9 @@ module ts {
332332
// symbol as its sole member. To the rest of the system, this symbol will be indistinguishable
333333
// from an actual type literal symbol you would have gotten had you used the long form.
334334

335-
var symbolKind = node.kind === SyntaxKind.FunctionType ? SymbolFlags.CallSignature : SymbolFlags.ConstructSignature;
336-
var symbol = createSymbol(symbolKind, getDeclarationName(node));
337-
addDeclarationToSymbol(symbol, node, symbolKind);
338-
bindChildren(node, symbolKind, /*isBlockScopeContainer:*/ false);
335+
var symbol = createSymbol(SymbolFlags.Signature, getDeclarationName(node));
336+
addDeclarationToSymbol(symbol, node, SymbolFlags.Signature);
337+
bindChildren(node, SymbolFlags.Signature, /*isBlockScopeContainer:*/ false);
339338

340339
var typeLiteralSymbol = createSymbol(SymbolFlags.TypeLiteral, "__type");
341340
addDeclarationToSymbol(typeLiteralSymbol, node, SymbolFlags.TypeLiteral);
@@ -376,21 +375,33 @@ module ts {
376375
}
377376
declareSymbol(blockScopeContainer.locals, undefined, node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
378377
}
379-
380378
bindChildren(node, SymbolFlags.BlockScopedVariable, /*isBlockScopeContainer*/ false);
381379
}
382380

381+
function getDestructuringParameterName(node: Declaration) {
382+
return "__" + indexOf((<SignatureDeclaration>node.parent).parameters, node);
383+
}
384+
383385
function bind(node: Node) {
384386
node.parent = parent;
385387
switch (node.kind) {
386388
case SyntaxKind.TypeParameter:
387389
bindDeclaration(<Declaration>node, SymbolFlags.TypeParameter, SymbolFlags.TypeParameterExcludes, /*isBlockScopeContainer*/ false);
388390
break;
389391
case SyntaxKind.Parameter:
390-
bindDeclaration(<Declaration>node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes, /*isBlockScopeContainer*/ false);
392+
if (isBindingPattern((<Declaration>node).name)) {
393+
bindAnonymousDeclaration(<Declaration>node, SymbolFlags.FunctionScopedVariable, getDestructuringParameterName(<Declaration>node), /*isBlockScopeContainer*/ false);
394+
}
395+
else {
396+
bindDeclaration(<Declaration>node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes, /*isBlockScopeContainer*/ false);
397+
}
391398
break;
392399
case SyntaxKind.VariableDeclaration:
393-
if (node.flags & NodeFlags.BlockScoped) {
400+
case SyntaxKind.BindingElement:
401+
if (isBindingPattern((<Declaration>node).name)) {
402+
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
403+
}
404+
else if (node.flags & NodeFlags.BlockScoped) {
394405
bindBlockScopedVariableDeclaration(<Declaration>node);
395406
}
396407
else {
@@ -399,6 +410,8 @@ module ts {
399410
break;
400411
case SyntaxKind.PropertyDeclaration:
401412
case SyntaxKind.PropertySignature:
413+
bindDeclaration(<Declaration>node, SymbolFlags.Property | ((<PropertyDeclaration>node).questionToken ? SymbolFlags.Optional : 0), SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
414+
break;
402415
case SyntaxKind.PropertyAssignment:
403416
case SyntaxKind.ShorthandPropertyAssignment:
404417
bindDeclaration(<Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes, /*isBlockScopeContainer*/ false);
@@ -407,23 +420,19 @@ module ts {
407420
bindDeclaration(<Declaration>node, SymbolFlags.EnumMember, SymbolFlags.EnumMemberExcludes, /*isBlockScopeContainer*/ false);
408421
break;
409422
case SyntaxKind.CallSignature:
410-
bindDeclaration(<Declaration>node, SymbolFlags.CallSignature, 0, /*isBlockScopeContainer*/ false);
411-
break;
412423
case SyntaxKind.ConstructSignature:
413-
bindDeclaration(<Declaration>node, SymbolFlags.ConstructSignature, 0, /*isBlockScopeContainer*/ true);
424+
case SyntaxKind.IndexSignature:
425+
bindDeclaration(<Declaration>node, SymbolFlags.Signature, 0, /*isBlockScopeContainer*/ false);
414426
break;
415427
case SyntaxKind.MethodDeclaration:
416428
case SyntaxKind.MethodSignature:
417429
// If this is an ObjectLiteralExpression method, then it sits in the same space
418430
// as other properties in the object literal. So we use SymbolFlags.PropertyExcludes
419431
// so that it will conflict with any other object literal members with the same
420432
// name.
421-
bindDeclaration(<Declaration>node, SymbolFlags.Method,
433+
bindDeclaration(<Declaration>node, SymbolFlags.Method | ((<MethodDeclaration>node).questionToken ? SymbolFlags.Optional : 0),
422434
isObjectLiteralMethod(node) ? SymbolFlags.PropertyExcludes : SymbolFlags.MethodExcludes, /*isBlockScopeContainer*/ true);
423435
break;
424-
case SyntaxKind.IndexSignature:
425-
bindDeclaration(<Declaration>node, SymbolFlags.IndexSignature, 0, /*isBlockScopeContainer*/ false);
426-
break;
427436
case SyntaxKind.FunctionDeclaration:
428437
bindDeclaration(<Declaration>node, SymbolFlags.Function, SymbolFlags.FunctionExcludes, /*isBlockScopeContainer*/ true);
429438
break;
@@ -483,17 +492,15 @@ module ts {
483492
bindAnonymousDeclaration(<SourceFile>node, SymbolFlags.ValueModule, '"' + removeFileExtension((<SourceFile>node).filename) + '"', /*isBlockScopeContainer*/ true);
484493
break;
485494
}
486-
487495
case SyntaxKind.Block:
488496
case SyntaxKind.TryBlock:
489497
case SyntaxKind.CatchClause:
490498
case SyntaxKind.FinallyBlock:
491499
case SyntaxKind.ForStatement:
492500
case SyntaxKind.ForInStatement:
493501
case SyntaxKind.SwitchStatement:
494-
bindChildren(node, 0 , true);
502+
bindChildren(node, 0, /*isBlockScopeContainer*/ true);
495503
break;
496-
497504
default:
498505
var saveParent = parent;
499506
parent = node;

0 commit comments

Comments
 (0)