Socket
Socket
Sign inDemoInstall

@babel/plugin-proposal-object-rest-spread

Package Overview
Dependencies
Maintainers
6
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@babel/plugin-proposal-object-rest-spread - npm Package Compare versions

Comparing version 7.0.0-beta.42 to 7.0.0-beta.43

339

lib/index.js
"use strict";
exports.__esModule = true;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
function _helperPluginUtils() {
const data = require("@babel/helper-plugin-utils");
var _pluginSyntaxObjectRestSpread = _interopRequireDefault(require("@babel/plugin-syntax-object-rest-spread"));
_helperPluginUtils = function () {
return data;
};
var _core = require("@babel/core");
return data;
}
function _pluginSyntaxObjectRestSpread() {
const data = _interopRequireDefault(require("@babel/plugin-syntax-object-rest-spread"));
_pluginSyntaxObjectRestSpread = function () {
return data;
};
return data;
}
function _core() {
const data = require("@babel/core");
_core = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = (0, _helperPluginUtils.declare)(function (api, opts) {
var _default = (0, _helperPluginUtils().declare)((api, opts) => {
api.assertVersion(7);
var _opts$useBuiltIns = opts.useBuiltIns,
useBuiltIns = _opts$useBuiltIns === void 0 ? false : _opts$useBuiltIns,
_opts$loose = opts.loose,
loose = _opts$loose === void 0 ? false : _opts$loose;
const {
useBuiltIns = false,
loose = false
} = opts;

@@ -26,8 +52,8 @@ if (typeof loose !== "boolean") {

function getExtendsHelper(file) {
return useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends");
return useBuiltIns ? _core().types.memberExpression(_core().types.identifier("Object"), _core().types.identifier("assign")) : file.addHelper("extends");
}
function hasRestElement(path) {
var foundRestElement = false;
visitRestElements(path, function () {
let foundRestElement = false;
visitRestElements(path, () => {
foundRestElement = true;

@@ -41,4 +67,4 @@ path.stop();

path.traverse({
Expression: function Expression(path) {
var parentType = path.parent.type;
Expression(path) {
const parentType = path.parent.type;

@@ -49,2 +75,3 @@ if (parentType == "AssignmentPattern" && path.key === "right" || parentType == "ObjectProperty" && path.parent.computed && path.key === "key") {

},
RestElement: visitor

@@ -55,17 +82,4 @@ });

function hasSpread(node) {
for (var _iterator = node.properties, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var _prop = _ref;
if (_core.types.isSpreadElement(_prop)) {
for (const prop of node.properties) {
if (_core().types.isSpreadElement(prop)) {
return true;

@@ -79,26 +93,13 @@ }

function extractNormalizedKeys(path) {
var props = path.node.properties;
var keys = [];
var allLiteral = true;
const props = path.node.properties;
const keys = [];
let allLiteral = true;
for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
for (const prop of props) {
if (_core().types.isIdentifier(prop.key) && !prop.computed) {
keys.push(_core().types.stringLiteral(prop.key.name));
} else if (_core().types.isLiteral(prop.key)) {
keys.push(_core().types.stringLiteral(String(prop.key.value)));
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var _prop2 = _ref2;
if (_core.types.isIdentifier(_prop2.key) && !_prop2.computed) {
keys.push(_core.types.stringLiteral(_prop2.key.name));
} else if (_core.types.isLiteral(_prop2.key)) {
keys.push(_core.types.stringLiteral(String(_prop2.key.value)));
} else {
keys.push(_core.types.cloneNode(_prop2.key));
keys.push(_core().types.cloneNode(prop.key));
allLiteral = false;

@@ -109,4 +110,4 @@ }

return {
keys: keys,
allLiteral: allLiteral
keys,
allLiteral
};

@@ -116,27 +117,14 @@ }

function replaceImpureComputedKeys(path) {
var impureComputedPropertyDeclarators = [];
const impureComputedPropertyDeclarators = [];
for (var _iterator3 = path.get("properties"), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
for (const propPath of path.get("properties")) {
const key = propPath.get("key");
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
if (propPath.node.computed && !key.isPure()) {
const name = path.scope.generateUidBasedOnNode(key.node);
var _propPath = _ref3;
const declarator = _core().types.variableDeclarator(_core().types.identifier(name), key.node);
var key = _propPath.get("key");
if (_propPath.node.computed && !key.isPure()) {
var name = path.scope.generateUidBasedOnNode(key.node);
var declarator = _core.types.variableDeclarator(_core.types.identifier(name), key.node);
impureComputedPropertyDeclarators.push(declarator);
key.replaceWith(_core.types.identifier(name));
key.replaceWith(_core().types.identifier(name));
}

@@ -149,29 +137,29 @@ }

function createObjectSpread(path, file, objRef) {
var props = path.get("properties");
var last = props[props.length - 1];
const props = path.get("properties");
const last = props[props.length - 1];
_core.types.assertRestElement(last.node);
_core().types.assertRestElement(last.node);
var restElement = _core.types.cloneNode(last.node);
const restElement = _core().types.cloneNode(last.node);
last.remove();
var impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
const {
keys,
allLiteral
} = extractNormalizedKeys(path);
var _extractNormalizedKey = extractNormalizedKeys(path),
keys = _extractNormalizedKey.keys,
allLiteral = _extractNormalizedKey.allLiteral;
if (keys.length === 0) {
return [impureComputedPropertyDeclarators, restElement.argument, _core.types.callExpression(getExtendsHelper(file), [_core.types.objectExpression([]), _core.types.cloneNode(objRef)])];
return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(getExtendsHelper(file), [_core().types.objectExpression([]), _core().types.cloneNode(objRef)])];
}
var keyExpression;
let keyExpression;
if (!allLiteral) {
keyExpression = _core.types.callExpression(_core.types.memberExpression(_core.types.arrayExpression(keys), _core.types.identifier("map")), [file.addHelper("toPropertyKey")]);
keyExpression = _core().types.callExpression(_core().types.memberExpression(_core().types.arrayExpression(keys), _core().types.identifier("map")), [file.addHelper("toPropertyKey")]);
} else {
keyExpression = _core.types.arrayExpression(keys);
keyExpression = _core().types.arrayExpression(keys);
}
return [impureComputedPropertyDeclarators, restElement.argument, _core.types.callExpression(file.addHelper("objectWithoutProperties"), [_core.types.cloneNode(objRef), keyExpression])];
return [impureComputedPropertyDeclarators, restElement.argument, _core().types.callExpression(file.addHelper("objectWithoutProperties"), [_core().types.cloneNode(objRef), keyExpression])];
}

@@ -186,6 +174,6 @@

if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
var elements = paramPath.get("elements");
const elements = paramPath.get("elements");
for (var _i4 = 0; _i4 < elements.length; _i4++) {
replaceRestElement(parentPath, elements[_i4], _i4, elements.length);
for (let i = 0; i < elements.length; i++) {
replaceRestElement(parentPath, elements[i], i, elements.length);
}

@@ -195,9 +183,9 @@ }

if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
var uid = parentPath.scope.generateUidIdentifier("ref");
const uid = parentPath.scope.generateUidIdentifier("ref");
var declar = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(paramPath.node, uid)]);
const declar = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(paramPath.node, uid)]);
parentPath.ensureBlock();
parentPath.get("body").unshiftContainer("body", declar);
paramPath.replaceWith(_core.types.cloneNode(uid));
paramPath.replaceWith(_core().types.cloneNode(uid));
}

@@ -207,12 +195,13 @@ }

return {
inherits: _pluginSyntaxObjectRestSpread.default,
inherits: _pluginSyntaxObjectRestSpread().default,
visitor: {
Function: function Function(path) {
var params = path.get("params");
Function(path) {
const params = path.get("params");
for (var i = params.length - 1; i >= 0; i--) {
for (let i = params.length - 1; i >= 0; i--) {
replaceRestElement(params[i].parentPath, params[i], i, params.length);
}
},
VariableDeclarator: function VariableDeclarator(path, file) {
VariableDeclarator(path, file) {
if (!path.get("id").isObjectPattern()) {

@@ -222,5 +211,5 @@ return;

var insertionPath = path;
var originalPath = path;
visitRestElements(path.get("id"), function (path) {
let insertionPath = path;
const originalPath = path;
visitRestElements(path.get("id"), path => {
if (!path.parentPath.isObjectPattern()) {

@@ -230,13 +219,13 @@ 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)));
if (originalPath.node.id.properties.length > 1 && !_core().types.isIdentifier(originalPath.node.init)) {
const 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) {
let ref = originalPath.node.init;
const refPropertyPath = [];
let kind;
path.findParent(path => {
if (path.isObjectProperty()) {

@@ -251,20 +240,14 @@ refPropertyPath.unshift(path.node.key.name);

if (refPropertyPath.length) {
refPropertyPath.forEach(function (prop) {
ref = _core.types.memberExpression(ref, _core.types.identifier(prop));
refPropertyPath.forEach(prop => {
ref = _core().types.memberExpression(ref, _core().types.identifier(prop));
});
}
var objectPatternPath = path.findParent(function (path) {
return path.isObjectPattern();
});
const objectPatternPath = path.findParent(path => path.isObjectPattern());
const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(objectPatternPath, file, ref);
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.insertAfter(_core().types.variableDeclarator(argument, callExpression));
insertionPath = insertionPath.getSibling(insertionPath.key + 1);

@@ -274,97 +257,92 @@ path.scope.registerBinding(kind, insertionPath);

if (objectPatternPath.node.properties.length === 0) {
objectPatternPath.findParent(function (path) {
return path.isObjectProperty() || path.isVariableDeclarator();
}).remove();
objectPatternPath.findParent(path => path.isObjectProperty() || path.isVariableDeclarator()).remove();
}
});
},
ExportNamedDeclaration: function ExportNamedDeclaration(path) {
var declaration = path.get("declaration");
ExportNamedDeclaration(path) {
const declaration = path.get("declaration");
if (!declaration.isVariableDeclaration()) return;
var hasRest = declaration.get("declarations").some(function (path) {
return hasRestElement(path.get("id"));
});
const hasRest = declaration.get("declarations").some(path => hasRestElement(path.get("id")));
if (!hasRest) return;
var specifiers = [];
const specifiers = [];
for (var name in path.getOuterBindingIdentifiers(path)) {
specifiers.push(_core.types.exportSpecifier(_core.types.identifier(name), _core.types.identifier(name)));
for (const name in path.getOuterBindingIdentifiers(path)) {
specifiers.push(_core().types.exportSpecifier(_core().types.identifier(name), _core().types.identifier(name)));
}
path.replaceWith(declaration.node);
path.insertAfter(_core.types.exportNamedDeclaration(null, specifiers));
path.insertAfter(_core().types.exportNamedDeclaration(null, specifiers));
},
CatchClause: function CatchClause(path) {
var paramPath = path.get("param");
CatchClause(path) {
const paramPath = path.get("param");
replaceRestElement(paramPath.parentPath, paramPath);
},
AssignmentExpression: function AssignmentExpression(path, file) {
var leftPath = path.get("left");
AssignmentExpression(path, file) {
const leftPath = path.get("left");
if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
var nodes = [];
var refName = path.scope.generateUidBasedOnNode(path.node.right, "ref");
nodes.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(refName), path.node.right)]));
const nodes = [];
const refName = path.scope.generateUidBasedOnNode(path.node.right, "ref");
nodes.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(refName), path.node.right)]));
const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectSpread(leftPath, file, _core().types.identifier(refName));
var _createObjectSpread2 = createObjectSpread(leftPath, file, _core.types.identifier(refName)),
impureComputedPropertyDeclarators = _createObjectSpread2[0],
argument = _createObjectSpread2[1],
callExpression = _createObjectSpread2[2];
if (impureComputedPropertyDeclarators.length > 0) {
nodes.push(_core.types.variableDeclaration("var", impureComputedPropertyDeclarators));
nodes.push(_core().types.variableDeclaration("var", impureComputedPropertyDeclarators));
}
var nodeWithoutSpread = _core.types.cloneNode(path.node);
const nodeWithoutSpread = _core().types.cloneNode(path.node);
nodeWithoutSpread.right = _core.types.identifier(refName);
nodes.push(_core.types.expressionStatement(nodeWithoutSpread));
nodes.push(_core.types.toStatement(_core.types.assignmentExpression("=", argument, callExpression)));
nodes.push(_core.types.expressionStatement(_core.types.identifier(refName)));
nodeWithoutSpread.right = _core().types.identifier(refName);
nodes.push(_core().types.expressionStatement(nodeWithoutSpread));
nodes.push(_core().types.toStatement(_core().types.assignmentExpression("=", argument, callExpression)));
nodes.push(_core().types.expressionStatement(_core().types.identifier(refName)));
path.replaceWithMultiple(nodes);
}
},
ForXStatement: function ForXStatement(path) {
var node = path.node,
scope = path.scope;
var leftPath = path.get("left");
var left = node.left;
if (_core.types.isObjectPattern(left) && hasRestElement(leftPath)) {
var temp = scope.generateUidIdentifier("ref");
node.left = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(temp)]);
ForXStatement(path) {
const {
node,
scope
} = path;
const leftPath = path.get("left");
const left = node.left;
if (_core().types.isObjectPattern(left) && hasRestElement(leftPath)) {
const temp = scope.generateUidIdentifier("ref");
node.left = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(temp)]);
path.ensureBlock();
node.body.body.unshift(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(left, _core.types.cloneNode(temp))]));
node.body.body.unshift(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(left, _core().types.cloneNode(temp))]));
return;
}
if (!_core.types.isVariableDeclaration(left)) return;
var pattern = left.declarations[0].id;
if (!_core.types.isObjectPattern(pattern)) return;
var key = scope.generateUidIdentifier("ref");
node.left = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(key, null)]);
if (!_core().types.isVariableDeclaration(left)) return;
const pattern = left.declarations[0].id;
if (!_core().types.isObjectPattern(pattern)) return;
const key = scope.generateUidIdentifier("ref");
node.left = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(key, null)]);
path.ensureBlock();
node.body.body.unshift(_core.types.variableDeclaration(node.left.kind, [_core.types.variableDeclarator(pattern, _core.types.cloneNode(key))]));
node.body.body.unshift(_core().types.variableDeclaration(node.left.kind, [_core().types.variableDeclarator(pattern, _core().types.cloneNode(key))]));
},
ObjectExpression: function ObjectExpression(path, file) {
ObjectExpression(path, file) {
if (!hasSpread(path.node)) return;
var args = [];
var props = [];
const args = [];
let props = [];
function push() {
if (!props.length) return;
args.push(_core.types.objectExpression(props));
args.push(_core().types.objectExpression(props));
props = [];
}
if (_core.types.isSpreadElement(path.node.properties[0])) {
args.push(_core.types.objectExpression([]));
if (_core().types.isSpreadElement(path.node.properties[0])) {
args.push(_core().types.objectExpression([]));
}
var _arr = path.node.properties;
for (var _i5 = 0; _i5 < _arr.length; _i5++) {
var prop = _arr[_i5];
if (_core.types.isSpreadElement(prop)) {
for (const prop of path.node.properties) {
if (_core().types.isSpreadElement(prop)) {
push();

@@ -378,3 +356,3 @@ args.push(prop.argument);

push();
var helper;
let helper;

@@ -387,4 +365,5 @@ if (loose) {

path.replaceWith(_core.types.callExpression(helper, args));
path.replaceWith(_core().types.callExpression(helper, args));
}
}

@@ -391,0 +370,0 @@ };

{
"name": "@babel/plugin-proposal-object-rest-spread",
"version": "7.0.0-beta.42",
"version": "7.0.0-beta.43",
"description": "Compile object rest and spread to ES5",

@@ -12,12 +12,12 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-object-rest-spread",

"dependencies": {
"@babel/helper-plugin-utils": "7.0.0-beta.42",
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.42"
"@babel/helper-plugin-utils": "7.0.0-beta.43",
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.43"
},
"peerDependencies": {
"@babel/core": "7.0.0-beta.42"
"@babel/core": "7.0.0-beta.43"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.42",
"@babel/helper-plugin-test-runner": "7.0.0-beta.42"
"@babel/core": "7.0.0-beta.43",
"@babel/helper-plugin-test-runner": "7.0.0-beta.43"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc