question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.hub is undefined in Babel traverse

See original GitHub issue

Bug Report

This related to #8617

  • I would like to work on a fix!

Current Behavior @babel/parser has a different behavior when estree enabled, and when not.

Input Code When estree disabled code with Duplicated variable declaration throws while parsing:

const {parse} = require('@babel/parser');
const traverse = require('@babel/traverse').default;

const code = `function square(n) {
  const n = 1;
}`;

// crash with an error: Identifier 'n' has already been declared (1:16)
const ast = parse(code);

When estree mode enabled code is parsed, but can’t be traversed because of this.hub is undefined:

const {parse} = require('@babel/parser');
const traverse = require('@babel/traverse').default;

const code = `function square(n) {
  const n = 1;
}`;

const ast = parse(code, {
    plugins: [
        'estree',
    ]
});

traverse(ast, {
  enter(path) {
      console.log('n');
  }
});

/*
    throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
                     ^

TypeError: Cannot read property 'buildError' of undefined
    at Scope.checkBlockScopedCollisions (/home/coderaiser/node_modules/@babel/traverse/lib/scope/index.js:402:22)
*/

Same result when instead of estree plugin, errorRecovery enabled.

Expected behavior/code Would be great if in estree mode babel throws while parsing and/or can throw while traversing about Duplicate declaration.

Environment

  • Babel version(s): v7.9.4
  • Node/npm version: Node 12(13)/npm 6
  • OS: Ubuntu
  • Monorepo: no
  • How you are using Babel: parse, traverse

Possible Solution

I suggest to use TypeError, because this is what hub actually does, this way:

throw TypeError(`Duplicate declaration "${name}"`);

If of course buildError doesn’t used in development purpose to set breakpoint on error building, anyways Chrome Developer Tools debugger already suggest similar feature pause on exceptions (caught and uncaught).

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
JLHwungcommented, Jun 15, 2020

@rob-myers Please don’t rely on the internal file structures on @babel/core, they could change. Instead you can import File from the wildcard babel

import * as babel from '@babel/core';
const File = babel.File;
1reaction
JLHwungcommented, Mar 30, 2020

You can wrap the parsed ast into a File class (defined in @babel/core) so this.hub is available, like we did in #10575.

I admit that this workaround is not ideal as ultimately babel/traverse should be allowed to run as a standalone package. Historically there has been attempt on fixing this issue (#5050) but later reverted in #5306.

@coderaiser I think we can surely revisit this issue and see if we can re-land #5050. PR is welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the babel-traverse function in babel-traverse - Snyk
To help you get started, we've selected a few babel-traverse examples, based on popular ways it is used in public projects. Secure your...
Read more >
babel/traverse - Babel.js
We can use it alongside the babel parser to traverse and update nodes: import * as parser from "@babel/parser"; import traverse from "@babel/traverse"; ......
Read more >
@babel/helper-wrap-function | Yarn - Package Manager
#10161 Improves the logic to import objects in helpers (@ifsnow). babel-traverse. #10243 perf: always return void 0 as undefined node (@JLHwung) ...
Read more >
How Babel Is Built - vivaxy's Blog
Identifier: Including variable names, undefined and null e.t.c. ... @babel/traverse provides a way of walk through all AST nodes, like:.
Read more >
How to get code as a string from Babel node during traverse
Each AST node returned by @babel/parser has a start and end properties pointing to it's location in the source code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found