-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfailing-code.js
69 lines (58 loc) · 1.24 KB
/
failing-code.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var failingLine = require('./failing-line');
var nodeRequire;
var fs;
//
// wut, yeah, this is for browserify to prevent it from bundling
// a file system polyfill.
//
if (require('is-node')) {
nodeRequire = require;
fs = nodeRequire('fs');
nodeRequire = null;
}
/**
* Failing code.
*
* @param {Error} error Error.
* @param {Object} options Configuration.
* @returns {Undefined|Array} Parsed failed code.
* @private
*/
function failingCode(error, options) {
var ln = failingLine(error, options.shift);
if (!ln) return;
var doc = options.read && options.read(ln);
if (!doc && fs) {
try {
doc = fs.readFileSync(ln.filename).toString();
} catch (e) {
return undefined;
}
}
if (!doc) return undefined;
var lines = typeof doc === 'string' ? doc.split('\n') : doc;
var result = [];
var i = ln.line - 3;
while (++i < ln.line + 1) {
if (i + 1 != ln.line) {
result.push({
line: ln.line - (ln.line - i -1),
code: lines[i]
});
continue;
}
result.push({
line: ln.line,
col: ln.col,
fn: ln.fn,
filename: ln.filename,
code: lines[i],
failed: true
});
}
return result;
}
//
// Expose the module.
//
module.exports = failingCode;