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

Destructuring assignment inside of AsyncIterator cause error 'Cannot read property 'file' of undefined' #5880

Closed
brn opened this Issue Jun 22, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@brn

brn commented Jun 22, 2017

In async iteration body, babel throw error and compilation failed, if compile code that receive result of async function as destructuring array, but babel not throw error and compilation succeeded, if code that received result as destructuring object.

Input Code

async function g(t) {
  return new Promise(r => setTimeout(() => r([true]), t));
}

async function r() {
  for await (const t of [1000, 2000, 3000]) {
    const [result] = await g(t);
  }
}

r();

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

  "dependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-transform-async-generator-functions": "^6.24.1",
    "babel-plugin-transform-regenerator": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.23.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-stage-3": "^6.24.1",
    "babel-runtime": "^6.23.0"
  },
  "babel": {
    "plugins": [
      [
        "transform-runtime",
        {
          "helpers": false,
          "polyfill": false,
          "regenerator": true,
          "moduleName": "babel-runtime"
        }
      ]
    ],
    "presets": [
      "es2015",
      "stage-3"
    ]
  }

Expected Behavior

Compiled.

Current Behavior

Throw 'Cannot read property 'file' of undefined' Error

StackTrace:

TypeError: test.js: Cannot read property 'file' of undefined
    at Scope.toArray (/Users/xxxx/xxx/node_modules/babel-traverse/lib/scope/index.js:437:24)
    at DestructuringTransformer.toArray (/Users/xxxx/xxx/node_modules/babel-plugin-transform-es2015-destructuring/lib/index.js:122:27)
    at DestructuringTransformer.pushArrayPattern (/Users/xxxx/xxx/node_modules/babel-plugin-transform-es2015-destructuring/lib/index.js:277:26)
    at DestructuringTransformer.push (/Users/xxxx/xxx/node_modules/babel-plugin-transform-es2015-destructuring/lib/index.js:110:14)
    at DestructuringTransformer.init (/Users/xxxx/xxx/node_modules/babel-plugin-transform-es2015-destructuring/lib/index.js:317:12)
    at PluginPass.VariableDeclaration (/Users/xxxx/xxx/node_modules/babel-plugin-transform-es2015-destructuring/lib/index.js:468:27)
    at newFn (/Users/xxxx/xxx/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/Users/xxxx/xxx/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/Users/xxxx/xxx/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/Users/xxxx/xxx/node_modules/babel-traverse/lib/path/context.js:105:12)

Your Environment

software version(s)
Babel 6.24.1
node 8.0.0
npm 5.0.2
Operating System OS X El Capitan 10.11.6
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Jun 22, 2017

Collaborator

Hey @brn! 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.

Collaborator

babel-bot commented Jun 22, 2017

Hey @brn! 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.

@PsychoLlama

This comment has been minimized.

Show comment
Hide comment
@PsychoLlama

PsychoLlama Jul 8, 2017

Some additional notes: this bug only happens when using array destructuring in a for-await-of loop and the es2015 preset. Object destructuring isn't affected, and using stage-3 without es2015 doesn't throw this error.

By troubleshooting in the repl (link) I found it works if you use the es2015-loose preset. Not ideal though, so I tracked it down further. The minimal workaround is enabling loose mode on the transform-es2015-destructuring plugin.

.babelrc workaround

{
  "presets": ["es2015", "stage-3"],
  "plugins": [
    ["transform-es2015-destructuring", { "loose": true }]
  ]
}

Here's the smallest amount of code to reproduce the error:

input.js

(async () => {
  for await (const [value] of iterable) {}
})()
$ babel input.js --presets=es2015,stage-3

Hope it helps!

PsychoLlama commented Jul 8, 2017

Some additional notes: this bug only happens when using array destructuring in a for-await-of loop and the es2015 preset. Object destructuring isn't affected, and using stage-3 without es2015 doesn't throw this error.

By troubleshooting in the repl (link) I found it works if you use the es2015-loose preset. Not ideal though, so I tracked it down further. The minimal workaround is enabling loose mode on the transform-es2015-destructuring plugin.

.babelrc workaround

{
  "presets": ["es2015", "stage-3"],
  "plugins": [
    ["transform-es2015-destructuring", { "loose": true }]
  ]
}

Here's the smallest amount of code to reproduce the error:

input.js

(async () => {
  for await (const [value] of iterable) {}
})()
$ babel input.js --presets=es2015,stage-3

Hope it helps!

@babel-bot babel-bot added the Has PR label Jul 9, 2017

@lock lock bot added the outdated label May 4, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.