Skip to content

Babel should output the current file being transformed if there's an unexpected error #10505

@hzoo

Description

@hzoo

Bug Report

Current Behavior
After reading #10502, I noticed that an important issue was that the error messages from @babel/traverse and thus @babel/core don't include the file being processed which would of made debugging much easier. I assumed this was the case already since we do output the file for an expected error like a syntax error, but there are a lot like this?

SyntaxError: /home/nicolo/Documenti/dev/babel/babel/demo.js: Legacy octal literals are not allowed in strict mode (11:0)

   9 | const parser = load("parser");
  10 | const generate = load("generator").default;
> 11 | 00 0
     | ^

But when there's an internal error it doesn't:

TypeError: Cannot read property 'name' of null
    at PluginPass.Program (/Users/henryzhu/dev/a/node_modules/@babel/plugin-transform-typescript/lib/index.js:167:57)
    at newFn (/Users/henryzhu/dev/a/node_modules/@babel/traverse/lib/visitors.js:193:21)

Input Code

This kind of error doesn't happen from a parsing error so it's more around how things are configured. There's probably an easier way to throw this kind of error via other bug reports but for now, I would reproduce the error from #10502? Another option is to just throw an error anywhere in @babel/core or some plugin/preset.

yarn add @babel/cli @babel/core @babel/preset-typescript

//.babelrc

{
  "presets": ["@babel/preset-typescript"]
}

make a a.d.ts file

export default function<T>(x: T): T;

run npx babel a.d.ts

Expected behavior/code

We'd need to add the filename (and maybe other relevant information could be in a debug mode like #2960) to the error message somehow like for our SyntaxError.

Possible Solution

We would need to wrap our main transform() call in @babel/core with a try/catch to modify the error message.

transformFile(file, config.passes);

try {
  transformFile(file, config.passes);
} catch (err) {
  // throw new Error(err); and add the filename
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: errorsoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions