-
Notifications
You must be signed in to change notification settings - Fork 4
/
insist.js
120 lines (102 loc) · 2.98 KB
/
insist.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const assert = require('assert');
const fs = require('fs');
const esprima = require('esprima');
const stack = require('stack-trace');
const NO_ASSERT = process.env.NO_ASSERT;
const DONT_DECORATE = ['ok', 'fail', 'AssertionError'];
function noop() {}
function traverse(node, func) {
func(node);
function _traverseChild(node) {
traverse(node, func);
}
for (var key in node) {
if (node.hasOwnProperty(key)) {
var child = node[key];
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
child.forEach(_traverseChild);
} else {
traverse(child, func);
}
}
}
}
}
function getSource(trace) {
var src = fs.readFileSync(trace.getFileName(), 'utf8');
var tree = esprima.parse(src, { loc: true });
var startLine = trace.getLineNumber();
var startCol = trace.getColumnNumber();
var endLine = Infinity;
var endCol = Infinity;
traverse(tree, function(node) {
if (node && node.type === 'CallExpression') {
if (node.loc.start.line > startLine || node.loc.start.col > startCol) {
return;
}
if (node.loc.end.line < startLine || node.loc.end.col < startCol) {
return;
}
if (node.loc.end.line < endLine) {
endLine = node.loc.end.line;
}
if (node.loc.end.col < endCol) {
endCol = node.loc.end.col;
}
}
});
var lines = String(src).split(/\r?\n/);
lines = lines.slice(startLine - 1, endLine - lines.length);
// add on assert or insist
// ex: equals() becomes assert.equals()
var a = 'assert';
var i = 'insist';
var mod = lines[0].substring(startCol - a.length - 2, startCol - 2);
if (mod === a || mod === i) {
startCol = startCol - a.length - 2;
}
lines[0] = lines[0].substring(startCol - 1);
lines[lines.length - 1] = lines[lines.length - 1].substring(0, endCol - 1);
return lines.join('\n').trim();
}
function getMessage() {
var trace = stack.get()[2];
var fileName = trace.getFileName();
if (fileName !== 'repl') {
return getSource(trace);
} else {
return trace.fun.toString();
}
}
function decorate(key) {
return function insist() {
try {
assert[key].apply(assert, arguments);
} catch (err) {
if (err instanceof assert.AssertionError) {
var srcMsg = getMessage();
if (err.message) {
err.message += ' from ' + srcMsg;
} else {
err.message = srcMsg;
}
}
Error.captureStackTrace(err, insist);
throw err;
}
};
}
var insist = NO_ASSERT ? noop : decorate('ok');
Object.keys(assert).forEach(function(key) {
if (DONT_DECORATE.indexOf(key) !== -1) {
insist[key] = assert[key];
} else {
insist[key] = NO_ASSERT ? noop : decorate(key);
}
});
insist.ok = insist;
module.exports = insist;