| 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.Maps; |
| 20 |
|
| 21 |
import com.google.javascript.jscomp.CompilerOptions.LanguageMode; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
|
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class AmbiguatePropertiesTest extends CompilerTestCase { |
| 31 |
private AmbiguateProperties lastPass; |
| 32 |
|
| 33 |
private static final String EXTERNS = |
| 34 |
"Function.prototype.call=function(){};" + |
| 35 |
"Function.prototype.inherits=function(){};" + |
| 36 |
"prop.toString;" + |
| 37 |
"var google = { gears: { factory: {}, workerPool: {} } };"; |
| 38 |
|
| 39 |
public AmbiguatePropertiesTest() { |
| 40 |
super(EXTERNS); |
| 41 |
enableNormalize(); |
| 42 |
enableTypeCheck(CheckLevel.WARNING); |
| 43 |
enableClosurePass(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 48 |
return new CompilerPass() { |
| 49 |
@Override |
| 50 |
public void process(Node externs, Node root) { |
| 51 |
lastPass = new AmbiguateProperties(compiler, new char[]{'$'}); |
| 52 |
lastPass.process(externs, root); |
| 53 |
} |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected int getNumRepetitions() { |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected CompilerOptions getOptions() { |
| 64 |
|
| 65 |
CompilerOptions options = new CompilerOptions(); |
| 66 |
options.setLanguageIn(LanguageMode.ECMASCRIPT5); |
| 67 |
return options; |
| 68 |
} |
| 69 |
|
| 70 |
public void testOneVar1() { |
| 71 |
test("/** @constructor */ var Foo = function(){};Foo.prototype.b = 0;", |
| 72 |
"var Foo = function(){};Foo.prototype.a = 0;"); |
| 73 |
} |
| 74 |
|
| 75 |
public void testOneVar2() { |
| 76 |
testSame("/** @constructor */ var Foo = function(){};" + |
| 77 |
"Foo.prototype = {b: 0};"); |
| 78 |
} |
| 79 |
|
| 80 |
public void testOneVar3() { |
| 81 |
testSame("/** @constructor */ var Foo = function(){};" + |
| 82 |
"Foo.prototype = {get b() {return 0}};"); |
| 83 |
} |
| 84 |
|
| 85 |
public void testOneVar4() { |
| 86 |
testSame("/** @constructor */ var Foo = function(){};" + |
| 87 |
"Foo.prototype = {set b(a) {}};"); |
| 88 |
} |
| 89 |
|
| 90 |
public void testTwoVar1() { |
| 91 |
String js = "" |
| 92 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 93 |
+ "Foo.prototype.z=0;\n" |
| 94 |
+ "Foo.prototype.z=0;\n" |
| 95 |
+ "Foo.prototype.x=0;"; |
| 96 |
String output = "" |
| 97 |
+ "var Foo = function(){};\n" |
| 98 |
+ "Foo.prototype.a=0;\n" |
| 99 |
+ "Foo.prototype.a=0;\n" |
| 100 |
+ "Foo.prototype.b=0;"; |
| 101 |
test(js, output); |
| 102 |
} |
| 103 |
|
| 104 |
public void testTwoVar2() { |
| 105 |
String js = "" |
| 106 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 107 |
+ "Foo.prototype={z:0, z:1, x:0};\n"; |
| 108 |
|
| 109 |
testSame(js); |
| 110 |
} |
| 111 |
|
| 112 |
public void testTwoIndependentVar() { |
| 113 |
String js = "" |
| 114 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 115 |
+ "Foo.prototype.b = 0;\n" |
| 116 |
+ "/** @constructor */ var Bar = function(){};\n" |
| 117 |
+ "Bar.prototype.c = 0;"; |
| 118 |
String output = "" |
| 119 |
+ "var Foo = function(){};" |
| 120 |
+ "Foo.prototype.a=0;" |
| 121 |
+ "var Bar = function(){};" |
| 122 |
+ "Bar.prototype.a=0;"; |
| 123 |
test(js, output); |
| 124 |
} |
| 125 |
|
| 126 |
public void testTwoTypesTwoVar() { |
| 127 |
String js = "" |
| 128 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 129 |
+ "Foo.prototype.r = 0;\n" |
| 130 |
+ "Foo.prototype.g = 0;\n" |
| 131 |
+ "/** @constructor */ var Bar = function(){};\n" |
| 132 |
+ "Bar.prototype.c = 0;" |
| 133 |
+ "Bar.prototype.r = 0;"; |
| 134 |
String output = "" |
| 135 |
+ "var Foo = function(){};" |
| 136 |
+ "Foo.prototype.a=0;" |
| 137 |
+ "Foo.prototype.b=0;" |
| 138 |
+ "var Bar = function(){};" |
| 139 |
+ "Bar.prototype.b=0;" |
| 140 |
+ "Bar.prototype.a=0;"; |
| 141 |
test(js, output); |
| 142 |
} |
| 143 |
|
| 144 |
public void testUnion() { |
| 145 |
String js = "" |
| 146 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 147 |
+ "/** @constructor */ var Bar = function(){};\n" |
| 148 |
+ "Foo.prototype.foodoo=0;\n" |
| 149 |
+ "Bar.prototype.bardoo=0;\n" |
| 150 |
+ "/** @type {Foo|Bar} */\n" |
| 151 |
+ "var U;\n" |
| 152 |
+ "U.joint;" |
| 153 |
+ "U.joint"; |
| 154 |
String output = "" |
| 155 |
+ "var Foo = function(){};\n" |
| 156 |
+ "var Bar = function(){};\n" |
| 157 |
+ "Foo.prototype.b=0;\n" |
| 158 |
+ "Bar.prototype.b=0;\n" |
| 159 |
+ "var U;\n" |
| 160 |
+ "U.a;" |
| 161 |
+ "U.a"; |
| 162 |
test(js, output); |
| 163 |
} |
| 164 |
|
| 165 |
public void testUnions() { |
| 166 |
String js = "" |
| 167 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 168 |
+ "/** @constructor */ var Bar = function(){};\n" |
| 169 |
+ "/** @constructor */ var Baz = function(){};\n" |
| 170 |
+ "/** @constructor */ var Bat = function(){};\n" |
| 171 |
+ "Foo.prototype.lone1=0;\n" |
| 172 |
+ "Bar.prototype.lone2=0;\n" |
| 173 |
+ "Baz.prototype.lone3=0;\n" |
| 174 |
+ "Bat.prototype.lone4=0;\n" |
| 175 |
+ "/** @type {Foo|Bar} */\n" |
| 176 |
+ "var U1;\n" |
| 177 |
+ "U1.j1;" |
| 178 |
+ "U1.j2;" |
| 179 |
+ "/** @type {Baz|Bar} */\n" |
| 180 |
+ "var U2;\n" |
| 181 |
+ "U2.j3;" |
| 182 |
+ "U2.j4;" |
| 183 |
+ "/** @type {Baz|Bat} */\n" |
| 184 |
+ "var U3;" |
| 185 |
+ "U3.j5;" |
| 186 |
+ "U3.j6"; |
| 187 |
String output = "" |
| 188 |
+ "var Foo = function(){};\n" |
| 189 |
+ "var Bar = function(){};\n" |
| 190 |
+ "var Baz = function(){};\n" |
| 191 |
+ "var Bat = function(){};\n" |
| 192 |
+ "Foo.prototype.c=0;\n" |
| 193 |
+ "Bar.prototype.e=0;\n" |
| 194 |
+ "Baz.prototype.e=0;\n" |
| 195 |
+ "Bat.prototype.c=0;\n" |
| 196 |
+ "var U1;\n" |
| 197 |
+ "U1.a;" |
| 198 |
+ "U1.b;" |
| 199 |
+ "var U2;\n" |
| 200 |
+ "U2.c;" |
| 201 |
+ "U2.d;" |
| 202 |
+ "var U3;" |
| 203 |
+ "U3.a;" |
| 204 |
+ "U3.b"; |
| 205 |
test(js, output); |
| 206 |
} |
| 207 |
|
| 208 |
public void testExtends() { |
| 209 |
String js = "" |
| 210 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 211 |
+ "Foo.prototype.x=0;\n" |
| 212 |
+ "/** @constructor \n @extends Foo */ var Bar = function(){};\n" |
| 213 |
+ "goog.inherits(Bar, Foo);\n" |
| 214 |
+ "Bar.prototype.y=0;\n" |
| 215 |
+ "Bar.prototype.z=0;\n" |
| 216 |
+ "/** @constructor */ var Baz = function(){};\n" |
| 217 |
+ "Baz.prototype.l=0;\n" |
| 218 |
+ "Baz.prototype.m=0;\n" |
| 219 |
+ "Baz.prototype.n=0;\n" |
| 220 |
+ "(new Baz).m\n"; |
| 221 |
String output = "" |
| 222 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 223 |
+ "Foo.prototype.a=0;\n" |
| 224 |
+ "/** @constructor \n @extends Foo */ var Bar = function(){};\n" |
| 225 |
+ "goog.inherits(Bar, Foo);\n" |
| 226 |
+ "Bar.prototype.b=0;\n" |
| 227 |
+ "Bar.prototype.c=0;\n" |
| 228 |
+ "/** @constructor */ var Baz = function(){};\n" |
| 229 |
+ "Baz.prototype.b=0;\n" |
| 230 |
+ "Baz.prototype.a=0;\n" |
| 231 |
+ "Baz.prototype.c=0;\n" |
| 232 |
+ "(new Baz).a\n"; |
| 233 |
test(js, output); |
| 234 |
} |
| 235 |
|
| 236 |
public void testLotsOfVars() { |
| 237 |
StringBuilder js = new StringBuilder(); |
| 238 |
StringBuilder output = new StringBuilder(); |
| 239 |
js.append("/** @constructor */ var Foo = function(){};\n"); |
| 240 |
js.append("/** @constructor */ var Bar = function(){};\n"); |
| 241 |
output.append(js.toString()); |
| 242 |
|
| 243 |
int vars = 10; |
| 244 |
for (int i = 0; i < vars; i++) { |
| 245 |
js.append("Foo.prototype.var" + i + " = 0;"); |
| 246 |
js.append("Bar.prototype.var" + (i + 10000) + " = 0;"); |
| 247 |
output.append("Foo.prototype." + (char) ('a' + i) + "=0;"); |
| 248 |
output.append("Bar.prototype." + (char) ('a' + i) + "=0;"); |
| 249 |
} |
| 250 |
test(js.toString(), output.toString()); |
| 251 |
} |
| 252 |
|
| 253 |
public void testLotsOfClasses() { |
| 254 |
StringBuilder b = new StringBuilder(); |
| 255 |
int classes = 10; |
| 256 |
for (int i = 0; i < classes; i++) { |
| 257 |
String c = "Foo" + i; |
| 258 |
b.append("/** @constructor */ var " + c + " = function(){};\n"); |
| 259 |
b.append(c + ".prototype.varness" + i + " = 0;"); |
| 260 |
} |
| 261 |
String js = b.toString(); |
| 262 |
test(js, js.replaceAll("varness\\d+", "a")); |
| 263 |
} |
| 264 |
|
| 265 |
public void testFunctionType() { |
| 266 |
String js = "" |
| 267 |
+ "/** @constructor */ function Foo(){};\n" |
| 268 |
+ "/** @return {Bar} */\n" |
| 269 |
+ "Foo.prototype.fun = function() { return new Bar(); };\n" |
| 270 |
+ "/** @constructor */ function Bar(){};\n" |
| 271 |
+ "Bar.prototype.bazz;\n" |
| 272 |
+ "(new Foo).fun().bazz();"; |
| 273 |
String output = "" |
| 274 |
+ "function Foo(){};\n" |
| 275 |
+ "Foo.prototype.a = function() { return new Bar(); };\n" |
| 276 |
+ "function Bar(){};\n" |
| 277 |
+ "Bar.prototype.a;\n" |
| 278 |
+ "(new Foo).a().a();"; |
| 279 |
test(js, output); |
| 280 |
} |
| 281 |
|
| 282 |
public void testPrototypePropertiesAsObjLitKeys1() { |
| 283 |
test("/** @constructor */ function Bar() {};" + |
| 284 |
"Bar.prototype = {2: function(){}, getA: function(){}};", |
| 285 |
"/** @constructor */ function Bar() {};" + |
| 286 |
"Bar.prototype = {2: function(){}, a: function(){}};"); |
| 287 |
} |
| 288 |
|
| 289 |
public void testPrototypePropertiesAsObjLitKeys2() { |
| 290 |
testSame("/** @constructor */ function Bar() {};" + |
| 291 |
"Bar.prototype = {2: function(){}, 'getA': function(){}};"); |
| 292 |
} |
| 293 |
|
| 294 |
public void testQuotedPrototypeProperty() { |
| 295 |
testSame("/** @constructor */ function Bar() {};" + |
| 296 |
"Bar.prototype['getA'] = function(){};" + |
| 297 |
"var bar = new Bar();" + |
| 298 |
"bar['getA']();"); |
| 299 |
} |
| 300 |
|
| 301 |
public void testOverlappingOriginalAndGeneratedNames() { |
| 302 |
test("/** @constructor */ function Bar(){};" |
| 303 |
+ "Bar.prototype.b = function(){};" |
| 304 |
+ "Bar.prototype.a = function(){};" |
| 305 |
+ "var bar = new Bar();" |
| 306 |
+ "bar.b();", |
| 307 |
"function Bar(){};" |
| 308 |
+ "Bar.prototype.a = function(){};" |
| 309 |
+ "Bar.prototype.b = function(){};" |
| 310 |
+ "var bar = new Bar();" |
| 311 |
+ "bar.a();"); |
| 312 |
} |
| 313 |
|
| 314 |
public void testPropertyAddedToObject() { |
| 315 |
testSame("var foo = {}; foo.prop = '';"); |
| 316 |
} |
| 317 |
|
| 318 |
public void testPropertyAddedToFunction() { |
| 319 |
test("var foo = function(){}; foo.prop = '';", |
| 320 |
"var foo = function(){}; foo.a = '';"); |
| 321 |
} |
| 322 |
|
| 323 |
public void testPropertyOfObjectOfUnknownType() { |
| 324 |
testSame("var foo = x(); foo.prop = '';"); |
| 325 |
} |
| 326 |
|
| 327 |
public void testPropertyOnParamOfUnknownType() { |
| 328 |
testSame("/** @constructor */ function Foo(){};\n" |
| 329 |
+ "Foo.prototype.prop = 0;" |
| 330 |
+ "function go(aFoo){\n" |
| 331 |
+ " aFoo.prop = 1;" |
| 332 |
+ "}"); |
| 333 |
} |
| 334 |
|
| 335 |
public void testSetPropertyOfGlobalThis() { |
| 336 |
testSame("this.prop = 'bar'"); |
| 337 |
} |
| 338 |
|
| 339 |
public void testReadPropertyOfGlobalThis() { |
| 340 |
testSame("f(this.prop);"); |
| 341 |
} |
| 342 |
|
| 343 |
public void testSetQuotedPropertyOfThis() { |
| 344 |
testSame("this['prop'] = 'bar';"); |
| 345 |
} |
| 346 |
|
| 347 |
public void testExternedPropertyName() { |
| 348 |
test("/** @constructor */ var Bar = function(){};" |
| 349 |
+ "/** @override */ Bar.prototype.toString = function(){};" |
| 350 |
+ "Bar.prototype.func = function(){};" |
| 351 |
+ "var bar = new Bar();" |
| 352 |
+ "bar.toString();", |
| 353 |
"var Bar = function(){};" |
| 354 |
+ "Bar.prototype.toString = function(){};" |
| 355 |
+ "Bar.prototype.a = function(){};" |
| 356 |
+ "var bar = new Bar();" |
| 357 |
+ "bar.toString();"); |
| 358 |
} |
| 359 |
|
| 360 |
public void testExternedPropertyNameDefinedByObjectLiteral() { |
| 361 |
testSame("/**@constructor*/function Bar(){};Bar.prototype.factory"); |
| 362 |
} |
| 363 |
|
| 364 |
public void testStaticAndInstanceMethodWithSameName() { |
| 365 |
test("/** @constructor */function Bar(){}; Bar.getA = function(){}; " + |
| 366 |
"Bar.prototype.getA = function(){}; Bar.getA();" + |
| 367 |
"var bar = new Bar(); bar.getA();", |
| 368 |
"function Bar(){}; Bar.a = function(){};" + |
| 369 |
"Bar.prototype.a = function(){}; Bar.a();" + |
| 370 |
"var bar = new Bar(); bar.a();"); |
| 371 |
} |
| 372 |
|
| 373 |
public void testStaticAndInstanceProperties() { |
| 374 |
test("/** @constructor */function Bar(){};" + |
| 375 |
"Bar.getA = function(){}; " + |
| 376 |
"Bar.prototype.getB = function(){};", |
| 377 |
"function Bar(){}; Bar.a = function(){};" + |
| 378 |
"Bar.prototype.a = function(){};"); |
| 379 |
} |
| 380 |
|
| 381 |
public void testStaticAndSubInstanceProperties() { |
| 382 |
String js = "" |
| 383 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 384 |
+ "Foo.x=0;\n" |
| 385 |
+ "/** @constructor \n @extends Foo */ var Bar = function(){};\n" |
| 386 |
+ "goog.inherits(Bar, Foo);\n" |
| 387 |
+ "Bar.y=0;\n" |
| 388 |
+ "Bar.prototype.z=0;\n"; |
| 389 |
String output = "" |
| 390 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 391 |
+ "Foo.a=0;\n" |
| 392 |
+ "/** @constructor \n @extends Foo */ var Bar = function(){};\n" |
| 393 |
+ "goog.inherits(Bar, Foo);\n" |
| 394 |
+ "Bar.a=0;\n" |
| 395 |
+ "Bar.prototype.a=0;\n"; |
| 396 |
test(js, output); |
| 397 |
} |
| 398 |
|
| 399 |
public void testStaticWithFunctions() { |
| 400 |
String js = "" |
| 401 |
+ "/** @constructor */ var Foo = function() {};\n" |
| 402 |
+ "Foo.x = 0;" |
| 403 |
+ "/** @param {!Function} x */ function f(x) { x.y = 1 }" |
| 404 |
+ "f(Foo)"; |
| 405 |
String output = "" |
| 406 |
+ "/** @constructor */ var Foo = function() {};\n" |
| 407 |
+ "Foo.a = 0;" |
| 408 |
+ "/** @param {!Function} x */ function f(x) { x.y = 1 }" |
| 409 |
+ "f(Foo)"; |
| 410 |
test(js, output); |
| 411 |
|
| 412 |
js = "" |
| 413 |
+ "/** @constructor */ var Foo = function() {};\n" |
| 414 |
+ "Foo.x = 0;" |
| 415 |
+ "/** @param {!Function} x */ function f(x) { x.y = 1; x.x = 2;}" |
| 416 |
+ "f(Foo)"; |
| 417 |
test(js, js); |
| 418 |
|
| 419 |
js = "" |
| 420 |
+ "/** @constructor */ var Foo = function() {};\n" |
| 421 |
+ "Foo.x = 0;" |
| 422 |
+ "/** @constructor */ var Bar = function() {};\n" |
| 423 |
+ "Bar.y = 0;"; |
| 424 |
|
| 425 |
output = "" |
| 426 |
+ "/** @constructor */ var Foo = function() {};\n" |
| 427 |
+ "Foo.a = 0;" |
| 428 |
+ "/** @constructor */ var Bar = function() {};\n" |
| 429 |
+ "Bar.a = 0;"; |
| 430 |
test(js, output); |
| 431 |
|
| 432 |
} |
| 433 |
|
| 434 |
public void testTypeMismatch() { |
| 435 |
testSame(EXTERNS, "/** @constructor */var Foo = function(){};\n" |
| 436 |
+ "/** @constructor */var Bar = function(){};\n" |
| 437 |
+ "Foo.prototype.b = 0;\n" |
| 438 |
+ "/** @type {Foo} */\n" |
| 439 |
+ "var F = new Bar();", |
| 440 |
TypeValidator.TYPE_MISMATCH_WARNING, |
| 441 |
"initializing variable\n" |
| 442 |
+ "found : Bar\n" |
| 443 |
+ "required: (Foo|null)"); |
| 444 |
} |
| 445 |
|
| 446 |
public void testRenamingMap() { |
| 447 |
String js = "" |
| 448 |
+ "/** @constructor */ var Foo = function(){};\n" |
| 449 |
+ "Foo.prototype.z=0;\n" |
| 450 |
+ "Foo.prototype.z=0;\n" |
| 451 |
+ "Foo.prototype.x=0;\n" |
| 452 |
+ "Foo.prototype.y=0;"; |
| 453 |
String output = "" |
| 454 |
+ "var Foo = function(){};\n" |
| 455 |
+ "Foo.prototype.a=0;\n" |
| 456 |
+ "Foo.prototype.a=0;\n" |
| 457 |
+ "Foo.prototype.b=0;\n" |
| 458 |
+ "Foo.prototype.c=0;"; |
| 459 |
test(js, output); |
| 460 |
|
| 461 |
Map<String, String> answerMap = Maps.newHashMap(); |
| 462 |
answerMap.put("x", "b"); |
| 463 |
answerMap.put("y", "c"); |
| 464 |
answerMap.put("z", "a"); |
| 465 |
assertEquals(answerMap, lastPass.getRenamingMap()); |
| 466 |
} |
| 467 |
|
| 468 |
public void testInline() { |
| 469 |
String js = "" |
| 470 |
+ "/** @interface */ function Foo(){}\n" |
| 471 |
+ "Foo.prototype.x = function(){};\n" |
| 472 |
+ "/**\n" |
| 473 |
+ " * @constructor\n" |
| 474 |
+ " * @implements {Foo}\n" |
| 475 |
+ " */\n" |
| 476 |
+ "function Bar(){}\n" |
| 477 |
+ "/** @inheritDoc */\n" |
| 478 |
+ "Bar.prototype.x = function() { return this.y; };\n" |
| 479 |
+ "Bar.prototype.z = function() {};\n" |
| 480 |
|
| 481 |
+ "/** @type {Foo} */ (new Bar).y;"; |
| 482 |
String output = "" |
| 483 |
+ "function Foo(){}\n" |
| 484 |
+ "Foo.prototype.a = function(){};\n" |
| 485 |
+ "function Bar(){}\n" |
| 486 |
+ "Bar.prototype.a = function() { return this.b; };\n" |
| 487 |
+ "Bar.prototype.c = function() {};\n" |
| 488 |
|
| 489 |
+ "(new Bar).b;"; |
| 490 |
test(js, output); |
| 491 |
} |
| 492 |
|
| 493 |
public void testImplementsAndExtends() { |
| 494 |
String js = "" |
| 495 |
+ "/** @interface */ function Foo() {}\n" |
| 496 |
+ "/**\n" |
| 497 |
+ " * @constructor\n" |
| 498 |
+ " */\n" |
| 499 |
+ "function Bar(){}\n" |
| 500 |
+ "Bar.prototype.y = function() { return 3; };\n" |
| 501 |
+ "/**\n" |
| 502 |
+ " * @constructor\n" |
| 503 |
+ " * @extends {Bar}\n" |
| 504 |
+ " * @implements {Foo}\n" |
| 505 |
+ " */\n" |
| 506 |
+ "function SubBar(){ }\n" |
| 507 |
+ "/** @param {Foo} x */ function f(x) { x.z = 3; }\n" |
| 508 |
+ "/** @param {SubBar} x */ function g(x) { x.z = 3; }"; |
| 509 |
String output = "" |
| 510 |
+ "function Foo(){}\n" |
| 511 |
+ "function Bar(){}\n" |
| 512 |
+ "Bar.prototype.b = function() { return 3; };\n" |
| 513 |
+ "function SubBar(){}\n" |
| 514 |
+ "function f(x) { x.a = 3; }\n" |
| 515 |
+ "function g(x) { x.a = 3; }"; |
| 516 |
test(js, output); |
| 517 |
} |
| 518 |
|
| 519 |
public void testImplementsAndExtends2() { |
| 520 |
String js = "" |
| 521 |
+ "/** @interface */ function A() {}\n" |
| 522 |
+ "/**\n" |
| 523 |
+ " * @constructor\n" |
| 524 |
+ " */\n" |
| 525 |
+ "function C1(){}\n" |
| 526 |
+ "/**\n" |
| 527 |
+ " * @constructor\n" |
| 528 |
+ " * @extends {C1}\n" |
| 529 |
+ " * @implements {A}\n" |
| 530 |
+ " */\n" |
| 531 |
+ "function C2(){}\n" |
| 532 |
+ "/** @param {C1} x */ function f(x) { x.y = 3; }\n" |
| 533 |
+ "/** @param {A} x */ function g(x) { x.z = 3; }\n"; |
| 534 |
String output = "" |
| 535 |
+ "function A(){}\n" |
| 536 |
+ "function C1(){}\n" |
| 537 |
+ "function C2(){}\n" |
| 538 |
+ "function f(x) { x.a = 3; }\n" |
| 539 |
+ "function g(x) { x.b = 3; }\n"; |
| 540 |
test(js, output); |
| 541 |
} |
| 542 |
|
| 543 |
public void testExtendsInterface() { |
| 544 |
String js = "" |
| 545 |
+ "/** @interface */ function A() {}\n" |
| 546 |
+ "/** @interface \n @extends {A} */ function B() {}\n" |
| 547 |
+ "/** @param {A} x */ function f(x) { x.y = 3; }\n" |
| 548 |
+ "/** @param {B} x */ function g(x) { x.z = 3; }\n"; |
| 549 |
String output = "" |
| 550 |
+ "function A(){}\n" |
| 551 |
+ "function B(){}\n" |
| 552 |
+ "function f(x) { x.a = 3; }\n" |
| 553 |
+ "function g(x) { x.b = 3; }\n"; |
| 554 |
test(js, output); |
| 555 |
} |
| 556 |
|
| 557 |
public void testFunctionSubType() { |
| 558 |
String js = "" |
| 559 |
+ "Function.prototype.a = 1;\n" |
| 560 |
+ "function f() {}\n" |
| 561 |
+ "f.y = 2;\n"; |
| 562 |
String output = "" |
| 563 |
+ "Function.prototype.a = 1;\n" |
| 564 |
+ "function f() {}\n" |
| 565 |
+ "f.b = 2;\n"; |
| 566 |
test(js, output); |
| 567 |
} |
| 568 |
|
| 569 |
public void testFunctionSubType2() { |
| 570 |
String js = "" |
| 571 |
+ "Function.prototype.a = 1;\n" |
| 572 |
+ "/** @constructor */ function F() {}\n" |
| 573 |
+ "F.y = 2;\n"; |
| 574 |
String output = "" |
| 575 |
+ "Function.prototype.a = 1;\n" |
| 576 |
+ "function F() {}\n" |
| 577 |
+ "F.b = 2;\n"; |
| 578 |
test(js, output); |
| 579 |
} |
| 580 |
|
| 581 |
public void testPredeclaredType() { |
| 582 |
String js = |
| 583 |
"goog.addDependency('zzz.js', ['goog.Foo'], []);" + |
| 584 |
"/** @constructor */ " + |
| 585 |
"function A() {" + |
| 586 |
" this.x = 3;" + |
| 587 |
"}" + |
| 588 |
"/** @param {goog.Foo} x */" + |
| 589 |
"function f(x) { x.y = 4; }"; |
| 590 |
String result = |
| 591 |
"0;" + |
| 592 |
"/** @constructor */ " + |
| 593 |
"function A() {" + |
| 594 |
" this.a = 3;" + |
| 595 |
"}" + |
| 596 |
"/** @param {goog.Foo} x */" + |
| 597 |
"function f(x) { x.y = 4; }"; |
| 598 |
test(js, result); |
| 599 |
} |
| 600 |
} |