| 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.common.collect.ImmutableList; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.javascript.jscomp.CheckLevel; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 23 |
import static com.google.javascript.jscomp.PureFunctionIdentifier.INVALID_NO_SIDE_EFFECT_ANNOTATION; |
| 24 |
import com.google.javascript.rhino.Node; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class PureFunctionIdentifierTest extends CompilerTestCase { |
| 32 |
List<String> noSideEffectCalls = Lists.newArrayList(); |
| 33 |
List<String> localResultCalls = Lists.newArrayList(); |
| 34 |
|
| 35 |
boolean regExpHaveSideEffects = true; |
| 36 |
|
| 37 |
private static String kExterns = |
| 38 |
CompilerTypeTestCase.DEFAULT_EXTERNS + |
| 39 |
"var window; window.setTimeout;" + |
| 40 |
"/**@nosideeffects*/ function externSENone(){}\n" + |
| 41 |
|
| 42 |
"/**@modifies{this}*/ function externSEThis(){}\n" + |
| 43 |
|
| 44 |
"/**@constructor\n" + |
| 45 |
" * @modifies{this}*/\n" + |
| 46 |
"function externObjSEThis(){}\n" + |
| 47 |
|
| 48 |
"/**\n" + |
| 49 |
" * @param {string} s id.\n" + |
| 50 |
" * @return {string}\n" + |
| 51 |
" * @modifies{this}\n" + |
| 52 |
" */\n" + |
| 53 |
"externObjSEThis.prototype.externObjSEThisMethod = function(s) {};" + |
| 54 |
|
| 55 |
"/**\n" + |
| 56 |
" * @param {string} s id.\n" + |
| 57 |
" * @return {string}\n" + |
| 58 |
" * @modifies{arguments}\n" + |
| 59 |
" */\n" + |
| 60 |
"externObjSEThis.prototype.externObjSEThisMethod2 = function(s) {};" + |
| 61 |
|
| 62 |
"/**@nosideeffects*/function Error(){}" + |
| 63 |
|
| 64 |
"function externSef1(){}" + |
| 65 |
|
| 66 |
"/**@nosideeffects*/function externNsef1(){}" + |
| 67 |
|
| 68 |
"var externSef2 = function(){};" + |
| 69 |
|
| 70 |
"/**@nosideeffects*/var externNsef2 = function(){};" + |
| 71 |
|
| 72 |
"var externNsef3 = /**@nosideeffects*/function(){};" + |
| 73 |
|
| 74 |
"var externObj;" + |
| 75 |
|
| 76 |
"externObj.sef1 = function(){};" + |
| 77 |
|
| 78 |
"/**@nosideeffects*/externObj.nsef1 = function(){};" + |
| 79 |
|
| 80 |
"externObj.nsef2 = /**@nosideeffects*/function(){};" + |
| 81 |
|
| 82 |
"externObj.partialFn;" + |
| 83 |
|
| 84 |
"externObj.partialSharedFn;" + |
| 85 |
|
| 86 |
"var externObj2;" + |
| 87 |
|
| 88 |
"externObj2.partialSharedFn = /**@nosideeffects*/function(){};" + |
| 89 |
|
| 90 |
"/**@constructor*/function externSefConstructor(){}" + |
| 91 |
|
| 92 |
"externSefConstructor.prototype.sefFnOfSefObj = function(){};" + |
| 93 |
|
| 94 |
"externSefConstructor.prototype.nsefFnOfSefObj = " + |
| 95 |
" /**@nosideeffects*/function(){};" + |
| 96 |
|
| 97 |
"externSefConstructor.prototype.externShared = function(){};" + |
| 98 |
|
| 99 |
"/**@constructor\n@nosideeffects*/function externNsefConstructor(){}" + |
| 100 |
|
| 101 |
"externNsefConstructor.prototype.sefFnOfNsefObj = function(){};" + |
| 102 |
|
| 103 |
"externNsefConstructor.prototype.nsefFnOfNsefObj = " + |
| 104 |
" /**@nosideeffects*/function(){};" + |
| 105 |
|
| 106 |
"externNsefConstructor.prototype.externShared = " + |
| 107 |
" /**@nosideeffects*/function(){};" + |
| 108 |
|
| 109 |
"/**@constructor\n@nosideeffects*/function externNsefConstructor2(){}" + |
| 110 |
"externNsefConstructor2.prototype.externShared = " + |
| 111 |
" /**@nosideeffects*/function(){};" + |
| 112 |
|
| 113 |
"externNsefConstructor.prototype.sharedPartialSef;" + |
| 114 |
"/**@nosideeffects*/externNsefConstructor.prototype.sharedPartialNsef;" + |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
"/**@constructor*/function externObj3(){}" + |
| 119 |
|
| 120 |
"externObj3.prototype.propWithStubBefore;" + |
| 121 |
|
| 122 |
"/**\n" + |
| 123 |
" * @param {string} s id.\n" + |
| 124 |
" * @return {string}\n" + |
| 125 |
" * @nosideeffects\n" + |
| 126 |
" */\n" + |
| 127 |
"externObj3.prototype.propWithStubBefore = function(s) {};" + |
| 128 |
|
| 129 |
|
| 130 |
"/**\n" + |
| 131 |
" * @see {foo}\n" + |
| 132 |
" */\n" + |
| 133 |
"externObj3.prototype.propWithStubBeforeWithJSDoc;" + |
| 134 |
|
| 135 |
"/**\n" + |
| 136 |
" * @param {string} s id.\n" + |
| 137 |
" * @return {string}\n" + |
| 138 |
" * @nosideeffects\n" + |
| 139 |
" */\n" + |
| 140 |
"externObj3.prototype.propWithStubBeforeWithJSDoc = function(s) {};" + |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
"/**@constructor*/function externObj4(){}" + |
| 145 |
|
| 146 |
"/**\n" + |
| 147 |
" * @param {string} s id.\n" + |
| 148 |
" * @return {string}\n" + |
| 149 |
" * @nosideeffects\n" + |
| 150 |
" */\n" + |
| 151 |
"externObj4.prototype.propWithStubAfter = function(s) {};" + |
| 152 |
|
| 153 |
"externObj4.prototype.propWithStubAfter;" + |
| 154 |
|
| 155 |
"/**\n" + |
| 156 |
" * @param {string} s id.\n" + |
| 157 |
" * @return {string}\n" + |
| 158 |
" * @nosideeffects\n" + |
| 159 |
" */\n" + |
| 160 |
"externObj4.prototype.propWithStubAfterWithJSDoc = function(s) {};" + |
| 161 |
|
| 162 |
|
| 163 |
"/**\n" + |
| 164 |
" * @see {foo}\n" + |
| 165 |
" */\n" + |
| 166 |
"externObj4.prototype.propWithStubAfterWithJSDoc;"; |
| 167 |
|
| 168 |
public PureFunctionIdentifierTest() { |
| 169 |
super(kExterns); |
| 170 |
enableTypeCheck(CheckLevel.ERROR); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
protected int getNumRepetitions() { |
| 175 |
|
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
protected void tearDown() throws Exception { |
| 181 |
super.tearDown(); |
| 182 |
noSideEffectCalls.clear(); |
| 183 |
localResultCalls.clear(); |
| 184 |
regExpHaveSideEffects = true; |
| 185 |
} |
| 186 |
|
| 187 |
public void testIssue303() throws Exception { |
| 188 |
checkMarkedCalls( |
| 189 |
"/** @constructor */ function F() {" + |
| 190 |
" var self = this;" + |
| 191 |
" window.setTimeout(function() {" + |
| 192 |
" window.location = self.location;" + |
| 193 |
" }, 0);" + |
| 194 |
"}" + |
| 195 |
"F.prototype.setLocation = function(x) {" + |
| 196 |
" this.location = x;" + |
| 197 |
"};" + |
| 198 |
"(new F()).setLocation('http://www.google.com/');", |
| 199 |
ImmutableList.<String>of()); |
| 200 |
} |
| 201 |
|
| 202 |
public void testIssue303b() throws Exception { |
| 203 |
checkMarkedCalls( |
| 204 |
"/** @constructor */ function F() {" + |
| 205 |
" var self = this;" + |
| 206 |
" window.setTimeout(function() {" + |
| 207 |
" window.location = self.location;" + |
| 208 |
" }, 0);" + |
| 209 |
"}" + |
| 210 |
"F.prototype.setLocation = function(x) {" + |
| 211 |
" this.location = x;" + |
| 212 |
"};" + |
| 213 |
"function x() {" + |
| 214 |
" (new F()).setLocation('http://www.google.com/');" + |
| 215 |
"} window['x'] = x;", |
| 216 |
ImmutableList.<String>of()); |
| 217 |
} |
| 218 |
|
| 219 |
public void testAnnotationInExterns_new1() throws Exception { |
| 220 |
checkMarkedCalls("externSENone()", |
| 221 |
ImmutableList.<String>of("externSENone")); |
| 222 |
} |
| 223 |
|
| 224 |
public void testAnnotationInExterns_new2() throws Exception { |
| 225 |
checkMarkedCalls("externSEThis()", |
| 226 |
ImmutableList.<String>of()); |
| 227 |
} |
| 228 |
|
| 229 |
public void testAnnotationInExterns_new3() throws Exception { |
| 230 |
checkMarkedCalls("new externObjSEThis()", |
| 231 |
ImmutableList.<String>of("externObjSEThis")); |
| 232 |
} |
| 233 |
|
| 234 |
public void testAnnotationInExterns_new4() throws Exception { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
checkMarkedCalls("new externObjSEThis().externObjSEThisMethod('')", |
| 239 |
ImmutableList.<String>of( |
| 240 |
"externObjSEThis", "NEW STRING externObjSEThisMethod")); |
| 241 |
} |
| 242 |
|
| 243 |
public void testAnnotationInExterns_new5() throws Exception { |
| 244 |
checkMarkedCalls( |
| 245 |
"function f() { new externObjSEThis() };" + |
| 246 |
"f();", |
| 247 |
ImmutableList.<String>of("externObjSEThis", "f")); |
| 248 |
} |
| 249 |
|
| 250 |
public void testAnnotationInExterns_new6() throws Exception { |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
checkMarkedCalls( |
| 258 |
"function f() {" + |
| 259 |
" new externObjSEThis().externObjSEThisMethod('') " + |
| 260 |
"};" + |
| 261 |
"f();", |
| 262 |
ImmutableList.<String>of( |
| 263 |
"externObjSEThis", "NEW STRING externObjSEThisMethod")); |
| 264 |
} |
| 265 |
|
| 266 |
public void testAnnotationInExterns_new7() throws Exception { |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
checkMarkedCalls( |
| 271 |
"function f() {" + |
| 272 |
" var x = new externObjSEThis(); " + |
| 273 |
" x.externObjSEThisMethod('') " + |
| 274 |
"};" + |
| 275 |
"f();", |
| 276 |
ImmutableList.<String>of("externObjSEThis")); |
| 277 |
} |
| 278 |
|
| 279 |
public void testAnnotationInExterns_new8() throws Exception { |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
checkMarkedCalls( |
| 284 |
"function f(x) {" + |
| 285 |
" x.externObjSEThisMethod('') " + |
| 286 |
"};" + |
| 287 |
"f(new externObjSEThis());", |
| 288 |
ImmutableList.<String>of("externObjSEThis")); |
| 289 |
} |
| 290 |
|
| 291 |
public void testAnnotationInExterns_new9() throws Exception { |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
checkMarkedCalls( |
| 297 |
"function f(x) {" + |
| 298 |
" x = new externObjSEThis(); " + |
| 299 |
" x.externObjSEThisMethod('') " + |
| 300 |
"};" + |
| 301 |
"f(g);", |
| 302 |
ImmutableList.<String>of("externObjSEThis")); |
| 303 |
} |
| 304 |
|
| 305 |
public void testAnnotationInExterns_new10() throws Exception { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
checkMarkedCalls( |
| 311 |
"function f() {" + |
| 312 |
" new externObjSEThis().externObjSEThisMethod2('') " + |
| 313 |
"};" + |
| 314 |
"f();", |
| 315 |
ImmutableList.<String>of("externObjSEThis")); |
| 316 |
} |
| 317 |
|
| 318 |
public void testAnnotationInExterns1() throws Exception { |
| 319 |
checkMarkedCalls("externSef1()", ImmutableList.<String>of()); |
| 320 |
} |
| 321 |
|
| 322 |
public void testAnnotationInExterns2() throws Exception { |
| 323 |
checkMarkedCalls("externSef2()", ImmutableList.<String>of()); |
| 324 |
} |
| 325 |
|
| 326 |
public void testAnnotationInExterns3() throws Exception { |
| 327 |
checkMarkedCalls("externNsef1()", ImmutableList.of("externNsef1")); |
| 328 |
} |
| 329 |
|
| 330 |
public void testAnnotationInExterns4() throws Exception { |
| 331 |
checkMarkedCalls("externNsef2()", ImmutableList.of("externNsef2")); |
| 332 |
} |
| 333 |
|
| 334 |
public void testAnnotationInExterns5() throws Exception { |
| 335 |
checkMarkedCalls("externNsef3()", ImmutableList.of("externNsef3")); |
| 336 |
} |
| 337 |
|
| 338 |
public void testNamespaceAnnotationInExterns1() throws Exception { |
| 339 |
checkMarkedCalls("externObj.sef1()", ImmutableList.<String>of()); |
| 340 |
} |
| 341 |
|
| 342 |
public void testNamespaceAnnotationInExterns2() throws Exception { |
| 343 |
checkMarkedCalls("externObj.nsef1()", ImmutableList.of("externObj.nsef1")); |
| 344 |
} |
| 345 |
|
| 346 |
public void testNamespaceAnnotationInExterns3() throws Exception { |
| 347 |
checkMarkedCalls("externObj.nsef2()", ImmutableList.of("externObj.nsef2")); |
| 348 |
} |
| 349 |
|
| 350 |
public void testNamespaceAnnotationInExterns4() throws Exception { |
| 351 |
checkMarkedCalls("externObj.partialFn()", |
| 352 |
ImmutableList.<String>of()); |
| 353 |
} |
| 354 |
|
| 355 |
public void testNamespaceAnnotationInExterns5() throws Exception { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
String templateSrc = "var o = {}; o.<fnName> = function(){}; o.<fnName>()"; |
| 360 |
|
| 361 |
|
| 362 |
checkMarkedCalls(templateSrc.replaceAll("<fnName>", "notPartialFn"), |
| 363 |
ImmutableList.of("o.notPartialFn")); |
| 364 |
|
| 365 |
checkMarkedCalls(templateSrc.replaceAll("<fnName>", "partialFn"), |
| 366 |
ImmutableList.<String>of()); |
| 367 |
} |
| 368 |
|
| 369 |
public void testNamespaceAnnotationInExterns6() throws Exception { |
| 370 |
checkMarkedCalls("externObj.partialSharedFn()", |
| 371 |
ImmutableList.<String>of()); |
| 372 |
} |
| 373 |
|
| 374 |
public void testConstructorAnnotationInExterns1() throws Exception { |
| 375 |
checkMarkedCalls("new externSefConstructor()", |
| 376 |
ImmutableList.<String>of()); |
| 377 |
} |
| 378 |
|
| 379 |
public void testConstructorAnnotationInExterns2() throws Exception { |
| 380 |
checkMarkedCalls("var a = new externSefConstructor();" + |
| 381 |
"a.sefFnOfSefObj()", |
| 382 |
ImmutableList.<String>of()); |
| 383 |
} |
| 384 |
|
| 385 |
public void testConstructorAnnotationInExterns3() throws Exception { |
| 386 |
checkMarkedCalls("var a = new externSefConstructor();" + |
| 387 |
"a.nsefFnOfSefObj()", |
| 388 |
ImmutableList.of("a.nsefFnOfSefObj")); |
| 389 |
} |
| 390 |
|
| 391 |
public void testConstructorAnnotationInExterns4() throws Exception { |
| 392 |
checkMarkedCalls("var a = new externSefConstructor();" + |
| 393 |
"a.externShared()", |
| 394 |
ImmutableList.<String>of()); |
| 395 |
} |
| 396 |
|
| 397 |
public void testConstructorAnnotationInExterns5() throws Exception { |
| 398 |
checkMarkedCalls("new externNsefConstructor()", |
| 399 |
ImmutableList.of("externNsefConstructor")); |
| 400 |
} |
| 401 |
|
| 402 |
public void testConstructorAnnotationInExterns6() throws Exception { |
| 403 |
checkMarkedCalls("var a = new externNsefConstructor();" + |
| 404 |
"a.sefFnOfNsefObj()", |
| 405 |
ImmutableList.of("externNsefConstructor")); |
| 406 |
} |
| 407 |
|
| 408 |
public void testConstructorAnnotationInExterns7() throws Exception { |
| 409 |
checkMarkedCalls("var a = new externNsefConstructor();" + |
| 410 |
"a.nsefFnOfNsefObj()", |
| 411 |
ImmutableList.of("externNsefConstructor", |
| 412 |
"a.nsefFnOfNsefObj")); |
| 413 |
} |
| 414 |
|
| 415 |
public void testConstructorAnnotationInExterns8() throws Exception { |
| 416 |
checkMarkedCalls("var a = new externNsefConstructor();" + |
| 417 |
"a.externShared()", |
| 418 |
ImmutableList.of("externNsefConstructor")); |
| 419 |
} |
| 420 |
|
| 421 |
public void testSharedFunctionName1() throws Exception { |
| 422 |
checkMarkedCalls("var a; " + |
| 423 |
"if (true) {" + |
| 424 |
" a = new externNsefConstructor()" + |
| 425 |
"} else {" + |
| 426 |
" a = new externSefConstructor()" + |
| 427 |
"}" + |
| 428 |
"a.externShared()", |
| 429 |
ImmutableList.of("externNsefConstructor")); |
| 430 |
} |
| 431 |
|
| 432 |
public void testSharedFunctionName2() throws Exception { |
| 433 |
|
| 434 |
|
| 435 |
boolean broken = true; |
| 436 |
if (broken) { |
| 437 |
checkMarkedCalls("var a; " + |
| 438 |
"if (true) {" + |
| 439 |
" a = new externNsefConstructor()" + |
| 440 |
"} else {" + |
| 441 |
" a = new externNsefConstructor2()" + |
| 442 |
"}" + |
| 443 |
"a.externShared()", |
| 444 |
ImmutableList.of("externNsefConstructor", |
| 445 |
"externNsefConstructor2")); |
| 446 |
} else { |
| 447 |
checkMarkedCalls("var a; " + |
| 448 |
"if (true) {" + |
| 449 |
" a = new externNsefConstructor()" + |
| 450 |
"} else {" + |
| 451 |
" a = new externNsefConstructor2()" + |
| 452 |
"}" + |
| 453 |
"a.externShared()", |
| 454 |
ImmutableList.of("externNsefConstructor", |
| 455 |
"externNsefConstructor2", |
| 456 |
"a.externShared")); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
public void testAnnotationInExternStubs1() throws Exception { |
| 461 |
checkMarkedCalls("o.propWithStubBefore('a');", |
| 462 |
ImmutableList.<String>of("o.propWithStubBefore")); |
| 463 |
} |
| 464 |
|
| 465 |
public void testAnnotationInExternStubs1b() throws Exception { |
| 466 |
checkMarkedCalls("o.propWithStubBeforeWithJSDoc('a');", |
| 467 |
ImmutableList.<String>of("o.propWithStubBeforeWithJSDoc")); |
| 468 |
} |
| 469 |
|
| 470 |
public void testAnnotationInExternStubs2() throws Exception { |
| 471 |
checkMarkedCalls("o.propWithStubAfter('a');", |
| 472 |
ImmutableList.<String>of("o.propWithStubAfter")); |
| 473 |
} |
| 474 |
|
| 475 |
public void testAnnotationInExternStubs2b() throws Exception { |
| 476 |
checkMarkedCalls("o.propWithStubAfter('a');", |
| 477 |
ImmutableList.<String>of("o.propWithStubAfter")); |
| 478 |
} |
| 479 |
|
| 480 |
public void testAnnotationInExternStubs3() throws Exception { |
| 481 |
checkMarkedCalls("propWithAnnotatedStubAfter('a');", |
| 482 |
ImmutableList.<String>of()); |
| 483 |
} |
| 484 |
|
| 485 |
public void testAnnotationInExternStubs4() throws Exception { |
| 486 |
|
| 487 |
|
| 488 |
String externs = |
| 489 |
"/**@constructor*/function externObj5(){}\n" + |
| 490 |
|
| 491 |
"externObj5.prototype.propWithAnnotatedStubAfter = function(s) {};\n" + |
| 492 |
|
| 493 |
"/**\n" + |
| 494 |
" * @param {string} s id.\n" + |
| 495 |
" * @return {string}\n" + |
| 496 |
" * @nosideeffects\n" + |
| 497 |
" */\n" + |
| 498 |
"externObj5.prototype.propWithAnnotatedStubAfter;\n"; |
| 499 |
|
| 500 |
List<String> expected = ImmutableList.<String>of(); |
| 501 |
testSame(externs, |
| 502 |
"o.prototype.propWithAnnotatedStubAfter", |
| 503 |
TypeValidator.DUP_VAR_DECLARATION, false); |
| 504 |
assertEquals(expected, noSideEffectCalls); |
| 505 |
noSideEffectCalls.clear(); |
| 506 |
} |
| 507 |
|
| 508 |
public void testAnnotationInExternStubs5() throws Exception { |
| 509 |
|
| 510 |
|
| 511 |
String externs = |
| 512 |
"/**@constructor*/function externObj5(){}\n" + |
| 513 |
|
| 514 |
"/**\n" + |
| 515 |
" * @param {string} s id.\n" + |
| 516 |
" * @return {string}\n" + |
| 517 |
" * @nosideeffects\n" + |
| 518 |
" */\n" + |
| 519 |
"externObj5.prototype.propWithAnnotatedStubAfter = function(s) {};\n" + |
| 520 |
|
| 521 |
"/**\n" + |
| 522 |
" * @param {string} s id.\n" + |
| 523 |
" * @return {string}\n" + |
| 524 |
" */\n" + |
| 525 |
"externObj5.prototype.propWithAnnotatedStubAfter;\n"; |
| 526 |
|
| 527 |
List<String> expected = ImmutableList.<String>of(); |
| 528 |
testSame(externs, |
| 529 |
"o.prototype.propWithAnnotatedStubAfter", |
| 530 |
TypeValidator.DUP_VAR_DECLARATION, false); |
| 531 |
assertEquals(expected, noSideEffectCalls); |
| 532 |
noSideEffectCalls.clear(); |
| 533 |
} |
| 534 |
|
| 535 |
public void testNoSideEffectsSimple() throws Exception { |
| 536 |
String prefix = "function f(){"; |
| 537 |
String suffix = "} f()"; |
| 538 |
List<String> expected = ImmutableList.of("f"); |
| 539 |
|
| 540 |
checkMarkedCalls( |
| 541 |
prefix + "" + suffix, expected); |
| 542 |
checkMarkedCalls( |
| 543 |
prefix + "return 1" + suffix, expected); |
| 544 |
checkMarkedCalls( |
| 545 |
prefix + "return 1 + 2" + suffix, expected); |
| 546 |
|
| 547 |
|
| 548 |
checkMarkedCalls( |
| 549 |
prefix + "var a = 1; return a" + suffix, expected); |
| 550 |
|
| 551 |
|
| 552 |
checkMarkedCalls( |
| 553 |
prefix + "var a = 1; a = 2; return a" + suffix, expected); |
| 554 |
checkMarkedCalls( |
| 555 |
prefix + "var a = 1; a = 2; return a + 1" + suffix, expected); |
| 556 |
|
| 557 |
|
| 558 |
checkMarkedCalls( |
| 559 |
prefix + "var a = {foo : 1}; return a.foo" + suffix, expected); |
| 560 |
checkMarkedCalls( |
| 561 |
prefix + "var a = {foo : 1}; return a.foo + 1" + suffix, expected); |
| 562 |
|
| 563 |
|
| 564 |
checkMarkedCalls( |
| 565 |
prefix + "return externObj" + suffix, expected); |
| 566 |
checkMarkedCalls( |
| 567 |
"function g(x) { x.foo = 3; }" + |
| 568 |
prefix + "return externObj.foo" + suffix, expected); |
| 569 |
} |
| 570 |
|
| 571 |
public void testResultLocalitySimple() throws Exception { |
| 572 |
String prefix = "var g; function f(){"; |
| 573 |
String suffix = "} f()"; |
| 574 |
List<String> expected = ImmutableList.of("f"); |
| 575 |
List<String> notExpected = ImmutableList.of(); |
| 576 |
|
| 577 |
|
| 578 |
checkLocalityOfMarkedCalls( |
| 579 |
prefix + "" + suffix, expected); |
| 580 |
|
| 581 |
checkLocalityOfMarkedCalls( |
| 582 |
prefix + "return 1" + suffix, expected); |
| 583 |
checkLocalityOfMarkedCalls( |
| 584 |
prefix + "return 1 + 2" + suffix, expected); |
| 585 |
|
| 586 |
|
| 587 |
checkLocalityOfMarkedCalls( |
| 588 |
prefix + "return g" + suffix, notExpected); |
| 589 |
|
| 590 |
|
| 591 |
checkLocalityOfMarkedCalls( |
| 592 |
prefix + "return 1; return 2" + suffix, expected); |
| 593 |
checkLocalityOfMarkedCalls( |
| 594 |
prefix + "return 1; return g" + suffix, notExpected); |
| 595 |
|
| 596 |
|
| 597 |
checkLocalityOfMarkedCalls( |
| 598 |
prefix + "var a = 1; return a" + suffix, notExpected); |
| 599 |
|
| 600 |
|
| 601 |
checkLocalityOfMarkedCalls( |
| 602 |
prefix + "var a = 1; a = 2; return a" + suffix, notExpected); |
| 603 |
checkLocalityOfMarkedCalls( |
| 604 |
prefix + "var a = 1; a = 2; return a + 1" + suffix, expected); |
| 605 |
|
| 606 |
|
| 607 |
checkLocalityOfMarkedCalls( |
| 608 |
prefix + "return {foo : 1}.foo" + suffix, |
| 609 |
notExpected); |
| 610 |
checkLocalityOfMarkedCalls( |
| 611 |
prefix + "var a = {foo : 1}; return a.foo" + suffix, |
| 612 |
notExpected); |
| 613 |
|
| 614 |
|
| 615 |
checkLocalityOfMarkedCalls( |
| 616 |
prefix + "return externObj" + suffix, notExpected); |
| 617 |
checkLocalityOfMarkedCalls( |
| 618 |
"function inner(x) { x.foo = 3; }" + |
| 619 |
prefix + "return externObj.foo" + suffix, notExpected); |
| 620 |
} |
| 621 |
|
| 622 |
public void testExternCalls() throws Exception { |
| 623 |
String prefix = "function f(){"; |
| 624 |
String suffix = "} f()"; |
| 625 |
|
| 626 |
checkMarkedCalls(prefix + "externNsef1()" + suffix, |
| 627 |
ImmutableList.of("externNsef1", "f")); |
| 628 |
checkMarkedCalls(prefix + "externObj.nsef1()" + suffix, |
| 629 |
ImmutableList.of("externObj.nsef1", "f")); |
| 630 |
|
| 631 |
checkMarkedCalls(prefix + "externSef1()" + suffix, |
| 632 |
ImmutableList.<String>of()); |
| 633 |
checkMarkedCalls(prefix + "externObj.sef1()" + suffix, |
| 634 |
ImmutableList.<String>of()); |
| 635 |
} |
| 636 |
|
| 637 |
public void testApply() throws Exception { |
| 638 |
checkMarkedCalls("function f() {return 42}" + |
| 639 |
"f.apply()", |
| 640 |
ImmutableList.of("f.apply")); |
| 641 |
} |
| 642 |
|
| 643 |
public void testCall() throws Exception { |
| 644 |
checkMarkedCalls("function f() {return 42}" + |
| 645 |
"f.call()", |
| 646 |
ImmutableList.<String>of("f.call")); |
| 647 |
} |
| 648 |
|
| 649 |
public void testInference1() throws Exception { |
| 650 |
checkMarkedCalls("function f() {return g()}" + |
| 651 |
"function g() {return 42}" + |
| 652 |
"f()", |
| 653 |
ImmutableList.of("g", "f")); |
| 654 |
} |
| 655 |
|
| 656 |
public void testInference2() throws Exception { |
| 657 |
checkMarkedCalls("var a = 1;" + |
| 658 |
"function f() {g()}" + |
| 659 |
"function g() {a=2}" + |
| 660 |
"f()", |
| 661 |
ImmutableList.<String>of()); |
| 662 |
} |
| 663 |
|
| 664 |
public void testInference3() throws Exception { |
| 665 |
checkMarkedCalls("var f = function() {return g()};" + |
| 666 |
"var g = function() {return 42};" + |
| 667 |
"f()", |
| 668 |
ImmutableList.of("g", "f")); |
| 669 |
} |
| 670 |
|
| 671 |
public void testInference4() throws Exception { |
| 672 |
checkMarkedCalls("var a = 1;" + |
| 673 |
"var f = function() {g()};" + |
| 674 |
"var g = function() {a=2};" + |
| 675 |
"f()", |
| 676 |
ImmutableList.<String>of()); |
| 677 |
} |
| 678 |
|
| 679 |
public void testInference5() throws Exception { |
| 680 |
checkMarkedCalls("var goog = {};" + |
| 681 |
"goog.f = function() {return goog.g()};" + |
| 682 |
"goog.g = function() {return 42};" + |
| 683 |
"goog.f()", |
| 684 |
ImmutableList.of("goog.g", "goog.f")); |
| 685 |
} |
| 686 |
|
| 687 |
public void testInference6() throws Exception { |
| 688 |
checkMarkedCalls("var a = 1;" + |
| 689 |
"var goog = {};" + |
| 690 |
"goog.f = function() {goog.g()};" + |
| 691 |
"goog.g = function() {a=2};" + |
| 692 |
"goog.f()", |
| 693 |
ImmutableList.<String>of()); |
| 694 |
} |
| 695 |
|
| 696 |
public void testLocalizedSideEffects1() throws Exception { |
| 697 |
|
| 698 |
|
| 699 |
checkMarkedCalls("function f() {" + |
| 700 |
" var x = {foo : 0}; return function() {x.foo++};" + |
| 701 |
"}" + |
| 702 |
"f()", |
| 703 |
ImmutableList.<String>of("f")); |
| 704 |
} |
| 705 |
|
| 706 |
public void testLocalizedSideEffects2() throws Exception { |
| 707 |
|
| 708 |
|
| 709 |
checkMarkedCalls("function f() {" + |
| 710 |
" var x = {foo : 0}; (function() {x.foo++})();" + |
| 711 |
"}" + |
| 712 |
"f()", |
| 713 |
ImmutableList.<String>of()); |
| 714 |
} |
| 715 |
|
| 716 |
public void testLocalizedSideEffects3() throws Exception { |
| 717 |
|
| 718 |
|
| 719 |
checkMarkedCalls("var g = {foo:1}; function f() {var x = g; x.foo++}" + |
| 720 |
"f()", |
| 721 |
ImmutableList.<String>of()); |
| 722 |
} |
| 723 |
|
| 724 |
public void testLocalizedSideEffects4() throws Exception { |
| 725 |
|
| 726 |
|
| 727 |
checkMarkedCalls("function f() {var x = []; x[0] = 1;}" + |
| 728 |
"f()", |
| 729 |
ImmutableList.<String>of("f")); |
| 730 |
} |
| 731 |
|
| 732 |
public void testLocalizedSideEffects5() throws Exception { |
| 733 |
|
| 734 |
|
| 735 |
checkMarkedCalls("var g = [];function f() {var x = g; x[0] = 1;}" + |
| 736 |
"f()", |
| 737 |
ImmutableList.<String>of()); |
| 738 |
} |
| 739 |
|
| 740 |
public void testLocalizedSideEffects6() throws Exception { |
| 741 |
|
| 742 |
|
| 743 |
checkMarkedCalls("function f() {" + |
| 744 |
" var x = {}; x.foo = 1; return x;" + |
| 745 |
"}" + |
| 746 |
"f()", |
| 747 |
ImmutableList.<String>of("f")); |
| 748 |
} |
| 749 |
|
| 750 |
public void testLocalizedSideEffects7() throws Exception { |
| 751 |
|
| 752 |
|
| 753 |
checkMarkedCalls("/** @constructor A */ function A() {};" + |
| 754 |
"function f() {" + |
| 755 |
" var a = []; a[1] = 1; return a;" + |
| 756 |
"}" + |
| 757 |
"f()", |
| 758 |
ImmutableList.<String>of("f")); |
| 759 |
} |
| 760 |
|
| 761 |
public void testLocalizedSideEffects8() throws Exception { |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
checkMarkedCalls("/** @constructor A */ function A() {};" + |
| 766 |
"function f() {" + |
| 767 |
" var a = new A; a.foo = 1; return a;" + |
| 768 |
"}" + |
| 769 |
"f()", |
| 770 |
ImmutableList.<String>of("A")); |
| 771 |
} |
| 772 |
|
| 773 |
public void testLocalizedSideEffects9() throws Exception { |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
checkMarkedCalls("/** @constructor A */ function A() {this.x = 1};" + |
| 778 |
"function f() {" + |
| 779 |
" var a = new A; a.foo = 1; return a;" + |
| 780 |
"}" + |
| 781 |
"f()", |
| 782 |
ImmutableList.<String>of("A")); |
| 783 |
} |
| 784 |
|
| 785 |
public void testLocalizedSideEffects10() throws Exception { |
| 786 |
|
| 787 |
|
| 788 |
checkMarkedCalls("/** @constructor A */ function A() {};" + |
| 789 |
"A.prototype.g = function() {this.x = 1};" + |
| 790 |
"function f() {" + |
| 791 |
" var a = new A; a.g(); return a;" + |
| 792 |
"}" + |
| 793 |
"f()", |
| 794 |
ImmutableList.<String>of("A")); |
| 795 |
} |
| 796 |
|
| 797 |
public void testLocalizedSideEffects11() throws Exception { |
| 798 |
|
| 799 |
checkMarkedCalls( |
| 800 |
"/** @constructor */ function A() {}" + |
| 801 |
"A.prototype.update = function() { this.x = 1; };" + |
| 802 |
"/** @constructor */ function B() { " + |
| 803 |
" this.a_ = new A();" + |
| 804 |
"}" + |
| 805 |
"B.prototype.updateA = function() {" + |
| 806 |
" var b = this.a_;" + |
| 807 |
" b.update();" + |
| 808 |
"};" + |
| 809 |
"var x = new B();" + |
| 810 |
"x.updateA();", |
| 811 |
ImmutableList.of("A", "B")); |
| 812 |
} |
| 813 |
|
| 814 |
public void testUnaryOperators1() throws Exception { |
| 815 |
checkMarkedCalls("function f() {var x = 1; x++}" + |
| 816 |
"f()", |
| 817 |
ImmutableList.of("f")); |
| 818 |
} |
| 819 |
|
| 820 |
public void testUnaryOperators2() throws Exception { |
| 821 |
checkMarkedCalls("var x = 1;" + |
| 822 |
"function f() {x++}" + |
| 823 |
"f()", |
| 824 |
ImmutableList.<String>of()); |
| 825 |
} |
| 826 |
|
| 827 |
public void testUnaryOperators3() throws Exception { |
| 828 |
checkMarkedCalls("function f() {var x = {foo : 0}; x.foo++}" + |
| 829 |
"f()", |
| 830 |
ImmutableList.<String>of("f")); |
| 831 |
} |
| 832 |
|
| 833 |
public void testUnaryOperators4() throws Exception { |
| 834 |
checkMarkedCalls("var x = {foo : 0};" + |
| 835 |
"function f() {x.foo++}" + |
| 836 |
"f()", |
| 837 |
ImmutableList.<String>of()); |
| 838 |
} |
| 839 |
|
| 840 |
public void testUnaryOperators5() throws Exception { |
| 841 |
checkMarkedCalls("function f(x) {x.foo++}" + |
| 842 |
"f({foo : 0})", |
| 843 |
ImmutableList.<String>of()); |
| 844 |
} |
| 845 |
|
| 846 |
public void testDeleteOperator1() throws Exception { |
| 847 |
checkMarkedCalls("var x = {};" + |
| 848 |
"function f() {delete x}" + |
| 849 |
"f()", |
| 850 |
ImmutableList.<String>of()); |
| 851 |
} |
| 852 |
|
| 853 |
public void testDeleteOperator2() throws Exception { |
| 854 |
checkMarkedCalls("function f() {var x = {}; delete x}" + |
| 855 |
"f()", |
| 856 |
ImmutableList.of("f")); |
| 857 |
} |
| 858 |
|
| 859 |
public void testOrOperator1() throws Exception { |
| 860 |
checkMarkedCalls("var f = externNsef1 || externNsef2;\n" + |
| 861 |
"f()", |
| 862 |
ImmutableList.<String>of()); |
| 863 |
} |
| 864 |
|
| 865 |
public void testOrOperator2() throws Exception { |
| 866 |
checkMarkedCalls("var f = function(){} || externNsef2;\n" + |
| 867 |
"f()", |
| 868 |
ImmutableList.<String>of()); |
| 869 |
} |
| 870 |
|
| 871 |
public void testOrOperator3() throws Exception { |
| 872 |
checkMarkedCalls("var f = externNsef2 || function(){};\n" + |
| 873 |
"f()", |
| 874 |
ImmutableList.<String>of()); |
| 875 |
} |
| 876 |
|
| 877 |
public void testOrOperators4() throws Exception { |
| 878 |
checkMarkedCalls("var f = function(){} || function(){};\n" + |
| 879 |
"f()", |
| 880 |
ImmutableList.<String>of()); |
| 881 |
} |
| 882 |
|
| 883 |
public void testAndOperator1() throws Exception { |
| 884 |
checkMarkedCalls("var f = externNsef1 && externNsef2;\n" + |
| 885 |
"f()", |
| 886 |
ImmutableList.<String>of()); |
| 887 |
} |
| 888 |
|
| 889 |
public void testAndOperator2() throws Exception { |
| 890 |
checkMarkedCalls("var f = function(){} && externNsef2;\n" + |
| 891 |
"f()", |
| 892 |
ImmutableList.<String>of()); |
| 893 |
} |
| 894 |
|
| 895 |
public void testAndOperator3() throws Exception { |
| 896 |
checkMarkedCalls("var f = externNsef2 && function(){};\n" + |
| 897 |
"f()", |
| 898 |
ImmutableList.<String>of()); |
| 899 |
} |
| 900 |
|
| 901 |
public void testAndOperators4() throws Exception { |
| 902 |
checkMarkedCalls("var f = function(){} && function(){};\n" + |
| 903 |
"f()", |
| 904 |
ImmutableList.<String>of()); |
| 905 |
} |
| 906 |
|
| 907 |
public void testHookOperator1() throws Exception { |
| 908 |
checkMarkedCalls("var f = true ? externNsef1 : externNsef2;\n" + |
| 909 |
"f()", |
| 910 |
ImmutableList.<String>of()); |
| 911 |
} |
| 912 |
|
| 913 |
public void testHookOperator2() throws Exception { |
| 914 |
checkMarkedCalls("var f = true ? function(){} : externNsef2;\n" + |
| 915 |
"f()", |
| 916 |
ImmutableList.<String>of()); |
| 917 |
} |
| 918 |
|
| 919 |
public void testHookOperator3() throws Exception { |
| 920 |
checkMarkedCalls("var f = true ? externNsef2 : function(){};\n" + |
| 921 |
"f()", |
| 922 |
ImmutableList.<String>of()); |
| 923 |
} |
| 924 |
|
| 925 |
public void testHookOperators4() throws Exception { |
| 926 |
checkMarkedCalls("var f = true ? function(){} : function(){};\n" + |
| 927 |
"f()", |
| 928 |
ImmutableList.<String>of()); |
| 929 |
} |
| 930 |
|
| 931 |
public void testThrow1() throws Exception { |
| 932 |
checkMarkedCalls("function f(){throw Error()};\n" + |
| 933 |
"f()", |
| 934 |
ImmutableList.<String>of("Error")); |
| 935 |
} |
| 936 |
|
| 937 |
public void testThrow2() throws Exception { |
| 938 |
checkMarkedCalls("/**@constructor*/function A(){throw Error()};\n" + |
| 939 |
"function f(){return new A()}\n" + |
| 940 |
"f()", |
| 941 |
ImmutableList.<String>of("Error")); |
| 942 |
} |
| 943 |
|
| 944 |
public void testAssignmentOverride() throws Exception { |
| 945 |
checkMarkedCalls("/**@constructor*/function A(){}\n" + |
| 946 |
"A.prototype.foo = function(){};\n" + |
| 947 |
"var a = new A;\n" + |
| 948 |
"a.foo();\n", |
| 949 |
ImmutableList.<String>of("A", "a.foo")); |
| 950 |
|
| 951 |
checkMarkedCalls("/**@constructor*/function A(){}\n" + |
| 952 |
"A.prototype.foo = function(){};\n" + |
| 953 |
"var x = 1\n" + |
| 954 |
"function f(){x = 10}\n" + |
| 955 |
"var a = new A;\n" + |
| 956 |
"a.foo = f;\n" + |
| 957 |
"a.foo();\n", |
| 958 |
ImmutableList.<String>of("A")); |
| 959 |
} |
| 960 |
|
| 961 |
public void testInheritance1() throws Exception { |
| 962 |
String source = |
| 963 |
CompilerTypeTestCase.CLOSURE_DEFS + |
| 964 |
"/**@constructor*/function I(){}\n" + |
| 965 |
"I.prototype.foo = function(){};\n" + |
| 966 |
"I.prototype.bar = function(){this.foo()};\n" + |
| 967 |
"/**@constructor\n@extends {I}*/function A(){};\n" + |
| 968 |
"goog.inherits(A, I)\n;" + |
| 969 |
"/** @override */A.prototype.foo = function(){var data=24};\n" + |
| 970 |
"var i = new I();i.foo();i.bar();\n" + |
| 971 |
"var a = new A();a.foo();a.bar();"; |
| 972 |
|
| 973 |
checkMarkedCalls(source, |
| 974 |
ImmutableList.of("this.foo", "goog.inherits", |
| 975 |
"I", "i.foo", "i.bar", |
| 976 |
"A", "a.foo", "a.bar")); |
| 977 |
} |
| 978 |
|
| 979 |
public void testInheritance2() throws Exception { |
| 980 |
String source = |
| 981 |
CompilerTypeTestCase.CLOSURE_DEFS + |
| 982 |
"/**@constructor*/function I(){}\n" + |
| 983 |
"I.prototype.foo = function(){};\n" + |
| 984 |
"I.prototype.bar = function(){this.foo()};\n" + |
| 985 |
"/**@constructor\n@extends {I}*/function A(){};\n" + |
| 986 |
"goog.inherits(A, I)\n;" + |
| 987 |
"/** @override */A.prototype.foo = function(){this.data=24};\n" + |
| 988 |
"var i = new I();i.foo();i.bar();\n" + |
| 989 |
"var a = new A();a.foo();a.bar();"; |
| 990 |
|
| 991 |
checkMarkedCalls(source, ImmutableList.of("goog.inherits", "I", "A")); |
| 992 |
} |
| 993 |
|
| 994 |
public void testCallBeforeDefinition() throws Exception { |
| 995 |
checkMarkedCalls("f(); function f(){}", |
| 996 |
ImmutableList.of("f")); |
| 997 |
|
| 998 |
checkMarkedCalls("var a = {}; a.f(); a.f = function (){}", |
| 999 |
ImmutableList.of("a.f")); |
| 1000 |
} |
| 1001 |
|
| 1002 |
public void testConstructorThatModifiesThis1() throws Exception { |
| 1003 |
String source = "/**@constructor*/function A(){this.foo = 1}\n" + |
| 1004 |
"function f() {return new A}" + |
| 1005 |
"f()"; |
| 1006 |
|
| 1007 |
checkMarkedCalls(source, ImmutableList.of("A", "f")); |
| 1008 |
} |
| 1009 |
|
| 1010 |
public void testConstructorThatModifiesThis2() throws Exception { |
| 1011 |
String source = "/**@constructor*/function A(){this.foo()}\n" + |
| 1012 |
"A.prototype.foo = function(){this.data=24};\n" + |
| 1013 |
"function f() {return new A}" + |
| 1014 |
"f()"; |
| 1015 |
|
| 1016 |
checkMarkedCalls(source, ImmutableList.of("A", "f")); |
| 1017 |
} |
| 1018 |
|
| 1019 |
public void testConstructorThatModifiesThis3() throws Exception { |
| 1020 |
|
| 1021 |
|
| 1022 |
String source = "/**@constructor*/function A(){this.foo()}\n" + |
| 1023 |
"A.prototype.foo = function(){this.bar()};\n" + |
| 1024 |
"A.prototype.bar = function(){this.data=24};\n" + |
| 1025 |
"function f() {return new A}" + |
| 1026 |
"f()"; |
| 1027 |
|
| 1028 |
checkMarkedCalls(source, ImmutableList.of("A", "f")); |
| 1029 |
} |
| 1030 |
|
| 1031 |
public void testConstructorThatModifiesThis4() throws Exception { |
| 1032 |
|
| 1033 |
|
| 1034 |
String source = "/**@constructor*/function A(){foo.call(this)}\n" + |
| 1035 |
"function foo(){this.data=24};\n" + |
| 1036 |
"function f() {return new A}" + |
| 1037 |
"f()"; |
| 1038 |
|
| 1039 |
checkMarkedCalls(source, ImmutableList.of("A", "f")); |
| 1040 |
} |
| 1041 |
|
| 1042 |
public void testConstructorThatModifiesGlobal1() throws Exception { |
| 1043 |
String source = "var b = 0;" + |
| 1044 |
"/**@constructor*/function A(){b=1};\n" + |
| 1045 |
"function f() {return new A}" + |
| 1046 |
"f()"; |
| 1047 |
|
| 1048 |
checkMarkedCalls(source, ImmutableList.<String>of()); |
| 1049 |
} |
| 1050 |
|
| 1051 |
public void testConstructorThatModifiesGlobal2() throws Exception { |
| 1052 |
String source = "var b = 0;" + |
| 1053 |
"/**@constructor*/function A(){this.foo()}\n" + |
| 1054 |
"A.prototype.foo = function(){b=1};\n" + |
| 1055 |
"function f() {return new A}" + |
| 1056 |
"f()"; |
| 1057 |
|
| 1058 |
checkMarkedCalls(source, ImmutableList.<String>of()); |
| 1059 |
} |
| 1060 |
|
| 1061 |
public void testCallFunctionThatModifiesThis() throws Exception { |
| 1062 |
String source = "/**@constructor*/function A(){}\n" + |
| 1063 |
"A.prototype.foo = function(){this.data=24};\n" + |
| 1064 |
"function f(){var a = new A; return a}\n" + |
| 1065 |
"function g(){var a = new A; a.foo(); return a}\n" + |
| 1066 |
"f(); g()"; |
| 1067 |
|
| 1068 |
checkMarkedCalls(source, ImmutableList.<String>of("A", "A", "f")); |
| 1069 |
} |
| 1070 |
|
| 1071 |
public void testCallFunctionFOrG() throws Exception { |
| 1072 |
String source = "function f(){}\n" + |
| 1073 |
"function g(){}\n" + |
| 1074 |
"function h(){ (f || g)() }\n" + |
| 1075 |
"h()"; |
| 1076 |
|
| 1077 |
checkMarkedCalls(source, ImmutableList.<String>of("(f || g)", "h")); |
| 1078 |
} |
| 1079 |
|
| 1080 |
public void testCallFunctionFOrGViaHook() throws Exception { |
| 1081 |
String source = "function f(){}\n" + |
| 1082 |
"function g(){}\n" + |
| 1083 |
"function h(){ (false ? f : g)() }\n" + |
| 1084 |
"h()"; |
| 1085 |
|
| 1086 |
checkMarkedCalls(source, ImmutableList.<String>of("(f : g)", "h")); |
| 1087 |
} |
| 1088 |
|
| 1089 |
public void testCallFunctionForGorH() throws Exception { |
| 1090 |
String source = "function f(){}\n" + |
| 1091 |
"function g(){}\n" + |
| 1092 |
"function h(){}\n" + |
| 1093 |
"function i(){ (false ? f : (g || h))() }\n" + |
| 1094 |
"i()"; |
| 1095 |
|
| 1096 |
checkMarkedCalls(source, ImmutableList.<String>of("(f : (g || h))", "i")); |
| 1097 |
} |
| 1098 |
|
| 1099 |
public void testCallFunctionFOrGWithSideEffects() throws Exception { |
| 1100 |
String source = "var x = 0;\n" + |
| 1101 |
"function f(){x = 10}\n" + |
| 1102 |
"function g(){}\n" + |
| 1103 |
"function h(){ (f || g)() }\n" + |
| 1104 |
"function i(){ (g || f)() }\n" + |
| 1105 |
"function j(){ (f || f)() }\n" + |
| 1106 |
"function k(){ (g || g)() }\n" + |
| 1107 |
"h(); i(); j(); k()"; |
| 1108 |
|
| 1109 |
checkMarkedCalls(source, ImmutableList.<String>of("(g || g)", "k")); |
| 1110 |
} |
| 1111 |
|
| 1112 |
public void testCallFunctionFOrGViaHookWithSideEffects() throws Exception { |
| 1113 |
String source = "var x = 0;\n" + |
| 1114 |
"function f(){x = 10}\n" + |
| 1115 |
"function g(){}\n" + |
| 1116 |
"function h(){ (false ? f : g)() }\n" + |
| 1117 |
"function i(){ (false ? g : f)() }\n" + |
| 1118 |
"function j(){ (false ? f : f)() }\n" + |
| 1119 |
"function k(){ (false ? g : g)() }\n" + |
| 1120 |
"h(); i(); j(); k()"; |
| 1121 |
|
| 1122 |
checkMarkedCalls(source, ImmutableList.<String>of("(g : g)", "k")); |
| 1123 |
} |
| 1124 |
|
| 1125 |
public void testCallRegExpWithSideEffects() throws Exception { |
| 1126 |
String source = "var x = 0;\n" + |
| 1127 |
"function k(){(/a/).exec('')}\n" + |
| 1128 |
"k()"; |
| 1129 |
|
| 1130 |
regExpHaveSideEffects = true; |
| 1131 |
checkMarkedCalls(source, ImmutableList.<String>of()); |
| 1132 |
regExpHaveSideEffects = false; |
| 1133 |
checkMarkedCalls(source, ImmutableList.<String>of( |
| 1134 |
"REGEXP STRING exec", "k")); |
| 1135 |
} |
| 1136 |
|
| 1137 |
public void testAnonymousFunction1() throws Exception { |
| 1138 |
String source = "(function (){})();"; |
| 1139 |
|
| 1140 |
checkMarkedCalls(source, ImmutableList.<String>of( |
| 1141 |
"FUNCTION")); |
| 1142 |
} |
| 1143 |
|
| 1144 |
public void testAnonymousFunction2() throws Exception { |
| 1145 |
String source = "(Error || function (){})();"; |
| 1146 |
|
| 1147 |
checkMarkedCalls(source, ImmutableList.<String>of( |
| 1148 |
"(Error || FUNCTION)")); |
| 1149 |
} |
| 1150 |
|
| 1151 |
public void testAnonymousFunction3() throws Exception { |
| 1152 |
String source = "var a = (Error || function (){})();"; |
| 1153 |
|
| 1154 |
checkMarkedCalls(source, ImmutableList.<String>of( |
| 1155 |
"(Error || FUNCTION)")); |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
public void testAnonymousFunction4() throws Exception { |
| 1160 |
String source = "var a = (Error || function (){});" + |
| 1161 |
"a();"; |
| 1162 |
|
| 1163 |
|
| 1164 |
checkMarkedCalls(source, ImmutableList.<String>of()); |
| 1165 |
} |
| 1166 |
|
| 1167 |
public void testInvalidAnnotation1() throws Exception { |
| 1168 |
test("/** @nosideeffects */ function foo() {}", |
| 1169 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 1170 |
} |
| 1171 |
|
| 1172 |
public void testInvalidAnnotation2() throws Exception { |
| 1173 |
test("var f = /** @nosideeffects */ function() {}", |
| 1174 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 1175 |
} |
| 1176 |
|
| 1177 |
public void testInvalidAnnotation3() throws Exception { |
| 1178 |
test("/** @nosideeffects */ var f = function() {}", |
| 1179 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 1180 |
} |
| 1181 |
|
| 1182 |
public void testInvalidAnnotation4() throws Exception { |
| 1183 |
test("var f = function() {};" + |
| 1184 |
"/** @nosideeffects */ f.x = function() {}", |
| 1185 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 1186 |
} |
| 1187 |
|
| 1188 |
public void testInvalidAnnotation5() throws Exception { |
| 1189 |
test("var f = function() {};" + |
| 1190 |
"f.x = /** @nosideeffects */ function() {}", |
| 1191 |
null, INVALID_NO_SIDE_EFFECT_ANNOTATION); |
| 1192 |
} |
| 1193 |
|
| 1194 |
void checkMarkedCalls(String source, List<String> expected) { |
| 1195 |
testSame(source); |
| 1196 |
assertEquals(expected, noSideEffectCalls); |
| 1197 |
noSideEffectCalls.clear(); |
| 1198 |
} |
| 1199 |
|
| 1200 |
void checkLocalityOfMarkedCalls(String source, List<String> expected) { |
| 1201 |
testSame(source); |
| 1202 |
assertEquals(expected, localResultCalls); |
| 1203 |
localResultCalls.clear(); |
| 1204 |
} |
| 1205 |
|
| 1206 |
@Override |
| 1207 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 1208 |
return new NoSideEffectCallEnumerator(compiler); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
private class NoSideEffectCallEnumerator |
| 1216 |
extends AbstractPostOrderCallback implements CompilerPass { |
| 1217 |
private final Compiler compiler; |
| 1218 |
|
| 1219 |
NoSideEffectCallEnumerator(Compiler compiler) { |
| 1220 |
this.compiler = compiler; |
| 1221 |
} |
| 1222 |
|
| 1223 |
@Override |
| 1224 |
public void process(Node externs, Node root) { |
| 1225 |
compiler.setHasRegExpGlobalReferences(regExpHaveSideEffects); |
| 1226 |
SimpleDefinitionFinder defFinder = new SimpleDefinitionFinder(compiler); |
| 1227 |
defFinder.process(externs, root); |
| 1228 |
PureFunctionIdentifier passUnderTest = |
| 1229 |
new PureFunctionIdentifier(compiler, defFinder); |
| 1230 |
passUnderTest.process(externs, root); |
| 1231 |
|
| 1232 |
|
| 1233 |
passUnderTest.getDebugReport(); |
| 1234 |
|
| 1235 |
NodeTraversal.traverse(compiler, externs, this); |
| 1236 |
NodeTraversal.traverse(compiler, root, this); |
| 1237 |
} |
| 1238 |
|
| 1239 |
@Override |
| 1240 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 1241 |
if (n.isNew()) { |
| 1242 |
if (!NodeUtil.constructorCallHasSideEffects(n)) { |
| 1243 |
noSideEffectCalls.add(generateNameString(n.getFirstChild())); |
| 1244 |
} |
| 1245 |
} else if (n.isCall()) { |
| 1246 |
if (!NodeUtil.functionCallHasSideEffects(n)) { |
| 1247 |
noSideEffectCalls.add(generateNameString(n.getFirstChild())); |
| 1248 |
} |
| 1249 |
if (NodeUtil.callHasLocalResult(n)) { |
| 1250 |
localResultCalls.add(generateNameString(n.getFirstChild())); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
private String generateNameString(Node node) { |
| 1256 |
if (node.isOr()) { |
| 1257 |
return "(" + generateNameString(node.getFirstChild()) + |
| 1258 |
" || " + generateNameString(node.getLastChild()) + ")"; |
| 1259 |
} else if (node.isHook()) { |
| 1260 |
return "(" + generateNameString(node.getFirstChild().getNext()) + |
| 1261 |
" : " + generateNameString(node.getLastChild()) + ")"; |
| 1262 |
} else { |
| 1263 |
String result = node.getQualifiedName(); |
| 1264 |
if (result == null) { |
| 1265 |
if (node.isFunction()) { |
| 1266 |
result = node.toString(false, false, false).trim(); |
| 1267 |
} else { |
| 1268 |
result = node.getFirstChild().toString(false, false, false); |
| 1269 |
result += " " + node.getLastChild().toString(false, false, false); |
| 1270 |
} |
| 1271 |
} |
| 1272 |
return result; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
} |
| 1276 |
} |