react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es7-rest-property-helpers.js
80540 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/*jslint node:true*/1718/**19* Desugars ES7 rest properties into ES5 object iteration.20*/2122var Syntax = require('esprima-fb').Syntax;2324// TODO: This is a pretty massive helper, it should only be defined once, in the25// transform's runtime environment. We don't currently have a runtime though.26var restFunction =27'(function(source, exclusion) {' +28'var rest = {};' +29'var hasOwn = Object.prototype.hasOwnProperty;' +30'if (source == null) {' +31'throw new TypeError();' +32'}' +33'for (var key in source) {' +34'if (hasOwn.call(source, key) && !hasOwn.call(exclusion, key)) {' +35'rest[key] = source[key];' +36'}' +37'}' +38'return rest;' +39'})';4041function getPropertyNames(properties) {42var names = [];43for (var i = 0; i < properties.length; i++) {44var property = properties[i];45if (property.type === Syntax.SpreadProperty) {46continue;47}48if (property.type === Syntax.Identifier) {49names.push(property.name);50} else {51names.push(property.key.name);52}53}54return names;55}5657function getRestFunctionCall(source, exclusion) {58return restFunction + '(' + source + ',' + exclusion + ')';59}6061function getSimpleShallowCopy(accessorExpression) {62// This could be faster with 'Object.assign({}, ' + accessorExpression + ')'63// but to unify code paths and avoid a ES6 dependency we use the same64// helper as for the exclusion case.65return getRestFunctionCall(accessorExpression, '{}');66}6768function renderRestExpression(accessorExpression, excludedProperties) {69var excludedNames = getPropertyNames(excludedProperties);70if (!excludedNames.length) {71return getSimpleShallowCopy(accessorExpression);72}73return getRestFunctionCall(74accessorExpression,75'{' + excludedNames.join(':1,') + ':1}'76);77}7879exports.renderRestExpression = renderRestExpression;808182