123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _core = require("@babel/core");
- var _schema = require("@istanbuljs/schema");
- var _visitor = _interopRequireDefault(require("./visitor"));
- var _readCoverage = _interopRequireDefault(require("./read-coverage"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- class Instrumenter {
- constructor(opts = {}) {
- this.opts = { ..._schema.defaults.instrumenter,
- ...opts
- };
- this.fileCoverage = null;
- this.sourceMap = null;
- }
-
- instrumentSync(code, filename, inputSourceMap) {
- if (typeof code !== 'string') {
- throw new Error('Code must be a string');
- }
- filename = filename || String(new Date().getTime()) + '.js';
- const {
- opts
- } = this;
- let output = {};
- const babelOpts = {
- configFile: false,
- babelrc: false,
- ast: true,
- filename: filename || String(new Date().getTime()) + '.js',
- inputSourceMap,
- sourceMaps: opts.produceSourceMap,
- compact: opts.compact,
- comments: opts.preserveComments,
- parserOpts: {
- allowReturnOutsideFunction: opts.autoWrap,
- sourceType: opts.esModules ? 'module' : 'script',
- plugins: opts.parserPlugins
- },
- plugins: [[({
- types
- }) => {
- const ee = (0, _visitor.default)(types, filename, {
- coverageVariable: opts.coverageVariable,
- coverageGlobalScope: opts.coverageGlobalScope,
- coverageGlobalScopeFunc: opts.coverageGlobalScopeFunc,
- ignoreClassMethods: opts.ignoreClassMethods,
- inputSourceMap
- });
- return {
- visitor: {
- Program: {
- enter: ee.enter,
- exit(path) {
- output = ee.exit(path);
- }
- }
- }
- };
- }]]
- };
- const codeMap = (0, _core.transformSync)(code, babelOpts);
- if (!output || !output.fileCoverage) {
- const initialCoverage = (0, _readCoverage.default)(codeMap.ast) ||
-
- {};
- this.fileCoverage = initialCoverage.coverageData;
- this.sourceMap = inputSourceMap;
- return code;
- }
- this.fileCoverage = output.fileCoverage;
- this.sourceMap = codeMap.map;
- const cb = this.opts.sourceMapUrlCallback;
- if (cb && output.sourceMappingURL) {
- cb(filename, output.sourceMappingURL);
- }
- return codeMap.code;
- }
-
- instrument(code, filename, callback, inputSourceMap) {
- if (!callback && typeof filename === 'function') {
- callback = filename;
- filename = null;
- }
- try {
- const out = this.instrumentSync(code, filename, inputSourceMap);
- callback(null, out);
- } catch (ex) {
- callback(ex);
- }
- }
-
- lastFileCoverage() {
- return this.fileCoverage;
- }
-
- lastSourceMap() {
- return this.sourceMap;
- }
- }
- var _default = Instrumenter;
- exports.default = _default;
|