Skip to content

Commit

Permalink
Update the entry point to return property symbol of destructuring ass…
Browse files Browse the repository at this point in the history
…ignment
  • Loading branch information
sheetalkamat committed Apr 13, 2016
1 parent 958a6a4 commit c492fc6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
15 changes: 14 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace ts {
getShorthandAssignmentValueSymbol,
getExportSpecifierLocalTargetSymbol,
getTypeAtLocation: getTypeOfNode,
getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment,
getPropertySymbolOfDestructuringAssignment,
typeToString,
getSymbolDisplayBuilder,
symbolToString,
Expand Down Expand Up @@ -16568,6 +16568,7 @@ namespace ts {
// [ a ] from
// [a] = [ some array ...]
function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr: Expression): Type {
Debug.assert(expr.kind === SyntaxKind.ObjectLiteralExpression || expr.kind === SyntaxKind.ArrayLiteralExpression);
// If this is from "for of"
// for ( { a } of elems) {
// }
Expand Down Expand Up @@ -16596,6 +16597,18 @@ namespace ts {
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
}

// Gets the property symbol corresponding to the property in destructuring assignment
// 'property1' from
// for ( { property1: a } of elems) {
// }
// 'property1' at location 'a' from:
// [a] = [ property1, property2 ]
function getPropertySymbolOfDestructuringAssignment(location: Identifier) {
// Get the type of the object or array literal and then look for property of given name in the type
const typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>location.parent.parent);
return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text);
}

function getTypeOfExpression(expr: Expression): Type {
if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) {
expr = <Expression>expr.parent;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ namespace ts {
getSymbolsOfParameterPropertyDeclaration(parameter: ParameterDeclaration, parameterName: string): Symbol[];
getShorthandAssignmentValueSymbol(location: Node): Symbol;
getExportSpecifierLocalTargetSymbol(location: ExportSpecifier): Symbol;
getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expression: Expression): Type;
getPropertySymbolOfDestructuringAssignment(location: Identifier): Symbol;
getTypeAtLocation(node: Node): Type;
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;
Expand Down
10 changes: 2 additions & 8 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5651,14 +5651,8 @@ namespace ts {
}

function getPropertySymbolOfDestructuringAssignment(location: Node) {
if (isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent)) {
// Do work to determine if this is a property symbol corresponding to the search symbol
const typeOfObjectLiteral = typeChecker.getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>location.parent.parent);
if (typeOfObjectLiteral) {
return typeChecker.getPropertyOfType(typeOfObjectLiteral, (<Identifier>location).text);
}
}
return undefined;
return isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent) &&
typeChecker.getPropertySymbolOfDestructuringAssignment(<Identifier>location);
}

function isObjectBindingPatternElementWithoutPropertyName(symbol: Symbol) {
Expand Down

0 comments on commit c492fc6

Please sign in to comment.