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
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
/*global foo*/
51
var code = transform([
52
'var foo = {',
53
' bar(x) {',
54
' return x;',
55
' }',
56
'};'
57
].join('\n'));
58
59
eval(code);
60
expect(foo.bar(42)).toEqual(42);
61
});
62
63
it('should transform concise method with literal property', function() {
64
var code = transform([
65
'var foo = {',
66
' "bar 1"(x) {',
67
' return x;',
68
' }',
69
'};'
70
].join('\n'));
71
72
eval(code);
73
expect(foo['bar 1'](42)).toEqual(42);
74
});
75
76
77
it('should work with rest params', function() {
78
var code = transform([
79
'({',
80
' init(x, ...rest) {',
81
' return rest.concat(x);',
82
' }',
83
'}).init(1, 2, 3);'
84
].join('\n'));
85
86
expect(eval(code)).toEqual([2, 3, 1]);
87
});
88
89
// Source code tests.
90
it('should transform concise methods', function() {
91
92
// Should transform simple concise method.
93
expectTransform(
94
'var foo = {bar() {}};',
95
'var foo = {bar:function() {}};'
96
);
97
98
// Should transform inner objects.
99
expectTransform(
100
'({bar(x) { return {baz(y) {}}; }});',
101
'({bar:function(x) { return {baz:function(y) {}}; }});'
102
);
103
});
104
105
it('should preserve generators', function() {
106
// Identifier properties
107
expectTransform(
108
'var foo = {*bar(x) {yield x;}};',
109
'var foo = {bar:function*(x) {yield x;}};'
110
);
111
112
// Literal properties
113
expectTransform(
114
'var foo = {*"abc"(x) {yield x;}, *42(x) {yield x;}};',
115
'var foo = {"abc":function*(x) {yield x;}, 42:function*(x) {yield x;}};'
116
);
117
118
// Dynamic properties
119
expectTransform(
120
'var foo = {*[a+b](x) {yield x;}}',
121
'var foo = {[a+b]:function*(x) {yield x;}}'
122
);
123
});
124
125
it('should handle reserved words', function() {
126
expectTransform(
127
'({delete(x) {}})',
128
'({"delete":function(x) {}})'
129
);
130
});
131
});
132
133
134
135