@@ -1494,7 +1494,7 @@ namespace ts {
14941494 }
14951495 // try to verify results of module resolution
14961496 for ( const { oldFile : oldSourceFile , newFile : newSourceFile } of modifiedSourceFiles ) {
1497- const moduleNames = getModuleNames ( newSourceFile , options ) ;
1497+ const moduleNames = getModuleNames ( newSourceFile ) ;
14981498 const resolutions = resolveModuleNamesReusingOldState ( moduleNames , newSourceFile ) ;
14991499 // ensure that module resolution results are still correct
15001500 const resolutionsChanged = hasChangesInResolutions ( moduleNames , resolutions , oldSourceFile . resolvedModules , moduleResolutionIsEqualTo ) ;
@@ -2201,6 +2201,15 @@ namespace ts {
22012201 : b . kind === SyntaxKind . StringLiteral && a . text === b . text ;
22022202 }
22032203
2204+ function createSyntheticImport ( text : string , file : SourceFile ) {
2205+ const externalHelpersModuleReference = factory . createStringLiteral ( text ) ;
2206+ const importDecl = factory . createImportDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*importClause*/ undefined , externalHelpersModuleReference ) ;
2207+ addEmitFlags ( importDecl , EmitFlags . NeverApplyImportHelper ) ;
2208+ setParent ( externalHelpersModuleReference , importDecl ) ;
2209+ setParent ( importDecl , file ) ;
2210+ return externalHelpersModuleReference ;
2211+ }
2212+
22042213 function collectExternalModuleReferences ( file : SourceFile ) : void {
22052214 if ( file . imports ) {
22062215 return ;
@@ -2216,16 +2225,18 @@ namespace ts {
22162225
22172226 // If we are importing helpers, we need to add a synthetic reference to resolve the
22182227 // helpers library.
2219- if ( options . importHelpers
2220- && ( options . isolatedModules || isExternalModuleFile )
2228+ if ( ( options . isolatedModules || isExternalModuleFile )
22212229 && ! file . isDeclarationFile ) {
2222- // synthesize 'import "tslib"' declaration
2223- const externalHelpersModuleReference = factory . createStringLiteral ( externalHelpersModuleNameText ) ;
2224- const importDecl = factory . createImportDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*importClause*/ undefined , externalHelpersModuleReference ) ;
2225- addEmitFlags ( importDecl , EmitFlags . NeverApplyImportHelper ) ;
2226- setParent ( externalHelpersModuleReference , importDecl ) ;
2227- setParent ( importDecl , file ) ;
2228- imports = [ externalHelpersModuleReference ] ;
2230+ if ( options . importHelpers ) {
2231+ // synthesize 'import "tslib"' declaration
2232+ imports = [ createSyntheticImport ( externalHelpersModuleNameText , file ) ] ;
2233+ }
2234+ const jsxImport = getJSXRuntimeImport ( getJSXImplicitImportBase ( options , file ) , options ) ;
2235+ if ( jsxImport ) {
2236+ // synthesize `import "base/jsx-runtime"` declaration
2237+ imports ||= [ ] ;
2238+ imports . push ( createSyntheticImport ( jsxImport , file ) ) ;
2239+ }
22292240 }
22302241
22312242 for ( const node of file . statements ) {
@@ -2840,16 +2851,11 @@ namespace ts {
28402851 if ( resolvedTypeReferenceDirective . isExternalLibraryImport ) currentNodeModulesDepth -- ;
28412852 }
28422853 else {
2843- // Don't issue an error when auto-inclusion lookup fails for the jsxImportSource (at this point)
2844- // It may be provided by an ambient module in the compilation, instead.
2845- // A usage of a JSX tag later should report that the module couldn't be resolved if it is not supplied.
2846- if ( refFile || typeReferenceDirective !== getJSXRuntimeImport ( getJSXImplicitImportBase ( options ) , options ) ) {
2847- fileProcessingDiagnostics . add ( createRefFileDiagnostic (
2848- refFile ,
2849- Diagnostics . Cannot_find_type_definition_file_for_0 ,
2850- typeReferenceDirective
2851- ) ) ;
2852- }
2854+ fileProcessingDiagnostics . add ( createRefFileDiagnostic (
2855+ refFile ,
2856+ Diagnostics . Cannot_find_type_definition_file_for_0 ,
2857+ typeReferenceDirective
2858+ ) ) ;
28532859 }
28542860
28552861 if ( saveResolution ) {
@@ -2896,9 +2902,9 @@ namespace ts {
28962902
28972903 function processImportedModules ( file : SourceFile ) {
28982904 collectExternalModuleReferences ( file ) ;
2899- if ( file . imports . length || file . moduleAugmentations . length || getJSXImplicitImportBase ( options , file ) ) {
2905+ if ( file . imports . length || file . moduleAugmentations . length ) {
29002906 // Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
2901- const moduleNames = getModuleNames ( file , options ) ;
2907+ const moduleNames = getModuleNames ( file ) ;
29022908 const resolutions = resolveModuleNamesReusingOldState ( moduleNames , file ) ;
29032909 Debug . assert ( resolutions . length === moduleNames . length ) ;
29042910 for ( let i = 0 ; i < moduleNames . length ; i ++ ) {
@@ -3887,19 +3893,14 @@ namespace ts {
38873893 }
38883894 }
38893895
3890- function getModuleNames ( file : SourceFile , options : CompilerOptions ) : string [ ] {
3891- const { imports, moduleAugmentations } = file ;
3896+ function getModuleNames ( { imports, moduleAugmentations } : SourceFile ) : string [ ] {
38923897 const res = imports . map ( i => i . text ) ;
38933898 for ( const aug of moduleAugmentations ) {
38943899 if ( aug . kind === SyntaxKind . StringLiteral ) {
38953900 res . push ( aug . text ) ;
38963901 }
38973902 // Do nothing if it's an Identifier; we don't need to do module resolution for `declare global`.
38983903 }
3899- const jsxRuntimeImport = getJSXRuntimeImport ( getJSXImplicitImportBase ( options , file ) , options ) ;
3900- if ( jsxRuntimeImport ) {
3901- res . push ( jsxRuntimeImport ) ;
3902- }
39033904 return res ;
39043905 }
39053906}
0 commit comments