Skip to content

Commit cc69d2b

Browse files
committed
- fix babel issues reported in tj#12 (and still not fixed in tj#16): when babel has transpiled all assert(...) calls to something else ((0, _.default)(...) for example), we look for that transpiled code instead. (assuming Babel 7)
- augmented the tests to serve as unit tests which can be executed via `npm test`, including a minimal test to see if a babel-transpiled better-assert library will work.
1 parent d6ab690 commit cc69d2b

6 files changed

Lines changed: 3104 additions & 33 deletions

File tree

babel.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const presets = [
2+
[
3+
"@babel/env",
4+
{
5+
targets: {
6+
edge: "17",
7+
firefox: "60",
8+
chrome: "67",
9+
safari: "11.1",
10+
node: "8",
11+
browsers: "> 5%",
12+
},
13+
useBuiltIns: "usage",
14+
loose: true,
15+
},
16+
],
17+
];
18+
19+
module.exports = { presets };

index.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
var AssertionError = require('assert').AssertionError
6-
, callsite = require('callsite')
6+
// , callsite = require('callsite')
77
, fs = require('fs')
88
, path = require('path');
99

@@ -22,24 +22,24 @@ module.exports = process.env.NO_ASSERT
2222
function assert(expr, msg) {
2323
if (expr) return;
2424

25-
var a = new Error();
26-
// 0 => Error
27-
// 1 => at assert
28-
// 2 => at Object.<anonymous> (/project/myproject/test/test-babel.js:15:1)', <= where the assert was raised !
29-
// .....
30-
//
31-
var errorline = a.stack.split('\n')[2];
32-
var m = errorline.match(/at (.*)\((.*):([0-9]*):([0-9]*)\)/);
33-
var func = m[1]; // Object.<anonymous> ( not very useful)
34-
var file = m[2]; // filename
35-
var lineno = parseInt(m[3]);
25+
var a = new Error();
26+
// 0 => Error
27+
// 1 => at assert
28+
// 2 => at Object.<anonymous> (/project/myproject/test/test-babel.js:15:1)', <= where the assert was raised !
29+
// .....
30+
//
31+
var errorline = a.stack.split('\n')[2];
32+
var m = errorline.match(/at (.*)\((.*):([0-9]*):([0-9]*)\)/);
33+
var func = m[1]; // Object.<anonymous> (not very useful)
34+
var file = m[2]; // filename
35+
var lineno = parseInt(m[3]);
3636
var src = getAssertMessage(file, lineno);
3737
var custom = (msg != null) ? msg : '';
3838

3939
if (custom) {
40-
// strip off the last (custom message) argument: we expect it to be a literal string!
41-
src = src.replace(/,\s*['"].*$/, '');
42-
src = '(' + src + ') ' + custom;
40+
// strip off the last (custom message) argument: we expect it to be a literal string!
41+
src = src.replace(/,\s*['"].*$/, '');
42+
src = '(' + src + ') ' + custom;
4343
}
4444

4545
var err = new AssertionError({
@@ -68,7 +68,14 @@ function getAssertMessage(file, lineno) {
6868
break;
6969
}
7070

71-
return line.match(/assert\((.*)\)/)[1];
71+
var m = line.match(/assert\((.*)\)/);
72+
if (!m) {
73+
// however, if the entire file doesn't carry any assert() lines any more,
74+
// our next bet is this source file was transpiled by babel:
75+
m = line.match(/\(0, [\w_]+\.default\)\((.*)\)/);
76+
}
77+
78+
return m ? m[1] : "???";
7279
}
7380

7481
/**

0 commit comments

Comments
 (0)