react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / visitors / __tests__ / es6-rest-param-visitors-test.js
81153 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/**17* @emails [email protected]18*/1920/*jshint evil:true*/2122require('mock-modules').autoMockOff();2324describe('es6-rest-param-visitors', () => {25var transformFn;26var visitorSet;27var arrowFuncVisitors;28var classVisitors;29var restParamVisitors;3031beforeEach(() => {32require('mock-modules').dumpCache();33arrowFuncVisitors = require('../es6-arrow-function-visitors').visitorList;34classVisitors = require('../es6-class-visitors').visitorList;35restParamVisitors = require('../es6-rest-param-visitors').visitorList;36transformFn = require('../../src/jstransform').transform;3738visitorSet =39arrowFuncVisitors40.concat(classVisitors)41.concat(restParamVisitors);42});4344function transform(code) {45return transformFn(visitorSet, code).code;46}4748function expectTransform(code, result) {49expect(transform(code)).toEqual(result);50}5152describe('function expressions', () => {53it('should capture 2 rest params, having 2 args', () => {54var code = transform([55'(function(x, y, ...args) {',56' return [x, y, args.length, args[0], args[1]];',57'})(1, 2, 3, 4);'58].join('\n'));5960expect(eval(code)).toEqual([1, 2, 2, 3, 4]);61});6263it('should transform rest parameters in nested functions', () => {64var code = transform([65'(function(x, ...args) {',66' return function(...params) {',67' return args.concat(params);',68' };',69'})(1, 2, 3)(4, 5);'70].join('\n'));7172expect(eval(code)).toEqual([2, 3, 4, 5]);73});7475it('should supply an array object', () => {76var code = transform([77'(function(...args) {',78' return Array.isArray(args);',79'})()'80].join('\n'));8182expect(eval(code)).toBe(true);83});84});8586describe('function declarations', () => {87it('should capture 2 rest params, having 2 args', () => {88var code = transform([89'function test(x, y, ...args) {',90' return [x, y, args.length, args[0], args[1]];',91'}'92].join('\n'));9394eval(code);9596expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);97});9899it('should transform rest parameters in nested functions', () => {100var code = transform([101'function testOuter(x, ...args) {',102' function testInner(...params) {',103' return args.concat(params);',104' }',105' return testInner;',106'}'107].join('\n'));108109eval(code);110111expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);112});113114it('should supply an array object', () => {115var code = transform([116'function test(...args) {',117' return Array.isArray(args);',118'}'119].join('\n'));120121eval(code);122123expect(test()).toBe(true);124});125});126127describe('arrow functions', () => {128it('should transform non-block bodied arrow functions', () => {129var code = transform([130'var test = (...args) => args;'131].join('\n'));132133eval(code);134135expect(test('foo', 'bar')).toEqual(['foo', 'bar'])136});137138it('should capture 2 rest params, having 2 args', () => {139var code = transform([140'var test = (x, y, ...args) => {',141' return [x, y, args.length, args[0], args[1]];',142'}'143].join('\n'));144145eval(code);146147expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);148});149150it('should transform rest parameters in nested arrow functions', () => {151var code = transform([152'var testOuter = (x, ...args) => {',153' var testInner = (...params) => {',154' return args.concat(params);',155' };',156' return testInner;',157'};'158].join('\n'));159160eval(code);161162expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);163});164165it('should supply an array object', () => {166var code = transform([167'var test = (...args) => {',168' return Array.isArray(args);',169'};'170].join('\n'));171172eval(code);173174expect(test()).toBe(true);175});176});177178describe('class methods', () => {179it('should capture 2 rest params, having 2 args', () => {180var code = transform([181'class Foo {',182' constructor(x, y, ...args) {',183' this.ctor = [x, y, args.length, args[0], args[1]];',184' }',185' testMethod(x, y, ...args) {',186' return [x, y, args.length, args[0], args[1]];',187' }',188' static testMethod(x, y, ...args) {',189' return [x, y, args.length, args[0], args[1]];',190' }',191'}'192].join('\n'));193194eval(code);195196var fooInst = new Foo(1, 2, 3, 4);197expect(fooInst.ctor).toEqual([1, 2, 2, 3, 4]);198expect(fooInst.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);199expect(Foo.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);200});201202it('should transform rest parameters in nested functions', () => {203var code = transform([204'class Foo {',205' constructor(x, ...args) {',206' function inner(...params) {',207' return args.concat(params);',208' }',209' this.ctor = inner;',210' }',211' testMethod(x, ...args) {',212' function inner(...params) {',213' return args.concat(params);',214' }',215' return inner;',216' }',217' static testMethod(x, ...args) {',218' function inner(...params) {',219' return args.concat(params);',220' }',221' return inner;',222' }',223'}'224].join('\n'));225226eval(code);227228var fooInst = new Foo(1, 2, 3);229expect(fooInst.ctor(4, 5)).toEqual([2, 3, 4, 5]);230expect(fooInst.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);231expect(Foo.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);232});233234it('should supply an array object', () => {235var code = transform([236'class Foo {',237' constructor(...args) {',238' this.ctor = Array.isArray(args);',239' }',240' testMethod(...args) {',241' return Array.isArray(args);',242' }',243' static testMethod(...args) {',244' return Array.isArray(args);',245' }',246'}'247].join('\n'));248249eval(code);250251var fooInst = new Foo();252expect(fooInst.ctor).toBe(true);253expect(fooInst.testMethod()).toBe(true);254expect(Foo.testMethod()).toBe(true);255});256});257258describe('whitespace preservation', () => {259it('1-line function decl with 2 args', () => {260expectTransform(261'function foo(x, y, ...args) { return x + y + args[0]; }',262'function foo(x, y ) {for (var args=[],$__0=2,$__1=arguments.length;' +263'$__0<$__1;$__0++) args.push(arguments[$__0]); return x + y + ' +264'args[0]; }'265);266})267268it('1-line function expression with 1 arg', () => {269expectTransform(270'(function(x, ...args) { return args;});',271'(function(x ) {for (var args=[],$__0=1,$__1=arguments.length;' +272'$__0<$__1;$__0++) args.push(arguments[$__0]); return args;});'273);274});275276it('1-line function expression with no args', () => {277expectTransform(278'map(function(...args) { return args.map(log); });',279'map(function() {for (var args=[],$__0=0,$__1=arguments.length;' +280'$__0<$__1;$__0++) args.push(arguments[$__0]); ' +281'return args.map(log); });'282);283});284285it('preserves lines for ugly code', () => {286expectTransform([287'function',288'',289'foo (',290' x,',291' ...args',292'',293')',294'',295' {',296' return args;',297'}'298].join('\n'), [299'function',300'',301'foo (',302' x',303' ',304'',305')',306'',307' {for (var args=[],$__0=1,$__1=arguments.length;$__0<$__1;' +308'$__0++) args.push(arguments[$__0]);',309' return args;',310'}'311].join('\n'));312});313314it('preserves inline comments', () => {315expectTransform(316'function foo(/*string*/foo, /*bool*/bar, ...args) { return args; }',317'function foo(/*string*/foo, /*bool*/bar ) {' +318'for (var args=[],$__0=2,$__1=arguments.length;$__0<$__1;$__0++) ' +319'args.push(arguments[$__0]); ' +320'return args; ' +321'}'322);323});324});325});326327328329