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

Deep optional chaining breaks in function argument destructuring #11166

Open
maxdavidson opened this issue Feb 24, 2020 · 3 comments
Open

Deep optional chaining breaks in function argument destructuring #11166

maxdavidson opened this issue Feb 24, 2020 · 3 comments

Comments

@maxdavidson
Copy link

@maxdavidson maxdavidson commented Feb 24, 2020

Bug Report

  • I would like to work on a fix!

Current Behavior

Optional chaining breaks when destructuring deep chains inside the function arguments, due to incorrect hoisting of temporary variables.

REPL

Input Code

function hello({
  a,
  b = a?.b?.c,
}) {
  return 0;
}

Output Code

"use strict";

function hello({
  a,
  b = a === null || a === void 0 ? void 0 : (_a$b = a.b) === null || _a$b === void 0 ? void 0 : _a$b.c
}) {
  var _a$b;

  return 0;
}

Expected behavior/code

The code should assign a value to b, and not fail with _a$b is not defined.

Babel Configuration

// .babelrc
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": { "node": "current" }
      }
    ]
  ]
}

Environment

  • Babel version(s): 7.8.4
  • Node/npm version: 13.8.0/6.13.7

Possible Solution

Either move the hoisted variable to the module scope, or use a temporary variable for the argument and perform the destructuring inside the function body.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Feb 24, 2020

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

@jridgewell

This comment has been minimized.

Copy link
Member

@jridgewell jridgewell commented Feb 24, 2020

We keep having bugs related to injecting temp variables in a param scope.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Feb 24, 2020

Maybe in Babel 8 we could add a "transparent" scope (transparent similar to how we handle block scopes) to represent params scope. Or even earlier if it can be done in a non-breaking way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.