@@ -32,7 +32,8 @@ namespace ts.FindAllReferences {
32
32
33
33
interface AmbientModuleDeclaration extends ModuleDeclaration { body ?: ModuleBlock ; }
34
34
type SourceFileLike = SourceFile | AmbientModuleDeclaration ;
35
- type Importer = AnyImportSyntax | ExportDeclaration ;
35
+ // Identifier for the case of `const x = require("y")`.
36
+ type Importer = AnyImportSyntax | Identifier | ExportDeclaration ;
36
37
type ImporterOrCallExpression = Importer | CallExpression ;
37
38
38
39
/** Returns import statements that directly reference the exporting module, and a list of files that may access the module through a namespace. */
@@ -55,7 +56,7 @@ namespace ts.FindAllReferences {
55
56
56
57
// Module augmentations may use this module's exports without importing it.
57
58
for ( const decl of exportingModuleSymbol . declarations ) {
58
- if ( ts . isExternalModuleAugmentation ( decl ) ) {
59
+ if ( isExternalModuleAugmentation ( decl ) ) {
59
60
addIndirectUser ( decl as SourceFileLike ) ;
60
61
}
61
62
}
@@ -74,6 +75,15 @@ namespace ts.FindAllReferences {
74
75
switch ( direct . kind ) {
75
76
case SyntaxKind . CallExpression :
76
77
if ( ! isAvailableThroughGlobal ) {
78
+ const parent = direct . parent ! ;
79
+ if ( exportKind === ExportKind . ExportEquals && parent . kind === SyntaxKind . VariableDeclaration ) {
80
+ const { name } = parent as ts . VariableDeclaration ;
81
+ if ( name . kind === SyntaxKind . Identifier ) {
82
+ directImports . push ( name ) ;
83
+ break ;
84
+ }
85
+ }
86
+
77
87
// Don't support re-exporting 'require()' calls, so just add a single indirect user.
78
88
addIndirectUser ( direct . getSourceFile ( ) ) ;
79
89
}
@@ -179,6 +189,11 @@ namespace ts.FindAllReferences {
179
189
return ;
180
190
}
181
191
192
+ if ( decl . kind === ts . SyntaxKind . Identifier ) {
193
+ handleNamespaceImportLike ( decl ) ;
194
+ return ;
195
+ }
196
+
182
197
// Ignore if there's a grammar error
183
198
if ( decl . moduleSpecifier . kind !== SyntaxKind . StringLiteral ) {
184
199
return ;
@@ -192,7 +207,7 @@ namespace ts.FindAllReferences {
192
207
const { importClause } = decl ;
193
208
194
209
const { namedBindings } = importClause ;
195
- if ( namedBindings && namedBindings . kind === ts . SyntaxKind . NamespaceImport ) {
210
+ if ( namedBindings && namedBindings . kind === SyntaxKind . NamespaceImport ) {
196
211
handleNamespaceImportLike ( namedBindings . name ) ;
197
212
return ;
198
213
}
@@ -333,13 +348,13 @@ namespace ts.FindAllReferences {
333
348
334
349
if ( sourceFile . flags & NodeFlags . JavaScriptFile ) {
335
350
// Find all 'require()' calls.
336
- recur ( sourceFile ) ;
337
- function recur ( node : Node ) : void {
351
+ sourceFile . forEachChild ( function recur ( node : Node ) : void {
338
352
if ( isRequireCall ( node , /*checkArgumentIsStringLiteral*/ true ) ) {
339
353
action ( node , node . arguments [ 0 ] as StringLiteral ) ;
354
+ } else {
355
+ node . forEachChild ( recur ) ;
340
356
}
341
- forEachChild ( node , recur ) ;
342
- }
357
+ } ) ;
343
358
}
344
359
}
345
360
}
@@ -379,18 +394,9 @@ namespace ts.FindAllReferences {
379
394
if ( parent . kind === SyntaxKind . PropertyAccessExpression ) {
380
395
// When accessing an export of a JS module, there's no alias. The symbol will still be flagged as an export even though we're at the use.
381
396
// So check that we are at the declaration.
382
- if ( ! symbol . declarations . some ( d => d === parent ) ) {
383
- return undefined ;
384
- }
385
-
386
- switch ( getSpecialPropertyAssignmentKind ( parent . parent ) ) {
387
- case SpecialPropertyAssignmentKind . ExportsProperty :
388
- return exportInfo ( symbol , ExportKind . Named ) ;
389
- case SpecialPropertyAssignmentKind . ModuleExports :
390
- return exportInfo ( symbol , ExportKind . ExportEquals ) ;
391
- default :
392
- return undefined ;
393
- }
397
+ return symbol . declarations . some ( d => d === parent ) && parent . parent . kind === ts . SyntaxKind . BinaryExpression
398
+ ? getSpecialPropertyExport ( parent . parent as ts . BinaryExpression , /*useLhsSymbol*/ false )
399
+ : undefined ;
394
400
}
395
401
else {
396
402
const { exportSymbol } = symbol ;
@@ -420,6 +426,29 @@ namespace ts.FindAllReferences {
420
426
Debug . assert ( ! ! exportingModuleSymbol ) ;
421
427
return { kind : ImportExport . Export , symbol, exportInfo : { exportingModuleSymbol, exportKind : ExportKind . ExportEquals } } ;
422
428
}
429
+ else if ( parent . kind === ts . SyntaxKind . BinaryExpression ) {
430
+ return getSpecialPropertyExport ( parent as ts . BinaryExpression , /*useLhsSymbol*/ true ) ;
431
+ }
432
+ else if ( parent . parent . kind === SyntaxKind . BinaryExpression ) {
433
+ return getSpecialPropertyExport ( parent . parent as ts . BinaryExpression , /*useLhsSymbol*/ true ) ;
434
+ }
435
+ }
436
+
437
+ function getSpecialPropertyExport ( node : ts . BinaryExpression , useLhsSymbol : boolean ) : ExportedSymbol | undefined {
438
+ let kind : ExportKind ;
439
+ switch ( getSpecialPropertyAssignmentKind ( node ) ) {
440
+ case SpecialPropertyAssignmentKind . ExportsProperty :
441
+ kind = ExportKind . Named ;
442
+ break ;
443
+ case SpecialPropertyAssignmentKind . ModuleExports :
444
+ kind = ExportKind . ExportEquals ;
445
+ break ;
446
+ default :
447
+ return undefined ;
448
+ }
449
+
450
+ const sym = useLhsSymbol ? checker . getSymbolAtLocation ( ( node . left as ts . PropertyAccessExpression ) . name ) : symbol ;
451
+ return sym && exportInfo ( sym , kind ) ;
423
452
}
424
453
}
425
454
0 commit comments