@@ -15,6 +15,12 @@ import (
15
15
"github.com/microsoft/typescript-go/internal/tspath"
16
16
)
17
17
18
+ type LibFile struct {
19
+ Name string
20
+ path string
21
+ Replaced bool
22
+ }
23
+
18
24
type fileLoader struct {
19
25
opts ProgramOptions
20
26
resolver * module.Resolver
@@ -34,7 +40,7 @@ type fileLoader struct {
34
40
projectReferenceFileMapper * projectReferenceFileMapper
35
41
dtsDirectories collections.Set [tspath.Path ]
36
42
37
- pathForLibFileCache collections.SyncMap [string , string ]
43
+ pathForLibFileCache collections.SyncMap [string , * LibFile ]
38
44
pathForLibFileResolutions collections.SyncMap [tspath.Path , module.ModeAwareCache [* module.ResolvedModule ]]
39
45
}
40
46
@@ -49,7 +55,7 @@ type processedFiles struct {
49
55
sourceFileMetaDatas map [tspath.Path ]ast.SourceFileMetaData
50
56
jsxRuntimeImportSpecifiers map [tspath.Path ]* jsxRuntimeImportSpecifier
51
57
importHelpersImportSpecifiers map [tspath.Path ]* ast.Node
52
- libFiles collections. Set [tspath.Path ]
58
+ libFiles map [tspath.Path ]* LibFile
53
59
// List of present unsupported extensions
54
60
unsupportedExtensions []string
55
61
sourceFilesFoundSearchingNodeModules collections.Set [tspath.Path ]
@@ -89,24 +95,26 @@ func processAllProgramFiles(
89
95
loader .addProjectReferenceTasks (singleThreaded )
90
96
loader .resolver = module .NewResolver (loader .projectReferenceFileMapper .host , compilerOptions , opts .TypingsLocation , opts .ProjectName )
91
97
92
- var libs []string
98
+ for _ , file := range rootFiles {
99
+ loader .addRootTask (file , nil )
100
+ }
101
+
93
102
if len (rootFiles ) > 0 && compilerOptions .NoLib .IsFalseOrUnknown () {
94
103
if compilerOptions .Lib == nil {
95
104
name := tsoptions .GetDefaultLibFileName (compilerOptions )
96
- libs = append (libs , loader .pathForLibFile (name ))
105
+ libFile := loader .pathForLibFile (name )
106
+ loader .addRootTask (libFile .path , libFile )
97
107
} else {
98
108
for _ , lib := range compilerOptions .Lib {
99
109
if name , ok := tsoptions .GetLibFileName (lib ); ok {
100
- libs = append (libs , loader .pathForLibFile (name ))
110
+ libFile := loader .pathForLibFile (name )
111
+ loader .addRootTask (libFile .path , libFile )
101
112
}
102
113
// !!! error on unknown name
103
114
}
104
115
}
105
116
}
106
117
107
- loader .addRootTasks (rootFiles , false )
108
- loader .addRootTasks (libs , true )
109
-
110
118
if len (rootFiles ) > 0 {
111
119
loader .addAutomaticTypeDirectiveTasks ()
112
120
}
@@ -131,7 +139,7 @@ func processAllProgramFiles(
131
139
var importHelpersImportSpecifiers map [tspath.Path ]* ast.Node
132
140
var unsupportedExtensions []string
133
141
var sourceFilesFoundSearchingNodeModules collections.Set [tspath.Path ]
134
- var libFileSet collections. Set [tspath.Path ]
142
+ libFilesMap := make ( map [tspath.Path ]* LibFile , libFileCount )
135
143
fileLoadDiagnostics := & ast.DiagnosticsCollection {}
136
144
137
145
loader .filesParser .collect (& loader , loader .rootTasks , func (task * parseTask ) {
@@ -149,9 +157,9 @@ func processAllProgramFiles(
149
157
missingFiles = append (missingFiles , task .normalizedFilePath )
150
158
return
151
159
}
152
- if task .isLib {
160
+ if task .libFile != nil {
153
161
libFiles = append (libFiles , file )
154
- libFileSet . Add ( path )
162
+ libFilesMap [ path ] = task . libFile
155
163
} else {
156
164
files = append (files , file )
157
165
}
@@ -222,7 +230,7 @@ func processAllProgramFiles(
222
230
importHelpersImportSpecifiers : importHelpersImportSpecifiers ,
223
231
unsupportedExtensions : unsupportedExtensions ,
224
232
sourceFilesFoundSearchingNodeModules : sourceFilesFoundSearchingNodeModules ,
225
- libFiles : libFileSet ,
233
+ libFiles : libFilesMap ,
226
234
fileLoadDiagnostics : fileLoadDiagnostics ,
227
235
}
228
236
}
@@ -231,12 +239,10 @@ func (p *fileLoader) toPath(file string) tspath.Path {
231
239
return tspath .ToPath (file , p .opts .Host .GetCurrentDirectory (), p .opts .Host .FS ().UseCaseSensitiveFileNames ())
232
240
}
233
241
234
- func (p * fileLoader ) addRootTasks (files []string , isLib bool ) {
235
- for _ , fileName := range files {
236
- absPath := tspath .GetNormalizedAbsolutePath (fileName , p .opts .Host .GetCurrentDirectory ())
237
- if core .Tristate .IsTrue (p .opts .Config .CompilerOptions ().AllowNonTsExtensions ) || slices .Contains (p .supportedExtensions , tspath .TryGetExtensionFromPath (absPath )) {
238
- p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : absPath , isLib : isLib , root : true })
239
- }
242
+ func (p * fileLoader ) addRootTask (fileName string , libFile * LibFile ) {
243
+ absPath := tspath .GetNormalizedAbsolutePath (fileName , p .opts .Host .GetCurrentDirectory ())
244
+ if core .Tristate .IsTrue (p .opts .Config .CompilerOptions ().AllowNonTsExtensions ) || slices .Contains (p .supportedExtensions , tspath .TryGetExtensionFromPath (absPath )) {
245
+ p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : absPath , libFile : libFile , root : true })
240
246
}
241
247
}
242
248
@@ -249,7 +255,7 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
249
255
containingDirectory = p .opts .Host .GetCurrentDirectory ()
250
256
}
251
257
containingFileName := tspath .CombinePaths (containingDirectory , module .InferredTypesContainingFile )
252
- p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : containingFileName , isLib : false , isForAutomaticTypeDirective : true })
258
+ p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : containingFileName , isForAutomaticTypeDirective : true })
253
259
}
254
260
255
261
func (p * fileLoader ) resolveAutomaticTypeDirectives (containingFileName string ) (
@@ -304,12 +310,12 @@ func (p *fileLoader) addProjectReferenceTasks(singleThreaded bool) {
304
310
}
305
311
if p .opts .canUseProjectReferenceSource () {
306
312
for _ , fileName := range resolved .FileNames () {
307
- p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : fileName , isLib : false })
313
+ p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : fileName })
308
314
}
309
315
} else {
310
316
for outputDts := range resolved .GetOutputDeclarationFileNames () {
311
317
if outputDts != "" {
312
- p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : outputDts , isLib : false })
318
+ p .rootTasks = append (p .rootTasks , & parseTask {normalizedFilePath : outputDts })
313
319
}
314
320
}
315
321
}
@@ -405,7 +411,7 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
405
411
increaseDepth : resolved .IsExternalLibraryImport ,
406
412
elideOnDepth : false ,
407
413
isFromExternalLibrary : resolved .IsExternalLibraryImport ,
408
- }, false )
414
+ }, nil )
409
415
}
410
416
}
411
417
@@ -496,7 +502,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
496
502
increaseDepth : resolvedModule .IsExternalLibraryImport ,
497
503
elideOnDepth : isJsFileFromNodeModules ,
498
504
isFromExternalLibrary : resolvedModule .IsExternalLibraryImport ,
499
- }, false )
505
+ }, nil )
500
506
}
501
507
}
502
508
@@ -517,26 +523,28 @@ func (p *fileLoader) createSyntheticImport(text string, file *ast.SourceFile) *a
517
523
return externalHelpersModuleReference
518
524
}
519
525
520
- func (p * fileLoader ) pathForLibFile (name string ) string {
526
+ func (p * fileLoader ) pathForLibFile (name string ) * LibFile {
521
527
if cached , ok := p .pathForLibFileCache .Load (name ); ok {
522
528
return cached
523
529
}
524
530
525
531
path := tspath .CombinePaths (p .defaultLibraryPath , name )
532
+ replaced := false
526
533
if p .opts .Config .CompilerOptions ().LibReplacement .IsTrue () {
527
534
libraryName := getLibraryNameFromLibFileName (name )
528
535
resolveFrom := getInferredLibraryNameResolveFrom (p .opts .Config .CompilerOptions (), p .opts .Host .GetCurrentDirectory (), name )
529
536
resolution := p .resolver .ResolveModuleName (libraryName , resolveFrom , core .ModuleKindCommonJS , nil )
530
537
if resolution .IsResolved () {
531
538
path = resolution .ResolvedFileName
539
+ replaced = true
532
540
p .pathForLibFileResolutions .LoadOrStore (p .toPath (resolveFrom ), module.ModeAwareCache [* module.ResolvedModule ]{
533
541
module.ModeAwareCacheKey {Name : libraryName , Mode : core .ModuleKindCommonJS }: resolution ,
534
542
})
535
543
}
536
544
}
537
545
538
- path , _ = p .pathForLibFileCache .LoadOrStore (name , path )
539
- return path
546
+ libPath , _ : = p .pathForLibFileCache .LoadOrStore (name , & LibFile { name , path , replaced } )
547
+ return libPath
540
548
}
541
549
542
550
func getLibraryNameFromLibFileName (libFileName string ) string {
0 commit comments