babel-plugin-transform-object-rest-spread
Advanced tools
Comparing version 6.19.0 to 6.20.0
124
lib/index.js
@@ -12,69 +12,22 @@ "use strict"; | ||
function hasRestProperty(node) { | ||
for (var _iterator = node.properties, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) { | ||
var _ref2; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref2 = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref2 = _i.value; | ||
function hasRestProperty(path) { | ||
var foundRestProperty = false; | ||
path.traverse({ | ||
RestProperty: function RestProperty() { | ||
foundRestProperty = true; | ||
} | ||
var property = _ref2; | ||
if (t.isRestProperty(property)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
return foundRestProperty; | ||
} | ||
function variableDeclarationHasRestProperty(node) { | ||
for (var _iterator2 = node.declarations, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) { | ||
var _ref3; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref3 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref3 = _i2.value; | ||
function hasSpread(path) { | ||
var foundSpreadProperty = false; | ||
path.traverse({ | ||
SpreadProperty: function SpreadProperty() { | ||
foundSpreadProperty = true; | ||
} | ||
var declar = _ref3; | ||
if (t.isObjectPattern(declar.id)) { | ||
return hasRestProperty(declar.id); | ||
} | ||
} | ||
return false; | ||
}); | ||
return foundSpreadProperty; | ||
} | ||
function hasSpread(node) { | ||
for (var _iterator3 = node.properties, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) { | ||
var _ref4; | ||
if (_isArray3) { | ||
if (_i3 >= _iterator3.length) break; | ||
_ref4 = _iterator3[_i3++]; | ||
} else { | ||
_i3 = _iterator3.next(); | ||
if (_i3.done) break; | ||
_ref4 = _i3.value; | ||
} | ||
var prop = _ref4; | ||
if (t.isSpreadProperty(prop)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function createObjectSpread(file, props, objRef) { | ||
@@ -84,15 +37,15 @@ var restProperty = props.pop(); | ||
var keys = []; | ||
for (var _iterator4 = props, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) { | ||
var _ref5; | ||
for (var _iterator = props, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) { | ||
var _ref2; | ||
if (_isArray4) { | ||
if (_i4 >= _iterator4.length) break; | ||
_ref5 = _iterator4[_i4++]; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref2 = _iterator[_i++]; | ||
} else { | ||
_i4 = _iterator4.next(); | ||
if (_i4.done) break; | ||
_ref5 = _i4.value; | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref2 = _i.value; | ||
} | ||
var prop = _ref5; | ||
var prop = _ref2; | ||
@@ -110,3 +63,3 @@ var key = prop.key; | ||
function replaceRestProperty(paramsPath, i, numParams) { | ||
if (paramsPath.isObjectPattern() && hasRestProperty(paramsPath.node)) { | ||
if (paramsPath.isObjectPattern() && hasRestProperty(paramsPath)) { | ||
var parentPath = paramsPath.parentPath; | ||
@@ -176,3 +129,3 @@ var uid = parentPath.scope.generateUidIdentifier("ref"); | ||
if (!declaration.isVariableDeclaration()) return; | ||
if (!variableDeclarationHasRestProperty(declaration.node)) return; | ||
if (!hasRestProperty(declaration)) return; | ||
@@ -194,3 +147,3 @@ var specifiers = []; | ||
var leftPath = path.get("left"); | ||
if (leftPath.isObjectPattern() && hasRestProperty(leftPath.node)) { | ||
if (leftPath.isObjectPattern() && hasRestProperty(leftPath)) { | ||
var nodes = []; | ||
@@ -212,3 +165,3 @@ | ||
nodes.push(t.expressionStatement(nodeWithoutSpread)); | ||
nodes.push(t.assignmentExpression("=", argument, callExpression)); | ||
nodes.push(t.toStatement(t.assignmentExpression("=", argument, callExpression))); | ||
@@ -226,5 +179,6 @@ if (ref) { | ||
var leftPath = path.get("left"); | ||
var left = node.left; | ||
if (t.isObjectPattern(left) && hasRestProperty(left)) { | ||
if (t.isObjectPattern(left) && hasRestProperty(leftPath)) { | ||
var temp = scope.generateUidIdentifier("ref"); | ||
@@ -254,3 +208,3 @@ | ||
ObjectExpression: function ObjectExpression(path, file) { | ||
if (!hasSpread(path.node)) return; | ||
if (!hasSpread(path)) return; | ||
@@ -271,15 +225,15 @@ var useBuiltIns = file.opts.useBuiltIns || false; | ||
for (var _iterator5 = path.node.properties, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) { | ||
var _ref6; | ||
for (var _iterator2 = path.node.properties, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) { | ||
var _ref3; | ||
if (_isArray5) { | ||
if (_i5 >= _iterator5.length) break; | ||
_ref6 = _iterator5[_i5++]; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref3 = _iterator2[_i2++]; | ||
} else { | ||
_i5 = _iterator5.next(); | ||
if (_i5.done) break; | ||
_ref6 = _i5.value; | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref3 = _i2.value; | ||
} | ||
var prop = _ref6; | ||
var prop = _ref3; | ||
@@ -286,0 +240,0 @@ if (t.isSpreadProperty(prop)) { |
{ | ||
"name": "babel-plugin-transform-object-rest-spread", | ||
"version": "6.19.0", | ||
"version": "6.20.0", | ||
"description": "Compile object rest and spread to ES5", | ||
@@ -13,3 +13,3 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-object-rest-spread", | ||
"babel-plugin-syntax-object-rest-spread": "^6.8.0", | ||
"babel-runtime": "^6.0.0" | ||
"babel-runtime": "^6.20.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
# babel-plugin-transform-object-rest-spread | ||
Compile object rest and spread to ES5 | ||
> This plugin allows Babel to transform rest properties for object destructuring assignment and spread properties for object literals. | ||
## Example | ||
```js | ||
// source | ||
z = { x, ...y }; | ||
// Rest properties | ||
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; | ||
console.log(x); // 1 | ||
console.log(y); // 2 | ||
console.log(z); // { a: 3, b: 4 } | ||
// compiled | ||
_extends = Object.assign || function(target) { ... } | ||
z = _extends({ x }, y); | ||
// Spread properties | ||
let n = { x, y, ...z }; | ||
console.log(n); // { x: 1, y: 2, a: 3, b: 4 } | ||
``` | ||
[Try in REPL](https://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=%2F%2F%20Rest%20properties%0Alet%20%7B%20x%2C%20y%2C%20...z%20%7D%20%3D%20%7B%20x%3A%201%2C%20y%3A%202%2C%20a%3A%203%2C%20b%3A%204%20%7D%3B%0Aconsole.log(x)%3B%20%2F%2F%201%0Aconsole.log(y)%3B%20%2F%2F%202%0Aconsole.log(z)%3B%20%2F%2F%20%7B%20a%3A%203%2C%20b%3A%204%20%7D%0A%0A%2F%2F%20Spread%20properties%0Alet%20n%20%3D%20%7B%20x%2C%20y%2C%20...z%20%7D%3B%0Aconsole.log(n)%3B%20%2F%2F%20%7B%20x%3A%201%2C%20y%3A%202%2C%20a%3A%203%2C%20b%3A%204%20%7D) | ||
## Installation | ||
```sh | ||
$ npm install babel-plugin-transform-object-rest-spread | ||
npm install --save-dev babel-plugin-transform-object-rest-spread | ||
``` | ||
@@ -54,3 +61,3 @@ | ||
```sh | ||
$ babel --plugins transform-object-rest-spread script.js | ||
babel --plugins transform-object-rest-spread script.js | ||
``` | ||
@@ -65,1 +72,6 @@ | ||
``` | ||
## References | ||
* [Proposal: Object Rest/Spread Properties for ECMAScript](https://github.com/sebmarkbage/ecmascript-rest-spread) | ||
* [Spec](http://sebmarkbage.github.io/ecmascript-rest-spread) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76
10686
198
Updatedbabel-runtime@^6.20.0