rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

TypeError: Cannot read property 'referencePaths' of undefined #59

Closed asos-craigmorten closed 3 years ago

asos-craigmorten commented 4 years ago

Ran into the error TypeError: Cannot read property 'referencePaths' of undefined while playing with swapping custom Rollup for microbundle on an existing project.

The error seems to arise when attempting to destructure the error argument in a catch statement.

Minimal code for reproduction:

const causesCatchBug = async () => {
  try {
    return await Promise.resolve(true);
  } catch ({ message }) { // Destructuring the error causes the error.
    console.log(message);
  }
};

module.exports = causesCatchBug;

As far as I'm concerned this is small issue and easily remedied from the consumers end by not destructuring (not sure if this is particularly best practice anyway - would certainly fall over if anything other than an Object was thrown!).

Raising this issue for reference so future consumers can easily find what the issue is and how to remedy it.

Abridged stack trace:

TypeError: Cannot read property 'referencePaths' of undefined
at Object.rewriteAwaitOrYieldPath (./microbundle-issue/node_modules/babel-plugin-transform-async-to-promises/async-to-promises.js:2475:75)
    at NodePath._call (./microbundle-issue/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (./microbundle-issue/node_modules/@babel/traverse/lib/path/context.js:42:17)
    at NodePath.visit (./microbundle-issue/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (./microbundle-issue/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitSingle (./microbundle-issue/node_modules/@babel/traverse/lib/context.js:84:19)
    at TraversalContext.visit (./microbundle-issue/node_modules/@babel/traverse/lib/context.js:140:19)
    at Function.traverse.node (./microbundle-issue/node_modules/@babel/traverse/lib/index.js:84:17)
    at NodePath.visit (./microbundle-issue/node_modules/@babel/traverse/lib/path/context.js:97:18)
    at TraversalContext.visitQueue (./microbundle-issue/node_modules/@babel/traverse/lib/context.js:112:16)

Issue location in code: https://github.com/rpetrich/babel-plugin-transform-async-to-promises/blob/d28455f0cc496d7c7005ea5975ad44cf9383eb6c/async-to-promises.ts#L3270

judofyr commented 4 years ago

I saw the same error, but it didn't seem to be caused by a destructuring in catch. Instead it happened due to a for await ….

Workaround: Adding await true; on top of the function.

asos-craigmorten commented 3 years ago

Closing as stale and no longer impacting my projects. If impacting others feel free to-reopen!