@babel/plugin-proposal-object-rest-spread
Advanced tools
Comparing version 7.0.0-beta.40 to 7.0.0-beta.41
144
lib/index.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = _default; | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
var _pluginSyntaxObjectRestSpread = _interopRequireDefault(require("@babel/plugin-syntax-object-rest-spread")); | ||
@@ -12,8 +14,11 @@ | ||
function _default(api, opts) { | ||
var _default = (0, _helperPluginUtils.declare)(function (api, opts) { | ||
api.assertVersion(7); | ||
var _opts$useBuiltIns = opts.useBuiltIns, | ||
useBuiltIns = _opts$useBuiltIns === void 0 ? false : _opts$useBuiltIns; | ||
useBuiltIns = _opts$useBuiltIns === void 0 ? false : _opts$useBuiltIns, | ||
_opts$loose = opts.loose, | ||
loose = _opts$loose === void 0 ? false : _opts$loose; | ||
if (typeof useBuiltIns !== "boolean") { | ||
throw new Error(".useBuiltIns must be a boolean, or undefined"); | ||
if (typeof loose !== "boolean") { | ||
throw new Error(".loose must be a boolean, or undefined"); | ||
} | ||
@@ -23,7 +28,5 @@ | ||
var foundRestElement = false; | ||
path.traverse({ | ||
RestElement: function RestElement() { | ||
foundRestElement = true; | ||
path.stop(); | ||
} | ||
visitRestElements(path, function () { | ||
foundRestElement = true; | ||
path.stop(); | ||
}); | ||
@@ -33,2 +36,15 @@ return foundRestElement; | ||
function visitRestElements(path, visitor) { | ||
path.traverse({ | ||
Expression: function Expression(path) { | ||
var parentType = path.parent.type; | ||
if (parentType == "AssignmentPattern" && path.key === "right" || parentType == "ObjectProperty" && path.parent.computed && path.key === "key") { | ||
path.skip(); | ||
} | ||
}, | ||
RestElement: visitor | ||
}); | ||
} | ||
function hasSpread(node) { | ||
@@ -191,57 +207,54 @@ for (var _iterator = node.properties, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { | ||
var insertionPath = path; | ||
path.get("id").traverse({ | ||
RestElement: function RestElement(path) { | ||
if (!path.parentPath.isObjectPattern()) { | ||
return; | ||
} | ||
var originalPath = path; | ||
visitRestElements(path.get("id"), function (path) { | ||
if (!path.parentPath.isObjectPattern()) { | ||
return; | ||
} | ||
if (this.originalPath.node.id.properties.length > 1 && !_core.types.isIdentifier(this.originalPath.node.init)) { | ||
var initRef = path.scope.generateUidIdentifierBasedOnNode(this.originalPath.node.init, "ref"); | ||
this.originalPath.insertBefore(_core.types.variableDeclarator(initRef, this.originalPath.node.init)); | ||
this.originalPath.replaceWith(_core.types.variableDeclarator(this.originalPath.node.id, _core.types.cloneNode(initRef))); | ||
return; | ||
if (originalPath.node.id.properties.length > 1 && !_core.types.isIdentifier(originalPath.node.init)) { | ||
var initRef = path.scope.generateUidIdentifierBasedOnNode(originalPath.node.init, "ref"); | ||
originalPath.insertBefore(_core.types.variableDeclarator(initRef, originalPath.node.init)); | ||
originalPath.replaceWith(_core.types.variableDeclarator(originalPath.node.id, _core.types.cloneNode(initRef))); | ||
return; | ||
} | ||
var ref = originalPath.node.init; | ||
var refPropertyPath = []; | ||
var kind; | ||
path.findParent(function (path) { | ||
if (path.isObjectProperty()) { | ||
refPropertyPath.unshift(path.node.key.name); | ||
} else if (path.isVariableDeclarator()) { | ||
kind = path.parentPath.node.kind; | ||
return true; | ||
} | ||
}); | ||
var ref = this.originalPath.node.init; | ||
var refPropertyPath = []; | ||
var kind; | ||
path.findParent(function (path) { | ||
if (path.isObjectProperty()) { | ||
refPropertyPath.unshift(path.node.key.name); | ||
} else if (path.isVariableDeclarator()) { | ||
kind = path.parentPath.node.kind; | ||
return true; | ||
} | ||
if (refPropertyPath.length) { | ||
refPropertyPath.forEach(function (prop) { | ||
ref = _core.types.memberExpression(ref, _core.types.identifier(prop)); | ||
}); | ||
} | ||
if (refPropertyPath.length) { | ||
refPropertyPath.forEach(function (prop) { | ||
ref = _core.types.memberExpression(ref, _core.types.identifier(prop)); | ||
}); | ||
} | ||
var objectPatternPath = path.findParent(function (path) { | ||
return path.isObjectPattern(); | ||
}); | ||
var objectPatternPath = path.findParent(function (path) { | ||
return path.isObjectPattern(); | ||
}); | ||
var _createObjectSpread = createObjectSpread(objectPatternPath, file, ref), | ||
impureComputedPropertyDeclarators = _createObjectSpread[0], | ||
argument = _createObjectSpread[1], | ||
callExpression = _createObjectSpread[2]; | ||
var _createObjectSpread = createObjectSpread(objectPatternPath, file, ref), | ||
impureComputedPropertyDeclarators = _createObjectSpread[0], | ||
argument = _createObjectSpread[1], | ||
callExpression = _createObjectSpread[2]; | ||
_core.types.assertIdentifier(argument); | ||
_core.types.assertIdentifier(argument); | ||
insertionPath.insertBefore(impureComputedPropertyDeclarators); | ||
insertionPath.insertAfter(_core.types.variableDeclarator(argument, callExpression)); | ||
insertionPath = insertionPath.getSibling(insertionPath.key + 1); | ||
path.scope.registerBinding(kind, insertionPath); | ||
insertionPath.insertBefore(impureComputedPropertyDeclarators); | ||
insertionPath.insertAfter(_core.types.variableDeclarator(argument, callExpression)); | ||
insertionPath = insertionPath.getSibling(insertionPath.key + 1); | ||
path.scope.registerBinding(kind, insertionPath); | ||
if (objectPatternPath.node.properties.length === 0) { | ||
objectPatternPath.findParent(function (path) { | ||
return path.isObjectProperty() || path.isVariableDeclarator(); | ||
}).remove(); | ||
} | ||
if (objectPatternPath.node.properties.length === 0) { | ||
objectPatternPath.findParent(function (path) { | ||
return path.isObjectProperty() || path.isVariableDeclarator(); | ||
}).remove(); | ||
} | ||
}, { | ||
originalPath: path | ||
}); | ||
@@ -252,3 +265,6 @@ }, | ||
if (!declaration.isVariableDeclaration()) return; | ||
if (!hasRestElement(declaration)) return; | ||
var hasRest = declaration.get("declarations").some(function (path) { | ||
return hasRestElement(path.get("id")); | ||
}); | ||
if (!hasRest) return; | ||
var specifiers = []; | ||
@@ -326,2 +342,6 @@ | ||
if (_core.types.isSpreadElement(path.node.properties[0])) { | ||
args.push(_core.types.objectExpression([])); | ||
} | ||
var _arr = path.node.properties; | ||
@@ -341,8 +361,10 @@ | ||
push(); | ||
var helper; | ||
if (!_core.types.isObjectExpression(args[0])) { | ||
args.unshift(_core.types.objectExpression([])); | ||
if (loose) { | ||
helper = useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends"); | ||
} else { | ||
helper = file.addHelper("objectSpread"); | ||
} | ||
var helper = useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends"); | ||
path.replaceWith(_core.types.callExpression(helper, args)); | ||
@@ -352,2 +374,4 @@ } | ||
}; | ||
} | ||
}); | ||
exports.default = _default; |
{ | ||
"name": "@babel/plugin-proposal-object-rest-spread", | ||
"version": "7.0.0-beta.40", | ||
"version": "7.0.0-beta.41", | ||
"description": "Compile object rest and spread to ES5", | ||
@@ -12,11 +12,12 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-object-rest-spread", | ||
"dependencies": { | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.40" | ||
"@babel/helper-plugin-utils": "7.0.0-beta.41", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.41" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.40" | ||
"@babel/core": "7.0.0-beta.41" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.40", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.40" | ||
"@babel/core": "7.0.0-beta.41", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.41" | ||
} | ||
} |
@@ -57,2 +57,15 @@ # @babel/plugin-proposal-object-rest-spread | ||
By default, this plugin will produce spec compliant code by using Babel's `objectSpread` helper. | ||
### `loose` | ||
`boolean`, defaults to `false`. | ||
Enabling this option will use Babel's `extends` helper, which is basically the same as `Object.assign` (see `useBuiltIns` below to use it directly). | ||
:warning: Please keep in mind that even if they're almost equivalent, there's an important difference between spread and `Object.assign`: **spread _defines_ new properties, while `Object.assign()` _sets_ them**, so using this mode might produce unexpected results in some cases. | ||
For detailed information please check out [Spread VS. Object.assign](http://2ality.com/2016/10/rest-spread-properties.html#spreading-objects-versus-objectassign) and [Assigning VS. defining properties](http://exploringjs.com/es6/ch_oop-besides-classes.html#sec_assigning-vs-defining-properties). | ||
### `useBuiltIns` | ||
@@ -62,4 +75,7 @@ | ||
By default, this plugin uses Babel's `extends` helper which polyfills `Object.assign`. Enabling this option will use `Object.assign` directly. | ||
Enabling this option will use `Object.assign` directly instead of the Babel's `extends` helper. Keep in mind that this flag only applies when `loose: true`. | ||
##### Example | ||
**.babelrc** | ||
@@ -70,3 +86,3 @@ | ||
"plugins": [ | ||
["@babel/plugin-proposal-object-rest-spread", { "useBuiltIns": true }] | ||
["@babel/plugin-proposal-object-rest-spread", { "loose": true, "useBuiltIns": true }] | ||
] | ||
@@ -92,1 +108,3 @@ } | ||
* [Spec](http://sebmarkbage.github.io/ecmascript-rest-spread) | ||
* [Spread VS. Object.assign](http://2ality.com/2016/10/rest-spread-properties.html#spreading-objects-versus-objectassign) | ||
* [Assigning VS. defining properties](http://exploringjs.com/es6/ch_oop-besides-classes.html#sec_assigning-vs-defining-properties) |
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
16430
295
107
3
+ Added@babel/code-frame@7.0.0-beta.41(transitive)
+ Added@babel/core@7.0.0-beta.41(transitive)
+ Added@babel/generator@7.0.0-beta.41(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.41(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.41(transitive)
+ Added@babel/helper-plugin-utils@7.0.0-beta.41(transitive)
+ Added@babel/helper-split-export-declaration@7.0.0-beta.41(transitive)
+ Added@babel/helpers@7.0.0-beta.41(transitive)
+ Added@babel/highlight@7.0.0-beta.41(transitive)
+ Added@babel/plugin-syntax-object-rest-spread@7.0.0-beta.41(transitive)
+ Added@babel/template@7.0.0-beta.41(transitive)
+ Added@babel/traverse@7.0.0-beta.41(transitive)
+ Added@babel/types@7.0.0-beta.41(transitive)
+ Addedbabylon@7.0.0-beta.41(transitive)
+ Addedsemver@5.7.2(transitive)
- Removed@babel/code-frame@7.0.0-beta.40(transitive)
- Removed@babel/core@7.0.0-beta.40(transitive)
- Removed@babel/generator@7.0.0-beta.40(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.40(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.40(transitive)
- Removed@babel/helpers@7.0.0-beta.40(transitive)
- Removed@babel/highlight@7.0.0-beta.40(transitive)
- Removed@babel/plugin-syntax-object-rest-spread@7.0.0-beta.40(transitive)
- Removed@babel/template@7.0.0-beta.40(transitive)
- Removed@babel/traverse@7.0.0-beta.40(transitive)
- Removed@babel/types@7.0.0-beta.40(transitive)
- Removedbabylon@7.0.0-beta.40(transitive)
Updated@babel/plugin-syntax-object-rest-spread@7.0.0-beta.41