1
+ import { Program , SourceFile , CancellationToken , CustomTransformers , EmitOutput , OutputFile , ReadonlyESMap , Path , ESMap , Symbol , mapDefined , getSourceFileOfNode , TypeChecker , StringLiteralLike , GetCanonicalFileName , toPath , getDirectoryPath , isStringLiteral , ModuleKind , Debug , emptyArray , firstOrUndefined , fileExtensionIsOneOf , Extension , getAnyExtensionFromPath , generateDjb2Hash , ExportedModulesFromDeclarationEmit , outFile , arrayFrom , mapDefinedIterator , isModuleWithStringLiteralName , some , isGlobalScopeAugmentation , ModuleDeclaration , isExternalOrCommonJsModule , isJsonSourceFile } from "./ts" ;
2
+ import * as ts from "./ts" ;
1
3
/*@internal */
2
- namespace ts {
3
- export function getFileEmitOutput ( program : Program , sourceFile : SourceFile , emitOnlyDtsFiles : boolean ,
4
- cancellationToken ?: CancellationToken , customTransformers ?: CustomTransformers , forceDtsEmit ?: boolean ) : EmitOutput {
4
+ export function getFileEmitOutput ( program : Program , sourceFile : SourceFile , emitOnlyDtsFiles : boolean , cancellationToken ?: CancellationToken , customTransformers ?: CustomTransformers , forceDtsEmit ?: boolean ) : EmitOutput {
5
5
const outputFiles : OutputFile [ ] = [ ] ;
6
6
const { emitSkipped, diagnostics, exportedModulesFromDeclarationEmit } = program . emit ( sourceFile , writeFile , cancellationToken , emitOnlyDtsFiles , customTransformers , forceDtsEmit ) ;
7
7
return { outputFiles, emitSkipped, diagnostics, exportedModulesFromDeclarationEmit } ;
@@ -11,6 +11,7 @@ export function getFileEmitOutput(program: Program, sourceFile: SourceFile, emit
11
11
}
12
12
}
13
13
14
+ /* @internal */
14
15
export interface ReusableBuilderState {
15
16
/**
16
17
* Information of the file eg. its version, signature etc
@@ -29,6 +30,7 @@ export interface ReusableBuilderState {
29
30
readonly exportedModulesMap ?: BuilderState . ReadonlyManyToManyPathMap | undefined ;
30
31
}
31
32
33
+ /* @internal */
32
34
export interface BuilderState {
33
35
/**
34
36
* Information of the file eg. its version, signature etc
@@ -49,8 +51,8 @@ export interface BuilderState {
49
51
readonly exportedModulesMap : BuilderState . ManyToManyPathMap | undefined ;
50
52
51
53
previousCache ?: {
52
- id : number ,
53
- version : number ,
54
+ id : number ;
55
+ version : number ;
54
56
} ;
55
57
56
58
/**
@@ -63,7 +65,7 @@ export interface BuilderState {
63
65
* That means hence forth these files are assumed to have
64
66
* no change in their signature for this version of the program
65
67
*/
66
- hasCalledUpdateShapeSignature : Set < Path > ;
68
+ hasCalledUpdateShapeSignature : ts . Set < Path > ;
67
69
/**
68
70
* Cache of all files excluding default library file for the current program
69
71
*/
@@ -74,6 +76,7 @@ export interface BuilderState {
74
76
allFileNames ?: readonly string [ ] ;
75
77
}
76
78
79
+ /* @internal */
77
80
export namespace BuilderState {
78
81
/**
79
82
* Information about the source file: Its version and optional signature from last emit
@@ -88,34 +91,34 @@ export namespace BuilderState {
88
91
export interface ReadonlyManyToManyPathMap {
89
92
readonly id : number ;
90
93
clone ( ) : ManyToManyPathMap ;
91
- forEach ( action : ( v : ReadonlySet < Path > , k : Path ) => void ) : void ;
92
- getKeys ( v : Path ) : ReadonlySet < Path > | undefined ;
93
- getValues ( k : Path ) : ReadonlySet < Path > | undefined ;
94
+ forEach ( action : ( v : ts . ReadonlySet < Path > , k : Path ) => void ) : void ;
95
+ getKeys ( v : Path ) : ts . ReadonlySet < Path > | undefined ;
96
+ getValues ( k : Path ) : ts . ReadonlySet < Path > | undefined ;
94
97
hasKey ( k : Path ) : boolean ;
95
- keys ( ) : Iterator < Path > ;
98
+ keys ( ) : ts . Iterator < Path > ;
96
99
97
100
/**
98
101
* The set of arguments to {@link deleteKeys} which have not subsequently
99
102
* been arguments to {@link set}. Note that a key does not have to have
100
103
* ever been in the map to appear in this set.
101
104
*/
102
- deletedKeys ( ) : ReadonlySet < Path > | undefined ;
105
+ deletedKeys ( ) : ts . ReadonlySet < Path > | undefined ;
103
106
}
104
107
105
108
export interface ManyToManyPathMap extends ReadonlyManyToManyPathMap {
106
109
version ( ) : number ; // Incremented each time the contents are changed
107
110
deleteKey ( k : Path ) : boolean ;
108
- set ( k : Path , v : ReadonlySet < Path > ) : void ;
111
+ set ( k : Path , v : ts . ReadonlySet < Path > ) : void ;
109
112
}
110
113
111
114
let manyToManyPathMapCount = 0 ;
112
115
export function createManyToManyPathMap ( ) : ManyToManyPathMap {
113
- function create ( forward : ESMap < Path , ReadonlySet < Path > > , reverse : ESMap < Path , Set < Path > > , deleted : Set < Path > | undefined ) : ManyToManyPathMap {
116
+ function create ( forward : ESMap < Path , ts . ReadonlySet < Path > > , reverse : ESMap < Path , ts . Set < Path > > , deleted : ts . Set < Path > | undefined ) : ManyToManyPathMap {
114
117
let version = 0 ;
115
118
const map : ManyToManyPathMap = {
116
119
id : manyToManyPathMapCount ++ ,
117
120
version : ( ) => version ,
118
- clone : ( ) => create ( new Map ( forward ) , new Map ( reverse ) , deleted && new Set ( deleted ) ) ,
121
+ clone : ( ) => create ( new ts . Map ( forward ) , new ts . Map ( reverse ) , deleted && new ts . Set ( deleted ) ) ,
119
122
forEach : fn => forward . forEach ( fn ) ,
120
123
getKeys : v => reverse . get ( v ) ,
121
124
getValues : k => forward . get ( k ) ,
@@ -124,7 +127,7 @@ export namespace BuilderState {
124
127
125
128
deletedKeys : ( ) => deleted ,
126
129
deleteKey : k => {
127
- ( deleted ||= new Set < Path > ( ) ) . add ( k ) ;
130
+ ( deleted ||= new ts . Set < Path > ( ) ) . add ( k ) ;
128
131
129
132
const set = forward . get ( k ) ;
130
133
if ( ! set ) {
@@ -167,19 +170,19 @@ export namespace BuilderState {
167
170
return map ;
168
171
}
169
172
170
- return create ( new Map < Path , Set < Path > > ( ) , new Map < Path , Set < Path > > ( ) , /*deleted*/ undefined ) ;
173
+ return create ( new ts . Map < Path , ts . Set < Path > > ( ) , new ts . Map < Path , ts . Set < Path > > ( ) , /*deleted*/ undefined ) ;
171
174
}
172
175
173
- function addToMultimap < K , V > ( map : ESMap < K , Set < V > > , k : K , v : V ) : void {
176
+ function addToMultimap < K , V > ( map : ESMap < K , ts . Set < V > > , k : K , v : V ) : void {
174
177
let set = map . get ( k ) ;
175
178
if ( ! set ) {
176
- set = new Set < V > ( ) ;
179
+ set = new ts . Set < V > ( ) ;
177
180
map . set ( k , set ) ;
178
181
}
179
182
set . add ( v ) ;
180
183
}
181
184
182
- function deleteFromMultimap < K , V > ( map : ESMap < K , Set < V > > , k : K , v : V , removeEmpty = true ) : boolean {
185
+ function deleteFromMultimap < K , V > ( map : ESMap < K , ts . Set < V > > , k : K , v : V , removeEmpty = true ) : boolean {
183
186
const set = map . get ( k ) ;
184
187
185
188
if ( set ?. delete ( v ) ) {
@@ -219,8 +222,8 @@ export namespace BuilderState {
219
222
/**
220
223
* Gets the referenced files for a file from the program with values for the keys as referenced file's path to be true
221
224
*/
222
- function getReferencedFiles ( program : Program , sourceFile : SourceFile , getCanonicalFileName : GetCanonicalFileName ) : Set < Path > | undefined {
223
- let referencedFiles : Set < Path > | undefined ;
225
+ function getReferencedFiles ( program : Program , sourceFile : SourceFile , getCanonicalFileName : GetCanonicalFileName ) : ts . Set < Path > | undefined {
226
+ let referencedFiles : ts . Set < Path > | undefined ;
224
227
225
228
// We need to use a set here since the code can contain the same import twice,
226
229
// but that will only be one dependency.
@@ -259,9 +262,11 @@ export namespace BuilderState {
259
262
if ( sourceFile . moduleAugmentations . length ) {
260
263
const checker = program . getTypeChecker ( ) ;
261
264
for ( const moduleName of sourceFile . moduleAugmentations ) {
262
- if ( ! isStringLiteral ( moduleName ) ) continue ;
265
+ if ( ! isStringLiteral ( moduleName ) )
266
+ continue ;
263
267
const symbol = checker . getSymbolAtLocation ( moduleName ) ;
264
- if ( ! symbol ) continue ;
268
+ if ( ! symbol )
269
+ continue ;
265
270
266
271
// Add any file other than our own as reference
267
272
addReferenceFromAmbientModule ( symbol ) ;
@@ -292,7 +297,7 @@ export namespace BuilderState {
292
297
}
293
298
294
299
function addReferencedFile ( referencedPath : Path ) {
295
- ( referencedFiles || ( referencedFiles = new Set ( ) ) ) . add ( referencedPath ) ;
300
+ ( referencedFiles || ( referencedFiles = new ts . Set ( ) ) ) . add ( referencedPath ) ;
296
301
}
297
302
}
298
303
@@ -307,10 +312,10 @@ export namespace BuilderState {
307
312
* Creates the state of file references and signature for the new program from oldState if it is safe
308
313
*/
309
314
export function create ( newProgram : Program , getCanonicalFileName : GetCanonicalFileName , oldState ?: Readonly < ReusableBuilderState > , disableUseFileVersionAsSignature ?: boolean ) : BuilderState {
310
- const fileInfos = new Map < Path , FileInfo > ( ) ;
315
+ const fileInfos = new ts . Map < Path , FileInfo > ( ) ;
311
316
const referencedMap = newProgram . getCompilerOptions ( ) . module !== ModuleKind . None ? createManyToManyPathMap ( ) : undefined ;
312
317
const exportedModulesMap = referencedMap ? createManyToManyPathMap ( ) : undefined ;
313
- const hasCalledUpdateShapeSignature = new Set < Path > ( ) ;
318
+ const hasCalledUpdateShapeSignature = new ts . Set < Path > ( ) ;
314
319
const useOldState = canReuseOldState ( referencedMap , oldState ) ;
315
320
316
321
// Ensure source files have parent pointers set
@@ -359,10 +364,10 @@ export namespace BuilderState {
359
364
export function clone ( state : Readonly < BuilderState > ) : BuilderState {
360
365
// Dont need to backup allFiles info since its cache anyway
361
366
return {
362
- fileInfos : new Map ( state . fileInfos ) ,
367
+ fileInfos : new ts . Map ( state . fileInfos ) ,
363
368
referencedMap : state . referencedMap ?. clone ( ) ,
364
369
exportedModulesMap : state . exportedModulesMap ?. clone ( ) ,
365
- hasCalledUpdateShapeSignature : new Set ( state . hasCalledUpdateShapeSignature ) ,
370
+ hasCalledUpdateShapeSignature : new ts . Set ( state . hasCalledUpdateShapeSignature ) ,
366
371
useFileVersionAsSignature : state . useFileVersionAsSignature ,
367
372
} ;
368
373
}
@@ -375,7 +380,7 @@ export namespace BuilderState {
375
380
// They will be committed once it is safe to use them
376
381
// eg when calling this api from tsserver, if there is no cancellation of the operation
377
382
// In the other cases the affected files signatures are committed only after the iteration through the result is complete
378
- const signatureCache = cacheToUpdateSignature || new Map ( ) ;
383
+ const signatureCache = cacheToUpdateSignature || new ts . Map ( ) ;
379
384
const sourceFile = programOfThisState . getSourceFileByPath ( path ) ;
380
385
if ( ! sourceFile ) {
381
386
return emptyArray ;
@@ -419,19 +424,16 @@ export namespace BuilderState {
419
424
}
420
425
421
426
const info = state . fileInfos . get ( sourceFile . resolvedPath ) ;
422
- if ( ! info ) return Debug . fail ( ) ;
427
+ if ( ! info )
428
+ return Debug . fail ( ) ;
423
429
424
430
const prevSignature = info . signature ;
425
431
let latestSignature : string | undefined ;
426
432
if ( ! sourceFile . isDeclarationFile && ! useFileVersionAsSignature ) {
427
- const emitOutput = getFileEmitOutput (
428
- programOfThisState ,
429
- sourceFile ,
430
- /*emitOnlyDtsFiles*/ true ,
431
- cancellationToken ,
433
+ const emitOutput = getFileEmitOutput ( programOfThisState , sourceFile ,
434
+ /*emitOnlyDtsFiles*/ true , cancellationToken ,
432
435
/*customTransformers*/ undefined ,
433
- /*forceDtsEmit*/ true
434
- ) ;
436
+ /*forceDtsEmit*/ true ) ;
435
437
const firstDts = firstOrUndefined ( emitOutput . outputFiles ) ;
436
438
if ( firstDts ) {
437
439
Debug . assert ( fileExtensionIsOneOf ( firstDts . name , [ Extension . Dts , Extension . Dmts , Extension . Dcts ] ) , "File extension for signature expected to be dts" , ( ) => `Found: ${ getAnyExtensionFromPath ( firstDts . name ) } for ${ firstDts . name } :: All output files: ${ JSON . stringify ( emitOutput . outputFiles . map ( f => f . name ) ) } ` ) ;
@@ -468,7 +470,7 @@ export namespace BuilderState {
468
470
return ;
469
471
}
470
472
471
- let exportedModules : Set < Path > | undefined ;
473
+ let exportedModules : ts . Set < Path > | undefined ;
472
474
exportedModulesFromDeclarationEmit . forEach ( symbol => addExportedModule ( getReferencedFilesFromImportedModuleSymbol ( symbol ) ) ) ;
473
475
if ( exportedModules ) {
474
476
exportedModulesMapCache . set ( sourceFile . resolvedPath , exportedModules ) ;
@@ -480,7 +482,7 @@ export namespace BuilderState {
480
482
function addExportedModule ( exportedModulePaths : Path [ ] | undefined ) {
481
483
if ( exportedModulePaths ?. length ) {
482
484
if ( ! exportedModules ) {
483
- exportedModules = new Set ( ) ;
485
+ exportedModules = new ts . Set ( ) ;
484
486
}
485
487
exportedModulePaths . forEach ( path => exportedModules ! . add ( path ) ) ;
486
488
}
@@ -531,7 +533,7 @@ export namespace BuilderState {
531
533
}
532
534
533
535
// Get the references, traversing deep from the referenceMap
534
- const seenMap = new Set < Path > ( ) ;
536
+ const seenMap = new ts . Set < Path > ( ) ;
535
537
const queue = [ sourceFile . resolvedPath ] ;
536
538
while ( queue . length ) {
537
539
const path = queue . pop ( ) ! ;
@@ -610,7 +612,8 @@ export namespace BuilderState {
610
612
}
611
613
612
614
let result : SourceFile [ ] | undefined ;
613
- if ( firstSourceFile ) addSourceFile ( firstSourceFile ) ;
615
+ if ( firstSourceFile )
616
+ addSourceFile ( firstSourceFile ) ;
614
617
for ( const sourceFile of programOfThisState . getSourceFiles ( ) ) {
615
618
if ( sourceFile !== firstSourceFile ) {
616
619
addSourceFile ( sourceFile ) ;
@@ -655,7 +658,7 @@ export namespace BuilderState {
655
658
// Now we need to if each file in the referencedBy list has a shape change as well.
656
659
// Because if so, its own referencedBy files need to be saved as well to make the
657
660
// emitting result consistent with files on disk.
658
- const seenFileNamesMap = new Map < Path , SourceFile > ( ) ;
661
+ const seenFileNamesMap = new ts . Map < Path , SourceFile > ( ) ;
659
662
660
663
// Start with the paths this file was referenced by
661
664
seenFileNamesMap . set ( sourceFileWithUpdatedShape . resolvedPath , sourceFileWithUpdatedShape ) ;
@@ -675,4 +678,3 @@ export namespace BuilderState {
675
678
return arrayFrom ( mapDefinedIterator ( seenFileNamesMap . values ( ) , value => value ) ) ;
676
679
}
677
680
}
678
- }
0 commit comments