source: trip-planner-front/node_modules/istanbul-lib-instrument/dist/instrumenter.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _core = require("@babel/core");
9
10var _schema = require("@istanbuljs/schema");
11
12var _visitor = _interopRequireDefault(require("./visitor"));
13
14var _readCoverage = _interopRequireDefault(require("./read-coverage"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18/*
19 Copyright 2012-2015, Yahoo Inc.
20 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
21 */
22
23/**
24 * Instrumenter is the public API for the instrument library.
25 * It is typically used for ES5 code. For ES6 code that you
26 * are already running under `babel` use the coverage plugin
27 * instead.
28 * @param {Object} opts optional.
29 * @param {string} [opts.coverageVariable=__coverage__] name of global coverage variable.
30 * @param {boolean} [opts.preserveComments=false] preserve comments in output
31 * @param {boolean} [opts.compact=true] generate compact code.
32 * @param {boolean} [opts.esModules=false] set to true to instrument ES6 modules.
33 * @param {boolean} [opts.autoWrap=false] set to true to allow `return` statements outside of functions.
34 * @param {boolean} [opts.produceSourceMap=false] set to true to produce a source map for the instrumented code.
35 * @param {Array} [opts.ignoreClassMethods=[]] set to array of class method names to ignore for coverage.
36 * @param {Function} [opts.sourceMapUrlCallback=null] a callback function that is called when a source map URL
37 * is found in the original code. This function is called with the source file name and the source map URL.
38 * @param {boolean} [opts.debug=false] - turn debugging on
39 * @param {array} [opts.parserPlugins] - set babel parser plugins, see @istanbuljs/schema for defaults.
40 */
41class Instrumenter {
42 constructor(opts = {}) {
43 this.opts = { ..._schema.defaults.instrumenter,
44 ...opts
45 };
46 this.fileCoverage = null;
47 this.sourceMap = null;
48 }
49 /**
50 * instrument the supplied code and track coverage against the supplied
51 * filename. It throws if invalid code is passed to it. ES5 and ES6 syntax
52 * is supported. To instrument ES6 modules, make sure that you set the
53 * `esModules` property to `true` when creating the instrumenter.
54 *
55 * @param {string} code - the code to instrument
56 * @param {string} filename - the filename against which to track coverage.
57 * @param {object} [inputSourceMap] - the source map that maps the not instrumented code back to it's original form.
58 * Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
59 * coverage to the untranspiled source.
60 * @returns {string} the instrumented code.
61 */
62
63
64 instrumentSync(code, filename, inputSourceMap) {
65 if (typeof code !== 'string') {
66 throw new Error('Code must be a string');
67 }
68
69 filename = filename || String(new Date().getTime()) + '.js';
70 const {
71 opts
72 } = this;
73 let output = {};
74 const babelOpts = {
75 configFile: false,
76 babelrc: false,
77 ast: true,
78 filename: filename || String(new Date().getTime()) + '.js',
79 inputSourceMap,
80 sourceMaps: opts.produceSourceMap,
81 compact: opts.compact,
82 comments: opts.preserveComments,
83 parserOpts: {
84 allowReturnOutsideFunction: opts.autoWrap,
85 sourceType: opts.esModules ? 'module' : 'script',
86 plugins: opts.parserPlugins
87 },
88 plugins: [[({
89 types
90 }) => {
91 const ee = (0, _visitor.default)(types, filename, {
92 coverageVariable: opts.coverageVariable,
93 coverageGlobalScope: opts.coverageGlobalScope,
94 coverageGlobalScopeFunc: opts.coverageGlobalScopeFunc,
95 ignoreClassMethods: opts.ignoreClassMethods,
96 inputSourceMap
97 });
98 return {
99 visitor: {
100 Program: {
101 enter: ee.enter,
102
103 exit(path) {
104 output = ee.exit(path);
105 }
106
107 }
108 }
109 };
110 }]]
111 };
112 const codeMap = (0, _core.transformSync)(code, babelOpts);
113
114 if (!output || !output.fileCoverage) {
115 const initialCoverage = (0, _readCoverage.default)(codeMap.ast) ||
116 /* istanbul ignore next: paranoid check */
117 {};
118 this.fileCoverage = initialCoverage.coverageData;
119 this.sourceMap = inputSourceMap;
120 return code;
121 }
122
123 this.fileCoverage = output.fileCoverage;
124 this.sourceMap = codeMap.map;
125 const cb = this.opts.sourceMapUrlCallback;
126
127 if (cb && output.sourceMappingURL) {
128 cb(filename, output.sourceMappingURL);
129 }
130
131 return codeMap.code;
132 }
133 /**
134 * callback-style instrument method that calls back with an error
135 * as opposed to throwing one. Note that in the current implementation,
136 * the callback will be called in the same process tick and is not asynchronous.
137 *
138 * @param {string} code - the code to instrument
139 * @param {string} filename - the filename against which to track coverage.
140 * @param {Function} callback - the callback
141 * @param {Object} inputSourceMap - the source map that maps the not instrumented code back to it's original form.
142 * Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
143 * coverage to the untranspiled source.
144 */
145
146
147 instrument(code, filename, callback, inputSourceMap) {
148 if (!callback && typeof filename === 'function') {
149 callback = filename;
150 filename = null;
151 }
152
153 try {
154 const out = this.instrumentSync(code, filename, inputSourceMap);
155 callback(null, out);
156 } catch (ex) {
157 callback(ex);
158 }
159 }
160 /**
161 * returns the file coverage object for the last file instrumented.
162 * @returns {Object} the file coverage object.
163 */
164
165
166 lastFileCoverage() {
167 return this.fileCoverage;
168 }
169 /**
170 * returns the source map produced for the last file instrumented.
171 * @returns {null|Object} the source map object.
172 */
173
174
175 lastSourceMap() {
176 return this.sourceMap;
177 }
178
179}
180
181var _default = Instrumenter;
182exports.default = _default;
Note: See TracBrowser for help on using the repository browser.