-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Es6ForOfConverterTest.java
239 lines (218 loc) · 7.86 KB
/
Es6ForOfConverterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright 2014 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.truth.Truth.assertThat;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
/**
* Test cases for {@link Es6ForOfConverter}
*
* @author lharker@google.com (Laura Harker)
*/
public final class Es6ForOfConverterTest extends CompilerTestCase {
private static final String EXTERNS_BASE =
lines(
MINIMAL_EXTERNS,
"/** @constructor @template T */",
"function Arguments() {}",
"/**",
" * @param {string|!Iterable<T>|!Iterator<T>|!Arguments<T>} iterable",
" * @return {!Iterator<T>}",
" * @template T",
" */",
"$jscomp.makeIterator = function(iterable) {};",
"var console;",
"console.log = function(s) {}");
public Es6ForOfConverterTest() {
super(EXTERNS_BASE);
}
@Override
protected void setUp() throws Exception {
super.setUp();
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
setLanguageOut(LanguageMode.ECMASCRIPT3);
enableTypeCheck();
enableTypeInfoValidation();
}
@Override
protected CompilerPass getProcessor(final Compiler compiler) {
return new Es6ForOfConverter(compiler);
}
@Override
protected int getNumRepetitions() {
return 1;
}
public void testForOf() {
// With array literal and declaring new bound variable.
test(
"for (var i of [1,2,3]) { console.log(i); }",
lines(
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" var i = $jscomp$key$i.value;",
" {",
" console.log(i);",
" }",
"}"));
assertThat(getLastCompiler().injected).containsExactly("es6/util/makeiterator");
// With simple assign instead of var declaration in bound variable.
test(
"for (i of [1,2,3]) { console.log(i); }",
lines(
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" i = $jscomp$key$i.value;",
" {",
" console.log(i);",
" }",
"}"));
// With name instead of array literal.
test(
"for (var i of arr) { console.log(i); }",
lines(
"for (var $jscomp$iter$0 = $jscomp.makeIterator(arr),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" var i = $jscomp$key$i.value;",
" {",
" console.log(i);",
" }",
"}"));
// With empty loop body.
test(
"for (var i of [1,2,3]);",
lines(
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" var i = $jscomp$key$i.value;",
" {}",
"}"));
// With no block in for loop body.
test(
"for (var i of [1,2,3]) console.log(i);",
lines(
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" var i = $jscomp$key$i.value;",
" {",
" console.log(i);",
" }",
"}"));
// Iteration var shadows an outer var ()
test(
"var i = 'outer'; for (let i of [1, 2, 3]) { alert(i); } alert(i);",
lines(
"var i = 'outer';",
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$i = $jscomp$iter$0.next();",
" !$jscomp$key$i.done; $jscomp$key$i = $jscomp$iter$0.next()) {",
" let i = $jscomp$key$i.value;",
" {",
" alert(i);",
" }",
"}",
"alert(i);"));
}
public void testForOfRedeclaredVar() {
test(
lines("for (let x of []) {", " let x = 0;", "}"),
lines(
"for(var $jscomp$iter$0=$jscomp.makeIterator([]),",
" $jscomp$key$x=$jscomp$iter$0.next();",
" !$jscomp$key$x.done; $jscomp$key$x=$jscomp$iter$0.next()) {",
" let x = $jscomp$key$x.value;",
" {",
" let x = 0;",
" }",
"}"));
}
public void testForOfJSDoc() {
test(
"for (/** @type {string} */ let x of []) {}",
lines(
"for(var $jscomp$iter$0=$jscomp.makeIterator([]),",
" $jscomp$key$x=$jscomp$iter$0.next();",
" !$jscomp$key$x.done;$jscomp$key$x=$jscomp$iter$0.next()) {",
" /** @type {string} */",
" let x = $jscomp$key$x.value;",
" {}",
"}"));
test(
"for (/** @type {string} */ x of []) {}",
lines(
"for(var $jscomp$iter$0=$jscomp.makeIterator([]),",
" $jscomp$key$x=$jscomp$iter$0.next();",
" !$jscomp$key$x.done;$jscomp$key$x=$jscomp$iter$0.next()) {",
" /** @type {string} */",
" x = $jscomp$key$x.value;",
" {}",
"}"));
}
public void testForOfOnNonIterable() {
testWarning(
lines(
"var arrayLike = {",
" 0: 'x',",
" 1: 'y',",
" length: 2,",
"};",
"for (var x of arrayLike) {}"),
TypeValidator.TYPE_MISMATCH_WARNING);
}
public void testForOfWithQualifiedNameInitializer() {
// TODO(b/79532975): handle this case in the type checker and remove disableTypeCheck();
disableTypeCheck();
disableTypeInfoValidation();
test(
"var obj = {a: 0}; for (obj.a of [1,2,3]) { console.log(obj.a); }",
lines(
"var obj = {a: 0};",
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$a = $jscomp$iter$0.next();",
" !$jscomp$key$a.done; $jscomp$key$a = $jscomp$iter$0.next()) {",
" obj.a = $jscomp$key$a.value;",
" {",
" console.log(obj.a);",
" }",
"}"));
}
public void testForOfWithComplexInitializer() {
// TODO(b/79532975): handle this case in the type checker and remove disableTypeCheck();
disableTypeCheck();
disableTypeInfoValidation();
test(
"function f() { return {}; } for (f()['x' + 1] of [1,2,3]) {}",
lines(
"function f() { return {}; }",
"for (var $jscomp$iter$0 = $jscomp.makeIterator([1,2,3]),",
" $jscomp$key$a = $jscomp$iter$0.next();",
" !$jscomp$key$a.done; $jscomp$key$a = $jscomp$iter$0.next()) {",
" f()['x' + 1] = $jscomp$key$a.value;",
" {}",
"}"));
}
@Override
protected Compiler createCompiler() {
return new NoninjectingCompiler();
}
@Override
protected NoninjectingCompiler getLastCompiler() {
return (NoninjectingCompiler) super.getLastCompiler();
}
}