From 8e32f5376eed6378b8c2eedffbb84bb830e1d88b Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Fri, 9 Sep 2016 18:38:50 -0400 Subject: [PATCH] object rest spread useBuiltIns option (#4491) * feat(transform-object-rest-spread): add polyfill=false option to avoid extends helper * object-rest-spread: add useBuiltIns option * add test for invalid option --- .../README.md | 28 +++++++++++++++++++ .../src/index.js | 11 +++++++- .../assignment-invalid-option/actual.js | 1 + .../assignment-invalid-option/options.json | 4 +++ .../useBuiltIns/assignment-true/actual.js | 1 + .../useBuiltIns/assignment-true/expected.js | 1 + .../test/fixtures/useBuiltIns/options.json | 3 ++ 7 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/actual.js create mode 100644 packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/options.json create mode 100644 packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/actual.js create mode 100644 packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/expected.js create mode 100644 packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/options.json diff --git a/packages/babel-plugin-transform-object-rest-spread/README.md b/packages/babel-plugin-transform-object-rest-spread/README.md index 2f50223182af3..622cfa208bffd 100644 --- a/packages/babel-plugin-transform-object-rest-spread/README.md +++ b/packages/babel-plugin-transform-object-rest-spread/README.md @@ -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 @@ -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 diff --git a/packages/babel-plugin-transform-object-rest-spread/src/index.js b/packages/babel-plugin-transform-object-rest-spread/src/index.js index ace0dbab896a6..ec78fd61a8fd4 100644 --- a/packages/babel-plugin-transform-object-rest-spread/src/index.js +++ b/packages/babel-plugin-transform-object-rest-spread/src/index.js @@ -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 = []; @@ -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)); } } }; diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/actual.js b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/actual.js new file mode 100644 index 0000000000000..8d610842d85f6 --- /dev/null +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/actual.js @@ -0,0 +1 @@ +z = { x, ...y }; diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/options.json b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/options.json new file mode 100644 index 0000000000000..f2e08e40c795f --- /dev/null +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-invalid-option/options.json @@ -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)" +} diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/actual.js b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/actual.js new file mode 100644 index 0000000000000..8d610842d85f6 --- /dev/null +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/actual.js @@ -0,0 +1 @@ +z = { x, ...y }; diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/expected.js b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/expected.js new file mode 100644 index 0000000000000..a12ddf13d4f86 --- /dev/null +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/assignment-true/expected.js @@ -0,0 +1 @@ +z = Object.assign({ x }, y); diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/options.json b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/options.json new file mode 100644 index 0000000000000..6cea69c74d78c --- /dev/null +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/useBuiltIns/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["transform-object-rest-spread", { "useBuiltIns": true }]] +}