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('es6ArrowFunctionsTransform', function() {
27
var transformFn;
28
var visitors;
29
30
beforeEach(function() {
31
require('mock-modules').dumpCache();
32
visitors = require('../es6-arrow-function-visitors').visitorList;
33
transformFn = require('../../src/jstransform').transform;
34
});
35
36
function transform(code) {
37
return transformFn(visitors, code).code;
38
}
39
40
function expectTransform(code, result) {
41
expect(transform(code)).toEqual(result);
42
}
43
44
it('should capture correct this value at different levels', function() {
45
var code = transform([
46
'var foo = {',
47
' createFooGetter: function() {',
48
' return (x) => [x, this];', // captures foo
49
' },',
50
' getParentThis: () => this', // captures parent this
51
'};'
52
].join('\n'));
53
54
eval(code);
55
56
expect(typeof foo.createFooGetter).toBe('function');
57
expect(typeof foo.createFooGetter()).toBe('function');
58
expect(typeof foo.getParentThis).toBe('function');
59
60
expect(foo.getParentThis()).toEqual(this);
61
expect(foo.createFooGetter()(10)).toEqual([10, foo]);
62
});
63
64
it('should map an array using arrow capturing this value', function() {
65
this.factor = 10;
66
67
var code = transform(
68
'[1, 2, 3].map(x => x * x * this.factor);'
69
);
70
71
expect(eval(code)).toEqual([10, 40, 90]);
72
});
73
74
it('binds if any `super` keyword is referenced', function() {
75
var code = transform(
76
'var fn=x=>super;'
77
);
78
79
// We have to do a source text comparison here because `super` is a reserved
80
// keyword (so we can't eval it).
81
expect(code).toEqual('var fn=function(x){return super;}.bind(this);');
82
});
83
84
it('should filter an array using arrow with two params', function() {
85
this.factor = 0;
86
87
var code = transform([
88
'[1, 2, 3].filter((v, idx) => {',
89
' if (idx > 1 && this.factor > 0) {',
90
' return true;',
91
' }',
92
' this.factor++;',
93
' return false;',
94
'});'
95
].join('\n'));
96
97
expect(eval(code)).toEqual([3]);
98
});
99
100
it('should fetch this value data from nested arrow', function() {
101
var code = transform([
102
'({',
103
' bird: 22,',
104
' run: function() {',
105
' return () => () => this.bird;',
106
' }',
107
'}).run()()();'
108
].join('\n'));
109
110
expect(eval(code)).toEqual(22);
111
});
112
113
// Syntax tests.
114
115
it('should correctly transform arrows', function() {
116
// 0 params, expression.
117
expectTransform(
118
'() => this.value;',
119
'(function() {return this.value;}.bind(this));'
120
);
121
122
// 0 params, expression wrapped in parens
123
expectTransform(
124
'() => (this.value);',
125
'(function() {return this.value;}.bind(this));'
126
);
127
128
// 1 param, no-parens, expression, no this.
129
expectTransform(
130
'x => x * x;',
131
'(function(x) {return x * x;});'
132
);
133
134
// 1 param, parens, expression, as argument, no this.
135
expectTransform(
136
'map((x) => x * x);',
137
'map(function(x) {return x * x;});'
138
);
139
140
// 2 params, block, as argument, nested.
141
expectTransform(
142
'makeRequest((response, error) => {'.concat(
143
' return this.update(data => this.onData(data), response);',
144
'});'),
145
'makeRequest(function(response, error) {'.concat(
146
' return this.update(function(data) {return this.onData(data);}.bind(this), response);',
147
'}.bind(this));')
148
);
149
150
// Assignment to a var, simple, 1 param.
151
expectTransform(
152
'var action = (value) => this.performAction(value);',
153
'var action = function(value) {return this.performAction(value);}.bind(this);'
154
);
155
156
// Preserve lines transforming ugly code.
157
expectTransform([
158
'(',
159
'',
160
'',
161
' x,',
162
' y',
163
'',
164
')',
165
'',
166
' =>',
167
'',
168
' {',
169
' return x + y;',
170
'};'
171
].join('\n'), [
172
'(function(',
173
'',
174
'',
175
' x,',
176
' y)',
177
'',
178
'',
179
'',
180
' ',
181
'',
182
' {',
183
' return x + y;',
184
'});'
185
].join('\n'));
186
187
// Preserve line numbers with single parens-free param ugly code.
188
expectTransform([
189
'x',
190
'',
191
' =>',
192
' x;'
193
].join('\n'), [
194
'(function(x)',
195
'',
196
' ',
197
' {return x;});'
198
].join('\n'));
199
200
// Preserve line numbers with single parens param ugly code.
201
expectTransform([
202
'(',
203
'',
204
' x',
205
'',
206
')',
207
'',
208
' =>',
209
' x;'
210
].join('\n'), [
211
'(function(',
212
'',
213
' x)',
214
'',
215
'',
216
'',
217
' ',
218
' {return x;});'
219
].join('\n'));
220
221
// Preserve line numbers with parens around expression.
222
expectTransform([
223
'(x) => (',
224
' x',
225
');'
226
].join('\n'), [
227
'(function(x) ',
228
' {return x;}',
229
');'
230
].join('\n'));
231
232
// Preserve typechecker annotation.
233
expectTransform(
234
'(/*string*/foo, /*bool*/bar) => foo;',
235
'(function(/*string*/foo, /*bool*/bar) {return foo;});'
236
);
237
});
238
});
239
240
241