Skip to content

Commit 958a6a4

Browse files
committed
Some restructuring according to PR feedback
1 parent edd0989 commit 958a6a4

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/services/services.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5624,8 +5624,14 @@ namespace ts {
56245624
};
56255625
}
56265626

5627-
function getImportOrExportSpecifierForPropertyNameSymbol(symbol: Symbol, location: Node): ImportOrExportSpecifier {
5627+
function getAliasSymbolForPropertyNameSymbol(symbol: Symbol, location: Node): Symbol {
56285628
if (symbol.flags & SymbolFlags.Alias) {
5629+
// Default import get alias
5630+
const defaultImport = getDeclarationOfKind(symbol, SyntaxKind.ImportClause);
5631+
if (defaultImport) {
5632+
return typeChecker.getAliasedSymbol(symbol);
5633+
}
5634+
56295635
const importOrExportSpecifier = <ImportOrExportSpecifier>forEach(symbol.declarations,
56305636
declaration => (declaration.kind === SyntaxKind.ImportSpecifier ||
56315637
declaration.kind === SyntaxKind.ExportSpecifier) ? declaration : undefined);
@@ -5634,7 +5640,22 @@ namespace ts {
56345640
(!importOrExportSpecifier.propertyName ||
56355641
// export {a as class } where a is location
56365642
importOrExportSpecifier.propertyName === location)) {
5637-
return importOrExportSpecifier;
5643+
// If Import specifier -> get alias
5644+
// else Export specifier -> get local target
5645+
return importOrExportSpecifier.kind === SyntaxKind.ImportSpecifier ?
5646+
typeChecker.getAliasedSymbol(symbol) :
5647+
typeChecker.getExportSpecifierLocalTargetSymbol(importOrExportSpecifier);
5648+
}
5649+
}
5650+
return undefined;
5651+
}
5652+
5653+
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);
56385659
}
56395660
}
56405661
return undefined;
@@ -6105,12 +6126,9 @@ namespace ts {
61056126
// If the location is name of property symbol from object literal destructuring pattern
61066127
// Search the property symbol
61076128
// for ( { property: p2 } of elems) { }
6108-
if (isNameOfPropertyAssignment(location) &&
6109-
location.parent.kind !== SyntaxKind.ShorthandPropertyAssignment &&
6110-
isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent)) {
6111-
const typeOfObjectLiteral = typeChecker.getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>location.parent.parent);
6112-
if (typeOfObjectLiteral) {
6113-
const propertySymbol = typeChecker.getPropertyOfType(typeOfObjectLiteral, (<Identifier>location).text);
6129+
if (isNameOfPropertyAssignment(location) && location.parent.kind !== SyntaxKind.ShorthandPropertyAssignment) {
6130+
const propertySymbol = getPropertySymbolOfDestructuringAssignment(location);
6131+
if (propertySymbol) {
61146132
result.push(propertySymbol);
61156133
}
61166134
}
@@ -6126,12 +6144,9 @@ namespace ts {
61266144
//// export {a as somethingElse}
61276145
//// We want the *local* declaration of 'a' as declared in the import,
61286146
//// *not* as declared within "mod" (or farther)
6129-
const importOrExportSpecifier = getImportOrExportSpecifierForPropertyNameSymbol(symbol, location);
6130-
if (importOrExportSpecifier || getDeclarationOfKind(symbol, SyntaxKind.ImportClause)) {
6131-
result = result.concat(populateSearchSymbolSet(
6132-
!importOrExportSpecifier || importOrExportSpecifier.kind === SyntaxKind.ImportSpecifier ?
6133-
typeChecker.getAliasedSymbol(symbol) :
6134-
typeChecker.getExportSpecifierLocalTargetSymbol(importOrExportSpecifier), location));
6147+
const aliasSymbol = getAliasSymbolForPropertyNameSymbol(symbol, location);
6148+
if (aliasSymbol) {
6149+
result = result.concat(populateSearchSymbolSet(aliasSymbol, location));
61356150
}
61366151

61376152
// If the location is in a context sensitive location (i.e. in an object literal) try
@@ -6257,15 +6272,11 @@ namespace ts {
62576272

62586273
// If the reference symbol is an alias, check if what it is aliasing is one of the search
62596274
// symbols but by looking up for related symbol of this alias so it can handle multiple level of indirectness.
6260-
const importOrExportSpecifier = getImportOrExportSpecifierForPropertyNameSymbol(referenceSymbol, referenceLocation);
6261-
if (importOrExportSpecifier || getDeclarationOfKind(referenceSymbol, SyntaxKind.ImportClause)) {
6262-
const aliasedSymbol = !importOrExportSpecifier || importOrExportSpecifier.kind === SyntaxKind.ImportSpecifier ?
6263-
typeChecker.getAliasedSymbol(referenceSymbol) :
6264-
typeChecker.getExportSpecifierLocalTargetSymbol(importOrExportSpecifier);
6265-
return getRelatedSymbol(searchSymbols, aliasedSymbol, referenceLocation);
6275+
const aliasSymbol = getAliasSymbolForPropertyNameSymbol(referenceSymbol, referenceLocation);
6276+
if (aliasSymbol) {
6277+
return getRelatedSymbol(searchSymbols, aliasSymbol, referenceLocation);
62666278
}
62676279

6268-
62696280
// If the reference location is in an object literal, try to get the contextual type for the
62706281
// object literal, lookup the property symbol in the contextual type, and use this symbol to
62716282
// compare to our searchSymbol
@@ -6282,15 +6293,9 @@ namespace ts {
62826293
// Get the property symbol from the object literal's type and look if thats the search symbol
62836294
// In below eg. get 'property' from type of elems iterating type
62846295
// for ( { property: p2 } of elems) { }
6285-
if (isArrayLiteralOrObjectLiteralDestructuringPattern(referenceLocation.parent.parent)) {
6286-
// Do work to determine if this is a property symbol corresponding to the search symbol
6287-
const typeOfObjectLiteral = typeChecker.getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>referenceLocation.parent.parent);
6288-
if (typeOfObjectLiteral) {
6289-
const propertySymbol = typeChecker.getPropertyOfType(typeOfObjectLiteral, (<Identifier>referenceLocation).text);
6290-
if (searchSymbols.indexOf(propertySymbol) >= 0) {
6291-
return propertySymbol;
6292-
}
6293-
}
6296+
const propertySymbol = getPropertySymbolOfDestructuringAssignment(referenceLocation);
6297+
if (propertySymbol && searchSymbols.indexOf(propertySymbol) >= 0) {
6298+
return propertySymbol;
62946299
}
62956300
}
62966301

0 commit comments

Comments
 (0)