better-assert icon indicating copy to clipboard operation
better-assert copied to clipboard

does not work with babel

Open raine opened this issue 9 years ago • 9 comments

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 avatar Oct 29 '15 21:10 raine

@raine I will fix this as soon as I can . Besides if you have good idea about this, a PR is welcomed.

adohe-zz avatar Oct 30 '15 01:10 adohe-zz

@raine Can you share steps to reproduce this error? I'm quite curious. This is important anyway

devinrhode2 avatar Jun 30 '16 05:06 devinrhode2

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.

thenickdude avatar Jun 30 '16 06:06 thenickdude

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.

guncha avatar Dec 07 '16 16:12 guncha

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)
[...]

erossignon avatar Jan 28 '17 14:01 erossignon

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 avatar Jan 28 '17 15:01 erossignon

@erossignon really glad to see this, could you please open a PR?

adohe-zz avatar Feb 04 '17 02:02 adohe-zz

Is there any reason #16 has not been merged?

thevtm avatar Mar 11 '18 10:03 thevtm

When the PR is going to be merged?

adeleur avatar Feb 06 '19 00:02 adeleur