Skip to content

object-rest-spread does not transform once sufficiently nested #5648

@CKarper

Description

@CKarper

Choose one: is this a bug report or feature request?

Bug.
babel-plugin-transform-object-rest-spread is not properly transforming code in some circumstances when deeply nested.

Input Code

REPL Example

function test([{ ...payload }]) {
	console.dir(payload);
}

test([{}]);

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

.babelrc:

{
	"presets": [
		["env", { targets: { node: 6 } }]
	],
	"plugins": ["transform-object-rest-spread"]
}

Expected Behavior

I would have expected output along the lines of:

"use strict";

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function test(_ref) {
	let payload = _objectWithoutProperties(_ref[0], []);

	console.dir(payload);
}

test([{}]);

Current Behavior

This transforms to:

"use strict";

function test([{ ...payload }]) {
	console.dir(payload);
}

test([{}]);

Possible Solution

I was using babel-preset-env alongside, and setting it's target to include node v4 or lower, or any browser version results in working code. Alternatively, you can add the following to the env config block in .babelrc:

	include: [
		"transform-es2015-parameters",
		"transform-es2015-destructuring",
	],

Context

I was trying to roll context forward in a Promise chain, so I was using Promise.all(), each .then() clause was destructuring to get its needed information... That gave the magic combination of destructuring an object arg, in an array, in function args.

Your Environment

software version(s)
Babel 6.24.1
babel-preset-env 1.4.0
babel-plugin-transform-object-rest-spread 6.23.0
node 6.10.0
npm 3.10.10
Operating System MacOS 10.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRSpec: Object Rest/SpreadoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions