| 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 |
public class RemoveUnusedVarsTest extends CompilerTestCase { |
| 21 |
|
| 22 |
private boolean removeGlobal; |
| 23 |
private boolean preserveFunctionExpressionNames; |
| 24 |
private boolean modifyCallSites; |
| 25 |
|
| 26 |
public RemoveUnusedVarsTest() { |
| 27 |
super("function alert() {}"); |
| 28 |
enableNormalize(); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public void setUp() { |
| 33 |
removeGlobal = true; |
| 34 |
preserveFunctionExpressionNames = false; |
| 35 |
modifyCallSites = false; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 40 |
return new RemoveUnusedVars( |
| 41 |
compiler, removeGlobal, preserveFunctionExpressionNames, |
| 42 |
modifyCallSites); |
| 43 |
} |
| 44 |
|
| 45 |
public void testRemoveUnusedVars() { |
| 46 |
|
| 47 |
test("var a;var b=3;var c=function(){};var x=A();var y; var z;" + |
| 48 |
"function A(){B()} function B(){C(b)} function C(){} " + |
| 49 |
"function X(){Y()} function Y(z){Z(x)} function Z(){y} " + |
| 50 |
"P=function(){A()}; " + |
| 51 |
"try{0}catch(e){a}", |
| 52 |
|
| 53 |
"var a;var b=3;A();function A(){B()}" + |
| 54 |
"function B(){C(b)}" + |
| 55 |
"function C(){}" + |
| 56 |
"P=function(){A()}" + |
| 57 |
";try{0}catch(e){a}"); |
| 58 |
|
| 59 |
|
| 60 |
test("var i=0;var j=0;if(i>0){var k=1;}", |
| 61 |
"var i=0;if(i>0);"); |
| 62 |
|
| 63 |
|
| 64 |
test("for (var i in booyah) {" + |
| 65 |
" if (i > 0) x += ', ';" + |
| 66 |
" var arg = 'foo';" + |
| 67 |
" if (arg.length > 40) {" + |
| 68 |
" var unused = 'bar';" + |
| 69 |
" arg = arg.substr(0, 40) + '...';" + |
| 70 |
" }" + |
| 71 |
" x += arg;" + |
| 72 |
"}", |
| 73 |
|
| 74 |
"for(var i in booyah){if(i>0)x+=\", \";" + |
| 75 |
"var arg=\"foo\";if(arg.length>40)arg=arg.substr(0,40)+\"...\";" + |
| 76 |
"x+=arg}"); |
| 77 |
|
| 78 |
|
| 79 |
test("function A(){}" + |
| 80 |
"if(0){function B(){}}win.setTimeout(function(){A()})", |
| 81 |
"function A(){}" + |
| 82 |
"if(0);win.setTimeout(function(){A()})"); |
| 83 |
|
| 84 |
|
| 85 |
test("function A(){A()}function B(){B()}B()", |
| 86 |
"function B(){B()}B()"); |
| 87 |
|
| 88 |
|
| 89 |
test("var x,y=2,z=3;A(x);B(z);var a,b,c=4;C()", |
| 90 |
"var x,z=3;A(x);B(z);C()"); |
| 91 |
|
| 92 |
|
| 93 |
test("for(var i=0,j=0;i<10;){}" + |
| 94 |
"for(var x=0,y=0;;y++){}" + |
| 95 |
"for(var a,b;;){a}" + |
| 96 |
"for(var c,d;;);" + |
| 97 |
"for(var item in items){}", |
| 98 |
|
| 99 |
"for(var i=0;i<10;);" + |
| 100 |
"for(var y=0;;y++);" + |
| 101 |
"for(var a;;)a;" + |
| 102 |
"for(;;);" + |
| 103 |
"for(var item in items);"); |
| 104 |
|
| 105 |
|
| 106 |
test("var a,b,c,d;var e=[b,c];var x=e[3];var f=[d];print(f[0])", |
| 107 |
"var d;var f=[d];print(f[0])"); |
| 108 |
|
| 109 |
|
| 110 |
test("var x;function A(){var x;B()}function B(){print(x)}A()", |
| 111 |
"var x;function A(){B()}function B(){print(x)}A()"); |
| 112 |
|
| 113 |
|
| 114 |
test("function A(){var x;return function(){print(x)}}A()", |
| 115 |
"function A(){var x;return function(){print(x)}}A()"); |
| 116 |
|
| 117 |
|
| 118 |
test("function A(){}function B(){" + |
| 119 |
"var c,d,e,f,g,h;" + |
| 120 |
"function C(){print(c)}" + |
| 121 |
"var handler=function(){print(d)};" + |
| 122 |
"var handler2=function(){handler()};" + |
| 123 |
"e=function(){print(e)};" + |
| 124 |
"if(1){function G(){print(g)}}" + |
| 125 |
"arr=[function(){print(h)}];" + |
| 126 |
"return function(){print(f)}}B()", |
| 127 |
|
| 128 |
"function B(){" + |
| 129 |
"var f,h;" + |
| 130 |
"if(1);" + |
| 131 |
"arr=[function(){print(h)}];" + |
| 132 |
"return function(){print(f)}}B()"); |
| 133 |
|
| 134 |
|
| 135 |
test("var a,b=1; function _A1() {this.foo(a)}", |
| 136 |
"var a;function _A1(){this.foo(a)}"); |
| 137 |
|
| 138 |
|
| 139 |
test("undefinedVar = 1", "undefinedVar=1"); |
| 140 |
|
| 141 |
|
| 142 |
test("var a,b=foo(),c=i++,d;var e=boo();var f;print(d);", |
| 143 |
"foo(); i++; var d; boo(); print(d)"); |
| 144 |
|
| 145 |
test("var a,b=foo()", "foo()"); |
| 146 |
test("var b=foo(),a", "foo()"); |
| 147 |
test("var a,b=foo(a)", "var a; foo(a);"); |
| 148 |
} |
| 149 |
|
| 150 |
public void testFunctionArgRemoval() { |
| 151 |
|
| 152 |
test("var b=function(c,d){return};b(1,2)", |
| 153 |
"var b=function(){return};b(1,2)"); |
| 154 |
|
| 155 |
|
| 156 |
testSame("var b=function(c,d){return c+d};b(1,2)"); |
| 157 |
testSame("var b=function(e,f,c,d){return c+d};b(1,2)"); |
| 158 |
|
| 159 |
|
| 160 |
test("var b=function(c,d,e,f){return c+d};b(1,2)", |
| 161 |
"var b=function(c,d){return c+d};b(1,2)"); |
| 162 |
test("var b=function(e,c,f,d,g){return c+d};b(1,2)", |
| 163 |
"var b=function(e,c,f,d){return c+d};b(1,2)"); |
| 164 |
} |
| 165 |
|
| 166 |
public void testFunctionArgRemovalFromCallSites() { |
| 167 |
this.modifyCallSites = true; |
| 168 |
|
| 169 |
|
| 170 |
test("var b=function(c,d){return};b(1,2)", |
| 171 |
"var b=function(){return};b()"); |
| 172 |
|
| 173 |
|
| 174 |
testSame("var b=function(c,d){return c+d};b(1,2)"); |
| 175 |
test("var b=function(e,f,c,d){return c+d};b(1,2)", |
| 176 |
"var b=function(c,d){return c+d};b()"); |
| 177 |
|
| 178 |
|
| 179 |
test("var b=function(c,d,e,f){return c+d};b(1,2)", |
| 180 |
"var b=function(c,d){return c+d};b(1,2)"); |
| 181 |
test("var b=function(e,c,f,d,g){return c+d};b(1,2)", |
| 182 |
"var b=function(c,d){return c+d};b(2)"); |
| 183 |
} |
| 184 |
|
| 185 |
public void testFunctionsDeadButEscaped() { |
| 186 |
testSame("function b(a) { a = 1; print(arguments[0]) }; b(6)"); |
| 187 |
testSame("function b(a) { a = 1; arguments=1; }; b(6)"); |
| 188 |
testSame("function b(a) { var c = 2; a = c; print(arguments[0]) }; b(6)"); |
| 189 |
} |
| 190 |
|
| 191 |
public void testVarInControlStructure() { |
| 192 |
test("if (true) var b = 3;", "if(true);"); |
| 193 |
test("if (true) var b = 3; else var c = 5;", "if(true);else;"); |
| 194 |
test("while (true) var b = 3;", "while(true);"); |
| 195 |
test("for (;;) var b = 3;", "for(;;);"); |
| 196 |
test("do var b = 3; while(true)", "do;while(true)"); |
| 197 |
test("with (true) var b = 3;", "with(true);"); |
| 198 |
test("f: var b = 3;","f:{}"); |
| 199 |
} |
| 200 |
|
| 201 |
public void testRValueHoisting() { |
| 202 |
test("var x = foo();", "foo()"); |
| 203 |
test("var x = {a: foo()};", "({a:foo()})"); |
| 204 |
|
| 205 |
test("var x=function y(){}", ""); |
| 206 |
} |
| 207 |
|
| 208 |
public void testModule() { |
| 209 |
test(createModules( |
| 210 |
"var unreferenced=1; function x() { foo(); }" + |
| 211 |
"function uncalled() { var x; return 2; }", |
| 212 |
"var a,b; function foo() { this.foo(a); } x()"), |
| 213 |
new String[] { |
| 214 |
"function x(){foo()}", |
| 215 |
"var a;function foo(){this.foo(a)}x()" |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
public void testRecursiveFunction1() { |
| 220 |
testSame("(function x(){return x()})()"); |
| 221 |
} |
| 222 |
|
| 223 |
public void testRecursiveFunction2() { |
| 224 |
test("var x = 3; (function x() { return x(); })();", |
| 225 |
"(function x$$1(){return x$$1()})()"); |
| 226 |
} |
| 227 |
|
| 228 |
public void testFunctionWithName1() { |
| 229 |
test("var x=function f(){};x()", |
| 230 |
"var x=function(){};x()"); |
| 231 |
|
| 232 |
preserveFunctionExpressionNames = true; |
| 233 |
testSame("var x=function f(){};x()"); |
| 234 |
} |
| 235 |
|
| 236 |
public void testFunctionWithName2() { |
| 237 |
test("foo(function bar(){})", |
| 238 |
"foo(function(){})"); |
| 239 |
|
| 240 |
preserveFunctionExpressionNames = true; |
| 241 |
testSame("foo(function bar(){})"); |
| 242 |
} |
| 243 |
|
| 244 |
public void testRemoveGlobal1() { |
| 245 |
removeGlobal = false; |
| 246 |
testSame("var x=1"); |
| 247 |
test("var y=function(x){var z;}", "var y=function(x){}"); |
| 248 |
} |
| 249 |
|
| 250 |
public void testRemoveGlobal2() { |
| 251 |
removeGlobal = false; |
| 252 |
testSame("var x=1"); |
| 253 |
test("function y(x){var z;}", "function y(x){}"); |
| 254 |
} |
| 255 |
|
| 256 |
public void testRemoveGlobal3() { |
| 257 |
removeGlobal = false; |
| 258 |
testSame("var x=1"); |
| 259 |
test("function x(){function y(x){var z;}y()}", |
| 260 |
"function x(){function y(x){}y()}"); |
| 261 |
} |
| 262 |
|
| 263 |
public void testRemoveGlobal4() { |
| 264 |
removeGlobal = false; |
| 265 |
testSame("var x=1"); |
| 266 |
test("function x(){function y(x){var z;}}", |
| 267 |
"function x(){}"); |
| 268 |
} |
| 269 |
|
| 270 |
public void testIssue168a() { |
| 271 |
test("function _a(){" + |
| 272 |
" (function(x){ _b(); })(1);" + |
| 273 |
"}" + |
| 274 |
"function _b(){" + |
| 275 |
" _a();" + |
| 276 |
"}", |
| 277 |
"function _a(){(function(){_b()})(1)}" + |
| 278 |
"function _b(){_a()}"); |
| 279 |
} |
| 280 |
|
| 281 |
public void testIssue168b() { |
| 282 |
removeGlobal = false; |
| 283 |
test("function a(){" + |
| 284 |
" (function(x){ b(); })(1);" + |
| 285 |
"}" + |
| 286 |
"function b(){" + |
| 287 |
" a();" + |
| 288 |
"}", |
| 289 |
"function a(){(function(x){b()})(1)}" + |
| 290 |
"function b(){a()}"); |
| 291 |
} |
| 292 |
|
| 293 |
public void testUnusedAssign1() { |
| 294 |
test("var x = 3; x = 5;", ""); |
| 295 |
} |
| 296 |
|
| 297 |
public void testUnusedAssign2() { |
| 298 |
test("function f(a) { a = 3; } this.x = f;", |
| 299 |
"function f(){} this.x=f"); |
| 300 |
} |
| 301 |
|
| 302 |
public void testUnusedAssign3() { |
| 303 |
|
| 304 |
|
| 305 |
test("try { throw ''; } catch (e) { e = 3; }", |
| 306 |
"try{throw\"\";}catch(e){e=3}"); |
| 307 |
} |
| 308 |
|
| 309 |
public void testUnusedAssign4() { |
| 310 |
test("function f(a, b) { this.foo(b); a = 3; } this.x = f;", |
| 311 |
"function f(a,b){this.foo(b);}this.x=f"); |
| 312 |
} |
| 313 |
|
| 314 |
public void testUnusedAssign5() { |
| 315 |
test("var z = function f() { f = 3; }; z();", |
| 316 |
"var z=function(){};z()"); |
| 317 |
} |
| 318 |
|
| 319 |
public void testUnusedAssign5b() { |
| 320 |
test("var z = function f() { f = alert(); }; z();", |
| 321 |
"var z=function(){alert()};z()"); |
| 322 |
} |
| 323 |
|
| 324 |
public void testUnusedAssign6() { |
| 325 |
test("var z; z = 3;", ""); |
| 326 |
} |
| 327 |
|
| 328 |
public void testUnusedAssign6b() { |
| 329 |
test("var z; z = alert();", "alert()"); |
| 330 |
} |
| 331 |
|
| 332 |
public void testUnusedAssign7() { |
| 333 |
|
| 334 |
test("var a = 3; for (var i in {}) { i = a; }", |
| 335 |
|
| 336 |
"var a = 3; var i; for (i in {}) {i = a;}"); |
| 337 |
} |
| 338 |
|
| 339 |
public void testUnusedAssign8() { |
| 340 |
|
| 341 |
test("var a = 3; for (var i in {}) { i = a; } alert(a);", |
| 342 |
|
| 343 |
"var a = 3; var i; for (i in {}) {i = a} alert(a);"); |
| 344 |
} |
| 345 |
|
| 346 |
public void testUnusedPropAssign1() { |
| 347 |
test("var x = {}; x.foo = 3;", ""); |
| 348 |
} |
| 349 |
|
| 350 |
public void testUnusedPropAssign1b() { |
| 351 |
test("var x = {}; x.foo = alert();", "alert()"); |
| 352 |
} |
| 353 |
|
| 354 |
public void testUnusedPropAssign2() { |
| 355 |
test("var x = {}; x['foo'] = 3;", ""); |
| 356 |
} |
| 357 |
|
| 358 |
public void testUnusedPropAssign2b() { |
| 359 |
test("var x = {}; x[alert()] = alert();", "alert(),alert()"); |
| 360 |
} |
| 361 |
|
| 362 |
public void testUnusedPropAssign3() { |
| 363 |
test("var x = {}; x['foo'] = {}; x['bar'] = 3", ""); |
| 364 |
} |
| 365 |
|
| 366 |
public void testUnusedPropAssign3b() { |
| 367 |
test("var x = {}; x[alert()] = alert(); x[alert() + alert()] = alert()", |
| 368 |
"alert(),alert();(alert() + alert()),alert()"); |
| 369 |
} |
| 370 |
|
| 371 |
public void testUnusedPropAssign4() { |
| 372 |
test("var x = {foo: 3}; x['foo'] = 5;", ""); |
| 373 |
} |
| 374 |
|
| 375 |
public void testUnusedPropAssign5() { |
| 376 |
test("var x = {foo: bar()}; x['foo'] = 5;", |
| 377 |
"var x={foo:bar()};x[\"foo\"]=5"); |
| 378 |
} |
| 379 |
|
| 380 |
public void testUnusedPropAssign6() { |
| 381 |
test("var x = function() {}; x.prototype.bar = function() {};", ""); |
| 382 |
} |
| 383 |
|
| 384 |
public void testUnusedPropAssign7() { |
| 385 |
test("var x = {}; x[x.foo] = x.bar;", ""); |
| 386 |
} |
| 387 |
|
| 388 |
public void testUnusedPropAssign7b() { |
| 389 |
testSame("var x = {}; x[x.foo] = alert(x.bar);"); |
| 390 |
} |
| 391 |
|
| 392 |
public void testUnusedPropAssign7c() { |
| 393 |
test("var x = {}; x[alert(x.foo)] = x.bar;", |
| 394 |
"var x={};x[alert(x.foo)]=x.bar"); |
| 395 |
} |
| 396 |
|
| 397 |
public void testUsedPropAssign1() { |
| 398 |
test("function f(x) { x.bar = 3; } f({});", |
| 399 |
"function f(x){x.bar=3}f({})"); |
| 400 |
} |
| 401 |
|
| 402 |
public void testUsedPropAssign2() { |
| 403 |
test("try { throw z; } catch (e) { e.bar = 3; }", |
| 404 |
"try{throw z;}catch(e){e.bar=3}"); |
| 405 |
} |
| 406 |
|
| 407 |
public void testUsedPropAssign3() { |
| 408 |
|
| 409 |
test("var x = {}; x.foo = 3; x = bar();", |
| 410 |
"var x={};x.foo=3;x=bar()"); |
| 411 |
} |
| 412 |
|
| 413 |
public void testUsedPropAssign4() { |
| 414 |
test("var y = foo(); var x = {}; x.foo = 3; y[x.foo] = 5;", |
| 415 |
"var y=foo();var x={};x.foo=3;y[x.foo]=5"); |
| 416 |
} |
| 417 |
|
| 418 |
public void testUsedPropAssign5() { |
| 419 |
test("var y = foo(); var x = 3; y[x] = 5;", |
| 420 |
"var y=foo();var x=3;y[x]=5"); |
| 421 |
} |
| 422 |
|
| 423 |
public void testUsedPropAssign6() { |
| 424 |
test("var x = newNodeInDom(doc); x.innerHTML = 'new text';", |
| 425 |
"var x=newNodeInDom(doc);x.innerHTML=\"new text\""); |
| 426 |
} |
| 427 |
|
| 428 |
public void testUsedPropAssign7() { |
| 429 |
testSame("var x = {}; for (x in alert()) { x.foo = 3; }"); |
| 430 |
} |
| 431 |
|
| 432 |
public void testUsedPropAssign8() { |
| 433 |
testSame("for (var x in alert()) { x.foo = 3; }"); |
| 434 |
} |
| 435 |
|
| 436 |
public void testUsedPropAssign9() { |
| 437 |
testSame( |
| 438 |
"var x = {}; x.foo = newNodeInDom(doc); x.foo.innerHTML = 'new test';"); |
| 439 |
} |
| 440 |
|
| 441 |
public void testDependencies1() { |
| 442 |
test("var a = 3; var b = function() { alert(a); };", ""); |
| 443 |
} |
| 444 |
|
| 445 |
public void testDependencies1b() { |
| 446 |
test("var a = 3; var b = alert(function() { alert(a); });", |
| 447 |
"var a=3;alert(function(){alert(a)})"); |
| 448 |
} |
| 449 |
|
| 450 |
public void testDependencies1c() { |
| 451 |
test("var a = 3; var _b = function() { alert(a); };", |
| 452 |
"var a=3;var _b=function(){alert(a)}"); |
| 453 |
} |
| 454 |
|
| 455 |
public void testDependencies2() { |
| 456 |
test("var a = 3; var b = 3; b = function() { alert(a); };", ""); |
| 457 |
} |
| 458 |
|
| 459 |
public void testDependencies2b() { |
| 460 |
test("var a = 3; var b = 3; b = alert(function() { alert(a); });", |
| 461 |
"var a=3;alert(function(){alert(a)})"); |
| 462 |
} |
| 463 |
|
| 464 |
public void testDependencies2c() { |
| 465 |
testSame("var a=3;var _b=3;_b=function(){alert(a)}"); |
| 466 |
} |
| 467 |
|
| 468 |
public void testGlobalVarReferencesLocalVar() { |
| 469 |
testSame("var a=3;function f(){var b=4;a=b}alert(a + f())"); |
| 470 |
} |
| 471 |
|
| 472 |
public void testLocalVarReferencesGlobalVar1() { |
| 473 |
testSame("var a=3;function f(b, c){b=a; alert(b + c);} f();"); |
| 474 |
} |
| 475 |
|
| 476 |
public void testLocalVarReferencesGlobalVar2() { |
| 477 |
test("var a=3;function f(b, c){b=a; alert(c);} f();", |
| 478 |
"function f(b, c) { alert(c); } f();"); |
| 479 |
this.modifyCallSites = true; |
| 480 |
test("var a=3;function f(b, c){b=a; alert(c);} f();", |
| 481 |
"function f(c) { alert(c); } f();"); |
| 482 |
} |
| 483 |
|
| 484 |
public void testNestedAssign1() { |
| 485 |
test("var b = null; var a = (b = 3); alert(a);", |
| 486 |
"var a = 3; alert(a);"); |
| 487 |
} |
| 488 |
|
| 489 |
public void testNestedAssign2() { |
| 490 |
test("var a = 1; var b = 2; var c = (b = a); alert(c);", |
| 491 |
"var a = 1; var c = a; alert(c);"); |
| 492 |
} |
| 493 |
|
| 494 |
public void testNestedAssign3() { |
| 495 |
test("var b = 0; var z; z = z = b = 1; alert(b);", |
| 496 |
"var b = 0; b = 1; alert(b);"); |
| 497 |
} |
| 498 |
|
| 499 |
public void testCallSiteInteraction() { |
| 500 |
this.modifyCallSites = true; |
| 501 |
|
| 502 |
testSame("var b=function(){return};b()"); |
| 503 |
testSame("var b=function(c){return c};b(1)"); |
| 504 |
test("var b=function(c){};b.call(null, x)", |
| 505 |
"var b=function(){};b.call(null)"); |
| 506 |
test("var b=function(c){};b.apply(null, x)", |
| 507 |
"var b=function(){};b.apply(null, x)"); |
| 508 |
|
| 509 |
test("var b=function(c){return};b(1)", |
| 510 |
"var b=function(){return};b()"); |
| 511 |
test("var b=function(c){return};b(1,2)", |
| 512 |
"var b=function(){return};b()"); |
| 513 |
test("var b=function(c){return};b(1,2);b(3,4)", |
| 514 |
"var b=function(){return};b();b()"); |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
test("var b=function(c,d){return d};b(1,2);b(3,4);b.length", |
| 519 |
"var b=function(c,d){return d};b(0,2);b(0,4);b.length"); |
| 520 |
|
| 521 |
test("var b=function(c){return};b(1,2);b(3,new x())", |
| 522 |
"var b=function(){return};b();b(new x())"); |
| 523 |
|
| 524 |
test("var b=function(c){return};b(1,2);b(new x(),4)", |
| 525 |
"var b=function(){return};b();b(new x())"); |
| 526 |
|
| 527 |
test("var b=function(c,d){return d};b(1,2);b(new x(),4)", |
| 528 |
"var b=function(c,d){return d};b(0,2);b(new x(),4)"); |
| 529 |
test("var b=function(c,d,e){return d};b(1,2,3);b(new x(),4,new x())", |
| 530 |
"var b=function(c,d){return d};b(0,2);b(new x(),4,new x())"); |
| 531 |
|
| 532 |
|
| 533 |
test("var b=function(c,d){b(1,2);return d};b(3,4);b(5,6)", |
| 534 |
"var b=function(d){b(2);return d};b(4);b(6)"); |
| 535 |
|
| 536 |
testSame("var b=function(c){return arguments};b(1,2);b(3,4)"); |
| 537 |
|
| 538 |
|
| 539 |
test("var b=function(c,d){return};b(1,2)", |
| 540 |
"var b=function(){return};b()"); |
| 541 |
|
| 542 |
|
| 543 |
testSame("var b=function(c,d){return c+d};b(1,2)"); |
| 544 |
|
| 545 |
|
| 546 |
test("var b=function(e,f,c,d){return c+d};b(1,2)", |
| 547 |
"var b=function(c,d){return c+d};b()"); |
| 548 |
test("var b=function(c,d,e,f){return c+d};b(1,2)", |
| 549 |
"var b=function(c,d){return c+d};b(1,2)"); |
| 550 |
test("var b=function(e,c,f,d,g){return c+d};b(1,2)", |
| 551 |
"var b=function(c,d){return c+d};b(2)"); |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
test("var b=function(c,d){};var b=function(e,f){};b(1,2)", |
| 556 |
"var b=function(){};var b=function(){};b(1,2)"); |
| 557 |
} |
| 558 |
|
| 559 |
public void testCallSiteInteraction_contructors() { |
| 560 |
this.modifyCallSites = true; |
| 561 |
|
| 562 |
|
| 563 |
test("var Ctor1=function(a,b){return a};" + |
| 564 |
"var Ctor2=function(a,b){Ctor1.call(this,a,b)};" + |
| 565 |
"goog$inherits(Ctor2, Ctor1);" + |
| 566 |
"new Ctor2(1,2)", |
| 567 |
"var Ctor1=function(a){return a};" + |
| 568 |
"var Ctor2=function(a){Ctor1.call(this,a)};" + |
| 569 |
"goog$inherits(Ctor2, Ctor1);" + |
| 570 |
"new Ctor2(1)"); |
| 571 |
} |
| 572 |
|
| 573 |
public void testFunctionArgRemovalCausingInconsistency() { |
| 574 |
this.modifyCallSites = true; |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
test("var a=function(x,y){};" + |
| 579 |
"var b=function(z){};" + |
| 580 |
"a(new b, b)", |
| 581 |
"var a=function(){};" + |
| 582 |
"var b=function(){};" + |
| 583 |
"a(new b)"); |
| 584 |
} |
| 585 |
|
| 586 |
public void testRemoveUnusedVarsPossibleNpeCase() { |
| 587 |
this.modifyCallSites = true; |
| 588 |
test("var a = [];" + |
| 589 |
"var register = function(callback) {a[0] = callback};" + |
| 590 |
"register(function(transformer) {});" + |
| 591 |
"register(function(transformer) {});", |
| 592 |
"var register=function(){};register();register()"); |
| 593 |
} |
| 594 |
|
| 595 |
public void testDoNotOptimizeJSCompiler_renameProperty() { |
| 596 |
this.modifyCallSites = true; |
| 597 |
|
| 598 |
|
| 599 |
test("function JSCompiler_renameProperty(a) {};" + |
| 600 |
"JSCompiler_renameProperty('a');", |
| 601 |
"function JSCompiler_renameProperty() {};" + |
| 602 |
"JSCompiler_renameProperty('a');"); |
| 603 |
} |
| 604 |
|
| 605 |
public void testDoNotOptimizeJSCompiler_ObjectPropertyString() { |
| 606 |
this.modifyCallSites = true; |
| 607 |
test("function JSCompiler_ObjectPropertyString(a, b) {};" + |
| 608 |
"JSCompiler_ObjectPropertyString(window,'b');", |
| 609 |
"function JSCompiler_ObjectPropertyString() {};" + |
| 610 |
"JSCompiler_ObjectPropertyString(window,'b');"); |
| 611 |
} |
| 612 |
|
| 613 |
public void testDoNotOptimizeSetters() { |
| 614 |
testSame("({set s(a) {}})"); |
| 615 |
} |
| 616 |
|
| 617 |
public void testRemoveSingletonClass1() { |
| 618 |
test("function goog$addSingletonGetter(a){}" + |
| 619 |
"/**@constructor*/function a(){}" + |
| 620 |
"goog$addSingletonGetter(a);", |
| 621 |
""); |
| 622 |
} |
| 623 |
|
| 624 |
public void testRemoveInheritedClass1() { |
| 625 |
test("function goog$inherits(){}" + |
| 626 |
"/**@constructor*/function a(){}" + |
| 627 |
"/**@constructor*/function b(){}" + |
| 628 |
"goog$inherits(b,a); new a", |
| 629 |
"function a(){} new a"); |
| 630 |
} |
| 631 |
|
| 632 |
public void testRemoveInheritedClass2() { |
| 633 |
test("function goog$inherits(){}" + |
| 634 |
"function goog$mixin(){}" + |
| 635 |
"/**@constructor*/function a(){}" + |
| 636 |
"/**@constructor*/function b(){}" + |
| 637 |
"/**@constructor*/function c(){}" + |
| 638 |
"goog$inherits(b,a);" + |
| 639 |
"goog$mixin(c.prototype,b.prototype);", |
| 640 |
""); |
| 641 |
} |
| 642 |
|
| 643 |
public void testRemoveInheritedClass3() { |
| 644 |
testSame("/**@constructor*/function a(){}" + |
| 645 |
"/**@constructor*/function b(){}" + |
| 646 |
"goog$inherits(b,a); new b"); |
| 647 |
} |
| 648 |
|
| 649 |
public void testRemoveInheritedClass4() { |
| 650 |
testSame("function goog$inherits(){}" + |
| 651 |
"/**@constructor*/function a(){}" + |
| 652 |
"/**@constructor*/function b(){}" + |
| 653 |
"goog$inherits(b,a);" + |
| 654 |
"/**@constructor*/function c(){}" + |
| 655 |
"goog$inherits(c,b); new c"); |
| 656 |
} |
| 657 |
|
| 658 |
public void testRemoveInheritedClass5() { |
| 659 |
test("function goog$inherits(){}" + |
| 660 |
"/**@constructor*/function a(){}" + |
| 661 |
"/**@constructor*/function b(){}" + |
| 662 |
"goog$inherits(b,a);" + |
| 663 |
"/**@constructor*/function c(){}" + |
| 664 |
"goog$inherits(c,b); new b", |
| 665 |
"function goog$inherits(){}" + |
| 666 |
"function a(){}" + |
| 667 |
"function b(){}" + |
| 668 |
"goog$inherits(b,a); new b"); |
| 669 |
} |
| 670 |
|
| 671 |
public void testRemoveInheritedClass6() { |
| 672 |
test("function goog$mixin(){}" + |
| 673 |
"/**@constructor*/function a(){}" + |
| 674 |
"/**@constructor*/function b(){}" + |
| 675 |
"/**@constructor*/function c(){}" + |
| 676 |
"/**@constructor*/function d(){}" + |
| 677 |
"goog$mixin(b.prototype,a.prototype);" + |
| 678 |
"goog$mixin(c.prototype,a.prototype); new c;" + |
| 679 |
"goog$mixin(d.prototype,a.prototype)", |
| 680 |
"function goog$mixin(){}" + |
| 681 |
"function a(){}" + |
| 682 |
"function c(){}" + |
| 683 |
"goog$mixin(c.prototype,a.prototype); new c"); |
| 684 |
} |
| 685 |
|
| 686 |
public void testRemoveInheritedClass7() { |
| 687 |
test("function goog$mixin(){}" + |
| 688 |
"/**@constructor*/function a(){alert(goog$mixin(a, a))}" + |
| 689 |
"/**@constructor*/function b(){}" + |
| 690 |
"goog$mixin(b.prototype,a.prototype); new a", |
| 691 |
"function goog$mixin(){}" + |
| 692 |
"function a(){alert(goog$mixin(a, a))} new a"); |
| 693 |
} |
| 694 |
|
| 695 |
public void testRemoveInheritedClass8() { |
| 696 |
test("/**@constructor*/function a(){}" + |
| 697 |
"/**@constructor*/function b(){}" + |
| 698 |
"/**@constructor*/function c(){}" + |
| 699 |
"b.inherits(a);c.mixin(b.prototype)", |
| 700 |
""); |
| 701 |
} |
| 702 |
|
| 703 |
public void testRemoveInheritedClass9() { |
| 704 |
testSame("/**@constructor*/function a(){}" + |
| 705 |
"/**@constructor*/function b(){}" + |
| 706 |
"/**@constructor*/function c(){}" + |
| 707 |
"b.inherits(a);c.mixin(b.prototype);new c"); |
| 708 |
} |
| 709 |
|
| 710 |
public void testRemoveInheritedClass10() { |
| 711 |
test("function goog$inherits(){}" + |
| 712 |
"/**@constructor*/function a(){}" + |
| 713 |
"/**@constructor*/function b(){}" + |
| 714 |
"goog$inherits(b,a); new a;" + |
| 715 |
"var c = a; var d = a.g; new b", |
| 716 |
"function goog$inherits(){}" + |
| 717 |
"function a(){} function b(){} goog$inherits(b,a); new a; new b"); |
| 718 |
} |
| 719 |
|
| 720 |
public void testRemoveInheritedClass11() { |
| 721 |
testSame("function goog$inherits(){}" + |
| 722 |
"function goog$mixin(a,b){goog$inherits(a,b)}" + |
| 723 |
"/**@constructor*/function a(){}" + |
| 724 |
"/**@constructor*/function b(){}" + |
| 725 |
"goog$mixin(b.prototype,a.prototype);new b"); |
| 726 |
} |
| 727 |
|
| 728 |
public void testRemoveInheritedClass12() { |
| 729 |
testSame("function goog$inherits(){}" + |
| 730 |
"/**@constructor*/function a(){}" + |
| 731 |
"var b = {};" + |
| 732 |
"goog$inherits(b.foo, a)"); |
| 733 |
} |
| 734 |
|
| 735 |
public void testReflectedMethods() { |
| 736 |
this.modifyCallSites = true; |
| 737 |
testSame( |
| 738 |
"/** @constructor */" + |
| 739 |
"function Foo() {}" + |
| 740 |
"Foo.prototype.handle = function(x, y) { alert(y); };" + |
| 741 |
"var x = goog.reflect.object(Foo, {handle: 1});" + |
| 742 |
"for (var i in x) { x[i].call(x); }" + |
| 743 |
"window['Foo'] = Foo;"); |
| 744 |
} |
| 745 |
|
| 746 |
public void testIssue618_1() { |
| 747 |
this.removeGlobal = false; |
| 748 |
testSame( |
| 749 |
"function f() {\n" + |
| 750 |
" var a = [], b;\n" + |
| 751 |
" a.push(b = []);\n" + |
| 752 |
" b[0] = 1;\n" + |
| 753 |
" return a;\n" + |
| 754 |
"}"); |
| 755 |
} |
| 756 |
|
| 757 |
public void testIssue618_2() { |
| 758 |
this.removeGlobal = false; |
| 759 |
testSame( |
| 760 |
"var b;\n" + |
| 761 |
"a.push(b = []);\n" + |
| 762 |
"b[0] = 1;\n"); |
| 763 |
} |
| 764 |
} |