Skip to content

Commit 871cd6a

Browse files
authoredFeb 9, 2021
feat(compiler): expose TsCompiler as public api (#2344)
1 parent 6cc48ce commit 871cd6a

File tree

1 file changed

+58
-35
lines changed

1 file changed

+58
-35
lines changed
 

‎src/compiler/ts-compiler.ts

+58-35
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,41 @@ import type {
1515
TransformerFactory,
1616
Bundle,
1717
CustomTransformerFactory,
18+
CustomTransformers,
1819
} from 'typescript'
1920

2021
import type { ConfigSet } from '../config/config-set'
2122
import { LINE_FEED } from '../constants'
22-
import type { ResolvedModulesMap, StringMap, TsCompilerInstance, TTypeScript } from '../types'
23+
import type { ResolvedModulesMap, StringMap, TsCompilerInstance, TsJestAstTransformer, TTypeScript } from '../types'
2324
import { rootLogger } from '../utils/logger'
2425
import { Errors, interpolate } from '../utils/messages'
2526

2627
import { updateOutput } from './compiler-utils'
2728

28-
/**
29-
* @internal
30-
*/
3129
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+
*/
3437
private readonly _parsedTsConfig: ParsedCommandLine
38+
/**
39+
* @internal
40+
*/
3541
private readonly _compilerCacheFS: Map<string, number> = new Map<string, number>()
36-
private readonly _initialCompilerOptions: CompilerOptions
37-
private _compilerOptions: CompilerOptions
42+
/**
43+
* @internal
44+
*/
3845
private _cachedReadFile: ((fileName: string) => string | undefined) | undefined
46+
/**
47+
* @internal
48+
*/
3949
private _projectVersion = 1
50+
/**
51+
* @internal
52+
*/
4053
private _languageService: LanguageService | undefined
4154
program: Program | undefined
4255

@@ -51,6 +64,9 @@ export class TsCompiler implements TsCompilerInstance {
5164
}
5265
}
5366

67+
/**
68+
* @internal
69+
*/
5470
private _createLanguageService(): void {
5571
const serviceHostTraceCtx = {
5672
namespace: 'ts:serviceHost',
@@ -122,17 +138,7 @@ export class TsCompiler implements TsCompilerInstance {
122138
getCurrentDirectory: () => this.configSet.cwd,
123139
getCompilationSettings: () => this._compilerOptions,
124140
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),
136142
resolveModuleNames: (moduleNames: string[], containingFile: string): (ResolvedModuleFull | undefined)[] =>
137143
moduleNames.map((moduleName) => {
138144
const { resolvedModule } = this._ts.resolveModuleName(
@@ -210,22 +216,7 @@ export class TsCompiler implements TsCompilerInstance {
210216
} else {
211217
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling as isolated module')
212218

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)
229220
if (result.diagnostics && this.configSet.shouldReportDiagnostics(fileName)) {
230221
this.configSet.raiseDiagnostics(result.diagnostics, fileName, this._logger)
231222
}
@@ -235,12 +226,41 @@ export class TsCompiler implements TsCompilerInstance {
235226
}
236227
}
237228

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+
*/
238255
private _isFileInCache(fileName: string): boolean {
239256
return (
240257
this.jestCacheFS.has(fileName) && this._compilerCacheFS.has(fileName) && this._compilerCacheFS.get(fileName) !== 0
241258
)
242259
}
243260

261+
/**
262+
* @internal
263+
*/
244264
/* istanbul ignore next */
245265
private _updateMemoryCache(contents: string, fileName: string): void {
246266
this._logger.debug({ fileName }, 'updateMemoryCache: update memory cache for language service')
@@ -271,6 +291,9 @@ export class TsCompiler implements TsCompilerInstance {
271291
if (shouldIncrementProjectVersion) this._projectVersion++
272292
}
273293

294+
/**
295+
* @internal
296+
*/
274297
private _doTypeChecking(fileName: string): void {
275298
if (this.configSet.shouldReportDiagnostics(fileName)) {
276299
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.

0 commit comments

Comments
 (0)
Please sign in to comment.