react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es6-call-spread-visitors.js
80540 views/**1* Copyright 2004-present Facebook. All Rights Reserved.2*/3/*global exports:true*/45/**6* Implements ES6 call spread.7*8* instance.method(a, b, c, ...d)9*10* instance.method.apply(instance, [a, b, c].concat(d))11*12*/1314var Syntax = require('esprima-fb').Syntax;15var utils = require('../src/utils');1617function process(traverse, node, path, state) {18utils.move(node.range[0], state);19traverse(node, path, state);20utils.catchup(node.range[1], state);21}2223function visitCallSpread(traverse, node, path, state) {24utils.catchup(node.range[0], state);2526if (node.type === Syntax.NewExpression) {27// Input = new Set(1, 2, ...list)28// Output = new (Function.prototype.bind.apply(Set, [null, 1, 2].concat(list)))29utils.append('new (Function.prototype.bind.apply(', state);30process(traverse, node.callee, path, state);31} else if (node.callee.type === Syntax.MemberExpression) {32// Input = get().fn(1, 2, ...more)33// Output = (_ = get()).fn.apply(_, [1, 2].apply(more))34var tempVar = utils.injectTempVar(state);35utils.append('(' + tempVar + ' = ', state);36process(traverse, node.callee.object, path, state);37utils.append(')', state);38if (node.callee.property.type === Syntax.Identifier) {39utils.append('.', state);40process(traverse, node.callee.property, path, state);41} else {42utils.append('[', state);43process(traverse, node.callee.property, path, state);44utils.append(']', state);45}46utils.append('.apply(' + tempVar, state);47} else {48// Input = max(1, 2, ...list)49// Output = max.apply(null, [1, 2].concat(list))50var needsToBeWrappedInParenthesis =51node.callee.type === Syntax.FunctionDeclaration ||52node.callee.type === Syntax.FunctionExpression;53if (needsToBeWrappedInParenthesis) {54utils.append('(', state);55}56process(traverse, node.callee, path, state);57if (needsToBeWrappedInParenthesis) {58utils.append(')', state);59}60utils.append('.apply(null', state);61}62utils.append(', ', state);6364var args = node.arguments.slice();65var spread = args.pop();66if (args.length || node.type === Syntax.NewExpression) {67utils.append('[', state);68if (node.type === Syntax.NewExpression) {69utils.append('null' + (args.length ? ', ' : ''), state);70}71while (args.length) {72var arg = args.shift();73utils.move(arg.range[0], state);74traverse(arg, path, state);75if (args.length) {76utils.catchup(args[0].range[0], state);77} else {78utils.catchup(arg.range[1], state);79}80}81utils.append('].concat(', state);82process(traverse, spread.argument, path, state);83utils.append(')', state);84} else {85process(traverse, spread.argument, path, state);86}87utils.append(node.type === Syntax.NewExpression ? '))' : ')', state);8889utils.move(node.range[1], state);90return false;91}9293visitCallSpread.test = function(node, path, state) {94return (95(96node.type === Syntax.CallExpression ||97node.type === Syntax.NewExpression98) &&99node.arguments.length > 0 &&100node.arguments[node.arguments.length - 1].type === Syntax.SpreadElement101);102};103104exports.visitorList = [105visitCallSpread,106];107108109