@@ -15,28 +15,41 @@ import type {
15
15
TransformerFactory ,
16
16
Bundle ,
17
17
CustomTransformerFactory ,
18
+ CustomTransformers ,
18
19
} from 'typescript'
19
20
20
21
import type { ConfigSet } from '../config/config-set'
21
22
import { LINE_FEED } from '../constants'
22
- import type { ResolvedModulesMap , StringMap , TsCompilerInstance , TTypeScript } from '../types'
23
+ import type { ResolvedModulesMap , StringMap , TsCompilerInstance , TsJestAstTransformer , TTypeScript } from '../types'
23
24
import { rootLogger } from '../utils/logger'
24
25
import { Errors , interpolate } from '../utils/messages'
25
26
26
27
import { updateOutput } from './compiler-utils'
27
28
28
- /**
29
- * @internal
30
- */
31
29
export class TsCompiler implements TsCompilerInstance {
32
- private readonly _logger : Logger
33
- private readonly _ts : TTypeScript
30
+ protected readonly _logger : Logger
31
+ protected readonly _ts : TTypeScript
32
+ protected readonly _initialCompilerOptions : CompilerOptions
33
+ protected _compilerOptions : CompilerOptions
34
+ /**
35
+ * @internal
36
+ */
34
37
private readonly _parsedTsConfig : ParsedCommandLine
38
+ /**
39
+ * @internal
40
+ */
35
41
private readonly _compilerCacheFS : Map < string , number > = new Map < string , number > ( )
36
- private readonly _initialCompilerOptions : CompilerOptions
37
- private _compilerOptions : CompilerOptions
42
+ /**
43
+ * @internal
44
+ */
38
45
private _cachedReadFile : ( ( fileName : string ) => string | undefined ) | undefined
46
+ /**
47
+ * @internal
48
+ */
39
49
private _projectVersion = 1
50
+ /**
51
+ * @internal
52
+ */
40
53
private _languageService : LanguageService | undefined
41
54
program : Program | undefined
42
55
@@ -51,6 +64,9 @@ export class TsCompiler implements TsCompilerInstance {
51
64
}
52
65
}
53
66
67
+ /**
68
+ * @internal
69
+ */
54
70
private _createLanguageService ( ) : void {
55
71
const serviceHostTraceCtx = {
56
72
namespace : 'ts:serviceHost' ,
@@ -122,17 +138,7 @@ export class TsCompiler implements TsCompilerInstance {
122
138
getCurrentDirectory : ( ) => this . configSet . cwd ,
123
139
getCompilationSettings : ( ) => this . _compilerOptions ,
124
140
getDefaultLibFileName : ( ) => this . _ts . getDefaultLibFilePath ( this . _compilerOptions ) ,
125
- getCustomTransformers : ( ) => ( {
126
- before : this . configSet . resolvedTransformers . before . map ( ( beforeTransformer ) =>
127
- beforeTransformer . factory ( this , beforeTransformer . options ) ,
128
- ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
129
- after : this . configSet . resolvedTransformers . after . map ( ( afterTransformer ) =>
130
- afterTransformer . factory ( this , afterTransformer . options ) ,
131
- ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
132
- afterDeclarations : this . configSet . resolvedTransformers . afterDeclarations . map ( ( afterDeclarations ) =>
133
- afterDeclarations . factory ( this , afterDeclarations . options ) ,
134
- ) as TransformerFactory < SourceFile | Bundle > [ ] ,
135
- } ) ,
141
+ getCustomTransformers : ( ) => this . _makeTransformers ( this . configSet . resolvedTransformers ) ,
136
142
resolveModuleNames : ( moduleNames : string [ ] , containingFile : string ) : ( ResolvedModuleFull | undefined ) [ ] =>
137
143
moduleNames . map ( ( moduleName ) => {
138
144
const { resolvedModule } = this . _ts . resolveModuleName (
@@ -210,22 +216,7 @@ export class TsCompiler implements TsCompilerInstance {
210
216
} else {
211
217
this . _logger . debug ( { fileName } , 'getCompiledOutput(): compiling as isolated module' )
212
218
213
- const result : TranspileOutput = this . _ts . transpileModule ( fileContent , {
214
- fileName,
215
- transformers : {
216
- before : this . configSet . resolvedTransformers . before . map ( ( beforeTransformer ) =>
217
- beforeTransformer . factory ( this , beforeTransformer . options ) ,
218
- ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
219
- after : this . configSet . resolvedTransformers . after . map ( ( afterTransformer ) =>
220
- afterTransformer . factory ( this , afterTransformer . options ) ,
221
- ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
222
- afterDeclarations : this . configSet . resolvedTransformers . afterDeclarations . map ( ( afterDeclarations ) =>
223
- afterDeclarations . factory ( this , afterDeclarations . options ) ,
224
- ) as TransformerFactory < SourceFile | Bundle > [ ] ,
225
- } ,
226
- compilerOptions : this . _compilerOptions ,
227
- reportDiagnostics : this . configSet . shouldReportDiagnostics ( fileName ) ,
228
- } )
219
+ const result : TranspileOutput = this . _transpileOutput ( fileContent , fileName )
229
220
if ( result . diagnostics && this . configSet . shouldReportDiagnostics ( fileName ) ) {
230
221
this . configSet . raiseDiagnostics ( result . diagnostics , fileName , this . _logger )
231
222
}
@@ -235,12 +226,41 @@ export class TsCompiler implements TsCompilerInstance {
235
226
}
236
227
}
237
228
229
+ protected _transpileOutput ( fileContent : string , fileName : string ) : TranspileOutput {
230
+ return this . _ts . transpileModule ( fileContent , {
231
+ fileName,
232
+ transformers : this . _makeTransformers ( this . configSet . resolvedTransformers ) ,
233
+ compilerOptions : this . _compilerOptions ,
234
+ reportDiagnostics : this . configSet . shouldReportDiagnostics ( fileName ) ,
235
+ } )
236
+ }
237
+
238
+ protected _makeTransformers ( customTransformers : TsJestAstTransformer ) : CustomTransformers {
239
+ return {
240
+ before : customTransformers . before . map ( ( beforeTransformer ) =>
241
+ beforeTransformer . factory ( this , beforeTransformer . options ) ,
242
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
243
+ after : customTransformers . after . map ( ( afterTransformer ) =>
244
+ afterTransformer . factory ( this , afterTransformer . options ) ,
245
+ ) as ( TransformerFactory < SourceFile > | CustomTransformerFactory ) [ ] ,
246
+ afterDeclarations : customTransformers . afterDeclarations . map ( ( afterDeclarations ) =>
247
+ afterDeclarations . factory ( this , afterDeclarations . options ) ,
248
+ ) as TransformerFactory < SourceFile | Bundle > [ ] ,
249
+ }
250
+ }
251
+
252
+ /**
253
+ * @internal
254
+ */
238
255
private _isFileInCache ( fileName : string ) : boolean {
239
256
return (
240
257
this . jestCacheFS . has ( fileName ) && this . _compilerCacheFS . has ( fileName ) && this . _compilerCacheFS . get ( fileName ) !== 0
241
258
)
242
259
}
243
260
261
+ /**
262
+ * @internal
263
+ */
244
264
/* istanbul ignore next */
245
265
private _updateMemoryCache ( contents : string , fileName : string ) : void {
246
266
this . _logger . debug ( { fileName } , 'updateMemoryCache: update memory cache for language service' )
@@ -271,6 +291,9 @@ export class TsCompiler implements TsCompilerInstance {
271
291
if ( shouldIncrementProjectVersion ) this . _projectVersion ++
272
292
}
273
293
294
+ /**
295
+ * @internal
296
+ */
274
297
private _doTypeChecking ( fileName : string ) : void {
275
298
if ( this . configSet . shouldReportDiagnostics ( fileName ) ) {
276
299
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
0 commit comments