Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
/**
18
* @emails [email protected]
19
*/
20
21
/*jshint evil:true*/
22
/*jshint -W117*/
23
24
require('mock-modules').autoMockOff();
25
26
describe('es6-rest-param-visitors', () => {
27
var transformFn;
28
var visitorSet;
29
var arrowFuncVisitors;
30
var classVisitors;
31
var restParamVisitors;
32
33
beforeEach(() => {
34
require('mock-modules').dumpCache();
35
arrowFuncVisitors = require('../es6-arrow-function-visitors').visitorList;
36
classVisitors = require('../es6-class-visitors').visitorList;
37
restParamVisitors = require('../es6-rest-param-visitors').visitorList;
38
transformFn = require('../../src/jstransform').transform;
39
40
visitorSet =
41
arrowFuncVisitors
42
.concat(classVisitors)
43
.concat(restParamVisitors);
44
});
45
46
function transform(code) {
47
return transformFn(visitorSet, code).code;
48
}
49
50
function expectTransform(code, result) {
51
expect(transform(code)).toEqual(result);
52
}
53
54
describe('function expressions', () => {
55
it('should capture 2 rest params, having 2 args', () => {
56
var code = transform([
57
'(function(x, y, ...args) {',
58
' return [x, y, args.length, args[0], args[1]];',
59
'})(1, 2, 3, 4);'
60
].join('\n'));
61
62
expect(eval(code)).toEqual([1, 2, 2, 3, 4]);
63
});
64
65
it('should transform rest parameters in nested functions', () => {
66
var code = transform([
67
'(function(x, ...args) {',
68
' return function(...params) {',
69
' return args.concat(params);',
70
' };',
71
'})(1, 2, 3)(4, 5);'
72
].join('\n'));
73
74
expect(eval(code)).toEqual([2, 3, 4, 5]);
75
});
76
77
it('should supply an array object', () => {
78
var code = transform([
79
'(function(...args) {',
80
' return Array.isArray(args);',
81
'})()'
82
].join('\n'));
83
84
expect(eval(code)).toBe(true);
85
});
86
});
87
88
describe('function declarations', () => {
89
it('should capture 2 rest params, having 2 args', () => {
90
var code = transform([
91
'function test(x, y, ...args) {',
92
' return [x, y, args.length, args[0], args[1]];',
93
'}'
94
].join('\n'));
95
96
eval(code);
97
98
expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
99
});
100
101
it('should transform rest parameters in nested functions', () => {
102
var code = transform([
103
'function testOuter(x, ...args) {',
104
' function testInner(...params) {',
105
' return args.concat(params);',
106
' }',
107
' return testInner;',
108
'}'
109
].join('\n'));
110
111
eval(code);
112
113
expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
114
});
115
116
it('should supply an array object', () => {
117
var code = transform([
118
'function test(...args) {',
119
' return Array.isArray(args);',
120
'}'
121
].join('\n'));
122
123
eval(code);
124
125
expect(test()).toBe(true);
126
});
127
});
128
129
describe('arrow functions', () => {
130
it('should transform non-block bodied arrow functions', () => {
131
var code = transform([
132
'var test = (...args) => args;'
133
].join('\n'));
134
135
eval(code);
136
137
expect(test('foo', 'bar')).toEqual(['foo', 'bar']);
138
});
139
140
it('should capture 2 rest params, having 2 args', () => {
141
var code = transform([
142
'var test = (x, y, ...args) => {',
143
' return [x, y, args.length, args[0], args[1]];',
144
'}'
145
].join('\n'));
146
147
eval(code);
148
149
expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
150
});
151
152
it('should transform rest parameters in nested arrow functions', () => {
153
var code = transform([
154
'var testOuter = (x, ...args) => {',
155
' var testInner = (...params) => {',
156
' return args.concat(params);',
157
' };',
158
' return testInner;',
159
'};'
160
].join('\n'));
161
162
eval(code);
163
164
expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
165
});
166
167
it('should supply an array object', () => {
168
var code = transform([
169
'var test = (...args) => {',
170
' return Array.isArray(args);',
171
'};'
172
].join('\n'));
173
174
eval(code);
175
176
expect(test()).toBe(true);
177
});
178
});
179
180
describe('class methods', () => {
181
it('should capture 2 rest params, having 2 args', () => {
182
var code = transform([
183
'class Foo {',
184
' constructor(x, y, ...args) {',
185
' this.ctor = [x, y, args.length, args[0], args[1]];',
186
' }',
187
' testMethod(x, y, ...args) {',
188
' return [x, y, args.length, args[0], args[1]];',
189
' }',
190
' static testMethod(x, y, ...args) {',
191
' return [x, y, args.length, args[0], args[1]];',
192
' }',
193
'}'
194
].join('\n'));
195
196
eval(code);
197
198
var fooInst = new Foo(1, 2, 3, 4);
199
expect(fooInst.ctor).toEqual([1, 2, 2, 3, 4]);
200
expect(fooInst.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
201
expect(Foo.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
202
});
203
204
it('should transform rest parameters in nested functions', () => {
205
var code = transform([
206
'class Foo {',
207
' constructor(x, ...args) {',
208
' function inner(...params) {',
209
' return args.concat(params);',
210
' }',
211
' this.ctor = inner;',
212
' }',
213
' testMethod(x, ...args) {',
214
' function inner(...params) {',
215
' return args.concat(params);',
216
' }',
217
' return inner;',
218
' }',
219
' static testMethod(x, ...args) {',
220
' function inner(...params) {',
221
' return args.concat(params);',
222
' }',
223
' return inner;',
224
' }',
225
'}'
226
].join('\n'));
227
228
eval(code);
229
230
var fooInst = new Foo(1, 2, 3);
231
expect(fooInst.ctor(4, 5)).toEqual([2, 3, 4, 5]);
232
expect(fooInst.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
233
expect(Foo.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
234
});
235
236
it('should supply an array object', () => {
237
var code = transform([
238
'class Foo {',
239
' constructor(...args) {',
240
' this.ctor = Array.isArray(args);',
241
' }',
242
' testMethod(...args) {',
243
' return Array.isArray(args);',
244
' }',
245
' static testMethod(...args) {',
246
' return Array.isArray(args);',
247
' }',
248
'}'
249
].join('\n'));
250
251
eval(code);
252
253
var fooInst = new Foo();
254
expect(fooInst.ctor).toBe(true);
255
expect(fooInst.testMethod()).toBe(true);
256
expect(Foo.testMethod()).toBe(true);
257
});
258
});
259
260
describe('whitespace preservation', () => {
261
it('1-line function decl with 2 args', () => {
262
expectTransform(
263
'function foo(x, y, ...args) { return x + y + args[0]; }',
264
'function foo(x, y ) {for (var args=[],$__0=2,$__1=arguments.length;' +
265
'$__0<$__1;$__0++) args.push(arguments[$__0]); return x + y + ' +
266
'args[0]; }'
267
);
268
});
269
270
it('1-line function expression with 1 arg', () => {
271
expectTransform(
272
'(function(x, ...args) { return args;});',
273
'(function(x ) {for (var args=[],$__0=1,$__1=arguments.length;' +
274
'$__0<$__1;$__0++) args.push(arguments[$__0]); return args;});'
275
);
276
});
277
278
it('1-line function expression with no args', () => {
279
expectTransform(
280
'map(function(...args) { return args.map(log); });',
281
'map(function() {for (var args=[],$__0=0,$__1=arguments.length;' +
282
'$__0<$__1;$__0++) args.push(arguments[$__0]); ' +
283
'return args.map(log); });'
284
);
285
});
286
287
it('preserves lines for ugly code', () => {
288
expectTransform([
289
'function',
290
'',
291
'foo (',
292
' x,',
293
' ...args',
294
'',
295
')',
296
'',
297
' {',
298
' return args;',
299
'}'
300
].join('\n'), [
301
'function',
302
'',
303
'foo (',
304
' x',
305
' ',
306
'',
307
')',
308
'',
309
' {for (var args=[],$__0=1,$__1=arguments.length;$__0<$__1;' +
310
'$__0++) args.push(arguments[$__0]);',
311
' return args;',
312
'}'
313
].join('\n'));
314
});
315
316
it('preserves inline comments', () => {
317
expectTransform(
318
'function foo(/*string*/foo, /*bool*/bar, ...args) { return args; }',
319
'function foo(/*string*/foo, /*bool*/bar ) {' +
320
'for (var args=[],$__0=2,$__1=arguments.length;$__0<$__1;$__0++) ' +
321
'args.push(arguments[$__0]); ' +
322
'return args; ' +
323
'}'
324
);
325
});
326
});
327
});
328
329
330