File tree Expand file tree Collapse file tree 5 files changed +53
-17
lines changed Expand file tree Collapse file tree 5 files changed +53
-17
lines changed Original file line number Diff line number Diff line change 1
1
.DS_Store
2
2
node_modules
3
3
* .sock
4
+ .idea
5
+
Original file line number Diff line number Diff line change 2
2
test
3
3
examples
4
4
* .sock
5
+ .idea /
6
+
Original file line number Diff line number Diff line change 1
1
2
2
# better-assert
3
3
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.
6
5
7
6
## Installation
8
7
Original file line number Diff line number Diff line change 4
4
5
5
var AssertionError = require ( 'assert' ) . AssertionError
6
6
, callsite = require ( 'callsite' )
7
- , fs = require ( 'fs' )
7
+ , fs = require ( 'fs' ) ;
8
8
9
9
/**
10
10
* Expose `assert`.
@@ -21,18 +21,24 @@ module.exports = process.env.NO_ASSERT
21
21
function assert ( expr ) {
22
22
if ( expr ) return ;
23
23
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 ( / a s s e r t \( ( .* ) \) / ) [ 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 ( / a t ( .* ) \( ( .* ) : ( [ 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 ( / .* a s s e r t \( ( .* ) \) / ) [ 1 ] ;
38
+ var err = new AssertionError ( {
39
+ message : src + "\n " ,
40
+ stackStartFunction : assert
41
+ } ) ;
42
+ throw err ;
38
43
}
44
+
Original file line number Diff line number Diff line change
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 ( ) ;
You can’t perform that action at this time.
0 commit comments