Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81153 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
23
require('mock-modules').autoMockOff();
24
25
describe('es6-rest-param-visitors', () => {
26
var transformFn;
27
var visitorSet;
28
var arrowFuncVisitors;
29
var classVisitors;
30
var restParamVisitors;
31
32
beforeEach(() => {
33
require('mock-modules').dumpCache();
34
arrowFuncVisitors = require('../es6-arrow-function-visitors').visitorList;
35
classVisitors = require('../es6-class-visitors').visitorList;
36
restParamVisitors = require('../es6-rest-param-visitors').visitorList;
37
transformFn = require('../../src/jstransform').transform;
38
39
visitorSet =
40
arrowFuncVisitors
41
.concat(classVisitors)
42
.concat(restParamVisitors);
43
});
44
45
function transform(code) {
46
return transformFn(visitorSet, code).code;
47
}
48
49
function expectTransform(code, result) {
50
expect(transform(code)).toEqual(result);
51
}
52
53
describe('function expressions', () => {
54
it('should capture 2 rest params, having 2 args', () => {
55
var 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'));
60
61
expect(eval(code)).toEqual([1, 2, 2, 3, 4]);
62
});
63
64
it('should transform rest parameters in nested functions', () => {
65
var 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'));
72
73
expect(eval(code)).toEqual([2, 3, 4, 5]);
74
});
75
76
it('should supply an array object', () => {
77
var code = transform([
78
'(function(...args) {',
79
' return Array.isArray(args);',
80
'})()'
81
].join('\n'));
82
83
expect(eval(code)).toBe(true);
84
});
85
});
86
87
describe('function declarations', () => {
88
it('should capture 2 rest params, having 2 args', () => {
89
var code = transform([
90
'function test(x, y, ...args) {',
91
' return [x, y, args.length, args[0], args[1]];',
92
'}'
93
].join('\n'));
94
95
eval(code);
96
97
expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
98
});
99
100
it('should transform rest parameters in nested functions', () => {
101
var code = transform([
102
'function testOuter(x, ...args) {',
103
' function testInner(...params) {',
104
' return args.concat(params);',
105
' }',
106
' return testInner;',
107
'}'
108
].join('\n'));
109
110
eval(code);
111
112
expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
113
});
114
115
it('should supply an array object', () => {
116
var code = transform([
117
'function test(...args) {',
118
' return Array.isArray(args);',
119
'}'
120
].join('\n'));
121
122
eval(code);
123
124
expect(test()).toBe(true);
125
});
126
});
127
128
describe('arrow functions', () => {
129
it('should transform non-block bodied arrow functions', () => {
130
var code = transform([
131
'var test = (...args) => args;'
132
].join('\n'));
133
134
eval(code);
135
136
expect(test('foo', 'bar')).toEqual(['foo', 'bar'])
137
});
138
139
it('should capture 2 rest params, having 2 args', () => {
140
var code = transform([
141
'var test = (x, y, ...args) => {',
142
' return [x, y, args.length, args[0], args[1]];',
143
'}'
144
].join('\n'));
145
146
eval(code);
147
148
expect(test(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
149
});
150
151
it('should transform rest parameters in nested arrow functions', () => {
152
var code = transform([
153
'var testOuter = (x, ...args) => {',
154
' var testInner = (...params) => {',
155
' return args.concat(params);',
156
' };',
157
' return testInner;',
158
'};'
159
].join('\n'));
160
161
eval(code);
162
163
expect(testOuter(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
164
});
165
166
it('should supply an array object', () => {
167
var code = transform([
168
'var test = (...args) => {',
169
' return Array.isArray(args);',
170
'};'
171
].join('\n'));
172
173
eval(code);
174
175
expect(test()).toBe(true);
176
});
177
});
178
179
describe('class methods', () => {
180
it('should capture 2 rest params, having 2 args', () => {
181
var 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'));
194
195
eval(code);
196
197
var fooInst = new Foo(1, 2, 3, 4);
198
expect(fooInst.ctor).toEqual([1, 2, 2, 3, 4]);
199
expect(fooInst.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
200
expect(Foo.testMethod(1, 2, 3, 4)).toEqual([1, 2, 2, 3, 4]);
201
});
202
203
it('should transform rest parameters in nested functions', () => {
204
var 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'));
226
227
eval(code);
228
229
var fooInst = new Foo(1, 2, 3);
230
expect(fooInst.ctor(4, 5)).toEqual([2, 3, 4, 5]);
231
expect(fooInst.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
232
expect(Foo.testMethod(1, 2, 3)(4, 5)).toEqual([2, 3, 4, 5]);
233
});
234
235
it('should supply an array object', () => {
236
var 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'));
249
250
eval(code);
251
252
var fooInst = new Foo();
253
expect(fooInst.ctor).toBe(true);
254
expect(fooInst.testMethod()).toBe(true);
255
expect(Foo.testMethod()).toBe(true);
256
});
257
});
258
259
describe('whitespace preservation', () => {
260
it('1-line function decl with 2 args', () => {
261
expectTransform(
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
})
268
269
it('1-line function expression with 1 arg', () => {
270
expectTransform(
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
});
276
277
it('1-line function expression with no args', () => {
278
expectTransform(
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
});
285
286
it('preserves lines for ugly code', () => {
287
expectTransform([
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
});
314
315
it('preserves inline comments', () => {
316
expectTransform(
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
});
327
328
329