react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es7-spread-property-visitors.js
80540 views/**1* Copyright 2004-present Facebook. All Rights Reserved.2*/3/*global exports:true*/45/**6* Implements ES7 object spread property.7* https://gist.github.com/sebmarkbage/aa849c7973cb4452c5478*9* { ...a, x: 1 }10*11* Object.assign({}, a, {x: 1 })12*13*/1415var Syntax = require('esprima-fb').Syntax;16var utils = require('../src/utils');1718function visitObjectLiteralSpread(traverse, node, path, state) {19utils.catchup(node.range[0], state);2021utils.append('Object.assign({', state);2223// Skip the original {24utils.move(node.range[0] + 1, state);2526var previousWasSpread = false;2728for (var i = 0; i < node.properties.length; i++) {29var property = node.properties[i];30if (property.type === Syntax.SpreadProperty) {3132// Close the previous object or initial object33if (!previousWasSpread) {34utils.append('}', state);35}3637if (i === 0) {38// Normally there will be a comma when we catch up, but not before39// the first property.40utils.append(',', state);41}4243utils.catchup(property.range[0], state);4445// skip ...46utils.move(property.range[0] + 3, state);4748traverse(property.argument, path, state);4950utils.catchup(property.range[1], state);5152previousWasSpread = true;5354} else {5556utils.catchup(property.range[0], state);5758if (previousWasSpread) {59utils.append('{', state);60}6162traverse(property, path, state);6364utils.catchup(property.range[1], state);6566previousWasSpread = false;6768}69}7071// Strip any non-whitespace between the last item and the end.72// We only catch up on whitespace so that we ignore any trailing commas which73// are stripped out for IE8 support. Unfortunately, this also strips out any74// trailing comments.75utils.catchupWhiteSpace(node.range[1] - 1, state);7677// Skip the trailing }78utils.move(node.range[1], state);7980if (!previousWasSpread) {81utils.append('}', state);82}8384utils.append(')', state);85return false;86}8788visitObjectLiteralSpread.test = function(node, path, state) {89if (node.type !== Syntax.ObjectExpression) {90return false;91}92// Tight loop optimization93var hasAtLeastOneSpreadProperty = false;94for (var i = 0; i < node.properties.length; i++) {95var property = node.properties[i];96if (property.type === Syntax.SpreadProperty) {97hasAtLeastOneSpreadProperty = true;98} else if (property.kind !== 'init') {99return false;100}101}102return hasAtLeastOneSpreadProperty;103};104105exports.visitorList = [106visitObjectLiteralSpread107];108109110