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