index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * espower - Power Assert feature instrumentor based on the ECMAScript AST.
  3. *
  4. * https://github.com/power-assert-js/espower
  5. *
  6. * Copyright (c) 2013-2019 Takuto Wada
  7. * Licensed under the MIT license.
  8. * https://github.com/power-assert-js/espower/blob/master/MIT-LICENSE.txt
  9. */
  10. 'use strict';
  11. var defaultOptions = require('./lib/default-options');
  12. var Instrumentor = require('./lib/instrumentor');
  13. /**
  14. * Instrument power assert feature into code. ECMAScript AST in, ECMAScript AST out.
  15. *
  16. * @param {object} ast ECMAScript AST to be instrumented (directly modified)
  17. * @param {object} options Instrumentation options.
  18. * @returns {object} instrumented AST
  19. * @throws {EspowerError} if `ast` is already instrumented
  20. * @throws {EspowerError} if `ast` does not contain location information
  21. * @throws {EspowerError} if `options` is not valid
  22. */
  23. function espower (ast, options) {
  24. var instrumentor = new Instrumentor(Object.assign(defaultOptions(), options));
  25. return instrumentor.instrument(ast);
  26. }
  27. /**
  28. * Generate visitor object to be used with `estraverse.replace`
  29. *
  30. * @param {object} ast ECMAScript AST to be instrumented (directly modified)
  31. * @param {object} options Instrumentation options.
  32. * @returns {object} visitor object for estraverse
  33. * @throws {EspowerError} if `ast` is already instrumented
  34. * @throws {EspowerError} if `ast` does not contain location information
  35. * @throws {EspowerError} if `options` is not valid
  36. */
  37. espower.createVisitor = function createVisitor (ast, options) {
  38. var instrumentor = new Instrumentor(Object.assign(defaultOptions(), options));
  39. return instrumentor.createVisitor(ast);
  40. };
  41. espower.defaultOptions = defaultOptions;
  42. espower.EspowerError = require('./lib/espower-error');
  43. module.exports = espower;