react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / __tests__ / es6-rest-param-visitors-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*/1516/**17* @emails [email protected]18*/1920/*jshint evil:true*/21/*jshint -W117*/2223require('mock-modules').autoMockOff();2425describe('es6-rest-param-visitors', () => {26var transformFn;27var visitorSet;28var arrowFuncVisitors;29var classVisitors;30var restParamVisitors;3132beforeEach(() => {33require('mock-modules').dumpCache();34arrowFuncVisitors = require('../es6-arrow-function-visitors').visitorList;35classVisitors = require('../es6-class-visitors').visitorList;36restParamVisitors = require('../es6-rest-param-visitors').visitorList;37transformFn = require('../../src/jstransform').transform;3839visitorSet =40arrowFuncVisitors41.concat(classVisitors)42.concat(restParamVisitors);43});4445function transform(code) {46return transformFn(visitorSet, code).code;47}4849function expectTransform(code, result) {50expect(transform(code)).toEqual(result);51}5253describe('function expressions', () => {54it('should capture 2 rest params, having 2 args', () => {55var code = transform([56'(function(x, y, ...args) {',57' return [x, y, args.length, args[0], args[1]];',58'})(1, 2, 3, 4);'59].join('\n'));6061expect(eval(code)).toEqual([1, 2, 2, 3, 4]);62});6364it('should transform rest parameters in nested functions', () => {65var code = transform([66'(function(x, ...args) {',67' return function(...params) {',68' return args.concat(params);',69' };',70'})(1, 2, 3)(4, 5);'71].join('\n'));7273expect(eval(code)).toEqual([2, 3, 4, 5]);74});7576it('should supply an array object', () => {77var code = transform([78'(function(...args) {',79' return Array.isArray(args);',80'})()'81].join('\n'));8283expect(eval(code)).toBe(true);84});85});8687describe('function declarations', () => {88it('should capture 2 rest params, having 2 args', () => {89var code = transform([90'function test(x, y, ...args) {',91' return [x, y, args.length, args[0], args[1]];',92'}'93].join('\n'));9495eval(code);9697expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);98});99100it('should transform rest parameters in nested functions', () => {101var code = transform([102'function testOuter(x, ...args) {',103' function testInner(...params) {',104' return args.concat(params);',105' }',106' return testInner;',107'}'108].join('\n'));109110eval(code);111112expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);113});114115it('should supply an array object', () => {116var code = transform([117'function test(...args) {',118' return Array.isArray(args);',119'}'120].join('\n'));121122eval(code);123124expect(test()).toBe(true);125});126});127128describe('arrow functions', () => {129it('should transform non-block bodied arrow functions', () => {130var code = transform([131'var test = (...args) => args;'132].join('\n'));133134eval(code);135136expect(test('foo', 'bar')).toEqual(['foo', 'bar']);137});138139it('should capture 2 rest params, having 2 args', () => {140var code = transform([141'var test = (x, y, ...args) => {',142' return [x, y, args.length, args[0], args[1]];',143'}'144].join('\n'));145146eval(code);147148expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);149});150151it('should transform rest parameters in nested arrow functions', () => {152var code = transform([153'var testOuter = (x, ...args) => {',154' var testInner = (...params) => {',155' return args.concat(params);',156' };',157' return testInner;',158'};'159].join('\n'));160161eval(code);162163expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);164});165166it('should supply an array object', () => {167var code = transform([168'var test = (...args) => {',169' return Array.isArray(args);',170'};'171].join('\n'));172173eval(code);174175expect(test()).toBe(true);176});177});178179describe('class methods', () => {180it('should capture 2 rest params, having 2 args', () => {181var code = transform([182'class Foo {',183' constructor(x, y, ...args) {',184' this.ctor = [x, y, args.length, args[0], args[1]];',185' }',186' testMethod(x, y, ...args) {',187' return [x, y, args.length, args[0], args[1]];',188' }',189' static testMethod(x, y, ...args) {',190' return [x, y, args.length, args[0], args[1]];',191' }',192'}'193].join('\n'));194195eval(code);196197var fooInst = new Foo(1, 2, 3, 4);198expect(fooInst.ctor).toEqual([1, 2, 2, 3, 4]);199expect(fooInst.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);200expect(Foo.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);201});202203it('should transform rest parameters in nested functions', () => {204var code = transform([205'class Foo {',206' constructor(x, ...args) {',207' function inner(...params) {',208' return args.concat(params);',209' }',210' this.ctor = inner;',211' }',212' testMethod(x, ...args) {',213' function inner(...params) {',214' return args.concat(params);',215' }',216' return inner;',217' }',218' static testMethod(x, ...args) {',219' function inner(...params) {',220' return args.concat(params);',221' }',222' return inner;',223' }',224'}'225].join('\n'));226227eval(code);228229var fooInst = new Foo(1, 2, 3);230expect(fooInst.ctor(4, 5)).toEqual([2, 3, 4, 5]);231expect(fooInst.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);232expect(Foo.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);233});234235it('should supply an array object', () => {236var code = transform([237'class Foo {',238' constructor(...args) {',239' this.ctor = Array.isArray(args);',240' }',241' testMethod(...args) {',242' return Array.isArray(args);',243' }',244' static testMethod(...args) {',245' return Array.isArray(args);',246' }',247'}'248].join('\n'));249250eval(code);251252var fooInst = new Foo();253expect(fooInst.ctor).toBe(true);254expect(fooInst.testMethod()).toBe(true);255expect(Foo.testMethod()).toBe(true);256});257});258259describe('whitespace preservation', () => {260it('1-line function decl with 2 args', () => {261expectTransform(262'function foo(x, y, ...args) { return x + y + args[0]; }',263'function foo(x, y ) {for (var args=[],$__0=2,$__1=arguments.length;' +264'$__0<$__1;$__0++) args.push(arguments[$__0]); return x + y + ' +265'args[0]; }'266);267});268269it('1-line function expression with 1 arg', () => {270expectTransform(271'(function(x, ...args) { return args;});',272'(function(x ) {for (var args=[],$__0=1,$__1=arguments.length;' +273'$__0<$__1;$__0++) args.push(arguments[$__0]); return args;});'274);275});276277it('1-line function expression with no args', () => {278expectTransform(279'map(function(...args) { return args.map(log); });',280'map(function() {for (var args=[],$__0=0,$__1=arguments.length;' +281'$__0<$__1;$__0++) args.push(arguments[$__0]); ' +282'return args.map(log); });'283);284});285286it('preserves lines for ugly code', () => {287expectTransform([288'function',289'',290'foo (',291' x,',292' ...args',293'',294')',295'',296' {',297' return args;',298'}'299].join('\n'), [300'function',301'',302'foo (',303' x',304' ',305'',306')',307'',308' {for (var args=[],$__0=1,$__1=arguments.length;$__0<$__1;' +309'$__0++) args.push(arguments[$__0]);',310' return args;',311'}'312].join('\n'));313});314315it('preserves inline comments', () => {316expectTransform(317'function foo(/*string*/foo, /*bool*/bar, ...args) { return args; }',318'function foo(/*string*/foo, /*bool*/bar ) {' +319'for (var args=[],$__0=2,$__1=arguments.length;$__0<$__1;$__0++) ' +320'args.push(arguments[$__0]); ' +321'return args; ' +322'}'323);324});325});326});327328329330