ts-jest 29.3.2 → 29.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ts-jest-digest +1 -1
- package/CHANGELOG.md +10 -0
- package/dist/cli/config/init.js +103 -128
- package/dist/cli/config/migrate.js +113 -156
- package/dist/cli/help.js +14 -42
- package/dist/cli/helpers/presets.js +7 -8
- package/dist/cli/index.js +47 -101
- package/dist/config/paths-to-module-name-mapper.js +37 -61
- package/dist/constants.js +1 -5
- package/dist/index.js +2 -2
- package/dist/legacy/compiler/compiler-utils.js +5 -5
- package/dist/legacy/compiler/ts-compiler.js +194 -213
- package/dist/legacy/compiler/ts-jest-compiler.js +9 -9
- package/dist/legacy/config/config-set.js +281 -292
- package/dist/legacy/index.js +2 -4
- package/dist/legacy/ts-jest-transformer.js +134 -201
- package/dist/presets/all-presets.js +2 -2
- package/dist/presets/create-jest-preset.js +101 -139
- package/dist/transformers/hoist-jest.js +30 -43
- package/dist/transpilers/typescript/transpile-module.js +69 -98
- package/dist/types.js +1 -1
- package/dist/utils/backports.js +26 -28
- package/dist/utils/get-package-version.js +2 -2
- package/dist/utils/importer.js +53 -108
- package/dist/utils/json.js +2 -2
- package/dist/utils/jsonable-value.js +20 -27
- package/dist/utils/logger.js +11 -15
- package/dist/utils/memoize.js +11 -17
- package/dist/utils/messages.js +2 -3
- package/dist/utils/sha1.js +7 -11
- package/dist/utils/ts-error.js +19 -35
- package/package.json +18 -18
- package/tsconfig.base.json +20 -0
- package/sonar-project.properties +0 -16
|
@@ -1,155 +1,150 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
-
if (ar || !(i in from)) {
|
|
32
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
-
ar[i] = from[i];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
|
-
var __values = (this && this.__values) || function(o) {
|
|
39
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
-
if (m) return m.call(o);
|
|
41
|
-
if (o && typeof o.length === "number") return {
|
|
42
|
-
next: function () {
|
|
43
|
-
if (o && i >= o.length) o = void 0;
|
|
44
|
-
return { value: o && o[i++], done: !o };
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
-
};
|
|
49
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
4
|
};
|
|
52
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
6
|
exports.TsCompiler = void 0;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const bs_logger_1 = require("bs-logger");
|
|
9
|
+
const lodash_memoize_1 = __importDefault(require("lodash.memoize"));
|
|
10
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
11
|
+
const constants_1 = require("../../constants");
|
|
12
|
+
const transpile_module_1 = require("../../transpilers/typescript/transpile-module");
|
|
13
|
+
const utils_1 = require("../../utils");
|
|
14
|
+
const messages_1 = require("../../utils/messages");
|
|
15
|
+
const compiler_utils_1 = require("./compiler-utils");
|
|
16
|
+
const assertCompilerOptionsWithJestTransformMode = (compilerOptions, isEsmMode, logger) => {
|
|
64
17
|
if (isEsmMode && compilerOptions.module === typescript_1.default.ModuleKind.CommonJS) {
|
|
65
18
|
logger.error("The current compiler option \"module\" value is not suitable for Jest ESM mode. Please either use ES module kinds or Node16/NodeNext module kinds with \"type: module\" in package.json" /* Errors.InvalidModuleKindForEsm */);
|
|
66
19
|
}
|
|
67
20
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
21
|
+
class TsCompiler {
|
|
22
|
+
configSet;
|
|
23
|
+
runtimeCacheFS;
|
|
24
|
+
_logger;
|
|
25
|
+
_ts;
|
|
26
|
+
_initialCompilerOptions;
|
|
27
|
+
_compilerOptions;
|
|
28
|
+
/**
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
31
|
+
_runtimeCacheFS;
|
|
32
|
+
/**
|
|
33
|
+
* @private
|
|
34
|
+
*/
|
|
35
|
+
_fileContentCache;
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
_parsedTsConfig;
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
_fileVersionCache;
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
_cachedReadFile;
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
_projectVersion = 1;
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
_languageService;
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
_moduleResolutionHost;
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
_moduleResolutionCache;
|
|
64
|
+
program;
|
|
65
|
+
constructor(configSet, runtimeCacheFS) {
|
|
72
66
|
this.configSet = configSet;
|
|
73
67
|
this.runtimeCacheFS = runtimeCacheFS;
|
|
74
|
-
/**
|
|
75
|
-
* @internal
|
|
76
|
-
*/
|
|
77
|
-
this._projectVersion = 1;
|
|
78
68
|
this._ts = configSet.compilerModule;
|
|
79
69
|
this._logger = utils_1.rootLogger.child({ namespace: 'ts-compiler' });
|
|
80
70
|
this._parsedTsConfig = this.configSet.parsedTsConfig;
|
|
81
|
-
this._initialCompilerOptions =
|
|
82
|
-
this._compilerOptions =
|
|
71
|
+
this._initialCompilerOptions = { ...this._parsedTsConfig.options };
|
|
72
|
+
this._compilerOptions = { ...this._initialCompilerOptions };
|
|
83
73
|
this._runtimeCacheFS = runtimeCacheFS;
|
|
84
74
|
if (!this.configSet.isolatedModules) {
|
|
85
75
|
this._fileContentCache = new Map();
|
|
86
76
|
this._fileVersionCache = new Map();
|
|
87
|
-
this._cachedReadFile = this._logger.wrap(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
_a), 'readFile', (0, lodash_memoize_1.default)(this._ts.sys.readFile));
|
|
77
|
+
this._cachedReadFile = this._logger.wrap({
|
|
78
|
+
namespace: 'ts:serviceHost',
|
|
79
|
+
call: null,
|
|
80
|
+
[bs_logger_1.LogContexts.logLevel]: bs_logger_1.LogLevels.trace,
|
|
81
|
+
}, 'readFile', (0, lodash_memoize_1.default)(this._ts.sys.readFile));
|
|
93
82
|
/* istanbul ignore next */
|
|
94
83
|
this._moduleResolutionHost = {
|
|
95
84
|
fileExists: (0, lodash_memoize_1.default)(this._ts.sys.fileExists),
|
|
96
85
|
readFile: this._cachedReadFile,
|
|
97
86
|
directoryExists: (0, lodash_memoize_1.default)(this._ts.sys.directoryExists),
|
|
98
|
-
getCurrentDirectory:
|
|
87
|
+
getCurrentDirectory: () => this.configSet.cwd,
|
|
99
88
|
realpath: this._ts.sys.realpath && (0, lodash_memoize_1.default)(this._ts.sys.realpath),
|
|
100
89
|
getDirectories: (0, lodash_memoize_1.default)(this._ts.sys.getDirectories),
|
|
101
|
-
useCaseSensitiveFileNames:
|
|
90
|
+
useCaseSensitiveFileNames: () => this._ts.sys.useCaseSensitiveFileNames,
|
|
102
91
|
};
|
|
103
|
-
this._moduleResolutionCache = this._ts.createModuleResolutionCache(this.configSet.cwd, this._ts.sys.useCaseSensitiveFileNames ?
|
|
92
|
+
this._moduleResolutionCache = this._ts.createModuleResolutionCache(this.configSet.cwd, this._ts.sys.useCaseSensitiveFileNames ? (x) => x : (x) => x.toLowerCase(), this._compilerOptions);
|
|
104
93
|
this._createLanguageService();
|
|
105
94
|
}
|
|
106
95
|
}
|
|
107
|
-
|
|
108
|
-
var _this = this;
|
|
96
|
+
getResolvedModules(fileContent, fileName, runtimeCacheFS) {
|
|
109
97
|
// In watch mode, it is possible that the initial cacheFS becomes empty
|
|
110
98
|
if (!this.runtimeCacheFS.size) {
|
|
111
99
|
this._runtimeCacheFS = runtimeCacheFS;
|
|
112
100
|
}
|
|
113
|
-
this._logger.debug({ fileName
|
|
114
|
-
|
|
115
|
-
this._logger.debug({ fileName
|
|
116
|
-
importedModulePaths.forEach(
|
|
117
|
-
|
|
118
|
-
importedModulePaths.push
|
|
101
|
+
this._logger.debug({ fileName }, 'getResolvedModules(): resolve direct imported module paths');
|
|
102
|
+
const importedModulePaths = Array.from(new Set(this._getImportedModulePaths(fileContent, fileName)));
|
|
103
|
+
this._logger.debug({ fileName }, 'getResolvedModules(): resolve nested imported module paths from directed imported module paths');
|
|
104
|
+
importedModulePaths.forEach((importedModulePath) => {
|
|
105
|
+
const resolvedFileContent = this._getFileContentFromCache(importedModulePath);
|
|
106
|
+
importedModulePaths.push(...this._getImportedModulePaths(resolvedFileContent, importedModulePath).filter((modulePath) => !importedModulePaths.includes(modulePath)));
|
|
119
107
|
});
|
|
120
108
|
return importedModulePaths;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
var moduleResolution = (_a = this._ts.ModuleResolutionKind.Node10) !== null && _a !== void 0 ? _a : this._ts.ModuleResolutionKind.NodeJs;
|
|
109
|
+
}
|
|
110
|
+
fixupCompilerOptionsForModuleKind(compilerOptions, isEsm) {
|
|
111
|
+
const moduleResolution = this._ts.ModuleResolutionKind.Node10 ?? this._ts.ModuleResolutionKind.NodeJs;
|
|
125
112
|
if (!isEsm) {
|
|
126
|
-
return
|
|
113
|
+
return {
|
|
114
|
+
...compilerOptions,
|
|
115
|
+
module: this._ts.ModuleKind.CommonJS,
|
|
116
|
+
moduleResolution,
|
|
127
117
|
/**
|
|
128
118
|
* This option is only supported in `Node16`/`NodeNext` and `Bundler` module, see https://www.typescriptlang.org/tsconfig/#customConditions
|
|
129
119
|
*/
|
|
130
|
-
customConditions: undefined
|
|
120
|
+
customConditions: undefined,
|
|
121
|
+
};
|
|
131
122
|
}
|
|
132
|
-
|
|
133
|
-
|
|
123
|
+
let moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext;
|
|
124
|
+
let esModuleInterop = compilerOptions.esModuleInterop;
|
|
134
125
|
if ((0, transpile_module_1.isModernNodeModuleKind)(moduleKind)) {
|
|
135
126
|
esModuleInterop = true;
|
|
136
127
|
moduleKind = this._ts.ModuleKind.ESNext;
|
|
137
128
|
}
|
|
138
|
-
return
|
|
129
|
+
return {
|
|
130
|
+
...compilerOptions,
|
|
131
|
+
module: moduleKind,
|
|
132
|
+
esModuleInterop,
|
|
133
|
+
moduleResolution,
|
|
139
134
|
/**
|
|
140
135
|
* This option is only supported in `Node16`/`NodeNext` and `Bundler` module, see https://www.typescriptlang.org/tsconfig/#customConditions
|
|
141
136
|
*/
|
|
142
|
-
customConditions: undefined
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
137
|
+
customConditions: undefined,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
getCompiledOutput(fileContent, fileName, options) {
|
|
141
|
+
const isEsmMode = this.configSet.useESM && options.supportsStaticESM;
|
|
147
142
|
this._compilerOptions = this.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode);
|
|
148
143
|
if (!this._initialCompilerOptions.isolatedModules && (0, transpile_module_1.isModernNodeModuleKind)(this._initialCompilerOptions.module)) {
|
|
149
144
|
this._logger.warn("Using hybrid module kind (Node16/18/Next) is only supported in \"isolatedModules: true\". Please set \"isolatedModules: true\" in your tsconfig.json." /* Helps.UsingModernNodeResolution */);
|
|
150
145
|
}
|
|
151
|
-
|
|
152
|
-
|
|
146
|
+
const moduleKind = this._initialCompilerOptions.module;
|
|
147
|
+
const currentModuleKind = this._compilerOptions.module;
|
|
153
148
|
if (this._languageService) {
|
|
154
149
|
if (constants_1.JS_JSX_REGEX.test(fileName) && !this._compilerOptions.allowJs) {
|
|
155
150
|
this._logger.warn({ fileName: fileName }, (0, messages_1.interpolate)("Got a `.js` file to compile while `allowJs` option is not set to `true` (file: {{path}}). To fix this:\n - if you want TypeScript to process JS files, set `allowJs` to `true` in your TypeScript config (usually tsconfig.json)\n - if you do not want TypeScript to process your `.js` files, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match `.js` files anymore" /* Errors.GotJsFileButAllowJsFalse */, { path: fileName }));
|
|
@@ -157,36 +152,31 @@ var TsCompiler = /** @class */ (function () {
|
|
|
157
152
|
code: fileContent,
|
|
158
153
|
};
|
|
159
154
|
}
|
|
160
|
-
this._logger.debug({ fileName
|
|
155
|
+
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service');
|
|
161
156
|
// Must set memory cache before attempting to compile
|
|
162
157
|
this._updateMemoryCache(fileContent, fileName, currentModuleKind === moduleKind);
|
|
163
|
-
|
|
164
|
-
|
|
158
|
+
const output = this._languageService.getEmitOutput(fileName);
|
|
159
|
+
const diagnostics = this.getDiagnostics(fileName);
|
|
165
160
|
if (!isEsmMode && diagnostics.length) {
|
|
166
161
|
this.configSet.raiseDiagnostics(diagnostics, fileName, this._logger);
|
|
167
162
|
if (options.watchMode) {
|
|
168
|
-
this._logger.debug({ fileName
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
this.
|
|
180
|
-
|
|
163
|
+
this._logger.debug({ fileName }, '_doTypeChecking(): starting watch mode computing diagnostics');
|
|
164
|
+
for (const entry of options.depGraphs.entries()) {
|
|
165
|
+
const normalizedModuleNames = entry[1].resolvedModuleNames.map((moduleName) => (0, path_1.normalize)(moduleName));
|
|
166
|
+
const fileToReTypeCheck = entry[0];
|
|
167
|
+
if (normalizedModuleNames.includes(fileName) && this.configSet.shouldReportDiagnostics(fileToReTypeCheck)) {
|
|
168
|
+
this._logger.debug({ fileToReTypeCheck }, '_doTypeChecking(): computing diagnostics using language service');
|
|
169
|
+
this._updateMemoryCache(this._getFileContentFromCache(fileToReTypeCheck), fileToReTypeCheck);
|
|
170
|
+
const importedModulesDiagnostics = [
|
|
171
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
172
|
+
...this._languageService.getSemanticDiagnostics(fileToReTypeCheck),
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
174
|
+
...this._languageService.getSyntacticDiagnostics(fileToReTypeCheck),
|
|
175
|
+
];
|
|
176
|
+
// will raise or just warn diagnostics depending on config
|
|
177
|
+
this.configSet.raiseDiagnostics(importedModulesDiagnostics, fileName, this._logger);
|
|
181
178
|
}
|
|
182
179
|
}
|
|
183
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
184
|
-
finally {
|
|
185
|
-
try {
|
|
186
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
187
|
-
}
|
|
188
|
-
finally { if (e_1) throw e_1.error; }
|
|
189
|
-
}
|
|
190
180
|
}
|
|
191
181
|
}
|
|
192
182
|
if (output.emitSkipped) {
|
|
@@ -206,21 +196,21 @@ var TsCompiler = /** @class */ (function () {
|
|
|
206
196
|
file: (0, path_1.basename)(fileName),
|
|
207
197
|
}));
|
|
208
198
|
}
|
|
209
|
-
|
|
199
|
+
const { outputFiles } = output;
|
|
210
200
|
return this._compilerOptions.sourceMap
|
|
211
201
|
? {
|
|
212
202
|
code: (0, compiler_utils_1.updateOutput)(outputFiles[1].text, fileName, outputFiles[0].text),
|
|
213
|
-
diagnostics
|
|
203
|
+
diagnostics,
|
|
214
204
|
}
|
|
215
205
|
: {
|
|
216
206
|
code: (0, compiler_utils_1.updateOutput)(outputFiles[0].text, fileName),
|
|
217
|
-
diagnostics
|
|
207
|
+
diagnostics,
|
|
218
208
|
};
|
|
219
209
|
}
|
|
220
210
|
else {
|
|
221
|
-
this._logger.debug({ fileName
|
|
211
|
+
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling as isolated module');
|
|
222
212
|
assertCompilerOptionsWithJestTransformMode(this._initialCompilerOptions, isEsmMode, this._logger);
|
|
223
|
-
|
|
213
|
+
const result = this._transpileOutput(fileContent, fileName);
|
|
224
214
|
if (result.diagnostics && this.configSet.shouldReportDiagnostics(fileName)) {
|
|
225
215
|
this.configSet.raiseDiagnostics(result.diagnostics, fileName, this._logger);
|
|
226
216
|
}
|
|
@@ -228,9 +218,8 @@ var TsCompiler = /** @class */ (function () {
|
|
|
228
218
|
code: (0, compiler_utils_1.updateOutput)(result.outputText, fileName, result.sourceMapText),
|
|
229
219
|
};
|
|
230
220
|
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
var _this = this;
|
|
221
|
+
}
|
|
222
|
+
_transpileOutput(fileContent, fileName) {
|
|
234
223
|
/**
|
|
235
224
|
* @deprecated
|
|
236
225
|
*
|
|
@@ -238,57 +227,48 @@ var TsCompiler = /** @class */ (function () {
|
|
|
238
227
|
*/
|
|
239
228
|
if (!(0, transpile_module_1.isModernNodeModuleKind)(this._initialCompilerOptions.module)) {
|
|
240
229
|
return this._ts.transpileModule(fileContent, {
|
|
241
|
-
fileName
|
|
230
|
+
fileName,
|
|
242
231
|
transformers: this._makeTransformers(this.configSet.resolvedTransformers),
|
|
243
232
|
compilerOptions: this._compilerOptions,
|
|
244
233
|
reportDiagnostics: this.configSet.shouldReportDiagnostics(fileName),
|
|
245
234
|
});
|
|
246
235
|
}
|
|
247
236
|
return (0, transpile_module_1.tsTranspileModule)(fileContent, {
|
|
248
|
-
fileName
|
|
249
|
-
transformers:
|
|
250
|
-
|
|
251
|
-
return
|
|
237
|
+
fileName,
|
|
238
|
+
transformers: (program) => {
|
|
239
|
+
this.program = program;
|
|
240
|
+
return this._makeTransformers(this.configSet.resolvedTransformers);
|
|
252
241
|
},
|
|
253
242
|
compilerOptions: this._initialCompilerOptions,
|
|
254
243
|
reportDiagnostics: fileName ? this.configSet.shouldReportDiagnostics(fileName) : false,
|
|
255
244
|
});
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
var _this = this;
|
|
245
|
+
}
|
|
246
|
+
_makeTransformers(customTransformers) {
|
|
259
247
|
return {
|
|
260
|
-
before: customTransformers.before.map(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
after: customTransformers.after.map(function (afterTransformer) {
|
|
264
|
-
return afterTransformer.factory(_this, afterTransformer.options);
|
|
265
|
-
}),
|
|
266
|
-
afterDeclarations: customTransformers.afterDeclarations.map(function (afterDeclarations) {
|
|
267
|
-
return afterDeclarations.factory(_this, afterDeclarations.options);
|
|
268
|
-
}),
|
|
248
|
+
before: customTransformers.before.map((beforeTransformer) => beforeTransformer.factory(this, beforeTransformer.options)),
|
|
249
|
+
after: customTransformers.after.map((afterTransformer) => afterTransformer.factory(this, afterTransformer.options)),
|
|
250
|
+
afterDeclarations: customTransformers.afterDeclarations.map((afterDeclarations) => afterDeclarations.factory(this, afterDeclarations.options)),
|
|
269
251
|
};
|
|
270
|
-
}
|
|
252
|
+
}
|
|
271
253
|
/**
|
|
272
254
|
* @internal
|
|
273
255
|
*/
|
|
274
|
-
|
|
275
|
-
var _this = this;
|
|
276
|
-
var _a;
|
|
256
|
+
_createLanguageService() {
|
|
277
257
|
// Initialize memory cache for typescript compiler
|
|
278
258
|
this._parsedTsConfig.fileNames
|
|
279
|
-
.filter(
|
|
259
|
+
.filter((fileName) => constants_1.TS_TSX_REGEX.test(fileName) && !this.configSet.isTestFile(fileName))
|
|
280
260
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
281
|
-
.forEach(
|
|
261
|
+
.forEach((fileName) => this._fileVersionCache.set(fileName, 0));
|
|
282
262
|
/* istanbul ignore next */
|
|
283
|
-
|
|
284
|
-
useCaseSensitiveFileNames:
|
|
285
|
-
getProjectVersion:
|
|
263
|
+
const serviceHost = {
|
|
264
|
+
useCaseSensitiveFileNames: () => this._ts.sys.useCaseSensitiveFileNames,
|
|
265
|
+
getProjectVersion: () => String(this._projectVersion),
|
|
286
266
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
287
|
-
getScriptFileNames:
|
|
288
|
-
getScriptVersion:
|
|
289
|
-
|
|
267
|
+
getScriptFileNames: () => [...this._fileVersionCache.keys()],
|
|
268
|
+
getScriptVersion: (fileName) => {
|
|
269
|
+
const normalizedFileName = (0, path_1.normalize)(fileName);
|
|
290
270
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
291
|
-
|
|
271
|
+
const version = this._fileVersionCache.get(normalizedFileName);
|
|
292
272
|
// We need to return `undefined` and not a string here because TypeScript will use
|
|
293
273
|
// `getScriptVersion` and compare against their own version - which can be `undefined`.
|
|
294
274
|
// If we don't return `undefined` it results in `undefined === "undefined"` and run
|
|
@@ -297,91 +277,90 @@ var TsCompiler = /** @class */ (function () {
|
|
|
297
277
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
298
278
|
return version === undefined ? undefined : String(version);
|
|
299
279
|
},
|
|
300
|
-
getScriptSnapshot:
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
_this._logger.trace({ normalizedFileName: normalizedFileName, cacheHit: hit }, 'getScriptSnapshot():', 'cache', hit ? 'hit' : 'miss');
|
|
280
|
+
getScriptSnapshot: (fileName) => {
|
|
281
|
+
const normalizedFileName = (0, path_1.normalize)(fileName);
|
|
282
|
+
const hit = this._isFileInCache(normalizedFileName);
|
|
283
|
+
this._logger.trace({ normalizedFileName, cacheHit: hit }, 'getScriptSnapshot():', 'cache', hit ? 'hit' : 'miss');
|
|
305
284
|
// Read file content from either memory cache or Jest runtime cache or fallback to file system read
|
|
306
285
|
if (!hit) {
|
|
307
|
-
|
|
286
|
+
const fileContent =
|
|
308
287
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
309
|
-
|
|
288
|
+
this._fileContentCache.get(normalizedFileName) ??
|
|
289
|
+
this._runtimeCacheFS.get(normalizedFileName) ??
|
|
290
|
+
this._cachedReadFile?.(normalizedFileName) ??
|
|
291
|
+
undefined;
|
|
310
292
|
if (fileContent !== undefined) {
|
|
311
293
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
312
|
-
|
|
294
|
+
this._fileContentCache.set(normalizedFileName, fileContent);
|
|
313
295
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
314
|
-
|
|
296
|
+
this._fileVersionCache.set(normalizedFileName, 1);
|
|
315
297
|
}
|
|
316
298
|
}
|
|
317
299
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
318
|
-
|
|
300
|
+
const contents = this._fileContentCache.get(normalizedFileName);
|
|
319
301
|
if (contents === undefined)
|
|
320
302
|
return;
|
|
321
|
-
return
|
|
303
|
+
return this._ts.ScriptSnapshot.fromString(contents);
|
|
322
304
|
},
|
|
323
305
|
fileExists: (0, lodash_memoize_1.default)(this._ts.sys.fileExists),
|
|
324
|
-
readFile:
|
|
306
|
+
readFile: this._cachedReadFile ?? this._ts.sys.readFile,
|
|
325
307
|
readDirectory: (0, lodash_memoize_1.default)(this._ts.sys.readDirectory),
|
|
326
308
|
getDirectories: (0, lodash_memoize_1.default)(this._ts.sys.getDirectories),
|
|
327
309
|
directoryExists: (0, lodash_memoize_1.default)(this._ts.sys.directoryExists),
|
|
328
310
|
realpath: this._ts.sys.realpath && (0, lodash_memoize_1.default)(this._ts.sys.realpath),
|
|
329
|
-
getNewLine:
|
|
330
|
-
getCurrentDirectory:
|
|
331
|
-
getCompilationSettings:
|
|
332
|
-
getDefaultLibFileName:
|
|
333
|
-
getCustomTransformers:
|
|
334
|
-
resolveModuleNames:
|
|
335
|
-
return moduleNames.map(function (moduleName) { return _this._resolveModuleName(moduleName, containingFile).resolvedModule; });
|
|
336
|
-
},
|
|
311
|
+
getNewLine: () => constants_1.LINE_FEED,
|
|
312
|
+
getCurrentDirectory: () => this.configSet.cwd,
|
|
313
|
+
getCompilationSettings: () => this._compilerOptions,
|
|
314
|
+
getDefaultLibFileName: () => this._ts.getDefaultLibFilePath(this._compilerOptions),
|
|
315
|
+
getCustomTransformers: () => this._makeTransformers(this.configSet.resolvedTransformers),
|
|
316
|
+
resolveModuleNames: (moduleNames, containingFile) => moduleNames.map((moduleName) => this._resolveModuleName(moduleName, containingFile).resolvedModule),
|
|
337
317
|
};
|
|
338
318
|
this._logger.debug('created language service');
|
|
339
319
|
this._languageService = this._ts.createLanguageService(serviceHost, this._ts.createDocumentRegistry(this._ts.sys.useCaseSensitiveFileNames, this.configSet.cwd));
|
|
340
320
|
this.program = this._languageService.getProgram();
|
|
341
|
-
}
|
|
321
|
+
}
|
|
342
322
|
/**
|
|
343
323
|
* @internal
|
|
344
324
|
*/
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
325
|
+
_getFileContentFromCache(filePath) {
|
|
326
|
+
const normalizedFilePath = (0, path_1.normalize)(filePath);
|
|
327
|
+
let resolvedFileContent = this._runtimeCacheFS.get(normalizedFilePath);
|
|
348
328
|
if (!resolvedFileContent) {
|
|
349
329
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
350
330
|
resolvedFileContent = this._moduleResolutionHost.readFile(normalizedFilePath);
|
|
351
331
|
this._runtimeCacheFS.set(normalizedFilePath, resolvedFileContent);
|
|
352
332
|
}
|
|
353
333
|
return resolvedFileContent;
|
|
354
|
-
}
|
|
334
|
+
}
|
|
355
335
|
/**
|
|
356
336
|
* @internal
|
|
357
337
|
*/
|
|
358
|
-
|
|
359
|
-
var _this = this;
|
|
338
|
+
_getImportedModulePaths(resolvedFileContent, containingFile) {
|
|
360
339
|
return this._ts
|
|
361
340
|
.preProcessFile(resolvedFileContent, true, true)
|
|
362
|
-
.importedFiles.map(
|
|
363
|
-
|
|
341
|
+
.importedFiles.map((importedFile) => {
|
|
342
|
+
const { resolvedModule } = this._resolveModuleName(importedFile.fileName, containingFile);
|
|
364
343
|
/* istanbul ignore next already covered */
|
|
365
|
-
|
|
344
|
+
const resolvedFileName = resolvedModule?.resolvedFileName;
|
|
366
345
|
/* istanbul ignore next already covered */
|
|
367
|
-
return resolvedFileName && !
|
|
346
|
+
return resolvedFileName && !resolvedModule?.isExternalLibraryImport ? resolvedFileName : '';
|
|
368
347
|
})
|
|
369
|
-
.filter(
|
|
370
|
-
}
|
|
348
|
+
.filter((resolveFileName) => !!resolveFileName);
|
|
349
|
+
}
|
|
371
350
|
/**
|
|
372
351
|
* @internal
|
|
373
352
|
*/
|
|
374
|
-
|
|
353
|
+
_resolveModuleName(moduleNameToResolve, containingFile) {
|
|
375
354
|
return this._ts.resolveModuleName(moduleNameToResolve, containingFile, this._compilerOptions,
|
|
376
355
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
377
356
|
this._moduleResolutionHost,
|
|
378
357
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
379
358
|
this._moduleResolutionCache);
|
|
380
|
-
}
|
|
359
|
+
}
|
|
381
360
|
/**
|
|
382
361
|
* @internal
|
|
383
362
|
*/
|
|
384
|
-
|
|
363
|
+
_isFileInCache(fileName) {
|
|
385
364
|
return (
|
|
386
365
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
387
366
|
this._fileContentCache.has(fileName) &&
|
|
@@ -389,15 +368,14 @@ var TsCompiler = /** @class */ (function () {
|
|
|
389
368
|
this._fileVersionCache.has(fileName) &&
|
|
390
369
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
391
370
|
this._fileVersionCache.get(fileName) !== 0);
|
|
392
|
-
}
|
|
371
|
+
}
|
|
393
372
|
/**
|
|
394
373
|
* @internal
|
|
395
374
|
*/
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
var hit = this._isFileInCache(fileName);
|
|
375
|
+
_updateMemoryCache(contents, fileName, isModuleKindTheSame = true) {
|
|
376
|
+
this._logger.debug({ fileName }, 'updateMemoryCache: update memory cache for language service');
|
|
377
|
+
let shouldIncrementProjectVersion = false;
|
|
378
|
+
const hit = this._isFileInCache(fileName);
|
|
401
379
|
if (!hit) {
|
|
402
380
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
403
381
|
this._fileVersionCache.set(fileName, 1);
|
|
@@ -405,9 +383,9 @@ var TsCompiler = /** @class */ (function () {
|
|
|
405
383
|
}
|
|
406
384
|
else {
|
|
407
385
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
408
|
-
|
|
386
|
+
const prevVersion = this._fileVersionCache.get(fileName);
|
|
409
387
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
410
|
-
|
|
388
|
+
const previousContents = this._fileContentCache.get(fileName);
|
|
411
389
|
// Avoid incrementing cache when nothing has changed.
|
|
412
390
|
if (previousContents !== contents) {
|
|
413
391
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
@@ -426,19 +404,22 @@ var TsCompiler = /** @class */ (function () {
|
|
|
426
404
|
}
|
|
427
405
|
if (shouldIncrementProjectVersion)
|
|
428
406
|
this._projectVersion++;
|
|
429
|
-
}
|
|
407
|
+
}
|
|
430
408
|
/**
|
|
431
409
|
* @internal
|
|
432
410
|
*/
|
|
433
|
-
|
|
434
|
-
|
|
411
|
+
getDiagnostics(fileName) {
|
|
412
|
+
const diagnostics = [];
|
|
435
413
|
if (this.configSet.shouldReportDiagnostics(fileName)) {
|
|
436
|
-
this._logger.debug({ fileName
|
|
414
|
+
this._logger.debug({ fileName }, '_doTypeChecking(): computing diagnostics using language service');
|
|
437
415
|
// Get the relevant diagnostics - this is 3x faster than `getPreEmitDiagnostics`.
|
|
438
|
-
diagnostics.push
|
|
416
|
+
diagnostics.push(
|
|
417
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
418
|
+
...this._languageService.getSemanticDiagnostics(fileName),
|
|
419
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
420
|
+
...this._languageService.getSyntacticDiagnostics(fileName));
|
|
439
421
|
}
|
|
440
422
|
return diagnostics;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
}());
|
|
423
|
+
}
|
|
424
|
+
}
|
|
444
425
|
exports.TsCompiler = TsCompiler;
|