react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / src / __tests__ / jstransform-utils-test.js
80551 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*15* @emails [email protected]16*/1718/*jshint evil:true*/1920require('mock-modules').autoMockOff();2122describe('jstransform-utils', function() {23var transform, utils;24var Syntax = require('esprima-fb').Syntax;2526beforeEach(function() {27require('mock-modules').dumpCache();28transform = require('../jstransform').transform;29utils = require('../utils');30});3132describe('temporary variables', function() {33it('should inject temporary variables at the start of functions', function() {34function visitFunctionBlock(traverse, node, path, state) {35utils.catchup(node.range[0] + 1, state);36var x = utils.injectTempVar(state);37var y = utils.injectTempVar(state);38traverse(node.body, path, state);39utils.append('return ' + x + ' + ' + y + ';', state);40utils.catchup(node.range[1], state);41return false;42}43visitFunctionBlock.test = function(node, path, state) {44var parentType = path.length && path[0].type;45return node.type === Syntax.BlockStatement && (46parentType === Syntax.FunctionDeclaration ||47parentType === Syntax.FunctionExpression48);49};5051expect(transform(52[visitFunctionBlock],53'var x = function() {};'54).code).toEqual(55'var x = function() {var $__0, $__1;return $__0 + $__1;};'56);5758expect(eval(transform(59[visitFunctionBlock],60'2 + (function sum(x, y)\t{ $__0 = x; $__1 = y; }(3, 5))'61).code)).toEqual(10);62});63});6465});666768