-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
script_transformer.js
514 lines (455 loc) · 14.6 KB
/
script_transformer.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Glob, Path, ProjectConfig} from 'types/Config';
import type {
Transformer,
TransformedSource,
TransformResult,
} from 'types/Transform';
import type {ErrorWithCode} from 'types/Errors';
import crypto from 'crypto';
import path from 'path';
import vm from 'vm';
import {createDirectory} from 'jest-util';
import fs from 'graceful-fs';
import {transform as babelTransform} from 'babel-core';
import babelPluginIstanbul from 'babel-plugin-istanbul';
import convertSourceMap from 'convert-source-map';
import HasteMap from 'jest-haste-map';
import stableStringify from 'fast-json-stable-stringify';
import slash from 'slash';
import {version as VERSION} from '../package.json';
import shouldInstrument from './should_instrument';
import writeFileAtomic from 'write-file-atomic';
import {sync as realpath} from 'realpath-native';
import {enhanceUnexpectedTokenMessage} from './helpers';
export type Options = {|
collectCoverage: boolean,
collectCoverageFrom: Array<Glob>,
collectCoverageOnlyFrom: ?{[key: string]: boolean, __proto__: null},
isCoreModule?: boolean,
isInternalModule?: boolean,
|};
const cache: Map<string, TransformResult> = new Map();
const configToJsonMap = new Map();
// Cache regular expressions to test whether the file needs to be preprocessed
const ignoreCache: WeakMap<ProjectConfig, ?RegExp> = new WeakMap();
// To reset the cache for specific changesets (rather than package version).
const CACHE_VERSION = '1';
export default class ScriptTransformer {
static EVAL_RESULT_VARIABLE: string;
_config: ProjectConfig;
_transformCache: Map<Path, ?Transformer>;
constructor(config: ProjectConfig) {
this._config = config;
this._transformCache = new Map();
}
_getCacheKey(fileData: string, filename: Path, instrument: boolean): string {
if (!configToJsonMap.has(this._config)) {
// We only need this set of config options that can likely influence
// cached output instead of all config options.
configToJsonMap.set(this._config, stableStringify(this._config));
}
const configString = configToJsonMap.get(this._config) || '';
const transformer = this._getTransformer(filename);
if (transformer && typeof transformer.getCacheKey === 'function') {
return crypto
.createHash('md5')
.update(
transformer.getCacheKey(fileData, filename, configString, {
instrument,
rootDir: this._config.rootDir,
}),
)
.update(CACHE_VERSION)
.digest('hex');
} else {
return crypto
.createHash('md5')
.update(fileData)
.update(configString)
.update(instrument ? 'instrument' : '')
.update(CACHE_VERSION)
.digest('hex');
}
}
_getFileCachePath(
filename: Path,
content: string,
instrument: boolean,
): Path {
const baseCacheDir = HasteMap.getCacheFilePath(
this._config.cacheDirectory,
'jest-transform-cache-' + this._config.name,
VERSION,
);
const cacheKey = this._getCacheKey(content, filename, instrument);
// Create sub folders based on the cacheKey to avoid creating one
// directory with many files.
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
const cachePath = slash(
path.join(
cacheDir,
path.basename(filename, path.extname(filename)) + '_' + cacheKey,
),
);
createDirectory(cacheDir);
return cachePath;
}
_getTransformPath(filename: Path) {
for (let i = 0; i < this._config.transform.length; i++) {
if (new RegExp(this._config.transform[i][0]).test(filename)) {
return this._config.transform[i][1];
}
}
return null;
}
_getTransformer(filename: Path) {
let transform: ?Transformer;
if (!this._config.transform || !this._config.transform.length) {
return null;
}
const transformPath = this._getTransformPath(filename);
if (transformPath) {
const transformer = this._transformCache.get(transformPath);
if (transformer != null) {
return transformer;
}
// $FlowFixMe
transform = (require(transformPath): Transformer);
if (typeof transform.createTransformer === 'function') {
transform = transform.createTransformer();
}
if (typeof transform.process !== 'function') {
throw new TypeError(
'Jest: a transform must export a `process` function.',
);
}
this._transformCache.set(transformPath, transform);
}
return transform;
}
_instrumentFile(filename: Path, content: string): string {
return babelTransform(content, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
filename,
plugins: [
[
babelPluginIstanbul,
{
compact: false,
// files outside `cwd` will not be instrumented
cwd: this._config.rootDir,
exclude: [],
useInlineSourceMaps: false,
},
],
],
}).code;
}
_getRealPath(filepath: Path): Path {
try {
return realpath(filepath) || filepath;
} catch (err) {
return filepath;
}
}
transformSource(filepath: Path, content: string, instrument: boolean) {
const filename = this._getRealPath(filepath);
const transform = this._getTransformer(filename);
const cacheFilePath = this._getFileCachePath(filename, content, instrument);
let sourceMapPath = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
const shouldCallTransform =
transform && shouldTransform(filename, this._config);
// That means that the transform has a custom instrumentation
// logic and will handle it based on `config.collectCoverage` option
const transformWillInstrument =
shouldCallTransform && transform && transform.canInstrument;
// If we handle the coverage instrumentation, we should try to map code
// coverage against original source with any provided source map
const mapCoverage = instrument && !transformWillInstrument;
if (code) {
// This is broken: we return the code, and a path for the source map
// directly from the cache. But, nothing ensures the source map actually
// matches that source code. They could have gotten out-of-sync in case
// two separate processes write concurrently to the same cache files.
return {
code,
mapCoverage,
sourceMapPath,
};
}
let transformed: TransformedSource = {
code: content,
map: null,
};
if (transform && shouldCallTransform) {
const processed = transform.process(content, filename, this._config, {
instrument,
});
if (typeof processed === 'string') {
transformed.code = processed;
} else if (processed != null && typeof processed.code === 'string') {
transformed = processed;
} else {
throw new TypeError(
"Jest: a transform's `process` function must return a string, " +
'or an object with `code` key containing this string.',
);
}
}
if (!transformed.map) {
//Could be a potential freeze here.
//See: https://github.com/facebook/jest/pull/5177#discussion_r158883570
const inlineSourceMap = convertSourceMap.fromSource(transformed.code);
if (inlineSourceMap) {
transformed.map = inlineSourceMap.toJSON();
}
}
if (!transformWillInstrument && instrument) {
code = this._instrumentFile(filename, transformed.code);
} else {
code = transformed.code;
}
if (transformed.map) {
const sourceMapContent =
typeof transformed.map === 'string'
? transformed.map
: JSON.stringify(transformed.map);
writeCacheFile(sourceMapPath, sourceMapContent);
} else {
sourceMapPath = null;
}
writeCodeCacheFile(cacheFilePath, code);
return {
code,
mapCoverage,
sourceMapPath,
};
}
_transformAndBuildScript(
filename: Path,
options: ?Options,
instrument: boolean,
fileSource?: string,
): TransformResult {
const isInternalModule = !!(options && options.isInternalModule);
const isCoreModule = !!(options && options.isCoreModule);
const content = stripShebang(
fileSource || fs.readFileSync(filename, 'utf8'),
);
let wrappedCode: string;
let sourceMapPath: ?string = null;
let mapCoverage = false;
const willTransform =
!isInternalModule &&
!isCoreModule &&
(shouldTransform(filename, this._config) || instrument);
try {
if (willTransform) {
const transformedSource = this.transformSource(
filename,
content,
instrument,
);
wrappedCode = wrap(transformedSource.code);
sourceMapPath = transformedSource.sourceMapPath;
mapCoverage = transformedSource.mapCoverage;
} else {
wrappedCode = wrap(content);
}
return {
mapCoverage,
script: new vm.Script(wrappedCode, {
displayErrors: true,
filename: isCoreModule ? 'jest-nodejs-core-' + filename : filename,
}),
sourceMapPath,
};
} catch (e) {
if (e.codeFrame) {
e.stack = e.codeFrame;
}
if (
e instanceof SyntaxError &&
e.message.includes('Unexpected token') &&
!e.message.includes(' expected')
) {
throw enhanceUnexpectedTokenMessage(e);
}
throw e;
}
}
transform(
filename: Path,
options: Options,
fileSource?: string,
): TransformResult {
let scriptCacheKey = null;
let instrument = false;
let result = '';
if (!options.isCoreModule) {
instrument = shouldInstrument(filename, options, this._config);
scriptCacheKey = getScriptCacheKey(filename, instrument);
result = cache.get(scriptCacheKey);
}
if (result) {
return result;
}
result = this._transformAndBuildScript(
filename,
options,
instrument,
fileSource,
);
if (scriptCacheKey) {
cache.set(scriptCacheKey, result);
}
return result;
}
}
const removeFile = (path: Path) => {
try {
fs.unlinkSync(path);
} catch (e) {}
};
const stripShebang = content => {
// If the file data starts with a shebang remove it. Leaves the empty line
// to keep stack trace line numbers correct.
if (content.startsWith('#!')) {
return content.replace(/^#!.*/, '');
} else {
return content;
}
};
/**
* This is like `writeCacheFile` but with an additional sanity checksum. We
* cannot use the same technique for source maps because we expose source map
* cache file paths directly to callsites, with the expectation they can read
* it right away. This is not a great system, because source map cache file
* could get corrupted, out-of-sync, etc.
*/
function writeCodeCacheFile(cachePath: Path, code: string) {
const checksum = crypto
.createHash('md5')
.update(code)
.digest('hex');
writeCacheFile(cachePath, checksum + '\n' + code);
}
/**
* Read counterpart of `writeCodeCacheFile`. We verify that the content of the
* file matches the checksum, in case some kind of corruption happened. This
* could happen if an older version of `jest-runtime` writes non-atomically to
* the same cache, for example.
*/
function readCodeCacheFile(cachePath: Path): ?string {
const content = readCacheFile(cachePath);
if (content == null) {
return null;
}
const code = content.substr(33);
const checksum = crypto
.createHash('md5')
.update(code)
.digest('hex');
if (checksum === content.substr(0, 32)) {
return code;
}
return null;
}
/**
* Writing to the cache atomically relies on 'rename' being atomic on most
* file systems. Doing atomic write reduces the risk of corruption by avoiding
* two processes to write to the same file at the same time. It also reduces
* the risk of reading a file that's being overwritten at the same time.
*/
const writeCacheFile = (cachePath: Path, fileData: string) => {
try {
writeFileAtomic.sync(cachePath, fileData, {encoding: 'utf8'});
} catch (e) {
if (cacheWriteErrorSafeToIgnore(e, cachePath)) {
return;
}
e.message =
'jest: failed to cache transform results in: ' +
cachePath +
'\nFailure message: ' +
e.message;
removeFile(cachePath);
throw e;
}
};
/**
* On Windows, renames are not atomic, leading to EPERM exceptions when two
* processes attempt to rename to the same target file at the same time.
* If the target file exists we can be reasonably sure another process has
* legitimately won a cache write race and ignore the error.
*/
const cacheWriteErrorSafeToIgnore = (e: ErrorWithCode, cachePath: Path) => {
return (
process.platform === 'win32' &&
e.code === 'EPERM' &&
fs.existsSync(cachePath)
);
};
const readCacheFile = (cachePath: Path): ?string => {
if (!fs.existsSync(cachePath)) {
return null;
}
let fileData;
try {
fileData = fs.readFileSync(cachePath, 'utf8');
} catch (e) {
e.message =
'jest: failed to read cache file: ' +
cachePath +
'\nFailure message: ' +
e.message;
removeFile(cachePath);
throw e;
}
if (fileData == null) {
// We must have somehow created the file but failed to write to it,
// let's delete it and retry.
removeFile(cachePath);
}
return fileData;
};
const getScriptCacheKey = (filename, instrument: boolean) => {
const mtime = fs.statSync(filename).mtime;
return filename + '_' + mtime.getTime() + (instrument ? '_instrumented' : '');
};
const shouldTransform = (filename: Path, config: ProjectConfig): boolean => {
if (!ignoreCache.has(config)) {
if (
!config.transformIgnorePatterns ||
config.transformIgnorePatterns.length === 0
) {
ignoreCache.set(config, null);
} else {
ignoreCache.set(
config,
new RegExp(config.transformIgnorePatterns.join('|')),
);
}
}
const ignoreRegexp = ignoreCache.get(config);
const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false;
return !!config.transform && !!config.transform.length && !isIgnored;
};
const wrap = content =>
'({"' +
ScriptTransformer.EVAL_RESULT_VARIABLE +
'":function(module,exports,require,__dirname,__filename,global,jest){' +
content +
'\n}});';
ScriptTransformer.EVAL_RESULT_VARIABLE = 'Object.<anonymous>';