better-assert
better-assert copied to clipboard
does not work with babel
TypeError: Cannot read property '1' of null
The callsite returns a line number for compiled code but it's trying to read the line from uncompiled one.
@raine I will fix this as soon as I can . Besides if you have good idea about this, a PR is welcomed.
@raine Can you share steps to reproduce this error? I'm quite curious. This is important anyway
Here's a repository that reproduces it:
https://github.com/thenickdude/better-assert-12
Clone it, run "npm install", then either "npm run-script test-babel-node" to run index.js with babel-node
, which produces this error:
/node_modules/better-assert/index.js:30
var src = line.match(/assert\((.*)\)/)[1];
^
TypeError: Cannot read property 'match' of undefined
at assert (/node_modules/better-assert/index.js:30:17)
at Object.<anonymous> (index.js:3:1)
at Module._compile (module.js:409:26)
at loader (/node_modules/babel-cli/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/node_modules/babel-cli/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at /node_modules/babel-cli/lib/_babel-node.js:160:24
at Object.<anonymous> (/node_modules/babel-cli/lib/_babel-node.js:161:7)
Or run "npm run-script test-precompile" to translate index.js using babel-cli
, then run it with regular node
, which produces this error:
/node_modules/better-assert/index.js:30
var src = line.match(/assert\((.*)\)/)[1];
^
TypeError: Cannot read property '1' of null
at assert (/node_modules/better-assert/index.js:30:41)
at Object.<anonymous> (/index-out.js:9:28)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
I'm using Node v4.4.4.
I'm running into the same issue, but with using Typescript and ts-node
. The issue is actually with the callsite
library as it seems to ignore source-maps. If I do a new Error().stack
and access the source line from there, it will take into account any source maps and give me the correct line number.
with this ```better-assert-bug-es6.js```` file:
import assert from "better-assert";
assert(false);
$babel-node better-assert-bug-es6.js
produces:
[...]\node_modules\better-assert\index.js:30
var src = line.match(/assert\((.*)\)/)[1];
^
TypeError: Cannot read property 'match' of undefined
at assert (C:\projects\node_modules\better-assert\index.js:30:17)
at Object.<anonymous> (C:/projects/better-assert-bug-es6.js:6:1)
[...]
here is my patch
function assert(expr) {
if (expr) return;
var a = new Error();
var errorline = a.stack.split("\n")[2];
var m = errorline.match(/.*(\w)*\((.*):([0-9]*):([0-9]*)\)/);
var func = m[1];
var file = m[2];
var lineno = parseInt(m[3]);
var fullsource = fs.readFileSync(file, 'utf8');
var line = fullsource.split('\n')[lineno-1];
var src = line.match(/assert\((.*)\)/)[1];
var err = new AssertionError({
message: src,
stackStartFunction: func
});
throw err;
}
@erossignon really glad to see this, could you please open a PR?
Is there any reason #16 has not been merged?
When the PR is going to be merged?