| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class PeepholeRemoveDeadCodeTest extends CompilerTestCase { |
| 26 |
|
| 27 |
private static final String MATH = |
| 28 |
"/** @const */ var Math = {};" + |
| 29 |
"/** @nosideeffects */ Math.random = function(){};" + |
| 30 |
"/** @nosideeffects */ Math.sin = function(){};"; |
| 31 |
|
| 32 |
public PeepholeRemoveDeadCodeTest() { |
| 33 |
super(MATH); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void setUp() throws Exception { |
| 38 |
super.setUp(); |
| 39 |
enableLineNumberCheck(true); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 44 |
return new CompilerPass() { |
| 45 |
@Override |
| 46 |
public void process(Node externs, Node root) { |
| 47 |
SimpleDefinitionFinder definitionFinder = |
| 48 |
new SimpleDefinitionFinder(compiler); |
| 49 |
definitionFinder.process(externs, root); |
| 50 |
new PureFunctionIdentifier(compiler, definitionFinder) |
| 51 |
.process(externs, root); |
| 52 |
PeepholeOptimizationsPass peepholePass = |
| 53 |
new PeepholeOptimizationsPass( |
| 54 |
compiler, new PeepholeRemoveDeadCode()); |
| 55 |
peepholePass.process(externs, root); |
| 56 |
} |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected int getNumRepetitions() { |
| 62 |
|
| 63 |
return 2; |
| 64 |
} |
| 65 |
|
| 66 |
private void foldSame(String js) { |
| 67 |
testSame(js); |
| 68 |
} |
| 69 |
|
| 70 |
private void fold(String js, String expected) { |
| 71 |
test(js, expected); |
| 72 |
} |
| 73 |
|
| 74 |
public void testFoldBlock() { |
| 75 |
fold("{{foo()}}", "foo()"); |
| 76 |
fold("{foo();{}}", "foo()"); |
| 77 |
fold("{{foo()}{}}", "foo()"); |
| 78 |
fold("{{foo()}{bar()}}", "foo();bar()"); |
| 79 |
fold("{if(false)foo(); {bar()}}", "bar()"); |
| 80 |
fold("{if(false)if(false)if(false)foo(); {bar()}}", "bar()"); |
| 81 |
|
| 82 |
fold("{'hi'}", ""); |
| 83 |
fold("{x==3}", ""); |
| 84 |
fold("{ (function(){x++}) }", ""); |
| 85 |
fold("function f(){return;}", "function f(){return;}"); |
| 86 |
fold("function f(){return 3;}", "function f(){return 3}"); |
| 87 |
fold("function f(){if(x)return; x=3; return; }", |
| 88 |
"function f(){if(x)return; x=3; return; }"); |
| 89 |
fold("{x=3;;;y=2;;;}", "x=3;y=2"); |
| 90 |
|
| 91 |
|
| 92 |
fold("while(x()){x}", "while(x());"); |
| 93 |
fold("while(x()){x()}", "while(x())x()"); |
| 94 |
fold("for(x=0;x<100;x++){x}", "for(x=0;x<100;x++);"); |
| 95 |
fold("for(x in y){x}", "for(x in y);"); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
public void testFoldBlocksWithManyChildren() { |
| 100 |
fold("function f() { if (false) {} }", "function f(){}"); |
| 101 |
fold("function f() { { if (false) {} if (true) {} {} } }", |
| 102 |
"function f(){}"); |
| 103 |
fold("{var x; var y; var z; function f() { { var a; { var b; } } } }", |
| 104 |
"var x;var y;var z;function f(){var a;var b}"); |
| 105 |
} |
| 106 |
|
| 107 |
public void testIf() { |
| 108 |
fold("if (1){ x=1; } else { x = 2;}", "x=1"); |
| 109 |
fold("if (false){ x = 1; } else { x = 2; }", "x=2"); |
| 110 |
fold("if (undefined){ x = 1; } else { x = 2; }", "x=2"); |
| 111 |
fold("if (null){ x = 1; } else { x = 2; }", "x=2"); |
| 112 |
fold("if (void 0){ x = 1; } else { x = 2; }", "x=2"); |
| 113 |
fold("if (void foo()){ x = 1; } else { x = 2; }", |
| 114 |
"foo();x=2"); |
| 115 |
fold("if (false){ x = 1; } else if (true) { x = 3; } else { x = 2; }", |
| 116 |
"x=3"); |
| 117 |
fold("if (x){ x = 1; } else if (false) { x = 3; }", |
| 118 |
"if(x)x=1"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testHook() { |
| 122 |
fold("true ? a() : b()", "a()"); |
| 123 |
fold("false ? a() : b()", "b()"); |
| 124 |
|
| 125 |
fold("a() ? b() : true", "a() && b()"); |
| 126 |
fold("a() ? true : b()", "a() || b()"); |
| 127 |
|
| 128 |
fold("(a = true) ? b() : c()", "a = true, b()"); |
| 129 |
fold("(a = false) ? b() : c()", "a = false, c()"); |
| 130 |
fold("do {f()} while((a = true) ? b() : c())", |
| 131 |
"do {f()} while((a = true) , b())"); |
| 132 |
fold("do {f()} while((a = false) ? b() : c())", |
| 133 |
"do {f()} while((a = false) , c())"); |
| 134 |
|
| 135 |
fold("var x = (true) ? 1 : 0", "var x=1"); |
| 136 |
fold("var y = (true) ? ((false) ? 12 : (cond ? 1 : 2)) : 13", |
| 137 |
"var y=cond?1:2"); |
| 138 |
|
| 139 |
foldSame("var z=x?void 0:y()"); |
| 140 |
foldSame("z=x?void 0:y()"); |
| 141 |
foldSame("z*=x?void 0:y()"); |
| 142 |
|
| 143 |
foldSame("var z=x?y():void 0"); |
| 144 |
foldSame("(w?x:void 0).y=z"); |
| 145 |
foldSame("(w?x:void 0).y+=z"); |
| 146 |
|
| 147 |
fold("y = (x ? void 0 : void 0)", "y = void 0"); |
| 148 |
fold("y = (x ? f() : f())", "y = f()"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testConstantConditionWithSideEffect1() { |
| 152 |
fold("if (b=true) x=1;", "b=true;x=1"); |
| 153 |
fold("if (b=/ab/) x=1;", "b=/ab/;x=1"); |
| 154 |
fold("if (b=/ab/){ x=1; } else { x=2; }", "b=/ab/;x=1"); |
| 155 |
fold("var b;b=/ab/;if(b)x=1;", "var b;b=/ab/;x=1"); |
| 156 |
foldSame("var b;b=f();if(b)x=1;"); |
| 157 |
fold("var b=/ab/;if(b)x=1;", "var b=/ab/;x=1"); |
| 158 |
foldSame("var b=f();if(b)x=1;"); |
| 159 |
foldSame("b=b++;if(b)x=b;"); |
| 160 |
fold("(b=0,b=1);if(b)x=b;", "b=0,b=1;if(b)x=b;"); |
| 161 |
fold("b=1;if(foo,b)x=b;","b=1;x=b;"); |
| 162 |
foldSame("b=1;if(foo=1,b)x=b;"); |
| 163 |
} |
| 164 |
|
| 165 |
public void testConstantConditionWithSideEffect2() { |
| 166 |
fold("(b=true)?x=1:x=2;", "b=true,x=1"); |
| 167 |
fold("(b=false)?x=1:x=2;", "b=false,x=2"); |
| 168 |
fold("if (b=/ab/) x=1;", "b=/ab/;x=1"); |
| 169 |
fold("var b;b=/ab/;(b)?x=1:x=2;", "var b;b=/ab/;x=1"); |
| 170 |
foldSame("var b;b=f();(b)?x=1:x=2;"); |
| 171 |
fold("var b=/ab/;(b)?x=1:x=2;", "var b=/ab/;x=1"); |
| 172 |
foldSame("var b=f();(b)?x=1:x=2;"); |
| 173 |
} |
| 174 |
|
| 175 |
public void testVarLifting() { |
| 176 |
fold("if(true)var a", "var a"); |
| 177 |
fold("if(false)var a", "var a"); |
| 178 |
|
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
public void testFoldUselessWhile() { |
| 183 |
fold("while(false) { foo() }", ""); |
| 184 |
|
| 185 |
fold("while(void 0) { foo() }", ""); |
| 186 |
fold("while(undefined) { foo() }", ""); |
| 187 |
|
| 188 |
foldSame("while(true) foo()"); |
| 189 |
|
| 190 |
fold("while(false) { var a = 0; }", "var a"); |
| 191 |
|
| 192 |
|
| 193 |
fold("while(false) { foo(); continue }", ""); |
| 194 |
|
| 195 |
fold("while(0) { foo() }", ""); |
| 196 |
} |
| 197 |
|
| 198 |
public void testFoldUselessFor() { |
| 199 |
fold("for(;false;) { foo() }", ""); |
| 200 |
fold("for(;void 0;) { foo() }", ""); |
| 201 |
fold("for(;undefined;) { foo() }", ""); |
| 202 |
fold("for(;true;) foo() ", "for(;;) foo() "); |
| 203 |
foldSame("for(;;) foo()"); |
| 204 |
fold("for(;false;) { var a = 0; }", "var a"); |
| 205 |
|
| 206 |
|
| 207 |
fold("for(;false;) { foo(); continue }", ""); |
| 208 |
} |
| 209 |
|
| 210 |
public void testFoldUselessDo() { |
| 211 |
fold("do { foo() } while(false);", "foo()"); |
| 212 |
fold("do { foo() } while(void 0);", "foo()"); |
| 213 |
fold("do { foo() } while(undefined);", "foo()"); |
| 214 |
fold("do { foo() } while(true);", "do { foo() } while(true);"); |
| 215 |
fold("do { var a = 0; } while(false);", "var a=0"); |
| 216 |
|
| 217 |
fold("do { var a = 0; } while(!{a:foo()});", "var a=0;foo()"); |
| 218 |
|
| 219 |
|
| 220 |
foldSame("do { foo(); continue; } while(0)"); |
| 221 |
foldSame("do { foo(); break; } while(0)"); |
| 222 |
} |
| 223 |
|
| 224 |
public void testMinimizeWhileConstantCondition() { |
| 225 |
fold("while(true) foo()", "while(true) foo()"); |
| 226 |
fold("while(0) foo()", ""); |
| 227 |
fold("while(0.0) foo()", ""); |
| 228 |
fold("while(NaN) foo()", ""); |
| 229 |
fold("while(null) foo()", ""); |
| 230 |
fold("while(undefined) foo()", ""); |
| 231 |
fold("while('') foo()", ""); |
| 232 |
} |
| 233 |
|
| 234 |
public void testFoldConstantCommaExpressions() { |
| 235 |
fold("if (true, false) {foo()}", ""); |
| 236 |
fold("if (false, true) {foo()}", "foo()"); |
| 237 |
fold("true, foo()", "foo()"); |
| 238 |
fold("(1 + 2 + ''), foo()", "foo()"); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
public void testRemoveUselessOps() { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
fold("Math.random()", ""); |
| 251 |
fold("Math.random(f() + g())", "f(),g();"); |
| 252 |
fold("Math.random(f(),g(),h())", "f(),g(),h();"); |
| 253 |
|
| 254 |
|
| 255 |
foldSame("f();"); |
| 256 |
foldSame("(function () { f(); })();"); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
fold("(function () {})();", ""); |
| 261 |
|
| 262 |
|
| 263 |
fold("(function () {});", ""); |
| 264 |
fold("(function f() {});", ""); |
| 265 |
|
| 266 |
fold("(function () {foo();});", ""); |
| 267 |
|
| 268 |
|
| 269 |
fold("+f()", "f()"); |
| 270 |
fold("a=(+f(),g())", "a=(f(),g())"); |
| 271 |
fold("a=(true,g())", "a=g()"); |
| 272 |
fold("f(),true", "f()"); |
| 273 |
fold("f() + g()", "f(),g()"); |
| 274 |
|
| 275 |
fold("for(;;+f()){}", "for(;;f()){}"); |
| 276 |
fold("for(+f();;g()){}", "for(f();;g()){}"); |
| 277 |
fold("for(;;Math.random(f(),g(),h())){}", "for(;;f(),g(),h()){}"); |
| 278 |
|
| 279 |
|
| 280 |
fold("g() && +f()", "g() && f()"); |
| 281 |
fold("g() || +f()", "g() || f()"); |
| 282 |
fold("x ? g() : +f()", "x ? g() : f()"); |
| 283 |
|
| 284 |
fold("+x()", "x()"); |
| 285 |
fold("+x() * 2", "x()"); |
| 286 |
fold("-(+x() * 2)", "x()"); |
| 287 |
fold("2 -(+x() * 2)", "x()"); |
| 288 |
fold("x().foo", "x()"); |
| 289 |
foldSame("x().foo()"); |
| 290 |
|
| 291 |
foldSame("x++"); |
| 292 |
foldSame("++x"); |
| 293 |
foldSame("x--"); |
| 294 |
foldSame("--x"); |
| 295 |
foldSame("x = 2"); |
| 296 |
foldSame("x *= 2"); |
| 297 |
|
| 298 |
|
| 299 |
foldSame("function f() {}"); |
| 300 |
foldSame("var x;"); |
| 301 |
} |
| 302 |
|
| 303 |
public void testOptimizeSwitch() { |
| 304 |
fold("switch(a){}", ""); |
| 305 |
fold("switch(foo()){}", "foo()"); |
| 306 |
fold("switch(a){default:}", ""); |
| 307 |
fold("switch(a){default:break;}", ""); |
| 308 |
fold("switch(a){default:var b;break;}", "var b"); |
| 309 |
fold("switch(a){case 1: default:}", ""); |
| 310 |
fold("switch(a){default: case 1:}", ""); |
| 311 |
fold("switch(a){default: break; case 1:break;}", ""); |
| 312 |
fold("switch(a){default: var b; break; case 1: var c; break;}", |
| 313 |
"var c; var b;"); |
| 314 |
|
| 315 |
|
| 316 |
foldSame("function f() {switch(a){default: return; case 1: break;}}"); |
| 317 |
foldSame("function f() {switch(a){case 1: foo();}}"); |
| 318 |
foldSame("function f() {switch(a){case 3: case 2: case 1: foo();}}"); |
| 319 |
|
| 320 |
fold("function f() {switch(a){case 2: case 1: default: foo();}}", |
| 321 |
"function f() {switch(a){default: foo();}}"); |
| 322 |
fold("switch(a){case 1: default:break; case 2: foo()}", |
| 323 |
"switch(a){case 2: foo()}"); |
| 324 |
foldSame("switch(a){case 1: goo(); default:break; case 2: foo()}"); |
| 325 |
|
| 326 |
|
| 327 |
foldSame("switch(a){case 1: goo(); case 2:break; case 3: foo()}"); |
| 328 |
|
| 329 |
|
| 330 |
fold("switch(1){case 2: var x=0;}", "var x;"); |
| 331 |
fold("switch ('repeated') {\n" + |
| 332 |
"case 'repeated':\n" + |
| 333 |
" foo();\n" + |
| 334 |
" break;\n" + |
| 335 |
"case 'repeated':\n" + |
| 336 |
" var x=0;\n" + |
| 337 |
" break;\n" + |
| 338 |
"}", |
| 339 |
"var x; {foo();}"); |
| 340 |
|
| 341 |
|
| 342 |
foldSame("switch(a){case 1: var c =2; break;}"); |
| 343 |
foldSame("function f() {switch(a){case 1: return;}}"); |
| 344 |
foldSame("x:switch(a){case 1: break x;}"); |
| 345 |
|
| 346 |
fold("switch ('foo') {\n" + |
| 347 |
"case 'foo':\n" + |
| 348 |
" foo();\n" + |
| 349 |
" break;\n" + |
| 350 |
"case 'bar':\n" + |
| 351 |
" bar();\n" + |
| 352 |
" break;\n" + |
| 353 |
"}", |
| 354 |
"{foo();}"); |
| 355 |
fold("switch ('noMatch') {\n" + |
| 356 |
"case 'foo':\n" + |
| 357 |
" foo();\n" + |
| 358 |
" break;\n" + |
| 359 |
"case 'bar':\n" + |
| 360 |
" bar();\n" + |
| 361 |
" break;\n" + |
| 362 |
"}", |
| 363 |
""); |
| 364 |
foldSame("switch ('fallThru') {\n" + |
| 365 |
"case 'fallThru':\n" + |
| 366 |
" if (foo(123) > 0) {\n" + |
| 367 |
" foobar(1);\n" + |
| 368 |
" break;\n" + |
| 369 |
" }\n" + |
| 370 |
" foobar(2);\n" + |
| 371 |
"case 'bar':\n" + |
| 372 |
" bar();\n" + |
| 373 |
"}"); |
| 374 |
foldSame("switch ('fallThru') {\n" + |
| 375 |
"case 'fallThru':\n" + |
| 376 |
" foo();\n" + |
| 377 |
"case 'bar':\n" + |
| 378 |
" bar();\n" + |
| 379 |
"}"); |
| 380 |
foldSame("switch ('hasDefaultCase') {\n" + |
| 381 |
"case 'foo':\n" + |
| 382 |
" foo();\n" + |
| 383 |
" break;\n" + |
| 384 |
"default:\n" + |
| 385 |
" bar();\n" + |
| 386 |
" break;\n" + |
| 387 |
"}"); |
| 388 |
fold("switch ('repeated') {\n" + |
| 389 |
"case 'repeated':\n" + |
| 390 |
" foo();\n" + |
| 391 |
" break;\n" + |
| 392 |
"case 'repeated':\n" + |
| 393 |
" bar();\n" + |
| 394 |
" break;\n" + |
| 395 |
"}", |
| 396 |
"{foo();}"); |
| 397 |
fold("switch ('foo') {\n" + |
| 398 |
"case 'bar':\n" + |
| 399 |
" bar();\n" + |
| 400 |
" break;\n" + |
| 401 |
"case notConstant:\n" + |
| 402 |
" foobar();\n" + |
| 403 |
" break;\n" + |
| 404 |
"case 'foo':\n" + |
| 405 |
" foo();\n" + |
| 406 |
" break;\n" + |
| 407 |
"}", |
| 408 |
"switch ('foo') {\n" + |
| 409 |
"case notConstant:\n" + |
| 410 |
" foobar();\n" + |
| 411 |
" break;\n" + |
| 412 |
"case 'foo':\n" + |
| 413 |
" foo();\n" + |
| 414 |
" break;\n" + |
| 415 |
"}"); |
| 416 |
fold("switch (1) {\n" + |
| 417 |
"case 1:\n" + |
| 418 |
" foo();\n" + |
| 419 |
" break;\n" + |
| 420 |
"case 2:\n" + |
| 421 |
" bar();\n" + |
| 422 |
" break;\n" + |
| 423 |
"}", |
| 424 |
"{foo();}"); |
| 425 |
fold("switch (1) {\n" + |
| 426 |
"case 1.1:\n" + |
| 427 |
" foo();\n" + |
| 428 |
" break;\n" + |
| 429 |
"case 2:\n" + |
| 430 |
" bar();\n" + |
| 431 |
" break;\n" + |
| 432 |
"}", |
| 433 |
""); |
| 434 |
foldSame("switch (0) {\n" + |
| 435 |
"case NaN:\n" + |
| 436 |
" foobar();\n" + |
| 437 |
" break;\n" + |
| 438 |
"case -0.0:\n" + |
| 439 |
" foo();\n" + |
| 440 |
" break;\n" + |
| 441 |
"case 2:\n" + |
| 442 |
" bar();\n" + |
| 443 |
" break;\n" + |
| 444 |
"}"); |
| 445 |
foldSame("switch ('\\v') {\n" + |
| 446 |
"case '\\u000B':\n" + |
| 447 |
" foo();\n" + |
| 448 |
"}"); |
| 449 |
foldSame("switch ('empty') {\n" + |
| 450 |
"case 'empty':\n" + |
| 451 |
"case 'foo':\n" + |
| 452 |
" foo();\n" + |
| 453 |
"}"); |
| 454 |
} |
| 455 |
|
| 456 |
public void testRemoveNumber() { |
| 457 |
test("3", ""); |
| 458 |
} |
| 459 |
|
| 460 |
public void testRemoveVarGet1() { |
| 461 |
test("a", ""); |
| 462 |
} |
| 463 |
|
| 464 |
public void testRemoveVarGet2() { |
| 465 |
test("var a = 1;a", "var a = 1"); |
| 466 |
} |
| 467 |
|
| 468 |
public void testRemoveNamespaceGet1() { |
| 469 |
test("var a = {};a.b", "var a = {}"); |
| 470 |
} |
| 471 |
|
| 472 |
public void testRemoveNamespaceGet2() { |
| 473 |
test("var a = {};a.b=1;a.b", "var a = {};a.b=1"); |
| 474 |
} |
| 475 |
|
| 476 |
public void testRemovePrototypeGet1() { |
| 477 |
test("var a = {};a.prototype.b", "var a = {}"); |
| 478 |
} |
| 479 |
|
| 480 |
public void testRemovePrototypeGet2() { |
| 481 |
test("var a = {};a.prototype.b = 1;a.prototype.b", |
| 482 |
"var a = {};a.prototype.b = 1"); |
| 483 |
} |
| 484 |
|
| 485 |
public void testRemoveAdd1() { |
| 486 |
test("1 + 2", ""); |
| 487 |
} |
| 488 |
|
| 489 |
public void testNoRemoveVar1() { |
| 490 |
testSame("var a = 1"); |
| 491 |
} |
| 492 |
|
| 493 |
public void testNoRemoveVar2() { |
| 494 |
testSame("var a = 1, b = 2"); |
| 495 |
} |
| 496 |
|
| 497 |
public void testNoRemoveAssign1() { |
| 498 |
testSame("a = 1"); |
| 499 |
} |
| 500 |
|
| 501 |
public void testNoRemoveAssign2() { |
| 502 |
testSame("a = b = 1"); |
| 503 |
} |
| 504 |
|
| 505 |
public void testNoRemoveAssign3() { |
| 506 |
test("1 + (a = 2)", "a = 2"); |
| 507 |
} |
| 508 |
|
| 509 |
public void testNoRemoveAssign4() { |
| 510 |
testSame("x.a = 1"); |
| 511 |
} |
| 512 |
|
| 513 |
public void testNoRemoveAssign5() { |
| 514 |
testSame("x.a = x.b = 1"); |
| 515 |
} |
| 516 |
|
| 517 |
public void testNoRemoveAssign6() { |
| 518 |
test("1 + (x.a = 2)", "x.a = 2"); |
| 519 |
} |
| 520 |
|
| 521 |
public void testNoRemoveCall1() { |
| 522 |
testSame("a()"); |
| 523 |
} |
| 524 |
|
| 525 |
public void testNoRemoveCall2() { |
| 526 |
test("a()+b()", "a(),b()"); |
| 527 |
} |
| 528 |
|
| 529 |
public void testNoRemoveCall3() { |
| 530 |
testSame("a() && b()"); |
| 531 |
} |
| 532 |
|
| 533 |
public void testNoRemoveCall4() { |
| 534 |
testSame("a() || b()"); |
| 535 |
} |
| 536 |
|
| 537 |
public void testNoRemoveCall5() { |
| 538 |
test("a() || 1", "a()"); |
| 539 |
} |
| 540 |
|
| 541 |
public void testNoRemoveCall6() { |
| 542 |
testSame("1 || a()"); |
| 543 |
} |
| 544 |
|
| 545 |
public void testNoRemoveThrow1() { |
| 546 |
testSame("function f(){throw a()}"); |
| 547 |
} |
| 548 |
|
| 549 |
public void testNoRemoveThrow2() { |
| 550 |
testSame("function f(){throw a}"); |
| 551 |
} |
| 552 |
|
| 553 |
public void testNoRemoveThrow3() { |
| 554 |
testSame("function f(){throw 10}"); |
| 555 |
} |
| 556 |
|
| 557 |
public void testRemoveInControlStructure1() { |
| 558 |
test("if(x()) 1", "x()"); |
| 559 |
} |
| 560 |
|
| 561 |
public void testRemoveInControlStructure2() { |
| 562 |
test("while(2) 1", "while(2);"); |
| 563 |
} |
| 564 |
|
| 565 |
public void testRemoveInControlStructure3() { |
| 566 |
test("for(1;2;3) 4", "for(;;);"); |
| 567 |
} |
| 568 |
|
| 569 |
public void testHook1() { |
| 570 |
test("1 ? 2 : 3", ""); |
| 571 |
} |
| 572 |
|
| 573 |
public void testHook2() { |
| 574 |
test("x ? a() : 3", "x && a()"); |
| 575 |
} |
| 576 |
|
| 577 |
public void testHook3() { |
| 578 |
test("x ? 2 : a()", "x || a()"); |
| 579 |
} |
| 580 |
|
| 581 |
public void testHook4() { |
| 582 |
testSame("x ? a() : b()"); |
| 583 |
} |
| 584 |
|
| 585 |
public void testHook5() { |
| 586 |
test("a() ? 1 : 2", "a()"); |
| 587 |
} |
| 588 |
|
| 589 |
public void testHook6() { |
| 590 |
test("a() ? b() : 2", "a() && b()"); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public void testHook7() { |
| 597 |
test("a() ? 1 : b()", "a() || b()"); |
| 598 |
} |
| 599 |
|
| 600 |
public void testHook8() { |
| 601 |
testSame("a() ? b() : c()"); |
| 602 |
} |
| 603 |
|
| 604 |
public void testShortCircuit1() { |
| 605 |
testSame("1 && a()"); |
| 606 |
} |
| 607 |
|
| 608 |
public void testShortCircuit2() { |
| 609 |
test("1 && a() && 2", "1 && a()"); |
| 610 |
} |
| 611 |
|
| 612 |
public void testShortCircuit3() { |
| 613 |
test("a() && 1 && 2", "a()"); |
| 614 |
} |
| 615 |
|
| 616 |
public void testShortCircuit4() { |
| 617 |
testSame("a() && 1 && b()"); |
| 618 |
} |
| 619 |
|
| 620 |
public void testComplex1() { |
| 621 |
test("1 && a() + b() + c()", "1 && (a(), b(), c())"); |
| 622 |
} |
| 623 |
|
| 624 |
public void testComplex2() { |
| 625 |
test("1 && (a() ? b() : 1)", "1 && a() && b()"); |
| 626 |
} |
| 627 |
|
| 628 |
public void testComplex3() { |
| 629 |
test("1 && (a() ? b() : 1 + c())", "1 && (a() ? b() : c())"); |
| 630 |
} |
| 631 |
|
| 632 |
public void testComplex4() { |
| 633 |
test("1 && (a() ? 1 : 1 + c())", "1 && (a() || c())"); |
| 634 |
} |
| 635 |
|
| 636 |
public void testComplex5() { |
| 637 |
|
| 638 |
testSame("(a() ? 1 : 1 + c()) && foo()"); |
| 639 |
} |
| 640 |
|
| 641 |
public void testNoRemoveFunctionDeclaration1() { |
| 642 |
testSame("function foo(){}"); |
| 643 |
} |
| 644 |
|
| 645 |
public void testNoRemoveFunctionDeclaration2() { |
| 646 |
testSame("var foo = function (){}"); |
| 647 |
} |
| 648 |
|
| 649 |
public void testNoSimplifyFunctionArgs1() { |
| 650 |
testSame("f(1 + 2, 3 + g())"); |
| 651 |
} |
| 652 |
|
| 653 |
public void testNoSimplifyFunctionArgs2() { |
| 654 |
testSame("1 && f(1 + 2, 3 + g())"); |
| 655 |
} |
| 656 |
|
| 657 |
public void testNoSimplifyFunctionArgs3() { |
| 658 |
testSame("1 && foo(a() ? b() : 1 + c())"); |
| 659 |
} |
| 660 |
|
| 661 |
public void testNoRemoveInherits1() { |
| 662 |
testSame("var a = {}; this.b = {}; var goog = {}; goog.inherits(b, a)"); |
| 663 |
} |
| 664 |
|
| 665 |
public void testNoRemoveInherits2() { |
| 666 |
test("var a = {}; this.b = {}; var goog = {}; goog.inherits(b, a) + 1", |
| 667 |
"var a = {}; this.b = {}; var goog = {}; goog.inherits(b, a)"); |
| 668 |
} |
| 669 |
|
| 670 |
public void testNoRemoveInherits3() { |
| 671 |
testSame("this.a = {}; var b = {}; b.inherits(a);"); |
| 672 |
} |
| 673 |
|
| 674 |
public void testNoRemoveInherits4() { |
| 675 |
test("this.a = {}; var b = {}; b.inherits(a) + 1;", |
| 676 |
"this.a = {}; var b = {}; b.inherits(a)"); |
| 677 |
} |
| 678 |
|
| 679 |
public void testRemoveFromLabel1() { |
| 680 |
test("LBL: void 0", "LBL: {}"); |
| 681 |
} |
| 682 |
|
| 683 |
public void testRemoveFromLabel2() { |
| 684 |
test("LBL: foo() + 1 + bar()", "LBL: foo(),bar()"); |
| 685 |
} |
| 686 |
|
| 687 |
public void testCall1() { |
| 688 |
test("Math.sin(0);", ""); |
| 689 |
} |
| 690 |
|
| 691 |
public void testCall2() { |
| 692 |
test("1 + Math.sin(0);", ""); |
| 693 |
} |
| 694 |
|
| 695 |
public void testNew1() { |
| 696 |
test("new Date;", ""); |
| 697 |
} |
| 698 |
|
| 699 |
public void testNew2() { |
| 700 |
test("1 + new Date;", ""); |
| 701 |
} |
| 702 |
|
| 703 |
public void testFoldAssign() { |
| 704 |
test("x=x", ""); |
| 705 |
testSame("x=xy"); |
| 706 |
testSame("x=x + 1"); |
| 707 |
testSame("x.a=x.a"); |
| 708 |
test("var y=(x=x)", "var y=x"); |
| 709 |
test("y=1 + (x=x)", "y=1 + x"); |
| 710 |
} |
| 711 |
|
| 712 |
public void testTryCatchFinally() { |
| 713 |
testSame("try {foo()} catch (e) {bar()}"); |
| 714 |
testSame("try { try {foo()} catch (e) {bar()}} catch (x) {bar()}"); |
| 715 |
test("try {var x = 1} finally {}", "var x = 1;"); |
| 716 |
testSame("try {var x = 1} finally {x()}"); |
| 717 |
test("function f() { return; try{var x = 1}finally{} }", |
| 718 |
"function f() { return; var x = 1; }"); |
| 719 |
test("try {} finally {x()}", "x()"); |
| 720 |
test("try {} catch (e) { bar()} finally {x()}", "x()"); |
| 721 |
test("try {} catch (e) { bar()}", ""); |
| 722 |
test("try {} catch (e) { var a = 0; } finally {x()}", "var a; x()"); |
| 723 |
test("try {} catch (e) {}", ""); |
| 724 |
test("try {} finally {}", ""); |
| 725 |
test("try {} catch (e) {} finally {}", ""); |
| 726 |
} |
| 727 |
|
| 728 |
public void testObjectLiteral() { |
| 729 |
test("({})", ""); |
| 730 |
test("({a:1})", ""); |
| 731 |
test("({a:foo()})", "foo()"); |
| 732 |
test("({'a':foo()})", "foo()"); |
| 733 |
} |
| 734 |
|
| 735 |
public void testArrayLiteral() { |
| 736 |
test("([])", ""); |
| 737 |
test("([1])", ""); |
| 738 |
test("([a])", ""); |
| 739 |
test("([foo()])", "foo()"); |
| 740 |
} |
| 741 |
} |