Skip to content

Commit 3b77ebf

Browse files
erossignonfatso83
authored andcommitted
tj#12 replace use of callsite with standard Error object to fix issues with babel
1 parent dadbe26 commit 3b77ebf

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.DS_Store
22
node_modules
33
*.sock
4+
.idea
5+

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ support
22
test
33
examples
44
*.sock
5+
.idea/
6+

Readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
# better-assert
33

4-
Better c-style assertions using [callsite](https://github.com/visionmedia/callsite) for
5-
self-documenting failure messages.
4+
Better c-style assertions for self-documenting failure messages.
65

76
## Installation
87

index.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
var AssertionError = require('assert').AssertionError
66
, callsite = require('callsite')
7-
, fs = require('fs')
7+
, fs = require('fs');
88

99
/**
1010
* Expose `assert`.
@@ -21,18 +21,24 @@ module.exports = process.env.NO_ASSERT
2121
function assert(expr) {
2222
if (expr) return;
2323

24-
var stack = callsite();
25-
var call = stack[1];
26-
var file = call.getFileName();
27-
var lineno = call.getLineNumber();
28-
var src = fs.readFileSync(file, 'utf8');
29-
var line = src.split('\n')[lineno-1];
30-
var src = line.match(/assert\((.*)\)/)[1];
31-
32-
var err = new AssertionError({
33-
message: src,
34-
stackStartFunction: stack[0].getFunction()
35-
});
36-
37-
throw err;
24+
var a = new Error();
25+
// 0 => Error
26+
// 1 => at assert
27+
// 2 => at Object.<anonymous> (/project/myproject/test/test-babel.js:15:1)', <= where the assert was raised !
28+
// .....
29+
//
30+
var errorline = a.stack.split('\n')[2];
31+
var m = errorline.match(/at (.*)\((.*):([0-9]*):([0-9]*)\)/);
32+
var func = m[1]; // Object.<anonymous> ( not very useful)
33+
var file = m[2]; // filename
34+
var lineno = parseInt(m[3]);
35+
var fullsource = fs.readFileSync(file, 'utf8');
36+
var line = fullsource.split('\n')[lineno-1];
37+
var src = line.match(/.*assert\((.*)\)/)[1];
38+
var err = new AssertionError({
39+
message: src + "\n ",
40+
stackStartFunction: assert
41+
});
42+
throw err;
3843
}
44+

test/test-babel.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run with babel-node
2+
// install:
3+
// * npm install --save-dev babel-core
4+
// * npm install --save-dev babel-preset-es2015
5+
// * npm install -g babel
6+
// babel-node --presets es2015 ./test/test-babel.hs
7+
//
8+
// should display:
9+
// throw err;
10+
// ^
11+
// AssertionError: 1==2,"1 should be 2"
12+
//
13+
// at myFunction (/projects/better-assert/test/test-babel.js:23:5)
14+
// at Object.<anonymous> /projects/better-assert/test/test-babel.js:20:1)
15+
// at Module._compile (module.js:570:32)
16+
//...
17+
18+
import assert from "..";
19+
20+
21+
22+
function myFunction() {
23+
assert(1==2,"1 should be 2");
24+
25+
}
26+
27+
myFunction();

0 commit comments

Comments
 (0)