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

The scope of the arguments default value is different from what was expected in ES2015 (参数默认值的作用域与 ES2015 的预期结果不同) #9947

Closed
Yangfan2016 opened this issue May 6, 2019 · 2 comments · Fixed by #10053
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Yangfan2016
Copy link

Yangfan2016 commented May 6, 2019

Bug Report

Current Behavior
The scope of the arguments default value is different from what was expected in ES2015

Input Code

online view

This is source code of ES2015:

let foo = 'outer';

function bar(func = () => foo) {
  let foo = 'inner';
  console.log(func());
}

bar(); // outer

The result of run it:

outer

This is transform code by babel:

"use strict";

var foo = 'outer';

function bar() {
  var func = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
    return foo;
  };
  var foo = 'inner';
  console.log(func());
}

bar(); // outer

The result of run it:

inner

Expected behavior/code

Expect the results of the Babel conversion to be consistent with ES2015 performance

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

Environment

  • Babel version(s): [v7.4.4]
  • Node/npm version: [Node 8.9/npm 5]
  • OS: [Windows 10]
  • Monorepo: [no]
  • How you are using Babel: [babeljs.io]

Possible Solution

"use strict";

var foo = 'outer';

function bar() {
    var func = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function() {
        return foo;
    }
    ;
    (function() {

        var foo = 'inner';
        console.log(func());

    }())
}

bar();

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot
Copy link
Collaborator

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

@Yangfan2016 Yangfan2016 changed the title The scope of the parameter default value is different from what was expected in ES2015 (参数默认值的作用域与 ES2015 的预期结果不同) The scope of the arguments default value is different from what was expected in ES2015 (参数默认值的作用域与 ES2015 的预期结果不同) May 6, 2019
@JLHwung
Copy link
Contributor

JLHwung commented Jun 3, 2019

Hi 杨帆,

Thanks for the reproducing examples. It helps a lot narrow down the problem.

The possible solution you mentioned above is promising. In fact there is a iife state in the transform-parameters to determine whether the whole function body should be wrapped into an extra closure.

It is interesting to find that there was a similar bug people had already solved here, so as a workaround, you can replace

function bar(func = () => foo) {
}

to

function bar(func = () => eval("foo")) {
}

And babel will work this time. I admit that it is ugly hack and the bug should be fixed in transform-parameters. I would look into this in a couple of days.

@JLHwung JLHwung added the Has PR label Oct 15, 2019
@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 Mar 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: bug 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