Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

instrumenter.js 6.0KB

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