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

`object-rest-spread` does not transpile correctly when binding is referenced in param initializer #11281

Open
JLHwung opened this issue Mar 18, 2020 · 5 comments · May be fixed by #11326
Open

`object-rest-spread` does not transpile correctly when binding is referenced in param initializer #11281

JLHwung opened this issue Mar 18, 2020 · 5 comments · May be fixed by #11326

Comments

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Mar 18, 2020

Bug Report

  • I would like to work on a fix!

Input Code

({ a, ...O }, b = O, c = a) => {}

Current Behavior
The code above is transformed to

(_ref, b = O, c = a) => {
  let {
    a
  } = _ref,
      O = babelHelpers.objectWithoutProperties(_ref, ["a"]);
};

where initializers b = O and c = a no longer refers to the destructured params.

Expected behavior/code
It should transformed to

(function (_ref) {
  let { a } = _ref,
      O = babelHelpers.objectWithoutProperties(_ref, ["a"]);
  var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : O;
  var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : a;
});

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

  • Filename: babel.config.js
{
  "plugins": { "@babel/proposal-object-rest-spread" }
}

Environment: REPL

Possible Solution
I haven't come up with a simple solution. Suggestions are welcome.

Solution 1: Fall back to transform-parameters when such pattern is detected

i) A function param declarator contains a RestElement.
ii) The declarator is also referenced by another params' initializers.

This solution will downgrade unnecessary codes, i.e.

({ a, ...O }, b = 2, c = O) => {}

will be transformed to

(function (_ref) {
  var a = _ref.a,
      O = babelHelpers.objectWithoutProperties(_ref, ["a"]);
  var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : O;
});

even when b = 2 is supported on the target platform.

Solution 2: Extract convertSingleParam from convertParams defined in transform-parameters, apply convertSingleParam if one of params satisfies the pattern mentioned above.

This solution should prevent downgrading the code as much as possible. i.e.

({ a, ...O }, b = 2, c = O) => {}

should be transformed to

(function (_ref, b = 2) {
 let { a } = _ref,
      O = babelHelpers.objectWithoutProperties(_ref, ["a"]);
   var c = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : O;
});

Additional context/Screenshots
This issue is discovered when I am investigating #11278.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Mar 18, 2020

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

@TrejGun

This comment has been minimized.

Copy link

@TrejGun TrejGun commented Mar 20, 2020

is it valid to use number as rest param?

@JLHwung

This comment has been minimized.

Copy link
Contributor Author

@JLHwung JLHwung commented Mar 20, 2020

@TrejGun In ({ a, ...O }, b = 2, c = O) => {}, the rest element is ...O while 2 is the initializer of param b. { a, ...O }, b = 2, c = O may looks like a big destructuring param but it is not. 😄

@TrejGun

This comment has been minimized.

Copy link

@TrejGun TrejGun commented Mar 21, 2020

i mean part {a, ...0} ,0 (zero) a not valid name for variable.
in other words you can't do const 0 = 0
why it should be possible with destruction?

@vedantroy

This comment has been minimized.

Copy link
Contributor

@vedantroy vedantroy commented Mar 21, 2020

This issue seems interesting, I'll try fixing it.

@TrejGun I think the 0 is actually a capital-O. Maybe a different letter should be used, to make it less confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.