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