Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not work with babel #12

Open
raine opened this issue Oct 29, 2015 · 9 comments · May be fixed by #20
Open

does not work with babel #12

raine opened this issue Oct 29, 2015 · 9 comments · May be fixed by #20

Comments

@raine
Copy link

raine commented Oct 29, 2015

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.

@adohe-zz
Copy link
Collaborator

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

@devinrhode2
Copy link
Contributor

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

@thenickdude
Copy link

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.

@guncha
Copy link

guncha commented Dec 7, 2016

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.

@erossignon
Copy link

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
Copy link

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;
 }

@adohe-zz
Copy link
Collaborator

adohe-zz commented Feb 4, 2017

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

erossignon added a commit to erossignon/better-assert that referenced this issue Feb 5, 2017
@thevtm
Copy link

thevtm commented Mar 11, 2018

Is there any reason #16 has not been merged?

fatso83 pushed a commit to fatso83/better-assert that referenced this issue Apr 23, 2018
fatso83 pushed a commit to fatso83/better-assert that referenced this issue Apr 23, 2018
GerHobbelt added a commit to GerHobbelt/better-assert that referenced this issue Dec 11, 2018
…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.
GerHobbelt added a commit to GerHobbelt/better-assert that referenced this issue Dec 11, 2018
…file is pulled through `babel` the tests coded in there would fail due the code having been prettyfied by babel. The tests have been adjusted.
GerHobbelt added a commit to GerHobbelt/better-assert that referenced this issue Dec 11, 2018
…when babel has transpiled all `assert(...)` calls to something else (`(0, _.default)(...)` for example), we look for that transpiled code instead. (assuming Babel 7)

  Defensive coding ensures no crash when these assumptions don't match reality: instead of the function arguments, '???' is reported instead.
- Completely removed obsolete callsite references in the code.
@adeleur
Copy link

adeleur commented Feb 6, 2019

When the PR is going to be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants