@@ -702,18 +702,18 @@ namespace ts.codefix {
702
702
}
703
703
}
704
704
705
- function getActionsForNonUMDImport ( context : CodeFixContext ) : CodeAction [ ] {
705
+ function getActionsForNonUMDImport ( context : CodeFixContext ) : CodeAction [ ] | undefined {
706
706
// This will always be an Identifier, since the diagnostics we fix only fail on identifiers.
707
707
const { sourceFile, span, program, cancellationToken } = context ;
708
708
const checker = program . getTypeChecker ( ) ;
709
709
const symbolToken = getTokenAtPosition ( sourceFile , span . start , /*includeJsDocComment*/ false ) ;
710
- const isJsxNamespace = isJsxOpeningLikeElement ( symbolToken . parent ) && symbolToken . parent . tagName === symbolToken ;
711
- if ( ! isJsxNamespace && ! isIdentifier ( symbolToken ) ) {
712
- return undefined ;
713
- }
714
- const symbolName = isJsxNamespace ? checker . getJsxNamespace ( ) : ( < Identifier > symbolToken ) . text ;
715
- const allSourceFiles = program . getSourceFiles ( ) ;
716
- const compilerOptions = program . getCompilerOptions ( ) ;
710
+ // If we're at `<Foo/>`, we must check if `Foo` is already in scope, and if so, get an import for `React` instead.
711
+ const symbolName = isJsxOpeningLikeElement ( symbolToken . parent )
712
+ && symbolToken . parent . tagName === symbolToken
713
+ && ( ! isIdentifier ( symbolToken ) || isIntrinsicJsxName ( symbolToken . text ) || checker . resolveName ( symbolToken . text , symbolToken , SymbolFlags . All , /*excludeGlobals*/ false ) )
714
+ ? checker . getJsxNamespace ( )
715
+ : isIdentifier ( symbolToken ) ? symbolToken . text : undefined ;
716
+ if ( ! symbolName ) return undefined ;
717
717
718
718
// "default" is a keyword and not a legal identifier for the import, so we don't expect it here
719
719
Debug . assert ( symbolName !== "default" ) ;
@@ -725,7 +725,7 @@ namespace ts.codefix {
725
725
function addSymbol ( moduleSymbol : Symbol , exportedSymbol : Symbol , importKind : ImportKind ) : void {
726
726
originalSymbolToExportInfos . add ( getUniqueSymbolId ( exportedSymbol , checker ) . toString ( ) , { moduleSymbol, importKind } ) ;
727
727
}
728
- forEachExternalModuleToImportFrom ( checker , sourceFile , allSourceFiles , moduleSymbol => {
728
+ forEachExternalModuleToImportFrom ( checker , sourceFile , program . getSourceFiles ( ) , moduleSymbol => {
729
729
cancellationToken . throwIfCancellationRequested ( ) ;
730
730
731
731
// check the default export
@@ -735,7 +735,7 @@ namespace ts.codefix {
735
735
if ( (
736
736
localSymbol && localSymbol . escapedName === symbolName ||
737
737
getEscapedNameForExportDefault ( defaultExport ) === symbolName ||
738
- moduleSymbolToValidIdentifier ( moduleSymbol , compilerOptions . target ) === symbolName
738
+ moduleSymbolToValidIdentifier ( moduleSymbol , program . getCompilerOptions ( ) . target ) === symbolName
739
739
) && checkSymbolHasMeaning ( localSymbol || defaultExport , currentTokenMeaning ) ) {
740
740
addSymbol ( moduleSymbol , localSymbol || defaultExport , ImportKind . Default ) ;
741
741
}
0 commit comments