instrumenter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  6. Copyright 2012-2015, Yahoo Inc.
  7. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  8. */
  9. var _babylon = require('babylon');
  10. var babylon = _interopRequireWildcard(_babylon);
  11. var _babelTypes = require('babel-types');
  12. var t = _interopRequireWildcard(_babelTypes);
  13. var _babelTraverse = require('babel-traverse');
  14. var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
  15. var _babelGenerator = require('babel-generator');
  16. var _babelGenerator2 = _interopRequireDefault(_babelGenerator);
  17. var _visitor = require('./visitor');
  18. var _visitor2 = _interopRequireDefault(_visitor);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  21. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  22. function defaultOpts() {
  23. return {
  24. coverageVariable: "__coverage__",
  25. preserveComments: false,
  26. compact: true,
  27. esModules: false,
  28. autoWrap: false,
  29. produceSourceMap: false,
  30. sourceMapUrlCallback: null,
  31. debug: false
  32. };
  33. }
  34. /**
  35. * Instrumenter is the public API for the instrument library.
  36. * It is typically used for ES5 code. For ES6 code that you
  37. * are already running under `babel` use the coverage plugin
  38. * instead.
  39. * @param {Object} opts optional.
  40. * @param {string} [opts.coverageVariable=__coverage__] name of global coverage variable.
  41. * @param {boolean} [opts.preserveComments=false] preserve comments in output
  42. * @param {boolean} [opts.compact=true] generate compact code.
  43. * @param {boolean} [opts.esModules=false] set to true to instrument ES6 modules.
  44. * @param {boolean} [opts.autoWrap=false] set to true to allow `return` statements outside of functions.
  45. * @param {boolean} [opts.produceSourceMap=false] set to true to produce a source map for the instrumented code.
  46. * @param {Function} [opts.sourceMapUrlCallback=null] a callback function that is called when a source map URL
  47. * is found in the original code. This function is called with the source file name and the source map URL.
  48. * @param {boolean} [opts.debug=false] - turn debugging on
  49. */
  50. var Instrumenter = function () {
  51. function Instrumenter() {
  52. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOpts();
  53. _classCallCheck(this, Instrumenter);
  54. this.opts = this.normalizeOpts(opts);
  55. this.fileCoverage = null;
  56. this.sourceMap = null;
  57. }
  58. /**
  59. * normalize options passed in and assign defaults.
  60. * @param opts
  61. * @private
  62. */
  63. _createClass(Instrumenter, [{
  64. key: 'normalizeOpts',
  65. value: function normalizeOpts(opts) {
  66. var normalize = function normalize(name, defaultValue) {
  67. if (!opts.hasOwnProperty(name)) {
  68. opts[name] = defaultValue;
  69. }
  70. };
  71. var defOpts = defaultOpts();
  72. Object.keys(defOpts).forEach(function (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. }, {
  91. key: 'instrumentSync',
  92. value: function instrumentSync(code, filename, inputSourceMap) {
  93. if (typeof code !== 'string') {
  94. throw new Error('Code must be a string');
  95. }
  96. filename = filename || String(new Date().getTime()) + '.js';
  97. var opts = this.opts;
  98. var ast = babylon.parse(code, {
  99. allowReturnOutsideFunction: opts.autoWrap,
  100. sourceType: opts.esModules ? "module" : "script",
  101. plugins: ['asyncGenerators', 'dynamicImport', 'objectRestSpread', 'flow', 'jsx']
  102. });
  103. var ee = (0, _visitor2.default)(t, filename, {
  104. coverageVariable: opts.coverageVariable,
  105. inputSourceMap: inputSourceMap
  106. });
  107. var output = {};
  108. var visitor = {
  109. Program: {
  110. enter: ee.enter,
  111. exit: function exit(path) {
  112. output = ee.exit(path);
  113. }
  114. }
  115. };
  116. (0, _babelTraverse2.default)(ast, visitor);
  117. var generateOptions = {
  118. compact: opts.compact,
  119. comments: opts.preserveComments,
  120. sourceMaps: opts.produceSourceMap,
  121. sourceFileName: filename
  122. };
  123. var codeMap = (0, _babelGenerator2.default)(ast, generateOptions, code);
  124. this.fileCoverage = output.fileCoverage;
  125. this.sourceMap = codeMap.map;
  126. var cb = this.opts.sourceMapUrlCallback;
  127. if (cb && output.sourceMappingURL) {
  128. cb(filename, output.sourceMappingURL);
  129. }
  130. return codeMap.code;
  131. }
  132. /**
  133. * callback-style instrument method that calls back with an error
  134. * as opposed to throwing one. Note that in the current implementation,
  135. * the callback will be called in the same process tick and is not asynchronous.
  136. *
  137. * @param {string} code - the code to instrument
  138. * @param {string} filename - the filename against which to track coverage.
  139. * @param {Function} callback - the callback
  140. * @param {Object} inputSourceMap - the source map that maps the not instrumented code back to it's original form.
  141. * Is assigned to the coverage object and therefore, is available in the json output and can be used to remap the
  142. * coverage to the untranspiled source.
  143. */
  144. }, {
  145. key: 'instrument',
  146. value: function instrument(code, filename, callback, inputSourceMap) {
  147. if (!callback && typeof filename === 'function') {
  148. callback = filename;
  149. filename = null;
  150. }
  151. try {
  152. var out = this.instrumentSync(code, filename, inputSourceMap);
  153. callback(null, out);
  154. } catch (ex) {
  155. callback(ex);
  156. }
  157. }
  158. /**
  159. * returns the file coverage object for the last file instrumented.
  160. * @returns {Object} the file coverage object.
  161. */
  162. }, {
  163. key: 'lastFileCoverage',
  164. value: function lastFileCoverage() {
  165. return this.fileCoverage;
  166. }
  167. /**
  168. * returns the source map produced for the last file instrumented.
  169. * @returns {null|Object} the source map object.
  170. */
  171. }, {
  172. key: 'lastSourceMap',
  173. value: function lastSourceMap() {
  174. return this.sourceMap;
  175. }
  176. }]);
  177. return Instrumenter;
  178. }();
  179. exports.default = Instrumenter;