@jest/transform
Advanced tools
Comparing version 29.1.2 to 29.2.0
@@ -8,17 +8,12 @@ 'use strict'; | ||
exports.enhanceUnexpectedTokenMessage = enhanceUnexpectedTokenMessage; | ||
function _chalk() { | ||
const data = _interopRequireDefault(require('chalk')); | ||
_chalk = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -30,9 +25,8 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const DOT = ' \u2022 '; | ||
function handlePotentialSyntaxError(e) { | ||
if (e.codeFrame) { | ||
if (e.codeFrame != null) { | ||
e.stack = `${e.message}\n${e.codeFrame}`; | ||
} | ||
if ( | ||
@@ -45,6 +39,4 @@ // `instanceof` might come from the wrong context | ||
} | ||
return e; | ||
} | ||
function enhanceUnexpectedTokenMessage(e) { | ||
@@ -85,4 +77,4 @@ e.stack = `${_chalk().default.bold.red( | ||
${e.stack}`; | ||
${e.stack ?? ''}`.trimRight(); | ||
return e; | ||
} |
@@ -30,13 +30,9 @@ 'use strict'; | ||
}); | ||
var _ScriptTransformer = require('./ScriptTransformer'); | ||
var _shouldInstrument = _interopRequireDefault(require('./shouldInstrument')); | ||
var _enhanceUnexpectedTokenMessage = _interopRequireDefault( | ||
require('./enhanceUnexpectedTokenMessage') | ||
); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} |
@@ -11,27 +11,19 @@ 'use strict'; | ||
void 0; | ||
function _chalk() { | ||
const data = _interopRequireDefault(require('chalk')); | ||
_chalk = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _slash() { | ||
const data = _interopRequireDefault(require('slash')); | ||
_slash = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -43,2 +35,3 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const BULLET = '\u25cf '; | ||
@@ -55,3 +48,2 @@ const DOCUMENTATION_NOTE = ` ${_chalk().default.bold( | ||
`; | ||
const makeInvalidReturnValueError = transformPath => | ||
@@ -70,5 +62,3 @@ _chalk().default.red( | ||
); | ||
exports.makeInvalidReturnValueError = makeInvalidReturnValueError; | ||
const makeInvalidSourceMapWarning = (filename, transformPath) => | ||
@@ -84,5 +74,3 @@ _chalk().default.yellow( | ||
); | ||
exports.makeInvalidSourceMapWarning = makeInvalidSourceMapWarning; | ||
const makeInvalidSyncTransformerError = transformPath => | ||
@@ -99,5 +87,3 @@ _chalk().default.red( | ||
); | ||
exports.makeInvalidSyncTransformerError = makeInvalidSyncTransformerError; | ||
const makeInvalidTransformerError = transformPath => | ||
@@ -114,3 +100,2 @@ _chalk().default.red( | ||
); | ||
exports.makeInvalidTransformerError = makeInvalidTransformerError; |
@@ -8,135 +8,94 @@ 'use strict'; | ||
exports.createTranspilingRequire = createTranspilingRequire; | ||
function _crypto() { | ||
const data = require('crypto'); | ||
_crypto = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function path() { | ||
const data = _interopRequireWildcard(require('path')); | ||
path = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _core() { | ||
const data = require('@babel/core'); | ||
_core = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _babelPluginIstanbul() { | ||
const data = _interopRequireDefault(require('babel-plugin-istanbul')); | ||
_babelPluginIstanbul = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _convertSourceMap() { | ||
const data = require('convert-source-map'); | ||
_convertSourceMap = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _fastJsonStableStringify() { | ||
const data = _interopRequireDefault(require('fast-json-stable-stringify')); | ||
_fastJsonStableStringify = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function fs() { | ||
const data = _interopRequireWildcard(require('graceful-fs')); | ||
fs = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _pirates() { | ||
const data = require('pirates'); | ||
_pirates = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _slash() { | ||
const data = _interopRequireDefault(require('slash')); | ||
_slash = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _writeFileAtomic() { | ||
const data = require('write-file-atomic'); | ||
_writeFileAtomic = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _jestHasteMap() { | ||
const data = _interopRequireDefault(require('jest-haste-map')); | ||
_jestHasteMap = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _jestUtil() { | ||
const data = require('jest-util'); | ||
_jestUtil = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
var _enhanceUnexpectedTokenMessage = _interopRequireDefault( | ||
require('./enhanceUnexpectedTokenMessage') | ||
); | ||
var _runtimeErrorsAndWarnings = require('./runtimeErrorsAndWarnings'); | ||
var _shouldInstrument = _interopRequireDefault(require('./shouldInstrument')); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
function _getRequireWildcardCache(nodeInterop) { | ||
@@ -150,3 +109,2 @@ if (typeof WeakMap !== 'function') return null; | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
@@ -184,3 +142,2 @@ if (!nodeInterop && obj && obj.__esModule) { | ||
} | ||
/** | ||
@@ -192,13 +149,14 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
// @ts-expect-error: should just be `require.resolve`, but the tests mess that up | ||
// Use `require` to avoid TS rootDir | ||
const {version: VERSION} = require('../package.json'); | ||
// This data structure is used to avoid recalculating some data every time that | ||
// we need to transform a file. Since ScriptTransformer is instantiated for each | ||
// file we need to keep this object in the local scope of this module. | ||
const projectCaches = new Map(); // To reset the cache for specific changesets (rather than package version). | ||
const projectCaches = new Map(); | ||
// To reset the cache for specific changesets (rather than package version). | ||
const CACHE_VERSION = '1'; | ||
async function waitForPromiseWithCleanup(promise, cleanup) { | ||
@@ -210,8 +168,8 @@ try { | ||
} | ||
} // type predicate | ||
} | ||
// type predicate | ||
function isTransformerFactory(t) { | ||
return typeof t.createTransformer === 'function'; | ||
} | ||
class ScriptTransformer { | ||
@@ -221,3 +179,2 @@ _cache; | ||
_transformsAreLoaded = false; | ||
constructor(_config, _cacheFS) { | ||
@@ -228,3 +185,2 @@ this._config = _config; | ||
let projectCache = projectCaches.get(configString); | ||
if (!projectCache) { | ||
@@ -239,6 +195,4 @@ projectCache = { | ||
} | ||
this._cache = projectCache; | ||
} | ||
_buildCacheKeyFromFileInfo( | ||
@@ -250,3 +204,3 @@ fileData, | ||
) { | ||
if (transformerCacheKey) { | ||
if (transformerCacheKey != null) { | ||
return (0, _crypto().createHash)('sha256') | ||
@@ -258,3 +212,2 @@ .update(transformerCacheKey) | ||
} | ||
return (0, _crypto().createHash)('sha256') | ||
@@ -269,7 +222,6 @@ .update(fileData) | ||
} | ||
_getCacheKey(fileData, filename, options) { | ||
const configString = this._cache.configString; | ||
const {transformer, transformerConfig = {}} = | ||
this._getTransformer(filename) || {}; | ||
this._getTransformer(filename) ?? {}; | ||
let transformerCacheKey = undefined; | ||
@@ -283,3 +235,2 @@ const transformOptions = { | ||
}; | ||
if (typeof transformer?.getCacheKey === 'function') { | ||
@@ -292,3 +243,2 @@ transformerCacheKey = transformer.getCacheKey( | ||
} | ||
return this._buildCacheKeyFromFileInfo( | ||
@@ -301,7 +251,6 @@ fileData, | ||
} | ||
async _getCacheKeyAsync(fileData, filename, options) { | ||
const configString = this._cache.configString; | ||
const {transformer, transformerConfig = {}} = | ||
this._getTransformer(filename) || {}; | ||
this._getTransformer(filename) ?? {}; | ||
let transformerCacheKey = undefined; | ||
@@ -315,7 +264,5 @@ const transformOptions = { | ||
}; | ||
if (transformer) { | ||
const getCacheKey = | ||
transformer.getCacheKeyAsync || transformer.getCacheKey; | ||
transformer.getCacheKeyAsync ?? transformer.getCacheKey; | ||
if (typeof getCacheKey === 'function') { | ||
@@ -329,3 +276,2 @@ transformerCacheKey = await getCacheKey( | ||
} | ||
return this._buildCacheKeyFromFileInfo( | ||
@@ -338,6 +284,4 @@ fileData, | ||
} | ||
_createFolderFromCacheKey(filename, cacheKey) { | ||
const HasteMapClass = _jestHasteMap().default.getStatic(this._config); | ||
const baseCacheDir = HasteMapClass.getCacheFilePath( | ||
@@ -347,5 +291,5 @@ this._config.cacheDirectory, | ||
VERSION | ||
); // Create sub folders based on the cacheKey to avoid creating one | ||
); | ||
// Create sub folders based on the cacheKey to avoid creating one | ||
// directory with many files. | ||
const cacheDir = path().join(baseCacheDir, cacheKey[0] + cacheKey[1]); | ||
@@ -361,9 +305,6 @@ const cacheFilenamePrefix = path() | ||
} | ||
_getFileCachePath(filename, content, options) { | ||
const cacheKey = this._getCacheKey(content, filename, options); | ||
return this._createFolderFromCacheKey(filename, cacheKey); | ||
} | ||
async _getFileCachePathAsync(filename, content, options) { | ||
@@ -373,10 +314,7 @@ const cacheKey = await this._getCacheKeyAsync(content, filename, options); | ||
} | ||
_getTransformPath(filename) { | ||
const transformRegExp = this._cache.transformRegExp; | ||
if (!transformRegExp) { | ||
return undefined; | ||
} | ||
for (let i = 0; i < transformRegExp.length; i++) { | ||
@@ -387,6 +325,4 @@ if (transformRegExp[i][0].test(filename)) { | ||
} | ||
return undefined; | ||
} | ||
async loadTransformers() { | ||
@@ -399,4 +335,3 @@ await Promise.all( | ||
); | ||
if (!transformer) { | ||
if (transformer == null) { | ||
throw new Error( | ||
@@ -408,7 +343,5 @@ (0, _runtimeErrorsAndWarnings.makeInvalidTransformerError)( | ||
} | ||
if (isTransformerFactory(transformer)) { | ||
transformer = transformer.createTransformer(transformerConfig); | ||
} | ||
if ( | ||
@@ -424,3 +357,2 @@ typeof transformer.process !== 'function' && | ||
} | ||
const res = { | ||
@@ -430,3 +362,2 @@ transformer, | ||
}; | ||
this._transformCache.set(transformPath, res); | ||
@@ -438,3 +369,2 @@ } | ||
} | ||
_getTransformer(filename) { | ||
@@ -446,19 +376,13 @@ if (!this._transformsAreLoaded) { | ||
} | ||
if (this._config.transform.length === 0) { | ||
return null; | ||
} | ||
const transformPath = this._getTransformPath(filename); | ||
if (!transformPath) { | ||
if (transformPath == null) { | ||
return null; | ||
} | ||
const cached = this._transformCache.get(transformPath); | ||
if (cached) { | ||
if (cached != null) { | ||
return cached; | ||
} | ||
throw new Error( | ||
@@ -468,3 +392,2 @@ `Jest was unable to load the transformer defined for ${filename}. This is a bug in Jest, please open up an issue` | ||
} | ||
_instrumentFile(filename, input, canMapToInput, options) { | ||
@@ -501,10 +424,7 @@ const inputCode = typeof input === 'string' ? input : input.code; | ||
}); | ||
if (result && result.code) { | ||
if (result?.code != null) { | ||
return result; | ||
} | ||
return input; | ||
} | ||
_buildTransformResult( | ||
@@ -524,3 +444,2 @@ filename, | ||
}; | ||
if (transformer && shouldCallTransform) { | ||
@@ -531,3 +450,2 @@ if (processed != null && typeof processed.code === 'string') { | ||
const transformPath = this._getTransformPath(filename); | ||
invariant(transformPath); | ||
@@ -541,4 +459,3 @@ throw new Error( | ||
} | ||
if (!transformed.map) { | ||
if (transformed.map == null || transformed.map === '') { | ||
try { | ||
@@ -550,3 +467,2 @@ //Could be a potential freeze here. | ||
); | ||
if (inlineSourceMap) { | ||
@@ -557,3 +473,2 @@ transformed.map = inlineSourceMap.toObject(); | ||
const transformPath = this._getTransformPath(filename); | ||
invariant(transformPath); | ||
@@ -567,12 +482,13 @@ console.warn( | ||
} | ||
} // That means that the transform has a custom instrumentation | ||
} | ||
// That means that the transform has a custom instrumentation | ||
// logic and will handle it based on `config.collectCoverage` option | ||
const transformWillInstrument = | ||
shouldCallTransform && transformer && transformer.canInstrument; // Apply instrumentation to the code if necessary, keeping the instrumented code and new map | ||
shouldCallTransform && transformer && transformer.canInstrument; | ||
// Apply instrumentation to the code if necessary, keeping the instrumented code and new map | ||
let map = transformed.map; | ||
let code; | ||
if (!transformWillInstrument && options.instrument) { | ||
if (transformWillInstrument !== true && options.instrument) { | ||
/** | ||
@@ -589,3 +505,2 @@ * We can map the original source code to the instrumented code ONLY if | ||
(transformer != null && map != null) || transformer == null; | ||
const instrumented = this._instrumentFile( | ||
@@ -597,3 +512,2 @@ filename, | ||
); | ||
code = | ||
@@ -605,4 +519,3 @@ typeof instrumented === 'string' ? instrumented : instrumented.code; | ||
} | ||
if (map) { | ||
if (map != null) { | ||
const sourceMapContent = | ||
@@ -615,3 +528,2 @@ typeof map === 'string' ? map : JSON.stringify(map); | ||
} | ||
writeCodeCacheFile(cacheFilePath, code); | ||
@@ -624,15 +536,11 @@ return { | ||
} | ||
transformSource(filepath, content, options) { | ||
const filename = (0, _jestUtil().tryRealpath)(filepath); | ||
const {transformer, transformerConfig = {}} = | ||
this._getTransformer(filename) || {}; | ||
this._getTransformer(filename) ?? {}; | ||
const cacheFilePath = this._getFileCachePath(filename, content, options); | ||
const sourceMapPath = `${cacheFilePath}.map`; // Ignore cache if `config.cache` is set (--no-cache) | ||
const sourceMapPath = `${cacheFilePath}.map`; | ||
// Ignore cache if `config.cache` is set (--no-cache) | ||
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null; | ||
if (code) { | ||
if (code != null) { | ||
// This is broken: we return the code, and a path for the source map | ||
@@ -648,6 +556,4 @@ // directly from the cache. But, nothing ensures the source map actually | ||
} | ||
let processed = null; | ||
let shouldCallTransform = false; | ||
if (transformer && this.shouldTransform(filename)) { | ||
@@ -664,3 +570,2 @@ shouldCallTransform = true; | ||
} | ||
return this._buildTransformResult( | ||
@@ -677,7 +582,6 @@ filename, | ||
} | ||
async transformSourceAsync(filepath, content, options) { | ||
const filename = (0, _jestUtil().tryRealpath)(filepath); | ||
const {transformer, transformerConfig = {}} = | ||
this._getTransformer(filename) || {}; | ||
this._getTransformer(filename) ?? {}; | ||
const cacheFilePath = await this._getFileCachePathAsync( | ||
@@ -688,7 +592,6 @@ filename, | ||
); | ||
const sourceMapPath = `${cacheFilePath}.map`; // Ignore cache if `config.cache` is set (--no-cache) | ||
const sourceMapPath = `${cacheFilePath}.map`; | ||
// Ignore cache if `config.cache` is set (--no-cache) | ||
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null; | ||
if (code) { | ||
if (code != null) { | ||
// This is broken: we return the code, and a path for the source map | ||
@@ -704,10 +607,9 @@ // directly from the cache. But, nothing ensures the source map actually | ||
} | ||
let processed = null; | ||
let shouldCallTransform = false; | ||
if (transformer && this.shouldTransform(filename)) { | ||
shouldCallTransform = true; | ||
const process = transformer.processAsync || transformer.process; // This is probably dead code since `_getTransformerAsync` already asserts this | ||
const process = transformer.processAsync ?? transformer.process; | ||
// This is probably dead code since `_getTransformerAsync` already asserts this | ||
invariant( | ||
@@ -725,3 +627,2 @@ typeof process === 'function', | ||
} | ||
return this._buildTransformResult( | ||
@@ -738,3 +639,2 @@ filename, | ||
} | ||
async _transformAndBuildScriptAsync( | ||
@@ -747,11 +647,7 @@ filename, | ||
const {isInternalModule} = options; | ||
let fileContent = fileSource ?? this._cacheFS.get(filename); | ||
if (!fileContent) { | ||
if (fileContent == null) { | ||
fileContent = fs().readFileSync(filename, 'utf8'); | ||
this._cacheFS.set(filename, fileContent); | ||
} | ||
const content = stripShebang(fileContent); | ||
@@ -761,5 +657,4 @@ let code = content; | ||
const willTransform = | ||
!isInternalModule && | ||
isInternalModule !== true && | ||
(transformOptions.instrument || this.shouldTransform(filename)); | ||
try { | ||
@@ -775,3 +670,2 @@ if (willTransform) { | ||
} | ||
return { | ||
@@ -783,17 +677,15 @@ code, | ||
} catch (e) { | ||
if (!(e instanceof Error)) { | ||
throw e; | ||
} | ||
throw (0, _enhanceUnexpectedTokenMessage.default)(e); | ||
} | ||
} | ||
_transformAndBuildScript(filename, options, transformOptions, fileSource) { | ||
const {isInternalModule} = options; | ||
let fileContent = fileSource ?? this._cacheFS.get(filename); | ||
if (!fileContent) { | ||
if (fileContent == null) { | ||
fileContent = fs().readFileSync(filename, 'utf8'); | ||
this._cacheFS.set(filename, fileContent); | ||
} | ||
const content = stripShebang(fileContent); | ||
@@ -803,5 +695,4 @@ let code = content; | ||
const willTransform = | ||
!isInternalModule && | ||
isInternalModule !== true && | ||
(transformOptions.instrument || this.shouldTransform(filename)); | ||
try { | ||
@@ -817,3 +708,2 @@ if (willTransform) { | ||
} | ||
return { | ||
@@ -825,6 +715,8 @@ code, | ||
} catch (e) { | ||
if (!(e instanceof Error)) { | ||
throw e; | ||
} | ||
throw (0, _enhanceUnexpectedTokenMessage.default)(e); | ||
} | ||
} | ||
async transformAsync(filename, options, fileSource) { | ||
@@ -835,23 +727,20 @@ const instrument = | ||
const scriptCacheKey = getScriptCacheKey(filename, instrument); | ||
let result = this._cache.transformedFiles.get(scriptCacheKey); | ||
if (result) { | ||
return result; | ||
} | ||
result = await this._transformAndBuildScriptAsync( | ||
filename, | ||
options, | ||
{...options, instrument}, | ||
{ | ||
...options, | ||
instrument | ||
}, | ||
fileSource | ||
); | ||
if (scriptCacheKey) { | ||
this._cache.transformedFiles.set(scriptCacheKey, result); | ||
} | ||
return result; | ||
} | ||
transform(filename, options, fileSource) { | ||
@@ -862,27 +751,24 @@ const instrument = | ||
const scriptCacheKey = getScriptCacheKey(filename, instrument); | ||
let result = this._cache.transformedFiles.get(scriptCacheKey); | ||
if (result) { | ||
return result; | ||
} | ||
result = this._transformAndBuildScript( | ||
filename, | ||
options, | ||
{...options, instrument}, | ||
{ | ||
...options, | ||
instrument | ||
}, | ||
fileSource | ||
); | ||
if (scriptCacheKey) { | ||
this._cache.transformedFiles.set(scriptCacheKey, result); | ||
} | ||
return result; | ||
} | ||
transformJson(filename, options, fileSource) { | ||
const {isInternalModule} = options; | ||
const willTransform = !isInternalModule && this.shouldTransform(filename); | ||
const willTransform = | ||
isInternalModule !== true && this.shouldTransform(filename); | ||
if (willTransform) { | ||
@@ -892,10 +778,11 @@ const {code: transformedJsonSource} = this.transformSource( | ||
fileSource, | ||
{...options, instrument: false} | ||
{ | ||
...options, | ||
instrument: false | ||
} | ||
); | ||
return transformedJsonSource; | ||
} | ||
return fileSource; | ||
} | ||
async requireAndTranspileModule( | ||
@@ -937,3 +824,2 @@ moduleName, | ||
} | ||
return this.shouldTransform(filename); | ||
@@ -943,3 +829,2 @@ } | ||
); | ||
try { | ||
@@ -950,3 +835,2 @@ const module = await (0, _jestUtil().requireOrImportModule)( | ||
); | ||
if (!callback) { | ||
@@ -956,11 +840,8 @@ revertHook(); | ||
} | ||
const cbResult = callback(module); | ||
if ((0, _jestUtil().isPromise)(cbResult)) { | ||
return waitForPromiseWithCleanup(cbResult, revertHook).then( | ||
return await waitForPromiseWithCleanup(cbResult, revertHook).then( | ||
() => module | ||
); | ||
} | ||
return module; | ||
@@ -971,3 +852,2 @@ } finally { | ||
} | ||
shouldTransform(filename) { | ||
@@ -978,4 +858,5 @@ const ignoreRegexp = this._cache.ignorePatternsRegExp; | ||
} | ||
} // TODO: do we need to define the generics twice? | ||
} | ||
// TODO: do we need to define the generics twice? | ||
async function createTranspilingRequire(config) { | ||
@@ -988,3 +869,4 @@ const transformer = await createScriptTransformer(config); | ||
const transpiledModule = await transformer.requireAndTranspileModule( | ||
resolverPath, // eslint-disable-next-line @typescript-eslint/no-empty-function | ||
resolverPath, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
() => {}, | ||
@@ -1004,3 +886,2 @@ { | ||
} | ||
const removeFile = path => { | ||
@@ -1011,3 +892,2 @@ try { | ||
}; | ||
const stripShebang = content => { | ||
@@ -1022,2 +902,3 @@ // If the file data starts with a shebang remove it. Leaves the empty line | ||
}; | ||
/** | ||
@@ -1030,3 +911,2 @@ * This is like `writeCacheFile` but with an additional sanity checksum. We | ||
*/ | ||
function writeCodeCacheFile(cachePath, code) { | ||
@@ -1039,2 +919,3 @@ const checksum = (0, _crypto().createHash)('sha256') | ||
} | ||
/** | ||
@@ -1046,10 +927,7 @@ * Read counterpart of `writeCodeCacheFile`. We verify that the content of the | ||
*/ | ||
function readCodeCacheFile(cachePath) { | ||
const content = readCacheFile(cachePath); | ||
if (content == null) { | ||
return null; | ||
} | ||
const code = content.substring(33); | ||
@@ -1060,9 +938,8 @@ const checksum = (0, _crypto().createHash)('sha256') | ||
.substring(0, 32); | ||
if (checksum === content.substring(0, 32)) { | ||
return code; | ||
} | ||
return null; | ||
} | ||
/** | ||
@@ -1074,3 +951,2 @@ * Writing to the cache atomically relies on 'rename' being atomic on most | ||
*/ | ||
const writeCacheFile = (cachePath, fileData) => { | ||
@@ -1083,6 +959,8 @@ try { | ||
} catch (e) { | ||
if (!(e instanceof Error)) { | ||
throw e; | ||
} | ||
if (cacheWriteErrorSafeToIgnore(e, cachePath)) { | ||
return; | ||
} | ||
e.message = `jest: failed to cache transform results in: ${cachePath}\nFailure message: ${e.message}`; | ||
@@ -1093,2 +971,3 @@ removeFile(cachePath); | ||
}; | ||
/** | ||
@@ -1100,3 +979,2 @@ * On Windows, renames are not atomic, leading to EPERM exceptions when two | ||
*/ | ||
const cacheWriteErrorSafeToIgnore = (e, cachePath) => | ||
@@ -1106,3 +984,2 @@ process.platform === 'win32' && | ||
fs().existsSync(cachePath); | ||
const readCacheFile = cachePath => { | ||
@@ -1112,8 +989,9 @@ if (!fs().existsSync(cachePath)) { | ||
} | ||
let fileData; | ||
try { | ||
fileData = fs().readFileSync(cachePath, 'utf8'); | ||
} catch (e) { | ||
if (!(e instanceof Error)) { | ||
throw e; | ||
} | ||
// on windows write-file-atomic is not atomic which can | ||
@@ -1124,3 +1002,2 @@ // result in this error | ||
} | ||
e.message = `jest: failed to read cache file: ${cachePath}\nFailure message: ${e.message}`; | ||
@@ -1130,3 +1007,2 @@ removeFile(cachePath); | ||
} | ||
if (fileData == null) { | ||
@@ -1137,6 +1013,4 @@ // We must have somehow created the file but failed to write to it, | ||
} | ||
return fileData; | ||
}; | ||
const getScriptCacheKey = (filename, instrument) => { | ||
@@ -1146,6 +1020,5 @@ const mtime = fs().statSync(filename).mtime; | ||
}; | ||
const calcIgnorePatternRegExp = config => { | ||
if ( | ||
!config.transformIgnorePatterns || | ||
config.transformIgnorePatterns == null || | ||
config.transformIgnorePatterns.length === 0 | ||
@@ -1155,6 +1028,4 @@ ) { | ||
} | ||
return new RegExp(config.transformIgnorePatterns.join('|')); | ||
}; | ||
const calcTransformRegExp = config => { | ||
@@ -1164,5 +1035,3 @@ if (!config.transform.length) { | ||
} | ||
const transformRegexp = []; | ||
for (let i = 0; i < config.transform.length; i++) { | ||
@@ -1175,12 +1044,9 @@ transformRegexp.push([ | ||
} | ||
return transformRegexp; | ||
}; | ||
function invariant(condition, message) { | ||
if (!condition) { | ||
if (condition == null || condition === false || condition === '') { | ||
throw new Error(message); | ||
} | ||
} | ||
function assertSyncTransformer(transformer, name) { | ||
@@ -1193,3 +1059,2 @@ invariant(name); | ||
} | ||
async function createScriptTransformer(config, cacheFS = new Map()) { | ||
@@ -1196,0 +1061,0 @@ const transformer = new ScriptTransformer(config, cacheFS); |
@@ -7,47 +7,33 @@ 'use strict'; | ||
exports.default = shouldInstrument; | ||
function path() { | ||
const data = _interopRequireWildcard(require('path')); | ||
path = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _micromatch() { | ||
const data = _interopRequireDefault(require('micromatch')); | ||
_micromatch = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _jestRegexUtil() { | ||
const data = require('jest-regex-util'); | ||
_jestRegexUtil = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _jestUtil() { | ||
const data = require('jest-util'); | ||
_jestUtil = function () { | ||
return data; | ||
}; | ||
return data; | ||
} | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
function _getRequireWildcardCache(nodeInterop) { | ||
@@ -61,3 +47,2 @@ if (typeof WeakMap !== 'function') return null; | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
@@ -95,3 +80,2 @@ if (!nodeInterop && obj && obj.__esModule) { | ||
} | ||
/** | ||
@@ -103,2 +87,3 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const MOCKS_PATTERN = new RegExp( | ||
@@ -110,3 +95,2 @@ (0, _jestRegexUtil().escapePathForRegex)( | ||
const cachedRegexes = new Map(); | ||
const getRegex = regexStr => { | ||
@@ -116,9 +100,8 @@ if (!cachedRegexes.has(regexStr)) { | ||
} | ||
const regex = cachedRegexes.get(regexStr); | ||
const regex = cachedRegexes.get(regexStr); // prevent stateful regexes from breaking, just in case | ||
// prevent stateful regexes from breaking, just in case | ||
regex.lastIndex = 0; | ||
return regex; | ||
}; | ||
function shouldInstrument(filename, options, config) { | ||
@@ -128,3 +111,2 @@ if (!options.collectCoverage) { | ||
} | ||
if ( | ||
@@ -136,3 +118,2 @@ config.forceCoverageMatch.length && | ||
} | ||
if ( | ||
@@ -146,3 +127,2 @@ !config.testPathIgnorePatterns.some(pattern => | ||
} | ||
if ( | ||
@@ -156,3 +136,2 @@ (0, _jestUtil().globsToMatcher)(config.testMatch)( | ||
} | ||
if ( | ||
@@ -169,3 +148,2 @@ // still cover if `only` is specified | ||
} | ||
if ( | ||
@@ -176,23 +154,17 @@ config.coveragePathIgnorePatterns.some(pattern => !!filename.match(pattern)) | ||
} | ||
if (config.globalSetup === filename) { | ||
return false; | ||
} | ||
if (config.globalTeardown === filename) { | ||
return false; | ||
} | ||
if (config.setupFiles.includes(filename)) { | ||
return false; | ||
} | ||
if (config.setupFilesAfterEnv.includes(filename)) { | ||
return false; | ||
} | ||
if (MOCKS_PATTERN.test(filename)) { | ||
return false; | ||
} | ||
if (options.changedFiles && !options.changedFiles.has(filename)) { | ||
@@ -202,3 +174,2 @@ if (!options.sourcesRelatedToTestsInChangedFiles) { | ||
} | ||
if (!options.sourcesRelatedToTestsInChangedFiles.has(filename)) { | ||
@@ -208,4 +179,3 @@ return false; | ||
} | ||
return true; | ||
} |
{ | ||
"name": "@jest/transform", | ||
"version": "29.1.2", | ||
"version": "29.2.0", | ||
"repository": { | ||
@@ -21,3 +21,3 @@ "type": "git", | ||
"@babel/core": "^7.11.6", | ||
"@jest/types": "^29.1.2", | ||
"@jest/types": "^29.2.0", | ||
"@jridgewell/trace-mapping": "^0.3.15", | ||
@@ -29,5 +29,5 @@ "babel-plugin-istanbul": "^6.1.1", | ||
"graceful-fs": "^4.2.9", | ||
"jest-haste-map": "^29.1.2", | ||
"jest-regex-util": "^29.0.0", | ||
"jest-util": "^29.1.2", | ||
"jest-haste-map": "^29.2.0", | ||
"jest-regex-util": "^29.2.0", | ||
"jest-util": "^29.2.0", | ||
"micromatch": "^4.0.4", | ||
@@ -39,3 +39,3 @@ "pirates": "^4.0.4", | ||
"devDependencies": { | ||
"@jest/test-utils": "^29.1.2", | ||
"@jest/test-utils": "^29.2.0", | ||
"@types/babel__core": "^7.1.14", | ||
@@ -54,3 +54,3 @@ "@types/convert-source-map": "^1.5.1", | ||
}, | ||
"gitHead": "3c31dd619e8c022cde53f40fa12ea2a67f4752ce" | ||
"gitHead": "ee5b37a4f4433afcfffb0356cea47739d8092287" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51215
1540
Updated@jest/types@^29.2.0
Updatedjest-haste-map@^29.2.0
Updatedjest-regex-util@^29.2.0
Updatedjest-util@^29.2.0