Advertisement
Guest User

Untitled

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