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-object-concise-method-visitors', function() {
26
var transformFn;
27
var conciseMethodVisitors;
28
var restParamVisitors;
29
var visitors;
30
31
beforeEach(function() {
32
require('mock-modules').dumpCache();
33
conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;
34
restParamVisitors = require('../es6-rest-param-visitors').visitorList;
35
transformFn = require('../../src/jstransform').transform;
36
visitors = conciseMethodVisitors.concat(restParamVisitors);
37
});
38
39
function transform(code) {
40
return transformFn(visitors, code).code;
41
}
42
43
function expectTransform(code, result) {
44
expect(transform(code)).toEqual(result);
45
}
46
47
// Functional tests.
48
49
it('should transform concise method and return 42', function() {
50
var code = transform([
51
'var foo = {',
52
' bar(x) {',
53
' return x;',
54
' }',
55
'};'
56
].join('\n'));
57
58
eval(code);
59
expect(foo.bar(42)).toEqual(42);
60
});
61
62
it('should transform concise method with literal property', function() {
63
var code = transform([
64
'var foo = {',
65
' "bar 1"(x) {',
66
' return x;',
67
' }',
68
'};'
69
].join('\n'));
70
71
eval(code);
72
expect(foo['bar 1'](42)).toEqual(42);
73
});
74
75
76
it('should work with rest params', function() {
77
var code = transform([
78
'({',
79
' init(x, ...rest) {',
80
' return rest.concat(x);',
81
' }',
82
'}).init(1, 2, 3);'
83
].join('\n'));
84
85
expect(eval(code)).toEqual([2, 3, 1]);
86
});
87
88
// Source code tests.
89
it('should transform concise methods', function() {
90
91
// Should transform simple concise method.
92
expectTransform(
93
'var foo = {bar() {}};',
94
'var foo = {bar:function() {}};'
95
);
96
97
// Should transform inner objects.
98
expectTransform(
99
'({bar(x) { return {baz(y) {}}; }});',
100
'({bar:function(x) { return {baz:function(y) {}}; }});'
101
);
102
});
103
104
it('should preserve generators', function() {
105
// Identifier properties
106
expectTransform(
107
'var foo = {*bar(x) {yield x;}};',
108
'var foo = {bar:function*(x) {yield x;}};'
109
);
110
111
// Literal properties
112
expectTransform(
113
'var foo = {*"abc"(x) {yield x;}, *42(x) {yield x;}};',
114
'var foo = {"abc":function*(x) {yield x;}, 42:function*(x) {yield x;}};'
115
);
116
117
// Dynamic properties
118
expectTransform(
119
'var foo = {*[a+b](x) {yield x;}}',
120
'var foo = {[a+b]:function*(x) {yield x;}}'
121
);
122
});
123
124
it('should handle reserved words', function() {
125
expectTransform(
126
'({delete(x) {}})',
127
'({"delete":function(x) {}})'
128
);
129
});
130
});
131
132
133
134