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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// Generated by CoffeeScript 1.8.0
(function() {
var Instrumenter, coffee, esprima, estraverse, fs, globalEval, istanbul, path, _,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
coffee = require('coffee-script');
istanbul = require('istanbul');
estraverse = require('estraverse');
_ = require('lodash');
esprima = require('esprima');
path = require('path');
fs = require('fs');
globalEval = function(source) {
var geval;
geval = eval;
return geval(source);
};
Instrumenter = (function(_super) {
__extends(Instrumenter, _super);
function Instrumenter(opt) {
istanbul.Instrumenter.call(this, opt);
}
Instrumenter.prototype.instrumentSync = function(code, filename) {
var e, program;
filename = filename || ("" + (Date.now()) + ".js");
if (typeof code !== 'string') {
throw new Error('Code must be string');
}
try {
code = coffee.compile(code, {
sourceMap: true
});
program = esprima.parse(code.js, {
loc: true
});
this.fixupLoc(program, code.sourceMap);
return this.instrumentASTSync(program, filename, code);
} catch (_error) {
e = _error;
e.message = "Error compiling " + filename + ": " + e.message;
throw e;
}
};
Instrumenter.prototype.include = function(filename) {
var code;
filename = path.resolve(filename);
code = fs.readFileSync(filename, 'utf8');
this.instrumentSync(code, filename);
globalEval("" + (this.getPreamble(null)));
};
Instrumenter.prototype.fixupLoc = function(program, sourceMap) {
return estraverse.traverse(program, {
leave: function(node, parent) {
var mappedLocation, _ref, _ref1;
mappedLocation = function(location) {
var column, line, locArray;
locArray = sourceMap.sourceLocation([location.line - 1, location.column]);
line = 0;
column = 0;
if (locArray) {
line = locArray[0] + 1;
column = locArray[1];
}
return {
line: line,
column: column
};
};
if ((_ref = node.loc) != null ? _ref.start : void 0) {
node.loc.start = mappedLocation(node.loc.start);
}
if ((_ref1 = node.loc) != null ? _ref1.end : void 0) {
node.loc.end = mappedLocation(node.loc.end);
}
}
});
};
return Instrumenter;
})(istanbul.Instrumenter);
module.exports = Instrumenter;
}).call(this);
|