Skip to content

Commit c492fc6

Browse files
committed
Update the entry point to return property symbol of destructuring assignment
1 parent 958a6a4 commit c492fc6

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace ts {
8383
getShorthandAssignmentValueSymbol,
8484
getExportSpecifierLocalTargetSymbol,
8585
getTypeAtLocation: getTypeOfNode,
86-
getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment,
86+
getPropertySymbolOfDestructuringAssignment,
8787
typeToString,
8888
getSymbolDisplayBuilder,
8989
symbolToString,
@@ -16568,6 +16568,7 @@ namespace ts {
1656816568
// [ a ] from
1656916569
// [a] = [ some array ...]
1657016570
function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr: Expression): Type {
16571+
Debug.assert(expr.kind === SyntaxKind.ObjectLiteralExpression || expr.kind === SyntaxKind.ArrayLiteralExpression);
1657116572
// If this is from "for of"
1657216573
// for ( { a } of elems) {
1657316574
// }
@@ -16596,6 +16597,18 @@ namespace ts {
1659616597
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
1659716598
}
1659816599

16600+
// Gets the property symbol corresponding to the property in destructuring assignment
16601+
// 'property1' from
16602+
// for ( { property1: a } of elems) {
16603+
// }
16604+
// 'property1' at location 'a' from:
16605+
// [a] = [ property1, property2 ]
16606+
function getPropertySymbolOfDestructuringAssignment(location: Identifier) {
16607+
// Get the type of the object or array literal and then look for property of given name in the type
16608+
const typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>location.parent.parent);
16609+
return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text);
16610+
}
16611+
1659916612
function getTypeOfExpression(expr: Expression): Type {
1660016613
if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) {
1660116614
expr = <Expression>expr.parent;

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ namespace ts {
17351735
getSymbolsOfParameterPropertyDeclaration(parameter: ParameterDeclaration, parameterName: string): Symbol[];
17361736
getShorthandAssignmentValueSymbol(location: Node): Symbol;
17371737
getExportSpecifierLocalTargetSymbol(location: ExportSpecifier): Symbol;
1738-
getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expression: Expression): Type;
1738+
getPropertySymbolOfDestructuringAssignment(location: Identifier): Symbol;
17391739
getTypeAtLocation(node: Node): Type;
17401740
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
17411741
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;

src/services/services.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,14 +5651,8 @@ namespace ts {
56515651
}
56525652

56535653
function getPropertySymbolOfDestructuringAssignment(location: Node) {
5654-
if (isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent)) {
5655-
// Do work to determine if this is a property symbol corresponding to the search symbol
5656-
const typeOfObjectLiteral = typeChecker.getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>location.parent.parent);
5657-
if (typeOfObjectLiteral) {
5658-
return typeChecker.getPropertyOfType(typeOfObjectLiteral, (<Identifier>location).text);
5659-
}
5660-
}
5661-
return undefined;
5654+
return isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent) &&
5655+
typeChecker.getPropertySymbolOfDestructuringAssignment(<Identifier>location);
56625656
}
56635657

56645658
function isObjectBindingPatternElementWithoutPropertyName(symbol: Symbol) {

0 commit comments

Comments
 (0)