question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
vedantroycommented, 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.

0reactions
TrejGuncommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is babel-loader not transforming object rest/spread?
I used the debug option of babel to check if the plugin is actual used and the output says it does. But when...
Read more >
babel/plugin-transform-typescript
This plugin adds support for the types syntax used by the TypeScript programming language. However, this plugin does not add the ability to...
Read more >
@babel/plugin-proposal-object-rest-spread - Package Manager
Fast, reliable, and secure dependency management.
Read more >
@babel/core - NPM Package Versions - Socket
#8566 Correctly update bindings of decorated class declarations (@nicolo-ribaudo) ... #14913 fix: do not remove type import used in TS import= (@JLHwung) ...
Read more >
Babel v6 Changelog - Fossies
This will help out with reusing a browserslist file for babel-preset-env and for plugins like https://github.com/tleunen/babel-plugin-module-resolver.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found