react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es6-rest-param-visitors.js
80542 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 ES6 rest parameters into an ES3 arguments array.20*21* function printf(template, ...args) {22* args.forEach(...);23* }24*25* We could use `Array.prototype.slice.call`, but that usage of arguments causes26* functions to be deoptimized in V8, so instead we use a for-loop.27*28* function printf(template) {29* for (var args = [], $__0 = 1, $__1 = arguments.length; $__0 < $__1; $__0++)30* args.push(arguments[$__0]);31* args.forEach(...);32* }33*34*/35var Syntax = require('esprima-fb').Syntax;36var utils = require('../src/utils');37383940function _nodeIsFunctionWithRestParam(node) {41return (node.type === Syntax.FunctionDeclaration42|| node.type === Syntax.FunctionExpression43|| node.type === Syntax.ArrowFunctionExpression)44&& node.rest;45}4647function visitFunctionParamsWithRestParam(traverse, node, path, state) {48if (node.parametricType) {49utils.catchup(node.parametricType.range[0], state);50path.unshift(node);51traverse(node.parametricType, path, state);52path.shift();53}5455// Render params.56if (node.params.length) {57path.unshift(node);58traverse(node.params, path, state);59path.shift();60} else {61// -3 is for ... of the rest.62utils.catchup(node.rest.range[0] - 3, state);63}64utils.catchupWhiteSpace(node.rest.range[1], state);6566path.unshift(node);67traverse(node.body, path, state);68path.shift();6970return false;71}7273visitFunctionParamsWithRestParam.test = function(node, path, state) {74return _nodeIsFunctionWithRestParam(node);75};7677function renderRestParamSetup(functionNode, state) {78var idx = state.localScope.tempVarIndex++;79var len = state.localScope.tempVarIndex++;8081return 'for (var ' + functionNode.rest.name + '=[],' +82utils.getTempVar(idx) + '=' + functionNode.params.length + ',' +83utils.getTempVar(len) + '=arguments.length;' +84utils.getTempVar(idx) + '<' + utils.getTempVar(len) + ';' +85utils.getTempVar(idx) + '++) ' +86functionNode.rest.name + '.push(arguments[' + utils.getTempVar(idx) + ']);';87}8889function visitFunctionBodyWithRestParam(traverse, node, path, state) {90utils.catchup(node.range[0] + 1, state);91var parentNode = path[0];92utils.append(renderRestParamSetup(parentNode, state), state);93return true;94}9596visitFunctionBodyWithRestParam.test = function(node, path, state) {97return node.type === Syntax.BlockStatement98&& _nodeIsFunctionWithRestParam(path[0]);99};100101exports.renderRestParamSetup = renderRestParamSetup;102exports.visitorList = [103visitFunctionParamsWithRestParam,104visitFunctionBodyWithRestParam105];106107108