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

Invalid output resulting in ReferenceErrors with plugin-proposal-optional-chaining #12026

Closed
1 task
felixfbecker opened this issue Sep 1, 2020 · 2 comments · Fixed by #12032
Closed
1 task
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@felixfbecker
Copy link

felixfbecker commented Sep 1, 2020

Bug Report

  • I would like to work on a fix!

Current behavior

When a nested optional chaining expression is found inside a parameter position (a parameter default value, or default value inside an object or array destructuring), a var declaration is added to the function body. That variable declaration is not in effect when evaluating parameter default values and causes a ReferenceError in strict mode when the function is called.

To repro, you can use the Babel CLI to compile the code below and run it with Node: babel test.js | node. Error output:

const foo = (param = (_obj$prop = obj.prop1) === null || _obj$prop === void 0 ? void 0 : _obj$prop.prop2) => {
                                ^

ReferenceError: _obj$prop is not defined
    at foo ([stdin]:5:33)
    at [stdin]:11:1
    at Script.runInThisContext (vm.js:132:18)
    at Object.runInThisContext (vm.js:315:38)
    at Object.<anonymous> ([stdin]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:1256:30)
    at evalScript (internal/process/execution.js:98:25)
    at internal/main/eval_stdin.js:29:5
    at Socket.<anonymous> (internal/process/execution.js:211:5)
    at Socket.emit (events.js:326:22)

Input Code

'use strict'
const obj = {}
const foo = (param = obj.prop1?.prop2) => {
  console.log(param)
}
foo()

Output Code

'use strict';

const obj = {};

const foo = (param = (_obj$prop = obj.prop1) === null || _obj$prop === void 0 ? void 0 : _obj$prop.prop2) => {
  var _obj$prop;

  console.log(param);
};

foo();

Expected behavior
Should not throw a ReferenceError, should log undefined to the console.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js

See REPL link above, or just add the plugin-proposal-optional-chaining (doesn't matter whether loose or not, happens in both modes)

{
  "plugings": [
    "@babel/plugin-proposal-optional-chaining"
  ]
}

Environment

  • Babel version(s): 7.11.5
  • Node/npm version: v14.7.0
  • OS: macOS
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: CLI for repro
@babel-bot
Copy link
Collaborator

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

@JLHwung
Copy link
Contributor

JLHwung commented Sep 1, 2020

Duplicate of #11166

@JLHwung JLHwung closed this as completed Sep 1, 2020
@JLHwung JLHwung marked this as a duplicate of #11166 Sep 1, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 2, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants