Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Side by Side Diff: tests/compiler/dart2js/js_parser_statements_test.dart

Issue 2345083003: dart2js: run dartfmt on tests (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 import 'package:expect/expect.dart'; 6 import 'package:expect/expect.dart';
7 import 'package:async_helper/async_helper.dart'; 7 import 'package:async_helper/async_helper.dart';
8 import 'mock_compiler.dart'; 8 import 'mock_compiler.dart';
9 import 'package:compiler/src/js/js.dart' as jsAst; 9 import 'package:compiler/src/js/js.dart' as jsAst;
10 import 'package:compiler/src/js/js.dart' show js; 10 import 'package:compiler/src/js/js.dart' show js;
11 11
12
13 Future testStatement(String statement, arguments, String expect) { 12 Future testStatement(String statement, arguments, String expect) {
14 jsAst.Node node = js.statement(statement, arguments); 13 jsAst.Node node = js.statement(statement, arguments);
15 return MockCompiler.create((MockCompiler compiler) { 14 return MockCompiler.create((MockCompiler compiler) {
16 String jsText = 15 String jsText =
17 jsAst.prettyPrint(node, compiler, allowVariableMinification: false); 16 jsAst.prettyPrint(node, compiler, allowVariableMinification: false);
18 17
19 Expect.stringEquals(expect.trim(), jsText.trim()); 18 Expect.stringEquals(expect.trim(), jsText.trim());
20 }); 19 });
21 } 20 }
22 21
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 #hole; 63 #hole;
65 }'''; 64 }''';
66 65
67 const MISC_1_1 = r''' 66 const MISC_1_1 = r'''
68 function foo() { 67 function foo() {
69 /a/; 68 /a/;
70 1; 69 1;
71 2; 70 2;
72 }'''; 71 }''';
73 72
74
75 void main() { 73 void main() {
76
77 var eOne = js('1'); 74 var eOne = js('1');
78 var eTwo = js('2'); 75 var eTwo = js('2');
79 var eTrue = js('true'); 76 var eTrue = js('true');
80 var eVar = js('x'); 77 var eVar = js('x');
81 var block12 = js.statement('{ 1; 2; }'); 78 var block12 = js.statement('{ 1; 2; }');
82 var stm = js.statement('foo();'); 79 var stm = js.statement('foo();');
83 var seq1 = js('1, 2, 3'); 80 var seq1 = js('1, 2, 3');
84 81
85 Expect.isTrue(eOne is jsAst.LiteralNumber); 82 Expect.isTrue(eOne is jsAst.LiteralNumber);
86 Expect.isTrue(eTrue is jsAst.LiteralBool); 83 Expect.isTrue(eTrue is jsAst.LiteralBool);
87 Expect.isTrue(block12 is jsAst.Block); 84 Expect.isTrue(block12 is jsAst.Block);
88 85
89 asyncTest(() => Future.wait([ 86 asyncTest(() => Future.wait([
90 // Interpolated Expressions are upgraded to ExpressionStatements. 87 // Interpolated Expressions are upgraded to ExpressionStatements.
91 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'), 88 testStatement('{ #; #; }', [eOne, eOne], '{\n 1;\n 1;\n}'),
92 testStatement('{ #a; #b; }', {'a': eOne, 'b': eOne}, '{\n 1;\n 1;\n}'), 89 testStatement(
90 '{ #a; #b; }', {'a': eOne, 'b': eOne}, '{\n 1;\n 1;\n}'),
93 91
94 // Interpolated sub-blocks are spliced. 92 // Interpolated sub-blocks are spliced.
95 testStatement('{ #; #; }', [block12, block12], 93 testStatement(
96 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), 94 '{ #; #; }', [block12, block12], '{\n 1;\n 2;\n 1;\n 2;\n}\n'),
97 testStatement('{ #a; #b; }', {'a': block12, 'b': block12}, 95 testStatement('{ #a; #b; }', {'a': block12, 'b': block12},
98 '{\n 1;\n 2;\n 1;\n 2;\n}\n'), 96 '{\n 1;\n 2;\n 1;\n 2;\n}\n'),
99 97
100 // If-condition. Dart booleans are evaluated, JS Expression booleans are 98 // If-condition. Dart booleans are evaluated, JS Expression booleans ar e
101 // substituted. 99 // substituted.
102 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'), 100 testStatement('if (#) #', [eOne, block12], 'if (1) {\n 1;\n 2;\n}'),
103 testStatement('if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'), 101 testStatement(
104 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'), 102 'if (#) #;', [eTrue, block12], 'if (true) {\n 1;\n 2;\n}'),
105 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'), 103 testStatement('if (#) #;', [eVar, block12], 'if (x) {\n 1;\n 2;\n}'),
106 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'), 104 testStatement('if (#) #;', ['a', block12], 'if (a) {\n 1;\n 2;\n}'),
107 testStatement('if (#) #;', [false, block12], ';'), 105 testStatement('if (#) #;', [true, block12], '{\n 1;\n 2;\n}'),
108 testStatement('if (#) 3; else #;', [true, block12], '3;'), 106 testStatement('if (#) #;', [false, block12], ';'),
109 testStatement('if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'), 107 testStatement('if (#) 3; else #;', [true, block12], '3;'),
110 testStatement('if (#a) #b', 108 testStatement(
111 {'a': eOne, 'b': block12}, 109 'if (#) 3; else #;', [false, block12], '{\n 1;\n 2;\n}'),
112 'if (1) {\n 1;\n 2;\n}'), 110 testStatement(
113 testStatement('if (#a) #b;', 111 'if (#a) #b', {'a': eOne, 'b': block12}, 'if (1) {\n 1;\n 2;\n}'),
114 {'a': eTrue, 'b': block12}, 112 testStatement('if (#a) #b;', {'a': eTrue, 'b': block12},
115 'if (true) {\n 1;\n 2;\n}'), 113 'if (true) {\n 1;\n 2;\n}'),
116 testStatement('if (#a) #b;', 114 testStatement('if (#a) #b;', {'a': eVar, 'b': block12},
117 {'a': eVar, 'b': block12}, 115 'if (x) {\n 1;\n 2;\n}'),
118 'if (x) {\n 1;\n 2;\n}'), 116 testStatement(
119 testStatement('if (#a) #b;', 117 'if (#a) #b;', {'a': 'a', 'b': block12}, 'if (a) {\n 1;\n 2;\n}'),
120 {'a': 'a', 'b': block12}, 118 testStatement(
121 'if (a) {\n 1;\n 2;\n}'), 119 'if (#a) #b;', {'a': true, 'b': block12}, '{\n 1;\n 2;\n}'),
122 testStatement('if (#a) #b;', 120 testStatement('if (#a) #b;', {'a': false, 'b': block12}, ';'),
123 {'a': true, 'b': block12}, 121 testStatement('if (#a) 3; else #b;', {'a': true, 'b': block12}, '3;'),
124 '{\n 1;\n 2;\n}'), 122 testStatement('if (#a) 3; else #b;', {'a': false, 'b': block12},
125 testStatement('if (#a) #b;', 123 '{\n 1;\n 2;\n}'),
126 {'a': false, 'b': block12},
127 ';'),
128 testStatement('if (#a) 3; else #b;',
129 {'a': true, 'b': block12},
130 '3;'),
131 testStatement('if (#a) 3; else #b;',
132 {'a': false, 'b': block12},
133 '{\n 1;\n 2;\n}'),
134 124
125 testStatement(
126 'while (#) #', [eOne, block12], 'while (1) {\n 1;\n 2;\n}'),
127 testStatement(
128 'while (#) #;', [eTrue, block12], 'while (true) {\n 1;\n 2;\n}'),
129 testStatement(
130 'while (#) #;', [eVar, block12], 'while (x) {\n 1;\n 2;\n}'),
131 testStatement(
132 'while (#) #;', ['a', block12], 'while (a) {\n 1;\n 2;\n}'),
133 testStatement('while (#) #;', ['a', stm], 'while (a)\n foo();'),
135 134
136 testStatement('while (#) #', [eOne, block12], 'while (1) {\n 1;\n 2;\n}'), 135 testStatement(
137 testStatement('while (#) #;', [eTrue, block12], 136 'do { {print(1);} do while(true); while (false) } while ( true )',
138 'while (true) {\n 1;\n 2;\n}'), 137 [],
139 testStatement('while (#) #;', [eVar, block12], 138 '''
140 'while (x) {\n 1;\n 2;\n}'),
141 testStatement('while (#) #;', ['a', block12],
142 'while (a) {\n 1;\n 2;\n}'),
143 testStatement('while (#) #;', ['a', stm],
144 'while (a)\n foo();'),
145
146 testStatement(
147 'do { {print(1);} do while(true); while (false) } while ( true )', [],
148 '''
149 do { 139 do {
150 print(1); 140 print(1);
151 do 141 do
152 while (true) 142 while (true)
153 ; 143 ;
154 while (false); 144 while (false);
155 } while (true); 145 } while (true);
156 '''), 146 '''),
157 testStatement('do #; while ( # )', [block12, eOne], 147 testStatement('do #; while ( # )', [block12, eOne],
158 'do {\n 1;\n 2;\n} while (1); '), 148 'do {\n 1;\n 2;\n} while (1); '),
159 testStatement('do #; while ( # )', [block12, eTrue], 149 testStatement('do #; while ( # )', [block12, eTrue],
160 'do {\n 1;\n 2;\n} while (true); '), 150 'do {\n 1;\n 2;\n} while (true); '),
161 testStatement('do #; while ( # );', [block12, eVar], 151 testStatement('do #; while ( # );', [block12, eVar],
162 'do {\n 1;\n 2;\n} while (x);'), 152 'do {\n 1;\n 2;\n} while (x);'),
163 testStatement('do { # } while ( # )', [block12, 'a'], 153 testStatement('do { # } while ( # )', [block12, 'a'],
164 'do {\n 1;\n 2;\n} while (a);'), 154 'do {\n 1;\n 2;\n} while (a);'),
165 testStatement('do #; while ( # )', [stm, 'a'], 155 testStatement(
166 'do\n foo();\nwhile (a);'), 156 'do #; while ( # )', [stm, 'a'], 'do\n foo();\nwhile (a);'),
167 157
168 testStatement('switch (#) {}', [eOne], 'switch (1) {\n}'), 158 testStatement('switch (#) {}', [eOne], 'switch (1) {\n}'),
169 testStatement(''' 159 testStatement(
160 '''
170 switch (#) { 161 switch (#) {
171 case #: { # } 162 case #: { # }
172 }''', [eTrue, eOne, block12], 163 }''',
173 'switch (true) {\n case 1:\n 1;\n 2;\n}'), 164 [eTrue, eOne, block12],
174 testStatement(''' 165 'switch (true) {\n case 1:\n 1;\n 2;\n}'),
166 testStatement(
167 '''
175 switch (#) { 168 switch (#) {
176 case #: { # } 169 case #: { # }
177 break; 170 break;
178 case #: { # } 171 case #: { # }
179 default: { # } 172 default: { # }
180 }''', [eTrue, eOne, block12, eTwo, block12, stm], ''' 173 }''',
174 [eTrue, eOne, block12, eTwo, block12, stm],
175 '''
181 switch (true) { 176 switch (true) {
182 case 1: 177 case 1:
183 1; 178 1;
184 2; 179 2;
185 break; 180 break;
186 case 2: 181 case 2:
187 1; 182 1;
188 2; 183 2;
189 default: 184 default:
190 foo(); 185 foo();
191 }'''), 186 }'''),
192 187
193 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE), 188 testStatement(NAMED_FUNCTION_1, [eOne], NAMED_FUNCTION_1_ONE),
194 testStatement(NAMED_FUNCTION_1_NAMED_HOLE, 189 testStatement(
195 {'hole': eOne}, 190 NAMED_FUNCTION_1_NAMED_HOLE, {'hole': eOne}, NAMED_FUNCTION_1_ONE),
196 NAMED_FUNCTION_1_ONE), 191
197 192 testStatement(MISC_1, [block12], MISC_1_1),
198 testStatement(MISC_1, [block12], MISC_1_1), 193 testStatement(MISC_1_NAMED_HOLE, {'hole': block12}, MISC_1_1),
199 testStatement(MISC_1_NAMED_HOLE, {'hole': block12}, MISC_1_1), 194
200 195 // Argument list splicing.
201 // Argument list splicing. 196 testStatement('foo(#)', [[]], 'foo();'),
202 testStatement('foo(#)', [[]], 'foo();'), 197 testStatement(
203 testStatement('foo(#)', [[eOne]], 'foo(1);'), 198 'foo(#)',
204 testStatement('foo(#)', [eOne], 'foo(1);'), 199 [
205 testStatement('foo(#)', [[eTrue,eOne]], 'foo(true, 1);'), 200 [eOne]
Siggi Cherem (dart-lang) 2016/09/16 20:55:22 this and a few other changes in this file are quit
Bob Nystrom 2016/09/16 21:16:03 dartfmt has a rule that an outer collection always
Harry Terkelsen 2016/09/16 21:44:03 Acknowledged.
206 testStatement('foo(#a)', {'a': []}, 'foo();'), 201 ],
207 testStatement('foo(#a)', {'a': [eOne]}, 'foo(1);'), 202 'foo(1);'),
208 testStatement('foo(#a)', {'a': eOne}, 'foo(1);'), 203 testStatement('foo(#)', [eOne], 'foo(1);'),
209 testStatement('foo(#a)', {'a': [eTrue,eOne]}, 'foo(true, 1);'), 204 testStatement(
210 205 'foo(#)',
211 testStatement('foo(2,#)', [[]], 'foo(2);'), 206 [
212 testStatement('foo(2,#)', [[eOne]], 'foo(2, 1);'), 207 [eTrue, eOne]
213 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'), 208 ],
214 testStatement('foo(2,#)', [[eTrue,eOne]], 'foo(2, true, 1);'), 209 'foo(true, 1);'),
215 testStatement('foo(2,#a)', {'a': []}, 'foo(2);'), 210 testStatement('foo(#a)', {'a': []}, 'foo();'),
216 testStatement('foo(2,#a)', {'a': [eOne]}, 'foo(2, 1);'), 211 testStatement(
217 testStatement('foo(2,#a)', {'a': eOne}, 'foo(2, 1);'), 212 'foo(#a)',
218 testStatement('foo(2,#a)', {'a': [eTrue,eOne]}, 'foo(2, true, 1);'), 213 {
219 214 'a': [eOne]
220 testStatement('foo(#,3)', [[]], 'foo(3);'), 215 },
221 testStatement('foo(#,3)', [[eOne]], 'foo(1, 3);'), 216 'foo(1);'),
222 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'), 217 testStatement('foo(#a)', {'a': eOne}, 'foo(1);'),
223 testStatement('foo(#,3)', [[eTrue,eOne]], 'foo(true, 1, 3);'), 218 testStatement(
224 testStatement('foo(#a,3)', {'a': []}, 'foo(3);'), 219 'foo(#a)',
225 testStatement('foo(#a,3)', {'a': [eOne]}, 'foo(1, 3);'), 220 {
226 testStatement('foo(#a,3)', {'a': eOne}, 'foo(1, 3);'), 221 'a': [eTrue, eOne]
227 testStatement('foo(#a,3)', {'a': [eTrue,eOne]}, 'foo(true, 1, 3);'), 222 },
228 223 'foo(true, 1);'),
229 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'), 224
230 testStatement('foo(2,#,3)', [[eOne]], 'foo(2, 1, 3);'), 225 testStatement('foo(2,#)', [[]], 'foo(2);'),
231 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'), 226 testStatement(
232 testStatement('foo(2,#,3)', [[eTrue,eOne]], 'foo(2, true, 1, 3);'), 227 'foo(2,#)',
233 testStatement('foo(2,#a,3)', {'a': []}, 'foo(2, 3);'), 228 [
234 testStatement('foo(2,#a,3)', {'a': [eOne]}, 'foo(2, 1, 3);'), 229 [eOne]
235 testStatement('foo(2,#a,3)', {'a': eOne}, 'foo(2, 1, 3);'), 230 ],
236 testStatement('foo(2,#a,3)', {'a': [eTrue,eOne]}, 'foo(2, true, 1, 3);'), 231 'foo(2, 1);'),
237 232 testStatement('foo(2,#)', [eOne], 'foo(2, 1);'),
238 // Interpolated Literals 233 testStatement(
239 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'), 234 'foo(2,#)',
240 testStatement('a = {#a: 1}', {'a': eOne}, 'a = {1: 1};'), 235 [
241 // Maybe we should make this work? 236 [eTrue, eOne]
242 testError('a = {#: 1}', [1], 'is not a Literal: 1'), 237 ],
243 testError('a = {#a: 1}', {'a': 1}, 'is not a Literal: 1'), 238 'foo(2, true, 1);'),
244 239 testStatement('foo(2,#a)', {'a': []}, 'foo(2);'),
245 // Interpolated parameter splicing. 240 testStatement(
246 testStatement('function foo(#){}', [new jsAst.Parameter('x')], 241 'foo(2,#a)',
247 'function foo(x) {\n}'), 242 {
248 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'), 243 'a': [eOne]
249 testStatement('function foo(#){}', [[]], 'function foo() {\n}'), 244 },
250 testStatement('function foo(#){}', [['x']], 'function foo(x) {\n}'), 245 'foo(2, 1);'),
251 testStatement('function foo(#){}', [['x', 'y']], 'function foo(x, y) {\n}'), 246 testStatement('foo(2,#a)', {'a': eOne}, 'foo(2, 1);'),
252 testStatement('function foo(#a){}', {'a': new jsAst.Parameter('x')}, 247 testStatement(
253 'function foo(x) {\n}'), 248 'foo(2,#a)',
254 testStatement('function foo(#a){}', {'a': 'x'}, 'function foo(x) {\n}'), 249 {
255 testStatement('function foo(#a){}', {'a': []}, 'function foo() {\n}'), 250 'a': [eTrue, eOne]
256 testStatement('function foo(#a){}', {'a': ['x']}, 'function foo(x) {\n}'), 251 },
257 testStatement('function foo(#a){}', 252 'foo(2, true, 1);'),
258 {'a': ['x', 'y']}, 253
259 'function foo(x, y) {\n}'), 254 testStatement('foo(#,3)', [[]], 'foo(3);'),
260 255 testStatement(
261 testStatement('function foo() async {}', [], 'function foo() async {\n}'), 256 'foo(#,3)',
262 testStatement('function foo() sync* {}', [], 'function foo() sync* {\n}'), 257 [
263 testStatement('function foo() async* {}', [], 'function foo() async* {\n}'), 258 [eOne]
264 259 ],
265 testStatement('a = #.#', [eVar,eOne], 'a = x[1];'), 260 'foo(1, 3);'),
266 testStatement('a = #.#', [eVar,'foo'], 'a = x.foo;'), 261 testStatement('foo(#,3)', [eOne], 'foo(1, 3);'),
267 testStatement('a = #a.#b', {'a': eVar, 'b': eOne}, 'a = x[1];'), 262 testStatement(
268 testStatement('a = #a.#b', {'a': eVar, 'b': 'foo'}, 'a = x.foo;'), 263 'foo(#,3)',
269 264 [
270 testStatement('function f(#) { return #.#; }', ['x', eVar,'foo'], 265 [eTrue, eOne]
271 'function f(x) {\n return x.foo;\n}'), 266 ],
272 testStatement('function f(#a) { return #b.#c; }', 267 'foo(true, 1, 3);'),
273 {'a': 'x', 'b': eVar, 'c': 'foo'}, 268 testStatement('foo(#a,3)', {'a': []}, 'foo(3);'),
274 'function f(x) {\n return x.foo;\n}'), 269 testStatement(
275 270 'foo(#a,3)',
276 testStatement('#.prototype.# = function(#) { return #.# };', 271 {
277 ['className', 'getterName', ['r', 'y'], 'r', 'fieldName'], 272 'a': [eOne]
278 'className.prototype.getterName = function(r, y) {\n' 273 },
279 ' return r.fieldName;\n' 274 'foo(1, 3);'),
280 '};'), 275 testStatement('foo(#a,3)', {'a': eOne}, 'foo(1, 3);'),
281 testStatement('#a.prototype.#b = function(#c) { return #d.#e };', 276 testStatement(
282 {'a': 'className', 277 'foo(#a,3)',
283 'b': 'getterName', 278 {
284 'c': ['r', 'y'], 279 'a': [eTrue, eOne]
285 'd': 'r', 280 },
286 'e': 'fieldName'}, 281 'foo(true, 1, 3);'),
287 'className.prototype.getterName = function(r, y) {\n' 282
288 ' return r.fieldName;\n' 283 testStatement('foo(2,#,3)', [[]], 'foo(2, 3);'),
289 '};'), 284 testStatement(
290 285 'foo(2,#,3)',
291 testStatement('function foo(r, #) { return #[r](#) }', 286 [
292 [['a', 'b'], 'g', ['b', 'a']], 287 [eOne]
293 'function foo(r, a, b) {\n return g[r](b, a);\n}'), 288 ],
294 testStatement('function foo(r, #a) { return #b[r](#c) }', 289 'foo(2, 1, 3);'),
295 {'a': ['a', 'b'], 'b': 'g', 'c': ['b', 'a']}, 290 testStatement('foo(2,#,3)', [eOne], 'foo(2, 1, 3);'),
296 'function foo(r, a, b) {\n return g[r](b, a);\n}'), 291 testStatement(
297 292 'foo(2,#,3)',
298 // Sequence is printed flattened 293 [
299 testStatement('x = #', [seq1], 'x = (1, 2, 3);'), 294 [eTrue, eOne]
300 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'), 295 ],
301 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'), 296 'foo(2, true, 1, 3);'),
302 testStatement( 297 testStatement('foo(2,#a,3)', {'a': []}, 'foo(2, 3);'),
303 'for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1], 298 testStatement(
304 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), 299 'foo(2,#a,3)',
305 testStatement('x = #a', {'a': seq1}, 'x = (1, 2, 3);'), 300 {
306 testStatement('x = (#a, #b)', 301 'a': [eOne]
307 {'a': seq1, 'b': seq1}, 302 },
308 'x = (1, 2, 3, 1, 2, 3);'), 303 'foo(2, 1, 3);'),
309 testStatement('x = #a, #b', 304 testStatement('foo(2,#a,3)', {'a': eOne}, 'foo(2, 1, 3);'),
310 {'a': seq1, 'b': seq1}, 305 testStatement(
311 'x = (1, 2, 3), 1, 2, 3;'), 306 'foo(2,#a,3)',
312 testStatement( 307 {
313 'for (i = 0, j = #a, k = 0; ; ++i, ++j, ++k){}', {'a': seq1}, 308 'a': [eTrue, eOne]
314 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'), 309 },
315 310 'foo(2, true, 1, 3);'),
316 // Use the same name several times. 311
317 testStatement('#a.prototype.#a = function(#b) { return #c.#c };', 312 // Interpolated Literals
318 {'a': 'name1_2', 313 testStatement('a = {#: 1}', [eOne], 'a = {1: 1};'),
319 'b': ['r', 'y'], 314 testStatement('a = {#a: 1}', {'a': eOne}, 'a = {1: 1};'),
320 'c': 'name4_5'}, 315 // Maybe we should make this work?
321 'name1_2.prototype.name1_2 = function(r, y) {\n' 316 testError('a = {#: 1}', [1], 'is not a Literal: 1'),
322 ' return name4_5.name4_5;\n' 317 testError('a = {#a: 1}', {'a': 1}, 'is not a Literal: 1'),
323 '};'), 318
324 319 // Interpolated parameter splicing.
325 testStatement('label: while (a) { label2: break label;}', [], 320 testStatement('function foo(#){}', [new jsAst.Parameter('x')],
326 'label:\n while (a)\n label2:\n break label;\n '), 321 'function foo(x) {\n}'),
327 322 testStatement('function foo(#){}', ['x'], 'function foo(x) {\n}'),
328 323 testStatement('function foo(#){}', [[]], 'function foo() {\n}'),
329 testStatement('var # = 3', ['x'], 'var x = 3;'), 324 testStatement(
330 testStatement('var # = 3', 325 'function foo(#){}',
331 [new jsAst.VariableDeclaration('x')], 326 [
332 'var x = 3;'), 327 ['x']
333 testStatement('var # = 3, # = #', 328 ],
334 ['x', 'y', js.number(2)], 329 'function foo(x) {\n}'),
335 'var x = 3, y = 2;'), 330 testStatement(
336 testStatement('var #a = 3, #b = #c', 331 'function foo(#){}',
337 {"a": 'x', "b": 'y', "c": js.number(2)}, 332 [
338 'var x = 3, y = 2;'), 333 ['x', 'y']
339 testStatement('function #() {}', ['x'], 'function x() {\n}'), 334 ],
340 testStatement('function #() {}', 335 'function foo(x, y) {\n}'),
341 [new jsAst.VariableDeclaration('x')], 336 testStatement('function foo(#a){}', {'a': new jsAst.Parameter('x')},
342 'function x() {\n}'), 337 'function foo(x) {\n}'),
343 testStatement('try {} catch (#) {}', ['x'], 'try {\n} catch (x) {\n}'), 338 testStatement('function foo(#a){}', {'a': 'x'}, 'function foo(x) {\n}'),
344 testStatement('try {} catch (#a) {}', {"a": 'x'}, 'try {\n} catch (x) {\n}') , 339 testStatement('function foo(#a){}', {'a': []}, 'function foo() {\n}'),
345 testStatement('try {} catch (#a) {}', 340 testStatement(
346 {"a": new jsAst.VariableDeclaration('x')}, 341 'function foo(#a){}',
347 'try {\n} catch (x) {\n}'), 342 {
348 343 'a': ['x']
349 // Test that braces around a single-statement block are removed by printer. 344 },
350 testStatement('while (a) {foo()}', [], 345 'function foo(x) {\n}'),
351 'while (a)\n foo();'), 346 testStatement(
352 testStatement('if (a) {foo();}', [], 347 'function foo(#a){}',
353 'if (a)\n foo();'), 348 {
354 testStatement('if (a) {foo();} else {foo2();}', [], 349 'a': ['x', 'y']
355 'if (a)\n foo();\nelse\n foo2();'), 350 },
356 testStatement('if (a) foo(); else {foo2();}', [], 351 'function foo(x, y) {\n}'),
357 'if (a)\n foo();\nelse\n foo2();'), 352
358 testStatement('do {foo();} while(a);', [], 353 testStatement(
359 'do\n foo();\nwhile (a);'), 354 'function foo() async {}', [], 'function foo() async {\n}'),
360 testStatement('label: {foo();}', [], 355 testStatement(
361 'label:\n foo();'), 356 'function foo() sync* {}', [], 'function foo() sync* {\n}'),
362 testStatement('for (var key in a) {foo();}', [], 357 testStatement(
363 'for (var key in a)\n foo();'), 358 'function foo() async* {}', [], 'function foo() async* {\n}'),
364 // `label: break label;` gives problems on IE. Test that it is avoided. 359
365 testStatement('label: {break label;}', [], 360 testStatement('a = #.#', [eVar, eOne], 'a = x[1];'),
366 ';'), 361 testStatement('a = #.#', [eVar, 'foo'], 'a = x.foo;'),
367 // This works on IE: 362 testStatement('a = #a.#b', {'a': eVar, 'b': eOne}, 'a = x[1];'),
368 testStatement('label: {label2: {break label;}}', [], 363 testStatement('a = #a.#b', {'a': eVar, 'b': 'foo'}, 'a = x.foo;'),
369 'label:\n label2:\n break label;\n'), 364
370 // Test dangling else: 365 testStatement('function f(#) { return #.#; }', ['x', eVar, 'foo'],
371 testStatement('if (a) {if (b) {foo1();}} else {foo2();}', [], """ 366 'function f(x) {\n return x.foo;\n}'),
367 testStatement(
368 'function f(#a) { return #b.#c; }',
369 {'a': 'x', 'b': eVar, 'c': 'foo'},
370 'function f(x) {\n return x.foo;\n}'),
371
372 testStatement(
373 '#.prototype.# = function(#) { return #.# };',
374 [
375 'className',
376 'getterName',
377 ['r', 'y'],
378 'r',
379 'fieldName'
380 ],
381 'className.prototype.getterName = function(r, y) {\n'
382 ' return r.fieldName;\n'
383 '};'),
384 testStatement(
385 '#a.prototype.#b = function(#c) { return #d.#e };',
386 {
387 'a': 'className',
388 'b': 'getterName',
389 'c': ['r', 'y'],
390 'd': 'r',
391 'e': 'fieldName'
392 },
393 'className.prototype.getterName = function(r, y) {\n'
394 ' return r.fieldName;\n'
395 '};'),
396
397 testStatement(
398 'function foo(r, #) { return #[r](#) }',
399 [
400 ['a', 'b'],
401 'g',
402 ['b', 'a']
403 ],
404 'function foo(r, a, b) {\n return g[r](b, a);\n}'),
405 testStatement(
406 'function foo(r, #a) { return #b[r](#c) }',
407 {
408 'a': ['a', 'b'],
409 'b': 'g',
410 'c': ['b', 'a']
411 },
412 'function foo(r, a, b) {\n return g[r](b, a);\n}'),
413
414 // Sequence is printed flattened
415 testStatement('x = #', [seq1], 'x = (1, 2, 3);'),
416 testStatement('x = (#, #)', [seq1, seq1], 'x = (1, 2, 3, 1, 2, 3);'),
417 testStatement('x = #, #', [seq1, seq1], 'x = (1, 2, 3), 1, 2, 3;'),
418 testStatement('for (i = 0, j = #, k = 0; ; ++i, ++j, ++k){}', [seq1],
419 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'),
420 testStatement('x = #a', {'a': seq1}, 'x = (1, 2, 3);'),
421 testStatement(
422 'x = (#a, #b)', {'a': seq1, 'b': seq1}, 'x = (1, 2, 3, 1, 2, 3);'),
423 testStatement(
424 'x = #a, #b', {'a': seq1, 'b': seq1}, 'x = (1, 2, 3), 1, 2, 3;'),
425 testStatement(
426 'for (i = 0, j = #a, k = 0; ; ++i, ++j, ++k){}',
427 {'a': seq1},
428 'for (i = 0, j = (1, 2, 3), k = 0;; ++i, ++j, ++k) {\n}'),
429
430 // Use the same name several times.
431 testStatement(
432 '#a.prototype.#a = function(#b) { return #c.#c };',
433 {
434 'a': 'name1_2',
435 'b': ['r', 'y'],
436 'c': 'name4_5'
437 },
438 'name1_2.prototype.name1_2 = function(r, y) {\n'
439 ' return name4_5.name4_5;\n'
440 '};'),
441
442 testStatement('label: while (a) { label2: break label;}', [],
443 'label:\n while (a)\n label2:\n break label;\n '),
444
445 testStatement('var # = 3', ['x'], 'var x = 3;'),
446 testStatement(
447 'var # = 3', [new jsAst.VariableDeclaration('x')], 'var x = 3;'),
448 testStatement(
449 'var # = 3, # = #', ['x', 'y', js.number(2)], 'var x = 3, y = 2;'),
450 testStatement('var #a = 3, #b = #c',
451 {"a": 'x', "b": 'y', "c": js.number(2)}, 'var x = 3, y = 2;'),
452 testStatement('function #() {}', ['x'], 'function x() {\n}'),
453 testStatement('function #() {}', [new jsAst.VariableDeclaration('x')],
454 'function x() {\n}'),
455 testStatement('try {} catch (#) {}', ['x'], 'try {\n} catch (x) {\n}'),
456 testStatement(
457 'try {} catch (#a) {}', {"a": 'x'}, 'try {\n} catch (x) {\n}'),
458 testStatement(
459 'try {} catch (#a) {}',
460 {"a": new jsAst.VariableDeclaration('x')},
461 'try {\n} catch (x) {\n}'),
462
463 // Test that braces around a single-statement block are removed by print er.
464 testStatement('while (a) {foo()}', [], 'while (a)\n foo();'),
465 testStatement('if (a) {foo();}', [], 'if (a)\n foo();'),
466 testStatement('if (a) {foo();} else {foo2();}', [],
467 'if (a)\n foo();\nelse\n foo2();'),
468 testStatement('if (a) foo(); else {foo2();}', [],
469 'if (a)\n foo();\nelse\n foo2();'),
470 testStatement('do {foo();} while(a);', [], 'do\n foo();\nwhile (a);'),
471 testStatement('label: {foo();}', [], 'label:\n foo();'),
472 testStatement(
473 'for (var key in a) {foo();}', [], 'for (var key in a)\n foo();'),
474 // `label: break label;` gives problems on IE. Test that it is avoided.
475 testStatement('label: {break label;}', [], ';'),
476 // This works on IE:
477 testStatement('label: {label2: {break label;}}', [],
478 'label:\n label2:\n break label;\n'),
479 // Test dangling else:
480 testStatement(
481 'if (a) {if (b) {foo1();}} else {foo2();}',
482 [],
483 """
372 if (a) { 484 if (a) {
373 if (b) 485 if (b)
374 foo1(); 486 foo1();
375 } else 487 } else
376 foo2();"""), 488 foo2();"""),
377 testStatement('if (a) {if (b) {foo1();} else {foo2();}}', [], """ 489 testStatement(
490 'if (a) {if (b) {foo1();} else {foo2();}}',
491 [],
492 """
378 if (a) 493 if (a)
379 if (b) 494 if (b)
380 foo1(); 495 foo1();
381 else 496 else
382 foo2(); 497 foo2();
383 """), 498 """),
384 testStatement('if (a) {if (b) {foo1();} else {foo2();}} else {foo3();}', 499 testStatement(
385 [], """ 500 'if (a) {if (b) {foo1();} else {foo2();}} else {foo3();}',
501 [],
502 """
386 if (a) 503 if (a)
387 if (b) 504 if (b)
388 foo1(); 505 foo1();
389 else 506 else
390 foo2(); 507 foo2();
391 else 508 else
392 foo3();"""), 509 foo3();"""),
393 testStatement('if (a) {while (true) if (b) {foo1();}} else {foo2();}', 510 testStatement(
394 [], """ 511 'if (a) {while (true) if (b) {foo1();}} else {foo2();}',
512 [],
513 """
395 if (a) { 514 if (a) {
396 while (true) 515 while (true)
397 if (b) 516 if (b)
398 foo1(); 517 foo1();
399 } else 518 } else
400 foo2();"""), 519 foo2();"""),
401 ])); 520 ]));
402 } 521 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698