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

path.buildCodeFrameError('Error') throws #8617

Open
pasaran opened this issue Sep 3, 2018 · 11 comments
Open

path.buildCodeFrameError('Error') throws #8617

pasaran opened this issue Sep 3, 2018 · 11 comments

Comments

@pasaran
Copy link

pasaran commented Sep 3, 2018

Bug Report

Current Behavior
path.buildCodeFrameError( 'Some error' ) throws error:

/Users/nop/tmp/node_modules/@babel/traverse/lib/path/index.js:157
    return this.hub.buildError(this.node, msg, Error);
                    ^

TypeError: Cannot read property 'buildError' of undefined
    at NodePath.buildCodeFrameError (/Users/nop/tmp/node_modules/@babel/traverse/lib/path/index.js:157:21)
    at Program (/Users/nop/tmp/test.js:9:20)
    at NodePath._call (/Users/nop/tmp/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/Users/nop/tmp/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/Users/nop/tmp/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/Users/nop/tmp/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitSingle (/Users/nop/tmp/node_modules/@babel/traverse/lib/context.js:90:19)
    at TraversalContext.visit (/Users/nop/tmp/node_modules/@babel/traverse/lib/context.js:146:19)
    at Function.traverse.node (/Users/nop/tmp/node_modules/@babel/traverse/lib/index.js:94:17)
    at traverse (/Users/nop/tmp/node_modules/@babel/traverse/lib/index.js:76:12)

Input Code

const parser = require( '@babel/parser' );
const traverse = require( '@babel/traverse' ).default;
const { codeFrameColumns } = require( '@babel/code-frame' );

const CODE = 'var a = 1;';
const ERROR = 'Number is too small';

const ast = parser.parse( CODE );

traverse( ast, {
    NumericLiteral: function( path ) {
        throw path.buildCodeFrameError( 'Some error' );
        //  throw codeFrameColumns( CODE, path.node.loc, ERROR );
    },
} );

Expected behavior/code
throw path.buildCodeFrameError( ERROR ) should do the same as throw codeFrameColumns( CODE, path.node.loc, ERROR ); (I guess).

Babel Configuration (.babelrc, package.json, cli command)

//  package.json:

  "devDependencies": {
    "@babel/parser": "^7.0.0",
    "@babel/traverse": "^7.0.0"
  }

Environment

  • Babel version(s): 7.0.0
  • Node/npm version: v9.0.0
  • OS: OSX 10.12.6 (16G1408)
  • How you are using Babel: cli
@babel-bot
Copy link
Collaborator

Hey @pasaran! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@loganfsmyth
Copy link
Member

Strange, I'd expect it to have that function from

buildError(node, msg, Error = TypeError): Error {
Maybe we missed initializing this.hub somewhere.

@monokrome
Copy link

@loganfsmyth It doesn't have that, because hub is the value that is undefined. Since hub is undefined, buildError can not be referenced. undefined.buildError throws this error.

@loganfsmyth
Copy link
Member

It looks like I probably pasted the wrong link when I posted that, woops.

@monokrome
Copy link

@loganfsmyth I'm running into this right now - where were you intending to link to?

@loganfsmyth
Copy link
Member

I honestly think at the moment we just don't ever set a default hub, and there is no way to set it via traverse without doing the same stuff that Babel itself does to set up the initial traversal.

Depending on your usecase for buildCodeFrameError, it might make more sense to use @babel/core to trigger the traversal. Otherwise, I'd avoid using buildCodeFrameError in your code. If you're seeing it error in code other that your own, then it's still an indication of an error, it's just failing to throw a nice error for you.

@monokrome
Copy link

When I import { generate } from '@babel/core', it doesn't actually exist. Is there a way to get a reference to and pass the hub manually?

@loganfsmyth
Copy link
Member

Sorry, I don't quite follow what you're using generate for in this context. Would you be up for joining our Slack to continue this discussion? I think it's hard to have back-and-forth like this on Github.

noppa added a commit to noppa/translation-compiler that referenced this issue Jun 5, 2019
TODO: This still does not work correctly because of babel/babel#8617
Must use @babel/core.traverse instead.
@lcoder
Copy link

lcoder commented Nov 29, 2019

Hub. buildError
It seems that the features have not been implemented

node parameter is not used

I solved with

const code = require( '@babel/generator' ).default( path.node , {} ).code
throw new Error( `\n${code}\n ^^^^ your message` )

@motiz88
Copy link
Contributor

motiz88 commented Mar 20, 2020

Some version of this issue seems to have been reintroduced by #10914 in 7.8.0.

Before 7.8.0, @babel/core's parseSync would call normalizeFile which in turn would initialise hub, so traverseing and calling path.buildCodeFrameError worked.

Since 7.8.0, @babel/core's parseSync does not call normalizeFile, so hub isn't initialised and calling path.buildCodeFrameError within a traversal fails again.

@skipjack
Copy link

skipjack commented Dec 30, 2020

Yeah I'm still seeing this error when using parcel but only when building for production (i.e. parcel serve works fine but parcel build fails). Reverting babel to an older version based on @motiz88's comment above to see if that addresses the issue (doesn't feel like a great fix though, would love to help find a cleaner solution).

Update: Installing older versions of @babel/core and @babel/traverse didn't work for me. That doesn't mean it won't work for anyone but at least with parcel@2.0.0-beta.1 I still get the following error:

image

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

No branches or pull requests

7 participants