@@ -180,7 +180,6 @@ namespace ts.FindAllReferences {
180
180
* But re-exports will be placed in 'singleReferences' since they cannot be locally referenced.
181
181
*/
182
182
function getSearchesFromDirectImports ( directImports : Importer [ ] , exportSymbol : Symbol , exportKind : ExportKind , checker : TypeChecker , isForRename : boolean ) : Pick < ImportsResult , "importSearches" | "singleReferences" > {
183
- const exportName = exportSymbol . escapedName ;
184
183
const importSearches : Array < [ Identifier , Symbol ] > = [ ] ;
185
184
const singleReferences : Identifier [ ] = [ ] ;
186
185
function addSearch ( location : Identifier , symbol : Symbol ) : void {
@@ -218,12 +217,11 @@ namespace ts.FindAllReferences {
218
217
return ;
219
218
}
220
219
221
- if ( ! decl . importClause ) {
220
+ const { importClause } = decl ;
221
+ if ( ! importClause ) {
222
222
return ;
223
223
}
224
224
225
- const { importClause } = decl ;
226
-
227
225
const { namedBindings } = importClause ;
228
226
if ( namedBindings && namedBindings . kind === SyntaxKind . NamespaceImport ) {
229
227
handleNamespaceImportLike ( namedBindings . name ) ;
@@ -245,7 +243,6 @@ namespace ts.FindAllReferences {
245
243
246
244
// 'default' might be accessed as a named import `{ default as foo }`.
247
245
if ( ! isForRename && exportKind === ExportKind . Default ) {
248
- Debug . assert ( exportName === "default" ) ;
249
246
searchForNamedImport ( namedBindings as NamedImports | undefined ) ;
250
247
}
251
248
}
@@ -258,36 +255,43 @@ namespace ts.FindAllReferences {
258
255
*/
259
256
function handleNamespaceImportLike ( importName : Identifier ) : void {
260
257
// Don't rename an import that already has a different name than the export.
261
- if ( exportKind === ExportKind . ExportEquals && ( ! isForRename || importName . escapedText === exportName ) ) {
258
+ if ( exportKind === ExportKind . ExportEquals && ( ! isForRename || isNameMatch ( importName . escapedText ) ) ) {
262
259
addSearch ( importName , checker . getSymbolAtLocation ( importName ) ) ;
263
260
}
264
261
}
265
262
266
263
function searchForNamedImport ( namedBindings : NamedImportsOrExports | undefined ) : void {
267
- if ( namedBindings ) {
268
- for ( const element of namedBindings . elements ) {
269
- const { name, propertyName } = element ;
270
- if ( ( propertyName || name ) . escapedText !== exportName ) {
271
- continue ;
272
- }
264
+ if ( ! namedBindings ) {
265
+ return ;
266
+ }
273
267
274
- if ( propertyName ) {
275
- // This is `import { foo as bar } from "./a"` or `export { foo as bar } from "./a"`. `foo` isn't a local in the file, so just add it as a single reference.
276
- singleReferences . push ( propertyName ) ;
277
- if ( ! isForRename ) { // If renaming `foo`, don't touch `bar`, just `foo`.
278
- // Search locally for `bar`.
279
- addSearch ( name , checker . getSymbolAtLocation ( name ) ) ;
280
- }
281
- }
282
- else {
283
- const localSymbol = element . kind === SyntaxKind . ExportSpecifier && element . propertyName
284
- ? checker . getExportSpecifierLocalTargetSymbol ( element ) // For re-exporting under a different name, we want to get the re-exported symbol.
285
- : checker . getSymbolAtLocation ( name ) ;
286
- addSearch ( name , localSymbol ) ;
268
+ for ( const element of namedBindings . elements ) {
269
+ const { name, propertyName } = element ;
270
+ if ( ! isNameMatch ( ( propertyName || name ) . escapedText ) ) {
271
+ continue ;
272
+ }
273
+
274
+ if ( propertyName ) {
275
+ // This is `import { foo as bar } from "./a"` or `export { foo as bar } from "./a"`. `foo` isn't a local in the file, so just add it as a single reference.
276
+ singleReferences . push ( propertyName ) ;
277
+ if ( ! isForRename ) { // If renaming `foo`, don't touch `bar`, just `foo`.
278
+ // Search locally for `bar`.
279
+ addSearch ( name , checker . getSymbolAtLocation ( name ) ) ;
287
280
}
288
281
}
282
+ else {
283
+ const localSymbol = element . kind === SyntaxKind . ExportSpecifier && element . propertyName
284
+ ? checker . getExportSpecifierLocalTargetSymbol ( element ) // For re-exporting under a different name, we want to get the re-exported symbol.
285
+ : checker . getSymbolAtLocation ( name ) ;
286
+ addSearch ( name , localSymbol ) ;
287
+ }
289
288
}
290
289
}
290
+
291
+ function isNameMatch ( name : __String ) : boolean {
292
+ // Use name of "default" even in `export =` case because we may have allowSyntheticDefaultImports
293
+ return name === exportSymbol . escapedName || exportKind !== ExportKind . Named && name === "default" ;
294
+ }
291
295
}
292
296
293
297
/** Returns 'true' is the namespace 'name' is re-exported from this module, and 'false' if it is only used locally. */
@@ -413,7 +417,7 @@ namespace ts.FindAllReferences {
413
417
case SyntaxKind . ExternalModuleReference :
414
418
return ( decl as ExternalModuleReference ) . parent ;
415
419
default :
416
- Debug . fail ( ` Unexpected module specifier parent: ${ decl . kind } ` ) ;
420
+ Debug . fail ( " Unexpected module specifier parent: " + decl . kind ) ;
417
421
}
418
422
}
419
423
@@ -468,11 +472,11 @@ namespace ts.FindAllReferences {
468
472
return exportInfo ( symbol , getExportKindForDeclaration ( exportNode ) ) ;
469
473
}
470
474
}
471
- // If we are in `export = a;`, `parent` is the export assignment.
475
+ // If we are in `export = a;` or `export default a;` , `parent` is the export assignment.
472
476
else if ( isExportAssignment ( parent ) ) {
473
477
return getExportAssignmentExport ( parent ) ;
474
478
}
475
- // If we are in `export = class A {};` at `A`, `parent.parent` is the export assignment.
479
+ // If we are in `export = class A {};` (or `export = class A {};`) at `A`, `parent.parent` is the export assignment.
476
480
else if ( isExportAssignment ( parent . parent ) ) {
477
481
return getExportAssignmentExport ( parent . parent ) ;
478
482
}
@@ -489,7 +493,8 @@ namespace ts.FindAllReferences {
489
493
// Get the symbol for the `export =` node; its parent is the module it's the export of.
490
494
const exportingModuleSymbol = ex . symbol . parent ;
491
495
Debug . assert ( ! ! exportingModuleSymbol ) ;
492
- return { kind : ImportExport . Export , symbol, exportInfo : { exportingModuleSymbol, exportKind : ExportKind . ExportEquals } } ;
496
+ const exportKind = ex . isExportEquals ? ExportKind . ExportEquals : ExportKind . Default ;
497
+ return { kind : ImportExport . Export , symbol, exportInfo : { exportingModuleSymbol, exportKind } } ;
493
498
}
494
499
495
500
function getSpecialPropertyExport ( node : ts . BinaryExpression , useLhsSymbol : boolean ) : ExportedSymbol | undefined {
@@ -525,7 +530,11 @@ namespace ts.FindAllReferences {
525
530
importedSymbol = getExportEqualsLocalSymbol ( importedSymbol , checker ) ;
526
531
}
527
532
528
- if ( symbolName ( importedSymbol ) === symbol . escapedName ) { // If this is a rename import, do not continue searching.
533
+ // If the import has a different name than the export, do not continue searching.
534
+ // If `importedName` is undefined, do continue searching as the export is anonymous.
535
+ // (All imports returned from this function will be ignored anyway if we are in rename and this is a not a named export.)
536
+ const importedName = symbolName ( importedSymbol ) ;
537
+ if ( importedName === undefined || importedName === "default" || importedName === symbol . escapedName ) {
529
538
return { kind : ImportExport . Import , symbol : importedSymbol , ...isImport } ;
530
539
}
531
540
}
0 commit comments