| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class InlineVariablesTest extends CompilerTestCase { |
| 27 |
|
| 28 |
private boolean inlineAllStrings = false; |
| 29 |
private boolean inlineLocalsOnly = false; |
| 30 |
|
| 31 |
public InlineVariablesTest() { |
| 32 |
enableNormalize(); |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void setUp() { |
| 37 |
super.enableLineNumberCheck(true); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 42 |
return new InlineVariables( |
| 43 |
compiler, |
| 44 |
(inlineLocalsOnly) |
| 45 |
? InlineVariables.Mode.LOCALS_ONLY |
| 46 |
: InlineVariables.Mode.ALL, |
| 47 |
inlineAllStrings); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void tearDown() { |
| 52 |
inlineAllStrings = false; |
| 53 |
inlineLocalsOnly = false; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void testInlineGlobal() { |
| 59 |
test("var x = 1; var z = x;", "var z = 1;"); |
| 60 |
} |
| 61 |
|
| 62 |
public void testNoInlineExportedName() { |
| 63 |
testSame("var _x = 1; var z = _x;"); |
| 64 |
} |
| 65 |
|
| 66 |
public void testNoInlineExportedName2() { |
| 67 |
testSame("var f = function() {}; var _x = f;" + |
| 68 |
"var y = function() { _x(); }; var _y = f;"); |
| 69 |
} |
| 70 |
|
| 71 |
public void testDoNotInlineIncrement() { |
| 72 |
testSame("var x = 1; x++;"); |
| 73 |
} |
| 74 |
|
| 75 |
public void testDoNotInlineDecrement() { |
| 76 |
testSame("var x = 1; x--;"); |
| 77 |
} |
| 78 |
|
| 79 |
public void testDoNotInlineIntoLhsOfAssign() { |
| 80 |
testSame("var x = 1; x += 3;"); |
| 81 |
} |
| 82 |
|
| 83 |
public void testInlineIntoRhsOfAssign() { |
| 84 |
test("var x = 1; var y = x;", "var y = 1;"); |
| 85 |
} |
| 86 |
|
| 87 |
public void testInlineInFunction() { |
| 88 |
test("function baz() { var x = 1; var z = x; }", |
| 89 |
"function baz() { var z = 1; }"); |
| 90 |
} |
| 91 |
|
| 92 |
public void testInlineInFunction2() { |
| 93 |
test("function baz() { " + |
| 94 |
"var a = new obj();"+ |
| 95 |
"result = a;" + |
| 96 |
"}", |
| 97 |
"function baz() { " + |
| 98 |
"result = new obj()" + |
| 99 |
"}"); |
| 100 |
} |
| 101 |
|
| 102 |
public void testInlineInFunction3() { |
| 103 |
testSame( |
| 104 |
"function baz() { " + |
| 105 |
"var a = new obj();" + |
| 106 |
"(function(){a;})();" + |
| 107 |
"result = a;" + |
| 108 |
"}"); |
| 109 |
} |
| 110 |
|
| 111 |
public void testInlineInFunction4() { |
| 112 |
testSame( |
| 113 |
"function baz() { " + |
| 114 |
"var a = new obj();" + |
| 115 |
"foo.result = a;" + |
| 116 |
"}"); |
| 117 |
} |
| 118 |
|
| 119 |
public void testInlineInFunction5() { |
| 120 |
testSame( |
| 121 |
"function baz() { " + |
| 122 |
"var a = (foo = new obj());" + |
| 123 |
"foo.x();" + |
| 124 |
"result = a;" + |
| 125 |
"}"); |
| 126 |
} |
| 127 |
|
| 128 |
public void testInlineAcrossModules() { |
| 129 |
|
| 130 |
test(createModules("var a = 2;", "var b = a;"), |
| 131 |
new String[] { "", "var b = 2;" }); |
| 132 |
} |
| 133 |
|
| 134 |
public void testDoNotExitConditional1() { |
| 135 |
testSame("if (true) { var x = 1; } var z = x;"); |
| 136 |
} |
| 137 |
|
| 138 |
public void testDoNotExitConditional2() { |
| 139 |
testSame("if (true) var x = 1; var z = x;"); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
public void testDoNotExitConditional3() { |
| 144 |
testSame("var x; if (true) x=1; var z = x;"); |
| 145 |
} |
| 146 |
|
| 147 |
public void testDoNotExitLoop() { |
| 148 |
testSame("while (z) { var x = 3; } var y = x;"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testDoNotExitForLoop() { |
| 152 |
test("for (var i = 1; false; false) var z = i;", |
| 153 |
"for (;false;false) var z = 1;"); |
| 154 |
testSame("for (; false; false) var i = 1; var z = i;"); |
| 155 |
testSame("for (var i in {}); var z = i;"); |
| 156 |
} |
| 157 |
|
| 158 |
public void testDoNotEnterSubscope() { |
| 159 |
testSame( |
| 160 |
"var x = function() {" + |
| 161 |
" var self = this; " + |
| 162 |
" return function() { var y = self; };" + |
| 163 |
"}"); |
| 164 |
testSame( |
| 165 |
"var x = function() {" + |
| 166 |
" var y = [1]; " + |
| 167 |
" return function() { var z = y; };" + |
| 168 |
"}"); |
| 169 |
} |
| 170 |
|
| 171 |
public void testDoNotExitTry() { |
| 172 |
testSame("try { var x = y; } catch (e) {} var z = y; "); |
| 173 |
testSame("try { throw e; var x = 1; } catch (e) {} var z = x; "); |
| 174 |
} |
| 175 |
|
| 176 |
public void testDoNotEnterCatch() { |
| 177 |
testSame("try { } catch (e) { var z = e; } "); |
| 178 |
} |
| 179 |
|
| 180 |
public void testDoNotEnterFinally() { |
| 181 |
testSame("try { throw e; var x = 1; } catch (e) {} " + |
| 182 |
"finally { var z = x; } "); |
| 183 |
} |
| 184 |
|
| 185 |
public void testInsideIfConditional() { |
| 186 |
test("var a = foo(); if (a) { alert(3); }", "if (foo()) { alert(3); }"); |
| 187 |
test("var a; a = foo(); if (a) { alert(3); }", "if (foo()) { alert(3); }"); |
| 188 |
} |
| 189 |
|
| 190 |
public void testOnlyReadAtInitialization() { |
| 191 |
test("var a; a = foo();", "foo();"); |
| 192 |
test("var a; if (a = foo()) { alert(3); }", "if (foo()) { alert(3); }"); |
| 193 |
test("var a; switch (a = foo()) {}", "switch(foo()) {}"); |
| 194 |
test("var a; function f(){ return a = foo(); }", |
| 195 |
"function f(){ return foo(); }"); |
| 196 |
test("function f(){ var a; return a = foo(); }", |
| 197 |
"function f(){ return foo(); }"); |
| 198 |
test("var a; with (a = foo()) { alert(3); }", "with (foo()) { alert(3); }"); |
| 199 |
|
| 200 |
test("var a; b = (a = foo());", "b = foo();"); |
| 201 |
test("var a; while(a = foo()) { alert(3); }", |
| 202 |
"while(foo()) { alert(3); }"); |
| 203 |
test("var a; for(;a = foo();) { alert(3); }", |
| 204 |
"for(;foo();) { alert(3); }"); |
| 205 |
test("var a; do {} while(a = foo()) { alert(3); }", |
| 206 |
"do {} while(foo()) { alert(3); }"); |
| 207 |
} |
| 208 |
|
| 209 |
public void testImmutableWithSingleReferenceAfterInitialzation() { |
| 210 |
test("var a; a = 1;", "1;"); |
| 211 |
test("var a; if (a = 1) { alert(3); }", "if (1) { alert(3); }"); |
| 212 |
test("var a; switch (a = 1) {}", "switch(1) {}"); |
| 213 |
test("var a; function f(){ return a = 1; }", |
| 214 |
"function f(){ return 1; }"); |
| 215 |
test("function f(){ var a; return a = 1; }", |
| 216 |
"function f(){ return 1; }"); |
| 217 |
test("var a; with (a = 1) { alert(3); }", "with (1) { alert(3); }"); |
| 218 |
|
| 219 |
test("var a; b = (a = 1);", "b = 1;"); |
| 220 |
test("var a; while(a = 1) { alert(3); }", |
| 221 |
"while(1) { alert(3); }"); |
| 222 |
test("var a; for(;a = 1;) { alert(3); }", |
| 223 |
"for(;1;) { alert(3); }"); |
| 224 |
test("var a; do {} while(a = 1) { alert(3); }", |
| 225 |
"do {} while(1) { alert(3); }"); |
| 226 |
} |
| 227 |
|
| 228 |
public void testSingleReferenceAfterInitialzation() { |
| 229 |
test("var a; a = foo();a;", "foo();"); |
| 230 |
testSame("var a; if (a = foo()) { alert(3); } a;"); |
| 231 |
testSame("var a; switch (a = foo()) {} a;"); |
| 232 |
testSame("var a; function f(){ return a = foo(); } a;"); |
| 233 |
testSame("function f(){ var a; return a = foo(); a;}"); |
| 234 |
testSame("var a; with (a = foo()) { alert(3); } a;"); |
| 235 |
testSame("var a; b = (a = foo()); a;"); |
| 236 |
testSame("var a; while(a = foo()) { alert(3); } a;"); |
| 237 |
testSame("var a; for(;a = foo();) { alert(3); } a;"); |
| 238 |
testSame("var a; do {} while(a = foo()) { alert(3); } a;"); |
| 239 |
} |
| 240 |
|
| 241 |
public void testInsideIfBranch() { |
| 242 |
testSame("var a = foo(); if (1) { alert(a); }"); |
| 243 |
} |
| 244 |
|
| 245 |
public void testInsideAndConditional() { |
| 246 |
test("var a = foo(); a && alert(3);", "foo() && alert(3);"); |
| 247 |
} |
| 248 |
|
| 249 |
public void testInsideAndBranch() { |
| 250 |
testSame("var a = foo(); 1 && alert(a);"); |
| 251 |
} |
| 252 |
|
| 253 |
public void testInsideOrBranch() { |
| 254 |
testSame("var a = foo(); 1 || alert(a);"); |
| 255 |
} |
| 256 |
|
| 257 |
public void testInsideHookBranch() { |
| 258 |
testSame("var a = foo(); 1 ? alert(a) : alert(3)"); |
| 259 |
} |
| 260 |
|
| 261 |
public void testInsideHookConditional() { |
| 262 |
test("var a = foo(); a ? alert(1) : alert(3)", |
| 263 |
"foo() ? alert(1) : alert(3)"); |
| 264 |
} |
| 265 |
|
| 266 |
public void testInsideOrBranchInsideIfConditional() { |
| 267 |
testSame("var a = foo(); if (x || a) {}"); |
| 268 |
} |
| 269 |
|
| 270 |
public void testInsideOrBranchInsideIfConditionalWithConstant() { |
| 271 |
|
| 272 |
testSame("var a = [false]; if (x || a) {}"); |
| 273 |
} |
| 274 |
|
| 275 |
public void testCrossFunctionsAsLeftLeaves() { |
| 276 |
|
| 277 |
test( |
| 278 |
new String[] { "var x = function() {};", "", |
| 279 |
"function cow() {} var z = x;"}, |
| 280 |
new String[] { "", "", "function cow() {} var z = function() {};" }); |
| 281 |
test( |
| 282 |
new String[] { "var x = function() {};", "", |
| 283 |
"var cow = function() {}; var z = x;"}, |
| 284 |
new String[] { "", "", |
| 285 |
"var cow = function() {}; var z = function() {};" }); |
| 286 |
testSame( |
| 287 |
new String[] { "var x = a;", "", |
| 288 |
"(function() { a++; })(); var z = x;"}); |
| 289 |
test( |
| 290 |
new String[] { "var x = a;", "", |
| 291 |
"function cow() { a++; }; cow(); var z = x;"}, |
| 292 |
new String[] { "var x = a;", "", |
| 293 |
";(function cow(){ a++; })(); var z = x;"}); |
| 294 |
testSame( |
| 295 |
new String[] { "var x = a;", "", |
| 296 |
"cow(); var z = x; function cow() { a++; };"}); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public void testDoCrossFunction() { |
| 302 |
|
| 303 |
|
| 304 |
test("var x = 1; foo(); var z = x;", "foo(); var z = 1;"); |
| 305 |
} |
| 306 |
|
| 307 |
public void testDoNotCrossReferencingFunction() { |
| 308 |
testSame( |
| 309 |
"var f = function() { var z = x; };" + |
| 310 |
"var x = 1;" + |
| 311 |
"f();" + |
| 312 |
"var z = x;" + |
| 313 |
"f();"); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public void testChainedAssignment() { |
| 320 |
test("var a = 2, b = 2; var c = b;", "var a = 2; var c = 2;"); |
| 321 |
test("var a = 2, b = 2; var c = a;", "var b = 2; var c = 2;"); |
| 322 |
test("var a = b = 2; var f = 3; var c = a;", "var f = 3; var c = b = 2;"); |
| 323 |
testSame("var a = b = 2; var c = b;"); |
| 324 |
} |
| 325 |
|
| 326 |
public void testForIn() { |
| 327 |
testSame("for (var i in j) { var c = i; }"); |
| 328 |
testSame("var i = 0; for (i in j) ;"); |
| 329 |
testSame("var i = 0; for (i in j) { var c = i; }"); |
| 330 |
testSame("i = 0; for (var i in j) { var c = i; }"); |
| 331 |
testSame("var j = {'key':'value'}; for (var i in j) {print(i)};"); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public void testDoCrossNewVariables() { |
| 337 |
test("var x = foo(); var z = x;", "var z = foo();"); |
| 338 |
} |
| 339 |
|
| 340 |
public void testDoNotCrossFunctionCalls() { |
| 341 |
testSame("var x = foo(); bar(); var z = x;"); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public void testDoNotCrossAssignment() { |
| 348 |
testSame("var x = {}; var y = x.a; x.a = 1; var z = y;"); |
| 349 |
testSame("var a = this.id; foo(this.id = 3, a);"); |
| 350 |
} |
| 351 |
|
| 352 |
public void testDoNotCrossDelete() { |
| 353 |
testSame("var x = {}; var y = x.a; delete x.a; var z = y;"); |
| 354 |
} |
| 355 |
|
| 356 |
public void testDoNotCrossAssignmentPlus() { |
| 357 |
testSame("var a = b; b += 2; var c = a;"); |
| 358 |
} |
| 359 |
|
| 360 |
public void testDoNotCrossIncrement() { |
| 361 |
testSame("var a = b.c; b.c++; var d = a;"); |
| 362 |
} |
| 363 |
|
| 364 |
public void testDoNotCrossConstructor() { |
| 365 |
testSame("var a = b; new Foo(); var c = a;"); |
| 366 |
} |
| 367 |
|
| 368 |
public void testDoCrossVar() { |
| 369 |
|
| 370 |
test("var a = b; var b = 3; alert(a)", "alert(3);"); |
| 371 |
} |
| 372 |
|
| 373 |
public void testOverlappingInlines() { |
| 374 |
String source = |
| 375 |
"a = function(el, x, opt_y) { " + |
| 376 |
" var cur = bar(el); " + |
| 377 |
" opt_y = x.y; " + |
| 378 |
" x = x.x; " + |
| 379 |
" var dx = x - cur.x; " + |
| 380 |
" var dy = opt_y - cur.y;" + |
| 381 |
" foo(el, el.offsetLeft + dx, el.offsetTop + dy); " + |
| 382 |
"};"; |
| 383 |
String expected = |
| 384 |
"a = function(el, x, opt_y) { " + |
| 385 |
" var cur = bar(el); " + |
| 386 |
" opt_y = x.y; " + |
| 387 |
" x = x.x; " + |
| 388 |
" foo(el, el.offsetLeft + (x - cur.x)," + |
| 389 |
" el.offsetTop + (opt_y - cur.y)); " + |
| 390 |
"};"; |
| 391 |
|
| 392 |
test(source, expected); |
| 393 |
} |
| 394 |
|
| 395 |
public void testOverlappingInlineFunctions() { |
| 396 |
String source = |
| 397 |
"a = function() { " + |
| 398 |
" var b = function(args) {var n;}; " + |
| 399 |
" var c = function(args) {}; " + |
| 400 |
" d(b,c); " + |
| 401 |
"};"; |
| 402 |
String expected = |
| 403 |
"a = function() { " + |
| 404 |
" d(function(args){var n;}, function(args){}); " + |
| 405 |
"};"; |
| 406 |
|
| 407 |
test(source, expected); |
| 408 |
} |
| 409 |
|
| 410 |
public void testInlineIntoLoops() { |
| 411 |
test("var x = true; while (true) alert(x);", |
| 412 |
"while (true) alert(true);"); |
| 413 |
test("var x = true; while (true) for (var i in {}) alert(x);", |
| 414 |
"while (true) for (var i in {}) alert(true);"); |
| 415 |
testSame("var x = [true]; while (true) alert(x);"); |
| 416 |
} |
| 417 |
|
| 418 |
public void testInlineIntoFunction() { |
| 419 |
test("var x = false; var f = function() { alert(x); };", |
| 420 |
"var f = function() { alert(false); };"); |
| 421 |
testSame("var x = [false]; var f = function() { alert(x); };"); |
| 422 |
} |
| 423 |
|
| 424 |
public void testNoInlineIntoNamedFunction() { |
| 425 |
testSame("f(); var x = false; function f() { alert(x); };"); |
| 426 |
} |
| 427 |
|
| 428 |
public void testInlineIntoNestedNonHoistedNamedFunctions() { |
| 429 |
test("f(); var x = false; if (false) function f() { alert(x); };", |
| 430 |
"f(); if (false) function f() { alert(false); };"); |
| 431 |
} |
| 432 |
|
| 433 |
public void testNoInlineIntoNestedNamedFunctions() { |
| 434 |
testSame("f(); var x = false; function f() { if (false) { alert(x); } };"); |
| 435 |
} |
| 436 |
|
| 437 |
public void testNoInlineMutatedVariable() { |
| 438 |
testSame("var x = false; if (true) { var y = x; x = true; }"); |
| 439 |
} |
| 440 |
|
| 441 |
public void testInlineImmutableMultipleTimes() { |
| 442 |
test("var x = null; var y = x, z = x;", |
| 443 |
"var y = null, z = null;"); |
| 444 |
test("var x = 3; var y = x, z = x;", |
| 445 |
"var y = 3, z = 3;"); |
| 446 |
} |
| 447 |
|
| 448 |
public void testNoInlineStringMultipleTimesIfNotWorthwhile() { |
| 449 |
testSame("var x = 'abcdefghijklmnopqrstuvwxyz'; var y = x, z = x;"); |
| 450 |
} |
| 451 |
|
| 452 |
public void testInlineStringMultipleTimesWhenAliasingAllStrings() { |
| 453 |
inlineAllStrings = true; |
| 454 |
test("var x = 'abcdefghijklmnopqrstuvwxyz'; var y = x, z = x;", |
| 455 |
"var y = 'abcdefghijklmnopqrstuvwxyz', " + |
| 456 |
" z = 'abcdefghijklmnopqrstuvwxyz';"); |
| 457 |
} |
| 458 |
|
| 459 |
public void testNoInlineBackwards() { |
| 460 |
testSame("var y = x; var x = null;"); |
| 461 |
} |
| 462 |
|
| 463 |
public void testNoInlineOutOfBranch() { |
| 464 |
testSame("if (true) var x = null; var y = x;"); |
| 465 |
} |
| 466 |
|
| 467 |
public void testInterferingInlines() { |
| 468 |
test("var a = 3; var f = function() { var x = a; alert(x); };", |
| 469 |
"var f = function() { alert(3); };"); |
| 470 |
} |
| 471 |
|
| 472 |
public void testInlineIntoTryCatch() { |
| 473 |
test("var a = true; " + |
| 474 |
"try { var b = a; } " + |
| 475 |
"catch (e) { var c = a + b; var d = true; } " + |
| 476 |
"finally { var f = a + b + c + d; }", |
| 477 |
"try { var b = true; } " + |
| 478 |
"catch (e) { var c = true + b; var d = true; } " + |
| 479 |
"finally { var f = true + b + c + d; }"); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public void testInlineConstants() { |
| 485 |
test("function foo() { return XXX; } var XXX = true;", |
| 486 |
"function foo() { return true; }"); |
| 487 |
} |
| 488 |
|
| 489 |
public void testInlineStringWhenWorthwhile() { |
| 490 |
test("var x = 'a'; foo(x, x, x);", "foo('a', 'a', 'a');"); |
| 491 |
} |
| 492 |
|
| 493 |
public void testInlineConstantAlias() { |
| 494 |
test("var XXX = new Foo(); q(XXX); var YYY = XXX; bar(YYY)", |
| 495 |
"var XXX = new Foo(); q(XXX); bar(XXX)"); |
| 496 |
} |
| 497 |
|
| 498 |
public void testInlineConstantAliasWithAnnotation() { |
| 499 |
test("/** @const */ var xxx = new Foo(); q(xxx); var YYY = xxx; bar(YYY)", |
| 500 |
"/** @const */ var xxx = new Foo(); q(xxx); bar(xxx)"); |
| 501 |
} |
| 502 |
|
| 503 |
public void testInlineConstantAliasWithNonConstant() { |
| 504 |
test("var XXX = new Foo(); q(XXX); var y = XXX; bar(y); baz(y)", |
| 505 |
"var XXX = new Foo(); q(XXX); bar(XXX); baz(XXX)"); |
| 506 |
} |
| 507 |
|
| 508 |
public void testCascadingInlines() { |
| 509 |
test("var XXX = 4; " + |
| 510 |
"function f() { var YYY = XXX; bar(YYY); baz(YYY); }", |
| 511 |
"function f() { bar(4); baz(4); }"); |
| 512 |
} |
| 513 |
|
| 514 |
public void testNoInlineGetpropIntoCall() { |
| 515 |
test("var a = b; a();", "b();"); |
| 516 |
test("var a = b.c; f(a);", "f(b.c);"); |
| 517 |
testSame("var a = b.c; a();"); |
| 518 |
} |
| 519 |
|
| 520 |
public void testInlineFunctionDeclaration() { |
| 521 |
test("var f = function () {}; var a = f;", |
| 522 |
"var a = function () {};"); |
| 523 |
test("var f = function () {}; foo(); var a = f;", |
| 524 |
"foo(); var a = function () {};"); |
| 525 |
test("var f = function () {}; foo(f);", |
| 526 |
"foo(function () {});"); |
| 527 |
|
| 528 |
testSame("var f = function () {}; function g() {var a = f;}"); |
| 529 |
testSame("var f = function () {}; function g() {h(f);}"); |
| 530 |
} |
| 531 |
|
| 532 |
public void test2388531() { |
| 533 |
testSame("var f = function () {};" + |
| 534 |
"var g = function () {};" + |
| 535 |
"goog.inherits(f, g);"); |
| 536 |
testSame("var f = function () {};" + |
| 537 |
"var g = function () {};" + |
| 538 |
"goog$inherits(f, g);"); |
| 539 |
} |
| 540 |
|
| 541 |
public void testRecursiveFunction1() { |
| 542 |
testSame("var x = 0; (function x() { return x ? x() : 3; })();"); |
| 543 |
} |
| 544 |
|
| 545 |
public void testRecursiveFunction2() { |
| 546 |
testSame("function y() { return y(); }"); |
| 547 |
} |
| 548 |
|
| 549 |
public void testUnreferencedBleedingFunction() { |
| 550 |
testSame("var x = function y() {}"); |
| 551 |
} |
| 552 |
|
| 553 |
public void testReferencedBleedingFunction() { |
| 554 |
testSame("var x = function y() { return y(); }"); |
| 555 |
} |
| 556 |
|
| 557 |
public void testInlineAliases1() { |
| 558 |
test("var x = this.foo(); this.bar(); var y = x; this.baz(y);", |
| 559 |
"var x = this.foo(); this.bar(); this.baz(x);"); |
| 560 |
} |
| 561 |
|
| 562 |
public void testInlineAliases1b() { |
| 563 |
test("var x = this.foo(); this.bar(); var y; y = x; this.baz(y);", |
| 564 |
"var x = this.foo(); this.bar(); x; this.baz(x);"); |
| 565 |
} |
| 566 |
|
| 567 |
public void testInlineAliases1c() { |
| 568 |
test("var x; x = this.foo(); this.bar(); var y = x; this.baz(y);", |
| 569 |
"var x; x = this.foo(); this.bar(); this.baz(x);"); |
| 570 |
} |
| 571 |
|
| 572 |
public void testInlineAliases1d() { |
| 573 |
test("var x; x = this.foo(); this.bar(); var y; y = x; this.baz(y);", |
| 574 |
"var x; x = this.foo(); this.bar(); x; this.baz(x);"); |
| 575 |
} |
| 576 |
|
| 577 |
public void testInlineAliases2() { |
| 578 |
test("var x = this.foo(); this.bar(); " + |
| 579 |
"function f() { var y = x; this.baz(y); }", |
| 580 |
"var x = this.foo(); this.bar(); function f() { this.baz(x); }"); |
| 581 |
} |
| 582 |
|
| 583 |
public void testInlineAliases2b() { |
| 584 |
test("var x = this.foo(); this.bar(); " + |
| 585 |
"function f() { var y; y = x; this.baz(y); }", |
| 586 |
"var x = this.foo(); this.bar(); function f() { this.baz(x); }"); |
| 587 |
} |
| 588 |
|
| 589 |
public void testInlineAliases2c() { |
| 590 |
test("var x; x = this.foo(); this.bar(); " + |
| 591 |
"function f() { var y = x; this.baz(y); }", |
| 592 |
"var x; x = this.foo(); this.bar(); function f() { this.baz(x); }"); |
| 593 |
} |
| 594 |
|
| 595 |
public void testInlineAliases2d() { |
| 596 |
test("var x; x = this.foo(); this.bar(); " + |
| 597 |
"function f() { var y; y = x; this.baz(y); }", |
| 598 |
"var x; x = this.foo(); this.bar(); function f() { this.baz(x); }"); |
| 599 |
} |
| 600 |
|
| 601 |
public void testInlineAliasesInLoop() { |
| 602 |
test( |
| 603 |
"function f() { " + |
| 604 |
" var x = extern();" + |
| 605 |
" for (var i = 0; i < 5; i++) {" + |
| 606 |
" (function() {" + |
| 607 |
" var y = x; window.setTimeout(function() { extern(y); }, 0);" + |
| 608 |
" })();" + |
| 609 |
" }" + |
| 610 |
"}", |
| 611 |
"function f() { " + |
| 612 |
" var x = extern();" + |
| 613 |
" for (var i = 0; i < 5; i++) {" + |
| 614 |
" (function() {" + |
| 615 |
" window.setTimeout(function() { extern(x); }, 0);" + |
| 616 |
" })();" + |
| 617 |
" }" + |
| 618 |
"}"); |
| 619 |
} |
| 620 |
|
| 621 |
public void testNoInlineAliasesInLoop() { |
| 622 |
testSame( |
| 623 |
"function f() { " + |
| 624 |
" for (var i = 0; i < 5; i++) {" + |
| 625 |
" var x = extern();" + |
| 626 |
" (function() {" + |
| 627 |
" var y = x; window.setTimeout(function() { extern(y); }, 0);" + |
| 628 |
" })();" + |
| 629 |
" }" + |
| 630 |
"}"); |
| 631 |
} |
| 632 |
|
| 633 |
public void testNoInlineAliases1() { |
| 634 |
testSame( |
| 635 |
"var x = this.foo(); this.bar(); var y = x; x = 3; this.baz(y);"); |
| 636 |
} |
| 637 |
|
| 638 |
public void testNoInlineAliases1b() { |
| 639 |
testSame( |
| 640 |
"var x = this.foo(); this.bar(); var y; y = x; x = 3; this.baz(y);"); |
| 641 |
} |
| 642 |
|
| 643 |
public void testNoInlineAliases2() { |
| 644 |
testSame( |
| 645 |
"var x = this.foo(); this.bar(); var y = x; y = 3; this.baz(y); "); |
| 646 |
} |
| 647 |
|
| 648 |
public void testNoInlineAliases2b() { |
| 649 |
testSame( |
| 650 |
"var x = this.foo(); this.bar(); var y; y = x; y = 3; this.baz(y); "); |
| 651 |
} |
| 652 |
|
| 653 |
public void testNoInlineAliases3() { |
| 654 |
testSame( |
| 655 |
"var x = this.foo(); this.bar(); " + |
| 656 |
"function f() { var y = x; g(); this.baz(y); } " + |
| 657 |
"function g() { x = 3; }"); |
| 658 |
} |
| 659 |
|
| 660 |
public void testNoInlineAliases3b() { |
| 661 |
testSame( |
| 662 |
"var x = this.foo(); this.bar(); " + |
| 663 |
"function f() { var y; y = x; g(); this.baz(y); } " + |
| 664 |
"function g() { x = 3; }"); |
| 665 |
} |
| 666 |
|
| 667 |
public void testNoInlineAliases4() { |
| 668 |
testSame( |
| 669 |
"var x = this.foo(); this.bar(); " + |
| 670 |
"function f() { var y = x; y = 3; this.baz(y); }"); |
| 671 |
} |
| 672 |
|
| 673 |
public void testNoInlineAliases4b() { |
| 674 |
testSame( |
| 675 |
"var x = this.foo(); this.bar(); " + |
| 676 |
"function f() { var y; y = x; y = 3; this.baz(y); }"); |
| 677 |
} |
| 678 |
|
| 679 |
public void testNoInlineAliases5() { |
| 680 |
testSame( |
| 681 |
"var x = this.foo(); this.bar(); var y = x; this.bing();" + |
| 682 |
"this.baz(y); x = 3;"); |
| 683 |
} |
| 684 |
|
| 685 |
public void testNoInlineAliases5b() { |
| 686 |
testSame( |
| 687 |
"var x = this.foo(); this.bar(); var y; y = x; this.bing();" + |
| 688 |
"this.baz(y); x = 3;"); |
| 689 |
} |
| 690 |
|
| 691 |
public void testNoInlineAliases6() { |
| 692 |
testSame( |
| 693 |
"var x = this.foo(); this.bar(); var y = x; this.bing();" + |
| 694 |
"this.baz(y); y = 3;"); |
| 695 |
} |
| 696 |
|
| 697 |
public void testNoInlineAliases6b() { |
| 698 |
testSame( |
| 699 |
"var x = this.foo(); this.bar(); var y; y = x; this.bing();" + |
| 700 |
"this.baz(y); y = 3;"); |
| 701 |
} |
| 702 |
|
| 703 |
public void testNoInlineAliases7() { |
| 704 |
testSame( |
| 705 |
"var x = this.foo(); this.bar(); " + |
| 706 |
"function f() { var y = x; this.bing(); this.baz(y); x = 3; }"); |
| 707 |
} |
| 708 |
|
| 709 |
public void testNoInlineAliases7b() { |
| 710 |
testSame( |
| 711 |
"var x = this.foo(); this.bar(); " + |
| 712 |
"function f() { var y; y = x; this.bing(); this.baz(y); x = 3; }"); |
| 713 |
} |
| 714 |
|
| 715 |
public void testNoInlineAliases8() { |
| 716 |
testSame( |
| 717 |
"var x = this.foo(); this.bar(); " + |
| 718 |
"function f() { var y = x; this.baz(y); y = 3; }"); |
| 719 |
} |
| 720 |
|
| 721 |
public void testNoInlineAliases8b() { |
| 722 |
testSame( |
| 723 |
"var x = this.foo(); this.bar(); " + |
| 724 |
"function f() { var y; y = x; this.baz(y); y = 3; }"); |
| 725 |
} |
| 726 |
|
| 727 |
public void testSideEffectOrder() { |
| 728 |
|
| 729 |
String EXTERNS = "var z; function f(){}"; |
| 730 |
test(EXTERNS, |
| 731 |
"var x = f(y.a, y); z = x;", |
| 732 |
"z = f(y.a, y);", null, null); |
| 733 |
|
| 734 |
testSame(EXTERNS, "var x = f(y.a, y); z.b = x;", null, null); |
| 735 |
} |
| 736 |
|
| 737 |
public void testInlineParameterAlias1() { |
| 738 |
test( |
| 739 |
"function f(x) {" + |
| 740 |
" var y = x;" + |
| 741 |
" g();" + |
| 742 |
" y;y;" + |
| 743 |
"}", |
| 744 |
"function f(x) {" + |
| 745 |
" g();" + |
| 746 |
" x;x;" + |
| 747 |
"}" |
| 748 |
); |
| 749 |
} |
| 750 |
|
| 751 |
public void testInlineParameterAlias2() { |
| 752 |
test( |
| 753 |
"function f(x) {" + |
| 754 |
" var y; y = x;" + |
| 755 |
" g();" + |
| 756 |
" y;y;" + |
| 757 |
"}", |
| 758 |
"function f(x) {" + |
| 759 |
" x;" + |
| 760 |
" g();" + |
| 761 |
" x;x;" + |
| 762 |
"}" |
| 763 |
); |
| 764 |
} |
| 765 |
|
| 766 |
public void testInlineFunctionAlias1a() { |
| 767 |
test( |
| 768 |
"function f(x) {}" + |
| 769 |
"var y = f;" + |
| 770 |
"g();" + |
| 771 |
"y();y();", |
| 772 |
"var y = function f(x) {};" + |
| 773 |
"g();" + |
| 774 |
"y();y();" |
| 775 |
); |
| 776 |
} |
| 777 |
|
| 778 |
public void testInlineFunctionAlias1b() { |
| 779 |
test( |
| 780 |
"function f(x) {};" + |
| 781 |
"f;var y = f;" + |
| 782 |
"g();" + |
| 783 |
"y();y();", |
| 784 |
"function f(x) {};" + |
| 785 |
"f;g();" + |
| 786 |
"f();f();" |
| 787 |
); |
| 788 |
} |
| 789 |
|
| 790 |
public void testInlineFunctionAlias2a() { |
| 791 |
test( |
| 792 |
"function f(x) {}" + |
| 793 |
"var y; y = f;" + |
| 794 |
"g();" + |
| 795 |
"y();y();", |
| 796 |
"var y; y = function f(x) {};" + |
| 797 |
"g();" + |
| 798 |
"y();y();" |
| 799 |
); |
| 800 |
} |
| 801 |
|
| 802 |
public void testInlineFunctionAlias2b() { |
| 803 |
test( |
| 804 |
"function f(x) {};" + |
| 805 |
"f; var y; y = f;" + |
| 806 |
"g();" + |
| 807 |
"y();y();", |
| 808 |
"function f(x) {};" + |
| 809 |
"f; f;" + |
| 810 |
"g();" + |
| 811 |
"f();f();" |
| 812 |
); |
| 813 |
} |
| 814 |
|
| 815 |
public void testInlineCatchAlias1() { |
| 816 |
test( |
| 817 |
"try {" + |
| 818 |
"} catch (e) {" + |
| 819 |
" var y = e;" + |
| 820 |
" g();" + |
| 821 |
" y;y;" + |
| 822 |
"}", |
| 823 |
"try {" + |
| 824 |
"} catch (e) {" + |
| 825 |
" g();" + |
| 826 |
" e;e;" + |
| 827 |
"}" |
| 828 |
); |
| 829 |
} |
| 830 |
|
| 831 |
public void testInlineCatchAlias2() { |
| 832 |
test( |
| 833 |
"try {" + |
| 834 |
"} catch (e) {" + |
| 835 |
" var y; y = e;" + |
| 836 |
" g();" + |
| 837 |
" y;y;" + |
| 838 |
"}", |
| 839 |
"try {" + |
| 840 |
"} catch (e) {" + |
| 841 |
" e;" + |
| 842 |
" g();" + |
| 843 |
" e;e;" + |
| 844 |
"}" |
| 845 |
); |
| 846 |
} |
| 847 |
|
| 848 |
public void testLocalsOnly1() { |
| 849 |
inlineLocalsOnly = true; |
| 850 |
test( |
| 851 |
"var x=1; x; function f() {var x = 1; x;}", |
| 852 |
"var x=1; x; function f() {1;}"); |
| 853 |
} |
| 854 |
|
| 855 |
public void testLocalsOnly2() { |
| 856 |
inlineLocalsOnly = true; |
| 857 |
test( |
| 858 |
"/** @const */\n" + |
| 859 |
"var X=1; X;\n" + |
| 860 |
"function f() {\n" + |
| 861 |
" /** @const */\n" + |
| 862 |
" var X = 1; X;\n" + |
| 863 |
"}", |
| 864 |
"var X=1; X; function f() {1;}"); |
| 865 |
} |
| 866 |
|
| 867 |
public void testInlineUndefined1() { |
| 868 |
test("var x; x;", |
| 869 |
"void 0;"); |
| 870 |
} |
| 871 |
|
| 872 |
public void testInlineUndefined2() { |
| 873 |
testSame("var x; x++;"); |
| 874 |
} |
| 875 |
|
| 876 |
public void testInlineUndefined3() { |
| 877 |
testSame("var x; var x;"); |
| 878 |
} |
| 879 |
|
| 880 |
public void testInlineUndefined4() { |
| 881 |
test("var x; x; x;", |
| 882 |
"void 0; void 0;"); |
| 883 |
} |
| 884 |
|
| 885 |
public void testInlineUndefined5() { |
| 886 |
test("var x; for(x in a) {}", |
| 887 |
"var x; for(x in a) {}"); |
| 888 |
} |
| 889 |
|
| 890 |
public void testIssue90() { |
| 891 |
test("var x; x && alert(1)", |
| 892 |
"void 0 && alert(1)"); |
| 893 |
} |
| 894 |
|
| 895 |
public void testRenamePropertyFunction() { |
| 896 |
testSame("var JSCompiler_renameProperty; " + |
| 897 |
"JSCompiler_renameProperty('foo')"); |
| 898 |
} |
| 899 |
|
| 900 |
public void testThisAlias() { |
| 901 |
test("function f() { var a = this; a.y(); a.z(); }", |
| 902 |
"function f() { this.y(); this.z(); }"); |
| 903 |
} |
| 904 |
|
| 905 |
public void testThisEscapedAlias() { |
| 906 |
testSame( |
| 907 |
"function f() { var a = this; var g = function() { a.y(); }; a.z(); }"); |
| 908 |
} |
| 909 |
|
| 910 |
public void testInlineNamedFunction() { |
| 911 |
test("function f() {} f();", "(function f(){})()"); |
| 912 |
} |
| 913 |
|
| 914 |
public void testIssue378ModifiedArguments1() { |
| 915 |
testSame( |
| 916 |
"function g(callback) {\n" + |
| 917 |
" var f = callback;\n" + |
| 918 |
" arguments[0] = this;\n" + |
| 919 |
" f.apply(this, arguments);\n" + |
| 920 |
"}"); |
| 921 |
} |
| 922 |
|
| 923 |
public void testIssue378ModifiedArguments2() { |
| 924 |
testSame( |
| 925 |
"function g(callback) {\n" + |
| 926 |
" /** @const */\n" + |
| 927 |
" var f = callback;\n" + |
| 928 |
" arguments[0] = this;\n" + |
| 929 |
" f.apply(this, arguments);\n" + |
| 930 |
"}"); |
| 931 |
} |
| 932 |
|
| 933 |
public void testIssue378EscapedArguments1() { |
| 934 |
testSame( |
| 935 |
"function g(callback) {\n" + |
| 936 |
" var f = callback;\n" + |
| 937 |
" h(arguments,this);\n" + |
| 938 |
" f.apply(this, arguments);\n" + |
| 939 |
"}\n" + |
| 940 |
"function h(a,b) {\n" + |
| 941 |
" a[0] = b;" + |
| 942 |
"}"); |
| 943 |
} |
| 944 |
|
| 945 |
public void testIssue378EscapedArguments2() { |
| 946 |
testSame( |
| 947 |
"function g(callback) {\n" + |
| 948 |
" /** @const */\n" + |
| 949 |
" var f = callback;\n" + |
| 950 |
" h(arguments,this);\n" + |
| 951 |
" f.apply(this);\n" + |
| 952 |
"}\n" + |
| 953 |
"function h(a,b) {\n" + |
| 954 |
" a[0] = b;" + |
| 955 |
"}"); |
| 956 |
} |
| 957 |
|
| 958 |
public void testIssue378EscapedArguments3() { |
| 959 |
test( |
| 960 |
"function g(callback) {\n" + |
| 961 |
" var f = callback;\n" + |
| 962 |
" f.apply(this, arguments);\n" + |
| 963 |
"}\n", |
| 964 |
"function g(callback) {\n" + |
| 965 |
" callback.apply(this, arguments);\n" + |
| 966 |
"}\n"); |
| 967 |
} |
| 968 |
|
| 969 |
public void testIssue378EscapedArguments4() { |
| 970 |
testSame( |
| 971 |
"function g(callback) {\n" + |
| 972 |
" var f = callback;\n" + |
| 973 |
" h(arguments[0],this);\n" + |
| 974 |
" f.apply(this, arguments);\n" + |
| 975 |
"}\n" + |
| 976 |
"function h(a,b) {\n" + |
| 977 |
" a[0] = b;" + |
| 978 |
"}"); |
| 979 |
} |
| 980 |
|
| 981 |
public void testIssue378ArgumentsRead1() { |
| 982 |
test( |
| 983 |
"function g(callback) {\n" + |
| 984 |
" var f = callback;\n" + |
| 985 |
" var g = arguments[0];\n" + |
| 986 |
" f.apply(this, arguments);\n" + |
| 987 |
"}", |
| 988 |
"function g(callback) {\n" + |
| 989 |
" var g = arguments[0];\n" + |
| 990 |
" callback.apply(this, arguments);\n" + |
| 991 |
"}"); |
| 992 |
} |
| 993 |
|
| 994 |
public void testIssue378ArgumentsRead2() { |
| 995 |
test( |
| 996 |
"function g(callback) {\n" + |
| 997 |
" var f = callback;\n" + |
| 998 |
" h(arguments[0],this);\n" + |
| 999 |
" f.apply(this, arguments[0]);\n" + |
| 1000 |
"}\n" + |
| 1001 |
"function h(a,b) {\n" + |
| 1002 |
" a[0] = b;" + |
| 1003 |
"}", |
| 1004 |
"function g(callback) {\n" + |
| 1005 |
" h(arguments[0],this);\n" + |
| 1006 |
" callback.apply(this, arguments[0]);\n" + |
| 1007 |
"}\n" + |
| 1008 |
"function h(a,b) {\n" + |
| 1009 |
" a[0] = b;" + |
| 1010 |
"}"); |
| 1011 |
} |
| 1012 |
|
| 1013 |
public void testArgumentsModifiedInOuterFunction() { |
| 1014 |
test( |
| 1015 |
"function g(callback) {\n" + |
| 1016 |
" var f = callback;\n" + |
| 1017 |
" arguments[0] = this;\n" + |
| 1018 |
" f.apply(this, arguments);\n" + |
| 1019 |
" function inner(callback) {" + |
| 1020 |
" var x = callback;\n" + |
| 1021 |
" x.apply(this);\n" + |
| 1022 |
" }" + |
| 1023 |
"}", |
| 1024 |
"function g(callback) {\n" + |
| 1025 |
" var f = callback;\n" + |
| 1026 |
" arguments[0] = this;\n" + |
| 1027 |
" f.apply(this, arguments);\n" + |
| 1028 |
" function inner(callback) {" + |
| 1029 |
" callback.apply(this);\n" + |
| 1030 |
" }" + |
| 1031 |
"}"); |
| 1032 |
} |
| 1033 |
|
| 1034 |
public void testArgumentsModifiedInInnerFunction() { |
| 1035 |
test( |
| 1036 |
"function g(callback) {\n" + |
| 1037 |
" var f = callback;\n" + |
| 1038 |
" f.apply(this, arguments);\n" + |
| 1039 |
" function inner(callback) {" + |
| 1040 |
" var x = callback;\n" + |
| 1041 |
" arguments[0] = this;\n" + |
| 1042 |
" x.apply(this);\n" + |
| 1043 |
" }" + |
| 1044 |
"}", |
| 1045 |
"function g(callback) {\n" + |
| 1046 |
" callback.apply(this, arguments);\n" + |
| 1047 |
" function inner(callback) {" + |
| 1048 |
" var x = callback;\n" + |
| 1049 |
" arguments[0] = this;\n" + |
| 1050 |
" x.apply(this);\n" + |
| 1051 |
" }" + |
| 1052 |
"}"); |
| 1053 |
} |
| 1054 |
|
| 1055 |
public void testNoInlineRedeclaredExterns() { |
| 1056 |
String externs = "var test = 1;"; |
| 1057 |
String code = "/** @suppress {duplicate} */ var test = 2;alert(test);"; |
| 1058 |
test(externs, code, code, null, null); |
| 1059 |
} |
| 1060 |
|
| 1061 |
public void testBug6598844() { |
| 1062 |
testSame( |
| 1063 |
"function F() { this.a = 0; }" + |
| 1064 |
"F.prototype.inc = function() { this.a++; return 10; };" + |
| 1065 |
"F.prototype.bar = function() { var val = inc(); this.a += val; };"); |
| 1066 |
} |
| 1067 |
} |