instrumenter.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.defaultOpts = defaultOpts;
  6. exports.default = void 0;
  7. var parser = _interopRequireWildcard(require("@babel/parser"));
  8. var t = _interopRequireWildcard(require("@babel/types"));
  9. var _traverse = _interopRequireDefault(require("@babel/traverse"));
  10. var _generator = _interopRequireDefault(require("@babel/generator"));
  11. var _visitor = _interopRequireDefault(require("./visitor"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  14. /*
  15. Copyright 2012-2015, Yahoo Inc.
  16. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  17. */
  18. function defaultOpts() {
  19. return {
  20. coverageVariable: '__coverage__',
  21. coverageGlobalScope: 'this',
  22. coverageGlobalScopeFunc: true,
  23. preserveComments: false,
  24. compact: true,
  25. esModules: false,
  26. autoWrap: false,
  27. produceSourceMap: false,
  28. ignoreClassMethods: [],
  29. sourceMapUrlCallback: null,
  30. debug: false,
  31. /* babel parser plugins are to be enabled when the feature is stage 3 and
  32. * implemented in a released version of node.js */
  33. plugins: ['asyncGenerators', 'bigInt', 'classProperties', 'classPrivateProperties', 'dynamicImport', 'importMeta', 'objectRestSpread', 'optionalCatchBinding', 'flow', 'jsx']
  34. };
  35. }
  36. /**
  37. * Instrumenter is the public API for the instrument library.
  38. * It is typically used for ES5 code. For ES6 code that you
  39. * are already running under `babel` use the coverage plugin
  40. * instead.
  41. * @param {Object} opts optional.
  42. * @param {string} [opts.coverageVariable=__coverage__] name of global coverage variable.
  43. * @param {boolean} [opts.preserveComments=false] preserve comments in output
  44. * @param {boolean} [opts.compact=true] generate compact code.
  45. * @param {boolean} [opts.esModules=false] set to true to instrument ES6 modules.
  46. * @param {boolean} [opts.autoWrap=false] set to true to allow `return` statements outside of functions.
  47. * @param {boolean} [opts.produceSourceMap=false] set to true to produce a source map for the instrumented code.
  48. * @param {Array} [opts.ignoreClassMethods=[]] set to array of class method names to ignore for coverage.
  49. * @param {Function} [opts.sourceMapUrlCallback=null] a callback function that is called when a source map URL
  50. * is found in the original code. This function is called with the source file name and the source map URL.
  51. * @param {boolean} [opts.debug=false] - turn debugging on
  52. * @param {array} [opts.plugins=['asyncGenerators','dynamicImport','objectRestSpread','optionalCatchBinding','flow','jsx']] - set plugins
  53. */
  54. class Instrumenter {
  55. constructor(opts = defaultOpts()) {
  56. this.opts = this.normalizeOpts(opts);
  57. this.fileCoverage = null;
  58. this.sourceMap = null;
  59. }
  60. /**
  61. * normalize options passed in and assign defaults.
  62. * @param opts
  63. * @private
  64. */
  65. normalizeOpts(opts) {
  66. const normalize = (name, defaultValue) => {
  67. if (!opts.hasOwnProperty(name)) {
  68. opts[name] = defaultValue;
  69. }
  70. };
  71. const defOpts = defaultOpts();
  72. Object.keys(defOpts).forEach(k => {
  73. normalize(k, defOpts[k]);
  74. });
  75. return opts;
  76. }
  77. /**
  78. * instrument the supplied code and track coverage against the supplied
  79. * filename. It throws if invalid code is passed to it. ES5 and ES6 syntax
  80. * is supported. To instrument ES6 modules, make sure that you set the
  81. * `esModules` property to `true` when creating the instrumenter.
  82. *
  83. * @param {string} code - the code to instrument
  84. * @param {string} filename - the filename against which to track coverage.
  85. * @param {object} [inputSourceMap] - the source map that maps the not instrumented code back to it's original form.
  86. * Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
  87. * coverage to the untranspiled source.
  88. * @returns {string} the instrumented code.
  89. */
  90. instrumentSync(code, filename, inputSourceMap) {
  91. if (typeof code !== 'string') {
  92. throw new Error('Code must be a string');
  93. }
  94. filename = filename || String(new Date().getTime()) + '.js';
  95. const opts = this.opts;
  96. const ast = parser.parse(code, {
  97. allowReturnOutsideFunction: opts.autoWrap,
  98. sourceType: opts.esModules ? 'module' : 'script',
  99. plugins: opts.plugins
  100. });
  101. const ee = (0, _visitor.default)(t, filename, {
  102. coverageVariable: opts.coverageVariable,
  103. coverageGlobalScope: opts.coverageGlobalScope,
  104. coverageGlobalScopeFunc: opts.coverageGlobalScopeFunc,
  105. ignoreClassMethods: opts.ignoreClassMethods,
  106. inputSourceMap
  107. });
  108. let output = {};
  109. const visitor = {
  110. Program: {
  111. enter: ee.enter,
  112. exit(path) {
  113. output = ee.exit(path);
  114. }
  115. }
  116. };
  117. (0, _traverse.default)(ast, visitor);
  118. const generateOptions = {
  119. compact: opts.compact,
  120. comments: opts.preserveComments,
  121. sourceMaps: opts.produceSourceMap,
  122. sourceFileName: filename
  123. };
  124. const codeMap = (0, _generator.default)(ast, generateOptions, code);
  125. this.fileCoverage = output.fileCoverage;
  126. this.sourceMap = codeMap.map;
  127. const cb = this.opts.sourceMapUrlCallback;
  128. if (cb && output.sourceMappingURL) {
  129. cb(filename, output.sourceMappingURL);
  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. instrument(code, filename, callback, inputSourceMap) {
  146. if (!callback && typeof filename === 'function') {
  147. callback = filename;
  148. filename = null;
  149. }
  150. try {
  151. const out = this.instrumentSync(code, filename, inputSourceMap);
  152. callback(null, out);
  153. } catch (ex) {
  154. callback(ex);
  155. }
  156. }
  157. /**
  158. * returns the file coverage object for the last file instrumented.
  159. * @returns {Object} the file coverage object.
  160. */
  161. lastFileCoverage() {
  162. return this.fileCoverage;
  163. }
  164. /**
  165. * returns the source map produced for the last file instrumented.
  166. * @returns {null|Object} the source map object.
  167. */
  168. lastSourceMap() {
  169. return this.sourceMap;
  170. }
  171. }
  172. var _default = Instrumenter;
  173. exports.default = _default;