@@ -460,6 +460,12 @@ namespace ts.refactor {
460
460
const oldImportsNeededByNewFile = new SymbolSet ( ) ;
461
461
const newFileImportsFromOldFile = new SymbolSet ( ) ;
462
462
463
+ const containsJsx = find ( toMove , statement => ! ! ( statement . transformFlags & TransformFlags . ContainsJsx ) ) ;
464
+ const jsxNamespaceSymbol = getJsxNamespaceSymbol ( containsJsx ) ;
465
+ if ( jsxNamespaceSymbol ) { // Might not exist (e.g. in non-compiling code)
466
+ oldImportsNeededByNewFile . add ( jsxNamespaceSymbol ) ;
467
+ }
468
+
463
469
for ( const statement of toMove ) {
464
470
forEachTopLevelDeclaration ( statement , decl => {
465
471
movedSymbols . add ( Debug . assertDefined ( isExpressionStatement ( decl ) ? checker . getSymbolAtLocation ( decl . expression . left ) : decl . symbol ) ) ;
@@ -485,13 +491,30 @@ namespace ts.refactor {
485
491
for ( const statement of oldFile . statements ) {
486
492
if ( contains ( toMove , statement ) ) continue ;
487
493
494
+ // jsxNamespaceSymbol will only be set iff it is in oldImportsNeededByNewFile.
495
+ if ( jsxNamespaceSymbol && ! ! ( statement . transformFlags & TransformFlags . ContainsJsx ) ) {
496
+ unusedImportsFromOldFile . delete ( jsxNamespaceSymbol ) ;
497
+ }
498
+
488
499
forEachReference ( statement , checker , symbol => {
489
500
if ( movedSymbols . has ( symbol ) ) oldFileImportsFromNewFile . add ( symbol ) ;
490
501
unusedImportsFromOldFile . delete ( symbol ) ;
491
502
} ) ;
492
503
}
493
504
494
505
return { movedSymbols, newFileImportsFromOldFile, oldFileImportsFromNewFile, oldImportsNeededByNewFile, unusedImportsFromOldFile } ;
506
+
507
+ function getJsxNamespaceSymbol ( containsJsx : Node | undefined ) {
508
+ if ( containsJsx === undefined ) {
509
+ return undefined ;
510
+ }
511
+
512
+ const jsxNamespace = checker . getJsxNamespace ( containsJsx ) ;
513
+ const jsxNamespaceSymbol = checker . resolveName ( jsxNamespace , containsJsx , SymbolFlags . Namespace , /*excludeGlobals*/ true ) ;
514
+ return ! ! jsxNamespaceSymbol && some ( jsxNamespaceSymbol . declarations , isInImport )
515
+ ? jsxNamespaceSymbol
516
+ : undefined ;
517
+ }
495
518
}
496
519
497
520
// Below should all be utilities
@@ -512,7 +535,7 @@ namespace ts.refactor {
512
535
}
513
536
function isVariableDeclarationInImport ( decl : VariableDeclaration ) {
514
537
return isSourceFile ( decl . parent . parent . parent ) &&
515
- decl . initializer && isRequireCall ( decl . initializer , /*checkArgumentIsStringLiteralLike*/ true ) ;
538
+ ! ! decl . initializer && isRequireCall ( decl . initializer , /*checkArgumentIsStringLiteralLike*/ true ) ;
516
539
}
517
540
518
541
function filterImport ( i : SupportedImport , moduleSpecifier : StringLiteralLike , keep : ( name : Identifier ) => boolean ) : SupportedImportStatement | undefined {
0 commit comments