1
+ import os from 'os' ;
2
+ import path from 'path' ;
3
+
1
4
import { type TsJestAstTransformer , TsCompiler , type ConfigSet } from 'ts-jest' ;
2
5
import type * as ts from 'typescript' ;
3
6
@@ -20,118 +23,45 @@ export class NgJestCompiler extends TsCompiler {
20
23
* and we need `Program` to be able to use Angular `replace-resources` transformer.
21
24
*/
22
25
protected _transpileOutput ( fileContent : string , filePath : string ) : ts . TranspileOutput {
23
- const diagnostics : ts . Diagnostic [ ] = [ ] ;
24
- const compilerOptions = { ...this . _compilerOptions } ;
25
- const options : ts . CompilerOptions = compilerOptions
26
- ? // @ts -expect-error internal TypeScript API
27
- this . _ts . fixupCompilerOptions ( compilerOptions , diagnostics )
28
- : { } ;
29
-
30
- // mix in default options
31
- const defaultOptions = this . _ts . getDefaultCompilerOptions ( ) ;
32
- for ( const key in defaultOptions ) {
33
- // @ts -expect-error internal TypeScript API
34
- if ( this . _ts . hasProperty ( defaultOptions , key ) && options [ key ] === undefined ) {
35
- options [ key ] = defaultOptions [ key ] ;
36
- }
37
- }
38
-
39
- // @ts -expect-error internal TypeScript API
40
- for ( const option of this . _ts . transpileOptionValueCompilerOptions ) {
41
- options [ option . name ] = option . transpileOptionValue ;
42
- }
43
-
44
- /**
45
- * transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between
46
- * input and output paths.
47
- */
48
- options . suppressOutputPathCheck = true ;
49
-
50
- // Filename can be non-ts file.
51
- options . allowNonTsExtensions = true ;
52
-
53
- // if jsx is specified then treat file as .tsx
54
- const inputFileName = filePath || ( compilerOptions && compilerOptions . jsx ? 'module.tsx' : 'module.ts' ) ;
55
26
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
56
- const sourceFile = this . _ts . createSourceFile ( inputFileName , fileContent , options . target ! ) ; // TODO: GH#18217
57
-
58
- // @ts -expect-error internal TypeScript API
59
- const newLine = this . _ts . getNewLineCharacter ( options ) ;
60
-
61
- // Output
62
- let outputText : string | undefined ;
63
- let sourceMapText : string | undefined ;
64
-
65
- // Create a compilerHost object to allow the compiler to read and write files
27
+ const sourceFile = this . _ts . createSourceFile ( filePath , fileContent , this . _compilerOptions . target ! ) ;
66
28
const compilerHost : ts . CompilerHost = {
67
- getSourceFile : ( fileName ) =>
68
- // @ts -expect-error internal TypeScript API
69
- fileName === this . _ts . normalizePath ( inputFileName ) ? sourceFile : undefined ,
70
- writeFile : ( name , text ) => {
71
- // @ts -expect-error internal TypeScript API
72
- if ( this . _ts . fileExtensionIs ( name , '.map' ) ) {
73
- // @ts -expect-error internal TypeScript API
74
- this . _ts . Debug . assertEqual (
75
- sourceMapText ,
76
- undefined ,
77
- 'Unexpected multiple source map outputs, file:' ,
78
- name ,
79
- ) ;
80
- sourceMapText = text ;
81
- } else {
82
- // @ts -expect-error internal TypeScript API
83
- this . _ts . Debug . assertEqual ( outputText , undefined , 'Unexpected multiple outputs, file:' , name ) ;
84
- outputText = text ;
85
- }
86
- } ,
29
+ getSourceFile : ( fileName ) => ( fileName === path . normalize ( filePath ) ? sourceFile : undefined ) ,
30
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
31
+ writeFile : ( ) => { } ,
87
32
getDefaultLibFileName : ( ) => 'lib.d.ts' ,
88
33
useCaseSensitiveFileNames : ( ) => false ,
89
34
getCanonicalFileName : ( fileName ) => fileName ,
90
35
getCurrentDirectory : ( ) => '' ,
91
- getNewLine : ( ) => newLine ,
92
- fileExists : ( fileName ) : boolean => fileName === inputFileName ,
36
+ getNewLine : ( ) => os . EOL ,
37
+ fileExists : ( fileName ) : boolean => fileName === filePath ,
93
38
readFile : ( ) => '' ,
94
39
directoryExists : ( ) => true ,
95
40
getDirectories : ( ) => [ ] ,
96
41
} ;
97
-
98
- this . program = this . _ts . createProgram ( [ inputFileName ] , options , compilerHost ) ;
99
- if ( this . configSet . shouldReportDiagnostics ( inputFileName ) ) {
100
- // @ts -expect-error internal TypeScript API
101
- this . _ts . addRange ( /*to*/ diagnostics , /*from*/ this . program . getSyntacticDiagnostics ( sourceFile ) ) ;
102
- // @ts -expect-error internal TypeScript API
103
- this . _ts . addRange ( /*to*/ diagnostics , /*from*/ this . program . getOptionsDiagnostics ( ) ) ;
104
- }
105
- // Emit
106
- this . program . emit (
107
- /*targetSourceFile*/ undefined ,
108
- /*writeFile*/ undefined ,
109
- /*cancellationToken*/ undefined ,
110
- /*emitOnlyDtsFiles*/ undefined ,
111
- this . _makeTransformers ( this . configSet . resolvedTransformers ) ,
112
- ) ;
113
- if ( outputText === undefined ) {
114
- // @ts -expect-error internal TypeScript API
115
- return this . _ts . Debug . fail ( 'Output generation failed' ) ;
116
- }
117
-
118
- return { outputText, diagnostics, sourceMapText } ;
42
+ this . program = this . _ts . createProgram ( [ filePath ] , this . _compilerOptions , compilerHost ) ;
43
+
44
+ return this . _ts . transpileModule ( fileContent , {
45
+ fileName : filePath ,
46
+ transformers : this . _makeTransformers ( this . configSet . resolvedTransformers ) ,
47
+ compilerOptions : this . _compilerOptions ,
48
+ reportDiagnostics : this . configSet . shouldReportDiagnostics ( filePath ) ,
49
+ } ) ;
119
50
}
120
51
121
52
protected _makeTransformers ( customTransformers : TsJestAstTransformer ) : ts . CustomTransformers {
122
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
123
- const program = this . program ! ;
53
+ const allTransformers = super . _makeTransformers ( customTransformers ) ;
124
54
125
55
return {
126
- ...super . _makeTransformers ( customTransformers ) . after ,
127
- ...super . _makeTransformers ( customTransformers ) . afterDeclarations ,
56
+ ...allTransformers . after ,
57
+ ...allTransformers . afterDeclarations ,
128
58
before : [
129
- ...customTransformers . before . map ( ( beforeTransformer ) =>
130
- beforeTransformer . factory ( this , beforeTransformer . options ) ,
131
- ) ,
132
- replaceResources ( this ) ,
133
- angularJitApplicationTransform ( program ) ,
134
- ] as Array < ts . TransformerFactory < ts . SourceFile > | ts . CustomTransformerFactory > ,
59
+ ...( allTransformers . before ?? [ ] ) ,
60
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61
+ replaceResources ( this . program ! ) ,
62
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63
+ angularJitApplicationTransform ( this . program ! ) ,
64
+ ] ,
135
65
} ;
136
66
}
137
67
}
0 commit comments