Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80552 views
1
/**
2
* @emails [email protected]
3
*/
4
5
/*jshint evil:true*/
6
/*jshint -W117*/
7
8
require('mock-modules').autoMockOff();
9
10
describe('es6-destructuring-visitors', function() {
11
var transformFn;
12
13
var destructuringVisitors;
14
var conciseMethodVisitors;
15
var shortObjectsVisitors;
16
var reservedWordsVisitors;
17
var restParamVisitors;
18
var classVisitorsVisitors;
19
var arrowFunctionVisitors;
20
21
var visitors;
22
23
beforeEach(function() {
24
require('mock-modules').dumpCache();
25
transformFn = require('../../src/jstransform').transform;
26
27
destructuringVisitors = require('../es6-destructuring-visitors').visitorList;
28
conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;
29
shortObjectsVisitors = require('../es6-object-short-notation-visitors').visitorList;
30
reservedWordsVisitors = require('../reserved-words-visitors').visitorList;
31
restParamVisitors = require('../es6-rest-param-visitors').visitorList;
32
classVisitorsVisitors = require('../es6-class-visitors').visitorList;
33
arrowFunctionVisitors = require('../es6-arrow-function-visitors').visitorList;
34
35
visitors = destructuringVisitors.concat(
36
conciseMethodVisitors,
37
shortObjectsVisitors,
38
restParamVisitors,
39
classVisitorsVisitors,
40
arrowFunctionVisitors,
41
reservedWordsVisitors
42
);
43
});
44
45
function transform(code) {
46
return transformFn(visitors, code).code;
47
}
48
49
function expectTransform(code, result) {
50
expect(transform(code)).toEqual(result);
51
}
52
53
it('should handle simple object pattern', function() {
54
var code = transform([
55
'var {x, y} = {x: 10, y: 20};',
56
'(x + y);'
57
].join('\n'));
58
59
expect(eval(code)).toEqual(30);
60
});
61
62
it('should handle literal property names', function() {
63
var code = transform([
64
'var {x, "y y": yy, 0: z} = {x: 10, "y y": 20, 0: 30};',
65
'([x, yy, z]);'
66
].join('\n'));
67
68
expect(eval(code)).toEqual([10, 20, 30]);
69
});
70
71
it('should handle array pattern assignment expression', function() {
72
var code = transform([
73
'var x = 10, y = 20;',
74
'[x, y] = [y, x];',
75
'([x, y]);'
76
].join('\n'));
77
78
expect(eval(code)).toEqual([20, 10]);
79
});
80
81
it('should should not redeclare vars with assignment expression', function() {
82
var code = transform([
83
'var x = 10, y = 20;',
84
'(function() {',
85
' [x, y] = [y, x];',
86
'})();',
87
'([x, y]);'
88
].join('\n'));
89
90
expect(eval(code)).toEqual([20, 10]);
91
});
92
93
it('should handle object pattern assignment expression', function() {
94
var code = transform([
95
'var x = 10, y = 20;',
96
'({x, y} = {y, x});',
97
'({x, y});'
98
].join('\n'));
99
100
expect(eval(code)).toEqual({x: 10, y: 20});
101
});
102
103
it('should destructure result of a function', function() {
104
var code = transform([
105
'var [x, y] = (function({x, y}) { return [x, y]; })({x: 1, y: 2});',
106
'([x, y]);'
107
].join('\n'));
108
109
expect(eval(code)).toEqual([1, 2]);
110
});
111
112
it('should handle skipped array elements', function() {
113
var code = transform([
114
'var [x, , y] = [1, 2, 3];',
115
'([x, y]);'
116
].join('\n'));
117
118
expect(eval(code)).toEqual([1, 3]);
119
});
120
121
it('should handle rest elements of an array', function() {
122
var code = transform([
123
'var [x, ...xs] = [1, 2, 3];'
124
].join('\n'));
125
126
eval(code);
127
128
expect(x).toEqual(1);
129
expect(xs).toEqual([2, 3]);
130
});
131
132
it('should swap two variables w/o third using pattern', function() {
133
var code = transform([
134
'var x = 10, y = 20;',
135
'var [x, y] = [y, x];',
136
'([x, y]);'
137
].join('\n'));
138
139
expect(eval(code)).toEqual([20, 10]);
140
});
141
142
it('should transform complex pattern argument', function() {
143
var code = transform([
144
'function init(user, {ip, coords: [x, y], port}) {',
145
' return [user, ip, x, y, port].join(", ");',
146
'}'
147
].join('\n'));
148
149
eval(code);
150
151
expect(init(
152
'John Doe', {
153
ip: '127.0.0.1',
154
coords: [1, 2],
155
port: 8080
156
}
157
)).toBe('John Doe, 127.0.0.1, 1, 2, 8080');
158
});
159
160
it('should work with rest params', function() {
161
var code = transform([
162
'function foo({bar, baz}, ...rest) {',
163
' return {bar, baz, qux: rest[0]};',
164
'}'
165
].join('\n'));
166
167
eval(code);
168
169
expect(foo({bar: 10, baz: 20}, 30))
170
.toEqual({bar: 10, baz: 20, qux: 30});
171
});
172
173
it('should work with class methods', function() {
174
var code = transform([
175
'class Point {',
176
' constructor({x, y}) {',
177
' this._x = x;',
178
' this._y = y;',
179
' }',
180
'',
181
' getData([deltaX, deltaY]) {',
182
' return this._x + deltaX + this._y + deltaY',
183
' }',
184
'}'
185
].join('\n'));
186
187
eval(code);
188
189
var x = 10, y = 20;
190
var foo = new Point({x: x, y: y});
191
var data = foo.getData([30, 40]);
192
193
expect(data).toBe(100);
194
});
195
196
it('should work with object concise methods', function() {
197
var code = transform([
198
'var foo = {',
199
' bar({x, y}) {',
200
' return {x, y};',
201
' }',
202
'}'
203
].join('\n'));
204
205
eval(code);
206
207
expect(foo.bar({x: 10, y: 20}))
208
.toEqual({x: 10, y: 20});
209
});
210
211
it('should work with arrows', function() {
212
var code = transform([
213
'var foo = ({x, y}, z) => x + y + z;'
214
].join('\n'));
215
216
eval(code);
217
218
expect(foo({x: 10, y: 20}, 30))
219
.toEqual(60);
220
});
221
222
// Auto-generated temp vars test.
223
it('should allocate correct temp index', function() {
224
var code = transform([
225
'function foo(x, {y}, {z}) {',
226
' var {q} = {q: 30};',
227
' return [$__0, $__1, $__2];',
228
'}'
229
].join('\n'));
230
231
eval(code);
232
233
expect(foo(1, {y: 10}, {z: 20}))
234
.toEqual([{y: 10}, {z: 20}, {q: 30}]);
235
});
236
237
it('should allocate correct temp nested index', function() {
238
var code = transform([
239
'var foo = function(x, {y}, {z}) {',
240
' var {q, m: {v}} = {q: 30, m: {v: 40}};',
241
' var {a} = (function({a}) { return $__0; })({a: 50});',
242
' return [$__0, $__1, $__2, $__3, a];',
243
'}'
244
].join('\n'));
245
246
eval(code);
247
248
expect(foo(1, {y: 10}, {z: 20}))
249
.toEqual([{y: 10}, {z: 20}, {q: 30, m: {v: 40}}, {v: 40}, 50]);
250
});
251
252
253
254
// Syntax tests.
255
256
it('should correctly transform structured patterns', function() {
257
258
// Variable declaration.
259
expectTransform(
260
'var a, {x, data: [y, , z]} = {x: 10, data: [1, 2, 3]};',
261
'var a, $__0= {x: 10, data: [1, 2, 3]},x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[2];'
262
);
263
264
// Function parameters.
265
expectTransform(
266
'function f(a, {x, data: [y, z]}, b) {}',
267
'function f(a, $__0 , b) {var x=$__0.x,$__1=$__0.data,y=$__1[0],z=$__1[1];}'
268
);
269
});
270
271
it('should handle reserved words', function() {
272
expectTransform(
273
'var {delete: x} = {delete: 1};',
274
'var $__0= {"delete": 1},x=$__0["delete"];'
275
);
276
});
277
});
278
279
280
281