/
compiler-utils.ts
215 lines (192 loc) · 7.03 KB
/
compiler-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { Logger } from 'bs-logger'
import { writeFileSync } from 'fs'
import micromatch = require('micromatch')
import { dirname, join, normalize, relative, resolve } from 'path'
import * as _ts from 'typescript'
import { ConfigSet } from '../config/config-set'
import { EXTENSION_REGEX, JSON_REGEX, TS_TSX_REGEX } from '../constants'
import { MemoryCache, SourceOutput, TSFiles } from '../types'
import { sha1 } from '../util/sha1'
/**
* @internal
*/
export const hasOwn = Object.prototype.hasOwnProperty
/**
* @internal
*/
export function getResolvedModulesCache(cacheDir: string): string {
return join(cacheDir, sha1('ts-jest-resolved-modules', '\x00'))
}
/**
* @internal
* Get resolved modules of a test file and put into memory cache
*/
/* istanbul ignore next (we leave this for e2e) */
export function cacheResolvedModules(
fileName: string,
fileContent: string,
memoryCache: MemoryCache,
program: _ts.Program,
cacheDir: string,
logger: Logger,
) {
// @ts-ignore
const importReferences = program.getSourceFile(fileName)!.imports
/**
* Ugly trick while waiting for https://github.com/microsoft/TypeScript/issues/33994
*/
if (importReferences.length) {
logger.debug({ fileName }, `cacheResolvedModules(): get resolved modules`)
memoryCache.resolvedModules[fileName] = Object.create(null)
memoryCache.resolvedModules[fileName].modulePaths = importReferences
.filter((importReference: any) => importReference.parent.parent.resolvedModules?.get(importReference.text))
.map((importReference: any) => {
return normalize(
(importReference.parent.parent.resolvedModules.get(importReference.text) as _ts.ResolvedModule)
.resolvedFileName,
)
})
.reduce((a: any, b: any) => a.concat(b), [])
memoryCache.resolvedModules[fileName].testFileContent = fileContent
writeFileSync(getResolvedModulesCache(cacheDir), JSON.stringify(memoryCache.resolvedModules))
}
}
/**
* @internal
*/
export function isTestFile(testMatchPatterns: (string | RegExp)[], fileName: string) {
return testMatchPatterns.some(pattern =>
typeof pattern === 'string' ? micromatch.isMatch(fileName, pattern) : pattern.test(fileName),
)
}
/* istanbul ignore next (we leave this for e2e) */
function isUsingProjectReferences(
program: _ts.Program,
projectReferences: ReadonlyArray<_ts.ProjectReference> | undefined,
) {
if (projectReferences && !!program.getProjectReferences) {
return Boolean(program && program.getProjectReferences())
}
return false
}
/* istanbul ignore next (we leave this for e2e) */
function getResolvedProjectReferences(
program: _ts.Program,
): ReadonlyArray<_ts.ResolvedProjectReference | undefined> | undefined {
const getProjectReferences = program.getResolvedProjectReferences || program.getProjectReferences
if (getProjectReferences) {
return getProjectReferences()
}
return
}
/* istanbul ignore next (we leave this for e2e) */
function getProjectReferenceForFile(
filePath: string,
program: _ts.Program,
projectReferences: ReadonlyArray<_ts.ProjectReference> | undefined,
) {
if (isUsingProjectReferences(program, projectReferences)) {
return (
program &&
getResolvedProjectReferences(program)!.find(
ref => (ref && ref.commandLine.fileNames.some(file => normalize(file) === filePath)) || false,
)
)
}
return
}
/**
* @internal
*/
/* istanbul ignore next (we leave this for e2e) */
export function getAndCacheProjectReference(
filePath: string,
program: _ts.Program,
files: TSFiles,
projectReferences: ReadonlyArray<_ts.ProjectReference> | undefined,
) {
const file = files[filePath]
if (file !== undefined && file.projectReference) {
return file.projectReference.project
}
const projectReference = getProjectReferenceForFile(filePath, program, projectReferences)
if (file !== undefined) {
file.projectReference = { project: projectReference }
}
return projectReference
}
// Adapted from https://github.com/Microsoft/TypeScript/blob/45101491c0b077c509b25830ef0ee5f85b293754/src/compiler/tsbuild.ts#L305
/* istanbul ignore next (we leave this for e2e) */
function getOutputJavaScriptFileName(inputFileName: string, projectReference: _ts.ResolvedProjectReference) {
const { options } = projectReference.commandLine
const projectDirectory = options.rootDir || dirname(projectReference.sourceFile.fileName)
const relativePath = relative(projectDirectory, inputFileName)
const outputPath = resolve(options.outDir || projectDirectory, relativePath)
const newExtension = JSON_REGEX.test(inputFileName)
? '.json'
: TS_TSX_REGEX.test(inputFileName) && options.jsx === _ts.JsxEmit.Preserve
? '.jsx'
: '.js'
return outputPath.replace(EXTENSION_REGEX, newExtension)
}
/**
* Gets the output JS file path for an input file governed by a composite project.
* Pulls from the cache if it exists; computes and caches the result otherwise.
*/
/* istanbul ignore next (we leave this for e2e) */
function getAndCacheOutputJSFileName(
inputFileName: string,
projectReference: _ts.ResolvedProjectReference,
files: TSFiles,
) {
const file = files[inputFileName]
if (file?.projectReference && file.projectReference.outputFileName) {
return file.projectReference.outputFileName
}
const outputFileName = getOutputJavaScriptFileName(inputFileName, projectReference)
if (file !== undefined) {
file.projectReference = file.projectReference || {
project: projectReference,
}
file.projectReference.outputFileName = outputFileName
}
return outputFileName
}
/**
* @internal
*/
/* istanbul ignore next (we leave this for e2e) */
export function getCompileResultFromReferencedProject(
fileName: string,
configs: ConfigSet,
files: TSFiles,
referencedProject: _ts.ResolvedProjectReference,
): SourceOutput {
const [relativeProjectConfigPath, relativeFilePath] = [
configs.resolvePath(referencedProject.sourceFile.fileName),
configs.resolvePath(fileName),
]
if (referencedProject.commandLine.options.outFile !== undefined) {
throw new Error(
`The referenced project at ${relativeProjectConfigPath} is using ` +
`the outFile' option, which is not supported with ts-jest.`,
)
}
const jsFileName = getAndCacheOutputJSFileName(fileName, referencedProject, files)
const relativeJSFileName = configs.resolvePath(jsFileName)
if (!configs.compilerModule.sys.fileExists(jsFileName)) {
throw new Error(
// tslint:disable-next-line:prefer-template
`Could not find output JavaScript file for input ` +
`${relativeFilePath} (looked at ${relativeJSFileName}).\n` +
`The input file is part of a project reference located at ` +
`${relativeProjectConfigPath}, so ts-jest is looking for the ` +
'project’s pre-built output on disk. Try running `tsc --build` ' +
'to build project references.',
)
}
const mapFileName = `${jsFileName}.map`
const outputText = configs.compilerModule.sys.readFile(jsFileName)
const sourceMapText = configs.compilerModule.sys.readFile(mapFileName)
return [outputText!, sourceMapText!]
}