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