Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
readInitialCoverage,
programVisitor
} from 'istanbul-lib-instrument';
import * as babelTypes from 'babel-types';
import { RawSourceMap } from 'source-map';
const code = 'foo';
const filename = 'bar.txt';
createInstrumenter();
createInstrumenter({});
createInstrumenter({
coverageVariable: 'coverage'
});
const instrumenter = createInstrumenter({
preserveComments: true,
compact: false,
esModules: true,
autoWrap: false,
produceSourceMap: true,
sourceMapUrlCallback: (filename: string, url: string) => {},
debug: false
});
const sourceMap: RawSourceMap = {
version: 1 as any as string, // Fixed by https://github.com/mozilla/source-map/pull/293 but the fix is not yet published
sources: ['foo', 'bar'],
names: ['foo', 'bar'],
mappings: 'foo',
file: 'foo'
};
let instrumenter;
const origCallback = callback;
const needBaseline = iOpts.saveBaseline();
const baselineFile = path.resolve(iOpts.baselineFile());
if (iOpts.completeCopy()) {
includes = ['**/*'];
} else {
includes = iOpts.extensions().map(ext => '**/*' + ext);
}
if (!input) {
return callback(new Error('No input specified'));
}
instrumenter = libInstrument.createInstrumenter(
iOpts.getInstrumenterOpts()
);
if (needBaseline) {
mkdirp.sync(path.dirname(baselineFile));
instrumenter = new BaselineCollector(instrumenter);
callback = function(err) {
/* istanbul ignore else */
if (!err) {
console.error('Saving baseline coverage at ' + baselineFile);
fs.writeFileSync(
baselineFile,
JSON.stringify(instrumenter.getCoverage()),
'utf8'
);
}
function InstrumenterIstanbul (options) {
const { plugins } = options
const configPlugins = plugins ? { plugins } : {}
const instrumenter = createInstrumenter(Object.assign({
autoWrap: true,
coverageVariable: '__coverage__',
embedSource: true,
compact: options.compact,
preserveComments: options.preserveComments,
produceSourceMap: options.produceSourceMap,
ignoreClassMethods: options.ignoreClassMethods,
esModules: options.esModules
}, configPlugins))
return {
instrumentSync (code, filename, sourceMap) {
var instrumented = instrumenter.instrumentSync(code, filename)
// the instrumenter can optionally produce source maps,
// this is useful for features like remapping stack-traces.
// TODO: test source-map merging logic.
function instrumentCode(code, filename) {
var createInstrumenter = require('istanbul-lib-instrument').createInstrumenter;
return createInstrumenter().instrumentSync(code, filename);
}
/**
'use strict';
const {Transform} = require('stream');
const {createInstrumenter} = require('istanbul-lib-instrument');
const instrumenter = createInstrumenter({
coverageVariable: '__runner_coverage__',
preserveComments: true,
compact: false,
esModules: false,
autoWrap: false,
produceSourceMap: false,
sourceMapUrlCallback: null,
debug: false,
});
const scriptContent = Symbol('scriptContent');
const scriptFileName = Symbol('scriptFileName');
class CoverageInstrumentationStream extends Transform {
constructor(options, fileName) {
super(options);
}
function report() {
//Add map files to transform .js into .ts in coverage report
for (let file in cov)
if (fs.existsSync(file + '.map'))
cov[file].inputSourceMap = JSON.parse(fs.readFileSync(file + '.map', 'utf-8'));
fs.writeFileSync(join(tmpd, `${process.type}.json`), JSON.stringify(cov), 'utf-8')
}
const customOpts = {
includeAllSources: true
};
const instrumenter = createInstrumenter(customOpts)
const transformer = instrumenter.instrumentSync.bind(instrumenter)
const cov = global.__coverage__ = {}
const tmpd = resolve(__dirname, '.nyc_output')
if (!fs.existsSync(tmpd)) {
fs.mkdirSync(tmpd)
}
const matched = match()
hookRequire(matched, transformer, {})
if (process.type === 'browser') {
process.on('exit', report)
} else {
window.addEventListener('unload', report)
constructor(private readonly settings: StrykerOptions, private readonly filesToInstrument: readonly string[]) {
this.instrumenter = createInstrumenter({ coverageVariable: this.coverageVariable, preserveComments: true });
}
constructor(private readonly settings: StrykerOptions, private readonly filesToInstrument: ReadonlyArray) {
this.instrumenter = createInstrumenter({ coverageVariable: this.coverageVariable, preserveComments: true });
}
module.exports = function createTransformerFn ({
root,
coverageVariable,
transformerCacheVariable,
sourceMapCacheVariable,
cacheDir,
disableCache = false,
ext = '.js',
verbose = false
}) {
const transformerCache = global[transformerCacheVariable] = global[transformerCacheVariable] || {};
const sourceMapCache = global[sourceMapCacheVariable] = global[sourceMapCacheVariable] || createSourceMapStore();
const instrumenter = createInstrumenter({
autoWrap: true,
coverageVariable,
embedSource: true,
noCompact: true,
preserveComments: true
});
const instrument = instrumenter.instrumentSync.bind(instrumenter);
return function transform (code, file) {
let instrumentedCode, hasChanged, cacheFile, codeHash;
cacheFile = resolve(cacheDir, relative(root, file)) + '.json';
codeHash = md5Hex(code);
const sourceMap = convertSourceMap.fromSource(code);
if (sourceMap) {
'use strict'
const path = require('path')
const fs = require('fs')
const istanbul = require('istanbul-lib-instrument')
const makeDir = require('make-dir')
const glob = require('glob')
const instrumenter = istanbul.createInstrumenter({
coverageVariable: '___NYC_SELF_COVERAGE___',
esModules: true
})
function instrumentFile (name) {
const indexPath = path.join(__dirname, name)
const outputPath = path.join(__dirname, 'self-coverage', name)
const source = fs.readFileSync(indexPath, 'utf8')
const instrumentedSource = name === 'package.json' ? source : instrumenter.instrumentSync(source, indexPath)
makeDir.sync(path.dirname(outputPath))
fs.writeFileSync(outputPath, instrumentedSource)
}
function instrumentGlob (pattern) {