-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PeepholeSubstituteAlternateSyntaxTest.java
560 lines (474 loc) · 19.1 KB
/
PeepholeSubstituteAlternateSyntaxTest.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright 2004 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 com.google.javascript.jscomp.CompilerOptions.LanguageMode;
/**
* Tests for {@link PeepholeSubstituteAlternateSyntax} in isolation.
* Tests for the interaction of multiple peephole passes are in
* PeepholeIntegrationTest.
*/
public final class PeepholeSubstituteAlternateSyntaxTest extends CompilerTestCase {
// Externs for built-in constructors
// Needed for testFoldLiteralObjectConstructors(),
// testFoldLiteralArrayConstructors() and testFoldRegExp...()
private static final String FOLD_CONSTANTS_TEST_EXTERNS =
"var window = {};\n" +
"var Object = function f(){};\n" +
"var RegExp = function f(a){};\n" +
"var Array = function f(a){};\n" +
"window.foo = null;\n";
private boolean late = true;
public PeepholeSubstituteAlternateSyntaxTest() {
super(FOLD_CONSTANTS_TEST_EXTERNS);
}
@Override
public void setUp() throws Exception {
super.setUp();
late = true;
disableNormalize();
}
@Override
public CompilerPass getProcessor(final Compiler compiler) {
PeepholeOptimizationsPass peepholePass = new PeepholeOptimizationsPass(
compiler, new PeepholeSubstituteAlternateSyntax(late));
peepholePass.setRetraverseOnChange(false);
return peepholePass;
}
@Override
protected int getNumRepetitions() {
return 1;
}
private void foldSame(String js) {
testSame(js);
}
private void fold(String js, String expected) {
test(js, expected);
}
public void testFoldRegExpConstructor() {
enableNormalize();
// Cannot fold all the way to a literal because there are too few arguments.
fold("x = new RegExp", "x = RegExp()");
// Empty regexp should not fold to // since that is a line comment in JS
fold("x = new RegExp(\"\")", "x = RegExp(\"\")");
fold("x = new RegExp(\"\", \"i\")", "x = RegExp(\"\",\"i\")");
// Bogus flags should not fold
testSame("x = RegExp(\"foobar\", \"bogus\")",
PeepholeSubstituteAlternateSyntax.INVALID_REGULAR_EXPRESSION_FLAGS);
// Don't fold if the argument is not a string. See issue 1260.
foldSame("x = new RegExp(y)");
// Can Fold
fold("x = new RegExp(\"foobar\")", "x = /foobar/");
fold("x = RegExp(\"foobar\")", "x = /foobar/");
fold("x = new RegExp(\"foobar\", \"i\")", "x = /foobar/i");
// Make sure that escaping works
fold("x = new RegExp(\"\\\\.\", \"i\")", "x = /\\./i");
fold("x = new RegExp(\"/\", \"\")", "x = /\\//");
fold("x = new RegExp(\"[/]\", \"\")", "x = /[/]/");
fold("x = new RegExp(\"///\", \"\")", "x = /\\/\\/\\//");
fold("x = new RegExp(\"\\\\\\/\", \"\")", "x = /\\//");
fold("x = new RegExp(\"\\n\")", "x = /\\n/");
fold("x = new RegExp('\\\\\\r')", "x = /\\r/");
// Shouldn't fold RegExp unnormalized because
// we can't be sure that RegExp hasn't been redefined
disableNormalize();
foldSame("x = new RegExp(\"foobar\")");
}
public void testVersionSpecificRegExpQuirks() {
enableNormalize();
// Don't fold if the flags contain 'g'
setAcceptedLanguage(LanguageMode.ECMASCRIPT3);
fold("x = new RegExp(\"foobar\", \"g\")",
"x = RegExp(\"foobar\",\"g\")");
fold("x = new RegExp(\"foobar\", \"ig\")",
"x = RegExp(\"foobar\",\"ig\")");
// ... unless in ECMAScript 5 mode per section 7.8.5 of ECMAScript 5.
setAcceptedLanguage(LanguageMode.ECMASCRIPT5);
fold("x = new RegExp(\"foobar\", \"ig\")",
"x = /foobar/ig");
// Don't fold things that crash older versions of Safari and that don't work
// as regex literals on other old versions of Safari
setAcceptedLanguage(LanguageMode.ECMASCRIPT3);
fold("x = new RegExp(\"\\u2028\")", "x = RegExp(\"\\u2028\")");
fold("x = new RegExp(\"\\\\\\\\u2028\")", "x = /\\\\u2028/");
// Sunset Safari exclusions for ECMAScript 5 and later.
setAcceptedLanguage(LanguageMode.ECMASCRIPT5);
fold("x = new RegExp(\"\\u2028\\u2029\")", "x = /\\u2028\\u2029/");
fold("x = new RegExp(\"\\\\u2028\")", "x = /\\u2028/");
fold("x = new RegExp(\"\\\\\\\\u2028\")", "x = /\\\\u2028/");
}
public void testFoldRegExpConstructorStringCompare() {
enableNormalize();
test("x = new RegExp(\"\\n\", \"i\")", "x = /\\n/i");
}
public void testContainsUnicodeEscape() throws Exception {
assertFalse(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape(""));
assertFalse(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape("foo"));
assertTrue(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape(
"\u2028"));
assertTrue(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape(
"\\u2028"));
assertTrue(
PeepholeSubstituteAlternateSyntax.containsUnicodeEscape("foo\\u2028"));
assertFalse(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape(
"foo\\\\u2028"));
assertTrue(PeepholeSubstituteAlternateSyntax.containsUnicodeEscape(
"foo\\\\u2028bar\\u2028"));
}
public void testFoldLiteralObjectConstructors() {
enableNormalize();
// Can fold when normalized
fold("x = new Object", "x = ({})");
fold("x = new Object()", "x = ({})");
fold("x = Object()", "x = ({})");
disableNormalize();
// Cannot fold above when not normalized
foldSame("x = new Object");
foldSame("x = new Object()");
foldSame("x = Object()");
enableNormalize();
// Cannot fold, the constructor being used is actually a local function
foldSame("x = " +
"(function f(){function Object(){this.x=4};return new Object();})();");
}
public void testFoldLiteralObjectConstructors_onWindow() {
enableNormalize();
// Can fold when normalized
fold("x = new window.Object", "x = ({})");
fold("x = new window.Object()", "x = ({})");
fold("x = window.Object()", "x = ({})");
disableNormalize();
// Cannot fold above when not normalized
foldSame("x = new window.Object");
foldSame("x = new window.Object()");
foldSame("x = window.Object()");
enableNormalize();
// Can fold, the window namespace ensures it's not a conflict with the local Object.
fold("x = (function f(){function Object(){this.x=4};return new window.Object;})();",
"x = (function f(){function Object(){this.x=4};return {};})();");
}
public void testFoldLiteralArrayConstructors() {
enableNormalize();
// No arguments - can fold when normalized
fold("x = new Array", "x = []");
fold("x = new Array()", "x = []");
fold("x = Array()", "x = []");
// One argument - can be fold when normalized
fold("x = new Array(0)", "x = []");
fold("x = Array(0)", "x = []");
fold("x = new Array(\"a\")", "x = [\"a\"]");
fold("x = Array(\"a\")", "x = [\"a\"]");
// One argument - cannot be fold when normalized
fold("x = new Array(7)", "x = Array(7)");
fold("x = Array(7)", "x = Array(7)");
fold("x = new Array(y)", "x = Array(y)");
fold("x = Array(y)", "x = Array(y)");
fold("x = new Array(foo())", "x = Array(foo())");
fold("x = Array(foo())", "x = Array(foo())");
// More than one argument - can be fold when normalized
fold("x = new Array(1, 2, 3, 4)", "x = [1, 2, 3, 4]");
fold("x = Array(1, 2, 3, 4)", "x = [1, 2, 3, 4]");
fold("x = new Array('a', 1, 2, 'bc', 3, {}, 'abc')",
"x = ['a', 1, 2, 'bc', 3, {}, 'abc']");
fold("x = Array('a', 1, 2, 'bc', 3, {}, 'abc')",
"x = ['a', 1, 2, 'bc', 3, {}, 'abc']");
fold("x = new Array(Array(1, '2', 3, '4'))", "x = [[1, '2', 3, '4']]");
fold("x = Array(Array(1, '2', 3, '4'))", "x = [[1, '2', 3, '4']]");
fold("x = new Array(Object(), Array(\"abc\", Object(), Array(Array())))",
"x = [{}, [\"abc\", {}, [[]]]]");
fold("x = new Array(Object(), Array(\"abc\", Object(), Array(Array())))",
"x = [{}, [\"abc\", {}, [[]]]]");
disableNormalize();
// Cannot fold above when not normalized
foldSame("x = new Array");
foldSame("x = new Array()");
foldSame("x = Array()");
foldSame("x = new Array(0)");
foldSame("x = Array(0)");
foldSame("x = new Array(\"a\")");
foldSame("x = Array(\"a\")");
foldSame("x = new Array(7)");
foldSame("x = Array(7)");
foldSame("x = new Array(foo())");
foldSame("x = Array(foo())");
foldSame("x = new Array(1, 2, 3, 4)");
foldSame("x = Array(1, 2, 3, 4)");
foldSame("x = new Array('a', 1, 2, 'bc', 3, {}, 'abc')");
foldSame("x = Array('a', 1, 2, 'bc', 3, {}, 'abc')");
foldSame("x = new Array(Array(1, '2', 3, '4'))");
foldSame("x = Array(Array(1, '2', 3, '4'))");
foldSame("x = new Array(" +
"Object(), Array(\"abc\", Object(), Array(Array())))");
foldSame("x = new Array(" +
"Object(), Array(\"abc\", Object(), Array(Array())))");
}
public void testRemoveWindowRefs() {
enableNormalize();
fold("x = window.Object", "x = Object");
fold("x = window.Object.keys", "x = Object.keys");
fold("if (window.Object) {}", "if (Object) {}");
fold("x = window.Object", "x = Object");
fold("x = window.Array", "x = Array");
fold("x = window.Error", "x = Error");
fold("x = window.RegExp", "x = RegExp");
fold("x = window.Math", "x = Math");
// Not currently handled by the pass but should be folded in the future.
foldSame("x = window.String");
// Don't fold properties on the window.
foldSame("x = window.foo");
disableNormalize();
foldSame("x = window.Object");
foldSame("x = window.Object.keys");
enableNormalize();
foldSame("var x = "
+ "(function f(){var window = {Object: function() {}};return new window.Object;})();");
}
public void testFoldStandardConstructors() {
foldSame("new Foo('a')");
foldSame("var x = new goog.Foo(1)");
foldSame("var x = new String(1)");
foldSame("var x = new Number(1)");
foldSame("var x = new Boolean(1)");
enableNormalize();
fold("var x = new Object('a')", "var x = Object('a')");
fold("var x = new RegExp('')", "var x = RegExp('')");
fold("var x = new Error('20')", "var x = Error(\"20\")");
fold("var x = new Array(20)", "var x = Array(20)");
}
public void testFoldTrueFalse() {
fold("x = true", "x = !0");
fold("x = false", "x = !1");
}
public void testFoldTrueFalseComparison() {
fold("x == true", "x == 1");
fold("x == false", "x == 0");
fold("x != true", "x != 1");
fold("x < true", "x < 1");
fold("x <= true", "x <= 1");
fold("x > true", "x > 1");
fold("x >= true", "x >= 1");
}
public void testFoldSubtractionAssignment() {
fold("x -= 1", "--x");
fold("x -= -1", "++x");
}
public void testFoldReturnResult() {
foldSame("function f(){return !1;}");
foldSame("function f(){return null;}");
fold("function f(){return void 0;}",
"function f(){return}");
foldSame("function f(){return void foo();}");
fold("function f(){return undefined;}",
"function f(){return}");
fold("function f(){if(a()){return undefined;}}",
"function f(){if(a()){return}}");
}
public void testUndefined() {
foldSame("var x = undefined");
foldSame("function f(f) {var undefined=2;var x = undefined;}");
enableNormalize();
fold("var x = undefined", "var x=void 0");
foldSame(
"var undefined = 1;" +
"function f() {var undefined=2;var x = undefined;}");
foldSame("function f(undefined) {}");
foldSame("try {} catch(undefined) {}");
foldSame("for (undefined in {}) {}");
foldSame("undefined++;");
disableNormalize();
foldSame("undefined += undefined;");
enableNormalize();
fold("undefined += undefined;", "undefined = void 0 + void 0;");
}
public void testSplitCommaExpressions() {
late = false;
// Don't try to split in expressions.
foldSame("while (foo(), !0) boo()");
foldSame("var a = (foo(), !0);");
foldSame("a = (foo(), !0);");
// Don't try to split COMMA under LABELs.
foldSame("a:a(),b()");
fold("(x=2), foo()", "x=2; foo()");
fold("foo(), boo();", "foo(); boo()");
fold("(a(), b()), (c(), d());", "a(); b(); (c(), d());");
fold("a(); b(); (c(), d());", "a(); b(); c(); d();");
fold("foo(), true", "foo();true");
foldSame("foo();true");
fold("function x(){foo(), !0}", "function x(){foo(); !0}");
foldSame("function x(){foo(); !0}");
}
public void testComma1() {
late = false;
fold("1, 2", "1; 2");
late = true;
foldSame("1, 2");
}
public void testComma2() {
late = false;
test("1, a()", "1; a()");
late = true;
foldSame("1, a()");
}
public void testComma3() {
late = false;
test("1, a(), b()", "1; a(); b()");
late = true;
foldSame("1, a(), b()");
}
public void testComma4() {
late = false;
test("a(), b()", "a();b()");
late = true;
foldSame("a(), b()");
}
public void testComma5() {
late = false;
test("a(), b(), 1", "a();b();1");
late = true;
foldSame("a(), b(), 1");
}
public void testStringArraySplitting() {
testSame("var x=['1','2','3','4']");
testSame("var x=['1','2','3','4','5']");
test("var x=['1','2','3','4','5','6']",
"var x='123456'.split('')");
test("var x=['1','2','3','4','5','00']",
"var x='1 2 3 4 5 00'.split(' ')");
test("var x=['1','2','3','4','5','6','7']",
"var x='1234567'.split('')");
test("var x=['1','2','3','4','5','6','00']",
"var x='1 2 3 4 5 6 00'.split(' ')");
test("var x=[' ,',',',',',',',',',',']",
"var x=' ,;,;,;,;,;,'.split(';')");
test("var x=[',,',' ',',',',',',',',']",
"var x=',,; ;,;,;,;,'.split(';')");
test("var x=['a,',' ',',',',',',',',']",
"var x='a,; ;,;,;,;,'.split(';')");
// all possible delimiters used, leave it alone
testSame("var x=[',', ' ', ';', '{', '}']");
}
public void testBindToCall1() {
test("(goog.bind(f))()", "f()");
test("(goog.bind(f,a))()", "f.call(a)");
test("(goog.bind(f,a,b))()", "f.call(a,b)");
test("(goog.bind(f))(a)", "f(a)");
test("(goog.bind(f,a))(b)", "f.call(a,b)");
test("(goog.bind(f,a,b))(c)", "f.call(a,b,c)");
test("(goog.partial(f))()", "f()");
test("(goog.partial(f,a))()", "f(a)");
test("(goog.partial(f,a,b))()", "f(a,b)");
test("(goog.partial(f))(a)", "f(a)");
test("(goog.partial(f,a))(b)", "f(a,b)");
test("(goog.partial(f,a,b))(c)", "f(a,b,c)");
test("((function(){}).bind())()", "((function(){}))()");
test("((function(){}).bind(a))()", "((function(){})).call(a)");
test("((function(){}).bind(a,b))()", "((function(){})).call(a,b)");
test("((function(){}).bind())(a)", "((function(){}))(a)");
test("((function(){}).bind(a))(b)", "((function(){})).call(a,b)");
test("((function(){}).bind(a,b))(c)", "((function(){})).call(a,b,c)");
// Without using type information we don't know "f" is a function.
testSame("(f.bind())()");
testSame("(f.bind(a))()");
testSame("(f.bind())(a)");
testSame("(f.bind(a))(b)");
// Don't rewrite if the bind isn't the immediate call target
testSame("(goog.bind(f)).call(g)");
}
public void testBindToCall2() {
test("(goog$bind(f))()", "f()");
test("(goog$bind(f,a))()", "f.call(a)");
test("(goog$bind(f,a,b))()", "f.call(a,b)");
test("(goog$bind(f))(a)", "f(a)");
test("(goog$bind(f,a))(b)", "f.call(a,b)");
test("(goog$bind(f,a,b))(c)", "f.call(a,b,c)");
test("(goog$partial(f))()", "f()");
test("(goog$partial(f,a))()", "f(a)");
test("(goog$partial(f,a,b))()", "f(a,b)");
test("(goog$partial(f))(a)", "f(a)");
test("(goog$partial(f,a))(b)", "f(a,b)");
test("(goog$partial(f,a,b))(c)", "f(a,b,c)");
// Don't rewrite if the bind isn't the immediate call target
testSame("(goog$bind(f)).call(g)");
}
public void testBindToCall3() {
// TODO(johnlenz): The code generator wraps free calls with (0,...) to
// prevent leaking "this", but the parser doesn't unfold it, making a
// AST comparison fail. For now do a string comparison to validate the
// correct code is in fact generated.
// The FREE call wrapping should be moved out of the code generator
// and into a denormalizing pass.
new StringCompareTestCase().testBindToCall3();
}
public void testSimpleFunctionCall1() {
test("var a = String(23)", "var a = '' + 23");
test("var a = String('hello')", "var a = '' + 'hello'");
testSame("var a = String('hello', bar());");
testSame("var a = String({valueOf: function() { return 1; }});");
}
public void testSimpleFunctionCall2() {
test("var a = Boolean(true)", "var a = !0");
test("var a = Boolean(false)", "var a = !1");
test("var a = Boolean(1)", "var a = !!1");
test("var a = Boolean(x)", "var a = !!x");
test("var a = Boolean({})", "var a = !!{}");
testSame("var a = Boolean()");
testSame("var a = Boolean(!0, !1);");
}
public void testRotateAssociativeOperators() {
test("a || (b || c); a * (b * c); a | (b | c)",
"(a || b) || c; (a * b) * c; (a | b) | c");
testSame("a % (b % c); a / (b / c); a - (b - c);");
test("a * (b % c);", "b % c * a");
test("a * b * (c / d)", "c / d * b * a");
test("(a + b) * (c % d)", "c % d * (a + b)");
testSame("(a / b) * (c % d)");
testSame("(c = 5) * (c % d)");
test("(a + b) * c * (d % e)", "d % e * c * (a + b)");
test("!a * c * (d % e)", "d % e * c * !a");
}
public void testNoRotateInfiniteLoop() {
test("1/x * (y/1 * (1/z))", "1/x * (y/1) * (1/z)");
testSame("1/x * (y/1) * (1/z)");
}
private static class StringCompareTestCase extends CompilerTestCase {
StringCompareTestCase() {
super("", false);
}
@Override
protected CompilerPass getProcessor(Compiler compiler) {
CompilerPass peepholePass =
new PeepholeOptimizationsPass(compiler,
new PeepholeSubstituteAlternateSyntax(false));
return peepholePass;
}
public void testBindToCall3() {
test("(goog.bind(f.m))()", "(0,f.m)()");
test("(goog.bind(f.m,a))()", "f.m.call(a)");
test("(goog.bind(f.m))(a)", "(0,f.m)(a)");
test("(goog.bind(f.m,a))(b)", "f.m.call(a,b)");
test("(goog.partial(f.m))()", "(0,f.m)()");
test("(goog.partial(f.m,a))()", "(0,f.m)(a)");
test("(goog.partial(f.m))(a)", "(0,f.m)(a)");
test("(goog.partial(f.m,a))(b)", "(0,f.m)(a,b)");
// Without using type information we don't know "f" is a function.
testSame("f.m.bind()()");
testSame("f.m.bind(a)()");
testSame("f.m.bind()(a)");
testSame("f.m.bind(a)(b)");
// Don't rewrite if the bind isn't the immediate call target
testSame("goog.bind(f.m).call(g)");
}
}
}