@@ -15,42 +15,42 @@ namespace ts {
15
15
/**
16
16
* Information of the file eg. its version, signature etc
17
17
*/
18
- fileInfos : ReadonlyMap < BuilderState . FileInfo > ;
18
+ fileInfos : ReadonlyMap < Path , BuilderState . FileInfo > ;
19
19
/**
20
20
* Contains the map of ReferencedSet=Referenced files of the file if module emit is enabled
21
21
* Otherwise undefined
22
22
* Thus non undefined value indicates, module emit
23
23
*/
24
- readonly referencedMap ?: ReadonlyMap < BuilderState . ReferencedSet > | undefined ;
24
+ readonly referencedMap ?: ReadonlyMap < Path , BuilderState . ReferencedSet > | undefined ;
25
25
/**
26
26
* Contains the map of exported modules ReferencedSet=exported module files from the file if module emit is enabled
27
27
* Otherwise undefined
28
28
*/
29
- readonly exportedModulesMap ?: ReadonlyMap < BuilderState . ReferencedSet > | undefined ;
29
+ readonly exportedModulesMap ?: ReadonlyMap < Path , BuilderState . ReferencedSet > | undefined ;
30
30
}
31
31
32
32
export interface BuilderState {
33
33
/**
34
34
* Information of the file eg. its version, signature etc
35
35
*/
36
- fileInfos : Map < BuilderState . FileInfo > ;
36
+ fileInfos : Map < Path , BuilderState . FileInfo > ;
37
37
/**
38
38
* Contains the map of ReferencedSet=Referenced files of the file if module emit is enabled
39
39
* Otherwise undefined
40
40
* Thus non undefined value indicates, module emit
41
41
*/
42
- readonly referencedMap : ReadonlyMap < BuilderState . ReferencedSet > | undefined ;
42
+ readonly referencedMap : ReadonlyMap < Path , BuilderState . ReferencedSet > | undefined ;
43
43
/**
44
44
* Contains the map of exported modules ReferencedSet=exported module files from the file if module emit is enabled
45
45
* Otherwise undefined
46
46
*/
47
- readonly exportedModulesMap : Map < BuilderState . ReferencedSet > | undefined ;
47
+ readonly exportedModulesMap : Map < Path , BuilderState . ReferencedSet > | undefined ;
48
48
/**
49
49
* Map of files that have already called update signature.
50
50
* That means hence forth these files are assumed to have
51
51
* no change in their signature for this version of the program
52
52
*/
53
- hasCalledUpdateShapeSignature : Map < true > ;
53
+ hasCalledUpdateShapeSignature : Set < Path > ;
54
54
/**
55
55
* Cache of all files excluding default library file for the current program
56
56
*/
@@ -73,7 +73,7 @@ namespace ts {
73
73
/**
74
74
* Referenced files with values for the keys as referenced file's path to be true
75
75
*/
76
- export type ReferencedSet = ReadonlyMap < true > ;
76
+ export type ReferencedSet = ReadonlySet < Path > ;
77
77
/**
78
78
* Compute the hash to store the shape of the file
79
79
*/
@@ -83,7 +83,7 @@ namespace ts {
83
83
* Exported modules to from declaration emit being computed.
84
84
* This can contain false in the affected file path to specify that there are no exported module(types from other modules) for this file
85
85
*/
86
- export type ComputingExportedModulesMap = Map < ReferencedSet | false > ;
86
+ export type ComputingExportedModulesMap = Map < Path , ReferencedSet | false > ;
87
87
88
88
/**
89
89
* Get the referencedFile from the imported module symbol
@@ -113,8 +113,8 @@ namespace ts {
113
113
/**
114
114
* Gets the referenced files for a file from the program with values for the keys as referenced file's path to be true
115
115
*/
116
- function getReferencedFiles ( program : Program , sourceFile : SourceFile , getCanonicalFileName : GetCanonicalFileName ) : Map < true > | undefined {
117
- let referencedFiles : Map < true > | undefined ;
116
+ function getReferencedFiles ( program : Program , sourceFile : SourceFile , getCanonicalFileName : GetCanonicalFileName ) : Set < Path > | undefined {
117
+ let referencedFiles : Set < Path > | undefined ;
118
118
119
119
// We need to use a set here since the code can contain the same import twice,
120
120
// but that will only be one dependency.
@@ -185,28 +185,25 @@ namespace ts {
185
185
}
186
186
187
187
function addReferencedFile ( referencedPath : Path ) {
188
- if ( ! referencedFiles ) {
189
- referencedFiles = createMap < true > ( ) ;
190
- }
191
- referencedFiles . set ( referencedPath , true ) ;
188
+ ( referencedFiles || ( referencedFiles = new Set ( ) ) ) . add ( referencedPath ) ;
192
189
}
193
190
}
194
191
195
192
/**
196
193
* Returns true if oldState is reusable, that is the emitKind = module/non module has not changed
197
194
*/
198
- export function canReuseOldState ( newReferencedMap : ReadonlyMap < ReferencedSet > | undefined , oldState : Readonly < ReusableBuilderState > | undefined ) {
195
+ export function canReuseOldState ( newReferencedMap : ReadonlyMap < Path , ReferencedSet > | undefined , oldState : Readonly < ReusableBuilderState > | undefined ) {
199
196
return oldState && ! oldState . referencedMap === ! newReferencedMap ;
200
197
}
201
198
202
199
/**
203
200
* Creates the state of file references and signature for the new program from oldState if it is safe
204
201
*/
205
202
export function create ( newProgram : Program , getCanonicalFileName : GetCanonicalFileName , oldState ?: Readonly < ReusableBuilderState > ) : BuilderState {
206
- const fileInfos = createMap < FileInfo > ( ) ;
207
- const referencedMap = newProgram . getCompilerOptions ( ) . module !== ModuleKind . None ? createMap < ReferencedSet > ( ) : undefined ;
208
- const exportedModulesMap = referencedMap ? createMap < ReferencedSet > ( ) : undefined ;
209
- const hasCalledUpdateShapeSignature = createMap < true > ( ) ;
203
+ const fileInfos = new Map < Path , FileInfo > ( ) ;
204
+ const referencedMap = newProgram . getCompilerOptions ( ) . module !== ModuleKind . None ? new Map < Path , ReferencedSet > ( ) : undefined ;
205
+ const exportedModulesMap = referencedMap ? new Map < Path , ReferencedSet > ( ) : undefined ;
206
+ const hasCalledUpdateShapeSignature = new Set < Path > ( ) ;
210
207
const useOldState = canReuseOldState ( referencedMap , oldState ) ;
211
208
212
209
// Create the reference map, and set the file infos
@@ -249,28 +246,24 @@ namespace ts {
249
246
* Creates a clone of the state
250
247
*/
251
248
export function clone ( state : Readonly < BuilderState > ) : BuilderState {
252
- const fileInfos = createMap < FileInfo > ( ) ;
253
- state . fileInfos . forEach ( ( value , key ) => {
254
- fileInfos . set ( key , { ...value } ) ;
255
- } ) ;
256
249
// Dont need to backup allFiles info since its cache anyway
257
250
return {
258
- fileInfos,
259
- referencedMap : cloneMapOrUndefined ( state . referencedMap ) ,
260
- exportedModulesMap : cloneMapOrUndefined ( state . exportedModulesMap ) ,
261
- hasCalledUpdateShapeSignature : cloneMap ( state . hasCalledUpdateShapeSignature ) ,
251
+ fileInfos : new Map ( state . fileInfos ) ,
252
+ referencedMap : state . referencedMap && new Map ( state . referencedMap ) ,
253
+ exportedModulesMap : state . exportedModulesMap && new Map ( state . exportedModulesMap ) ,
254
+ hasCalledUpdateShapeSignature : new Set ( state . hasCalledUpdateShapeSignature ) ,
262
255
} ;
263
256
}
264
257
265
258
/**
266
259
* Gets the files affected by the path from the program
267
260
*/
268
- export function getFilesAffectedBy ( state : BuilderState , programOfThisState : Program , path : Path , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash , cacheToUpdateSignature ?: Map < string > , exportedModulesMapCache ?: ComputingExportedModulesMap ) : readonly SourceFile [ ] {
261
+ export function getFilesAffectedBy ( state : BuilderState , programOfThisState : Program , path : Path , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash , cacheToUpdateSignature ?: Map < Path , string > , exportedModulesMapCache ?: ComputingExportedModulesMap ) : readonly SourceFile [ ] {
269
262
// Since the operation could be cancelled, the signatures are always stored in the cache
270
263
// They will be committed once it is safe to use them
271
264
// eg when calling this api from tsserver, if there is no cancellation of the operation
272
265
// In the other cases the affected files signatures are committed only after the iteration through the result is complete
273
- const signatureCache = cacheToUpdateSignature || createMap ( ) ;
266
+ const signatureCache = cacheToUpdateSignature || new Map ( ) ;
274
267
const sourceFile = programOfThisState . getSourceFileByPath ( path ) ;
275
268
if ( ! sourceFile ) {
276
269
return emptyArray ;
@@ -292,19 +285,19 @@ namespace ts {
292
285
* Updates the signatures from the cache into state's fileinfo signatures
293
286
* This should be called whenever it is safe to commit the state of the builder
294
287
*/
295
- export function updateSignaturesFromCache ( state : BuilderState , signatureCache : Map < string > ) {
296
- signatureCache . forEach ( ( signature , path ) => updateSignatureOfFile ( state , signature , path as Path ) ) ;
288
+ export function updateSignaturesFromCache ( state : BuilderState , signatureCache : Map < Path , string > ) {
289
+ signatureCache . forEach ( ( signature , path ) => updateSignatureOfFile ( state , signature , path ) ) ;
297
290
}
298
291
299
292
export function updateSignatureOfFile ( state : BuilderState , signature : string | undefined , path : Path ) {
300
293
state . fileInfos . get ( path ) ! . signature = signature ;
301
- state . hasCalledUpdateShapeSignature . set ( path , true ) ;
294
+ state . hasCalledUpdateShapeSignature . add ( path ) ;
302
295
}
303
296
304
297
/**
305
298
* Returns if the shape of the signature has changed since last emit
306
299
*/
307
- export function updateShapeSignature ( state : Readonly < BuilderState > , programOfThisState : Program , sourceFile : SourceFile , cacheToUpdateSignature : Map < string > , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash , exportedModulesMapCache ?: ComputingExportedModulesMap ) {
300
+ export function updateShapeSignature ( state : Readonly < BuilderState > , programOfThisState : Program , sourceFile : SourceFile , cacheToUpdateSignature : Map < Path , string > , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash , exportedModulesMapCache ?: ComputingExportedModulesMap ) {
308
301
Debug . assert ( ! ! sourceFile ) ;
309
302
Debug . assert ( ! exportedModulesMapCache || ! ! state . exportedModulesMap , "Compute visible to outside map only if visibleToOutsideReferencedMap present in the state" ) ;
310
303
@@ -365,16 +358,16 @@ namespace ts {
365
358
return ;
366
359
}
367
360
368
- let exportedModules : Map < true > | undefined ;
361
+ let exportedModules : Set < Path > | undefined ;
369
362
exportedModulesFromDeclarationEmit . forEach ( symbol => addExportedModule ( getReferencedFileFromImportedModuleSymbol ( symbol ) ) ) ;
370
363
exportedModulesMapCache . set ( sourceFile . resolvedPath , exportedModules || false ) ;
371
364
372
365
function addExportedModule ( exportedModulePath : Path | undefined ) {
373
366
if ( exportedModulePath ) {
374
367
if ( ! exportedModules ) {
375
- exportedModules = createMap < true > ( ) ;
368
+ exportedModules = new Set ( ) ;
376
369
}
377
- exportedModules . set ( exportedModulePath , true ) ;
370
+ exportedModules . add ( exportedModulePath ) ;
378
371
}
379
372
}
380
373
}
@@ -413,26 +406,23 @@ namespace ts {
413
406
}
414
407
415
408
// Get the references, traversing deep from the referenceMap
416
- const seenMap = createMap < true > ( ) ;
409
+ const seenMap = new Set < Path > ( ) ;
417
410
const queue = [ sourceFile . resolvedPath ] ;
418
411
while ( queue . length ) {
419
412
const path = queue . pop ( ) ! ;
420
413
if ( ! seenMap . has ( path ) ) {
421
- seenMap . set ( path , true ) ;
414
+ seenMap . add ( path ) ;
422
415
const references = state . referencedMap . get ( path ) ;
423
416
if ( references ) {
424
417
const iterator = references . keys ( ) ;
425
418
for ( let iterResult = iterator . next ( ) ; ! iterResult . done ; iterResult = iterator . next ( ) ) {
426
- queue . push ( iterResult . value as Path ) ;
419
+ queue . push ( iterResult . value ) ;
427
420
}
428
421
}
429
422
}
430
423
}
431
424
432
- return arrayFrom ( mapDefinedIterator ( seenMap . keys ( ) , path => {
433
- const file = programOfThisState . getSourceFileByPath ( path as Path ) ;
434
- return file ? file . fileName : path ;
435
- } ) ) ;
425
+ return arrayFrom ( mapDefinedIterator ( seenMap . keys ( ) , path => programOfThisState . getSourceFileByPath ( path ) ?. fileName ?? path ) ) ;
436
426
}
437
427
438
428
/**
@@ -451,7 +441,7 @@ namespace ts {
451
441
*/
452
442
export function getReferencedByPaths ( state : Readonly < BuilderState > , referencedFilePath : Path ) {
453
443
return arrayFrom ( mapDefinedIterator ( state . referencedMap ! . entries ( ) , ( [ filePath , referencesInFile ] ) =>
454
- referencesInFile . has ( referencedFilePath ) ? filePath as Path : undefined
444
+ referencesInFile . has ( referencedFilePath ) ? filePath : undefined
455
445
) ) ;
456
446
}
457
447
@@ -528,7 +518,7 @@ namespace ts {
528
518
/**
529
519
* When program emits modular code, gets the files affected by the sourceFile whose shape has changed
530
520
*/
531
- function getFilesAffectedByUpdatedShapeWhenModuleEmit ( state : BuilderState , programOfThisState : Program , sourceFileWithUpdatedShape : SourceFile , cacheToUpdateSignature : Map < string > , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash | undefined , exportedModulesMapCache : ComputingExportedModulesMap | undefined ) {
521
+ function getFilesAffectedByUpdatedShapeWhenModuleEmit ( state : BuilderState , programOfThisState : Program , sourceFileWithUpdatedShape : SourceFile , cacheToUpdateSignature : Map < Path , string > , cancellationToken : CancellationToken | undefined , computeHash : ComputeHash | undefined , exportedModulesMapCache : ComputingExportedModulesMap | undefined ) {
532
522
if ( isFileAffectingGlobalScope ( sourceFileWithUpdatedShape ) ) {
533
523
return getAllFilesExcludingDefaultLibraryFile ( state , programOfThisState , sourceFileWithUpdatedShape ) ;
534
524
}
@@ -541,7 +531,7 @@ namespace ts {
541
531
// Now we need to if each file in the referencedBy list has a shape change as well.
542
532
// Because if so, its own referencedBy files need to be saved as well to make the
543
533
// emitting result consistent with files on disk.
544
- const seenFileNamesMap = createMap < SourceFile > ( ) ;
534
+ const seenFileNamesMap = new Map < Path , SourceFile > ( ) ;
545
535
546
536
// Start with the paths this file was referenced by
547
537
seenFileNamesMap . set ( sourceFileWithUpdatedShape . resolvedPath , sourceFileWithUpdatedShape ) ;
@@ -562,8 +552,4 @@ namespace ts {
562
552
return arrayFrom ( mapDefinedIterator ( seenFileNamesMap . values ( ) , value => value ) ) ;
563
553
}
564
554
}
565
-
566
- export function cloneMapOrUndefined < T > ( map : ReadonlyMap < T > | undefined ) {
567
- return map ? cloneMap ( map ) : undefined ;
568
- }
569
555
}
0 commit comments