Skip to content

Commit

Permalink
object rest spread useBuiltIns option (babel#4491)
Browse files Browse the repository at this point in the history
* feat(transform-object-rest-spread): add polyfill=false option to avoid extends helper

* object-rest-spread: add useBuiltIns option

* add test for invalid option
  • Loading branch information
hzoo authored and panagosg7 committed Jan 17, 2017
1 parent 23a7ab7 commit 8e32f53
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/babel-plugin-transform-object-rest-spread/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

Compile object rest and spread to ES5

```js
// source
z = { x, ...y };

// compiled
_extends = Object.assign || function(target) { ... }
z = _extends({ x }, y);
```

## Installation

```sh
Expand All @@ -20,6 +29,25 @@ $ npm install babel-plugin-transform-object-rest-spread
}
```

## Options

This plugin will use babel's `extends` helper, which will polyfill `Object.assign` by default.

* `useBuiltIns` - Do not use Babel's helper's and just transform to use the built-in method (Disabled by default).

```js
{
"plugins": [
["transform-object-rest-spread", { "useBuiltIns": true }]
]
}

// source
z = { x, ...y };
// compiled
z = Object.assign({ x }, y);
```

### Via CLI

```sh
Expand Down
11 changes: 10 additions & 1 deletion packages/babel-plugin-transform-object-rest-spread/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default function ({ types: t }) {
ObjectExpression(path, file) {
if (!hasSpread(path.node)) return;

let useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {
throw new Error("transform-object-rest-spread currently only accepts a boolean option for useBuiltIns (defaults to false)");
}

let args = [];
let props = [];

Expand All @@ -39,7 +44,11 @@ export default function ({ types: t }) {
args.unshift(t.objectExpression([]));
}

path.replaceWith(t.callExpression(file.addHelper("extends"), args));
const helper = useBuiltIns ?
t.memberExpression(t.identifier("Object"), t.identifier("assign")) :
file.addHelper("extends");

path.replaceWith(t.callExpression(helper, args));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z = { x, ...y };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": [["transform-object-rest-spread", { "useBuiltIns": "invalidOption" }]],
"throws": "transform-object-rest-spread currently only accepts a boolean option for useBuiltIns (defaults to false)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z = { x, ...y };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z = Object.assign({ x }, y);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["transform-object-rest-spread", { "useBuiltIns": true }]]
}

0 comments on commit 8e32f53

Please sign in to comment.