| 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.base.Joiner; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.jscomp.Scope.Var; |
| 23 |
import com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter; |
| 24 |
import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter; |
| 25 |
import com.google.javascript.rhino.InputId; |
| 26 |
import com.google.javascript.rhino.Node; |
| 27 |
import com.google.javascript.rhino.Token; |
| 28 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 29 |
import com.google.javascript.rhino.jstype.JSType; |
| 30 |
import com.google.javascript.rhino.jstype.JSTypeNative; |
| 31 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 32 |
import com.google.javascript.rhino.testing.Asserts; |
| 33 |
|
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Set; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class TypeCheckTest extends CompilerTypeTestCase { |
| 43 |
|
| 44 |
private CheckLevel reportMissingOverrides = CheckLevel.WARNING; |
| 45 |
|
| 46 |
@Override |
| 47 |
public void setUp() throws Exception { |
| 48 |
super.setUp(); |
| 49 |
reportMissingOverrides = CheckLevel.WARNING; |
| 50 |
} |
| 51 |
|
| 52 |
public void testInitialTypingScope() { |
| 53 |
Scope s = new TypedScopeCreator(compiler, |
| 54 |
CodingConventions.getDefault()).createInitialScope( |
| 55 |
new Node(Token.BLOCK)); |
| 56 |
|
| 57 |
assertTypeEquals(ARRAY_FUNCTION_TYPE, s.getVar("Array").getType()); |
| 58 |
assertTypeEquals(BOOLEAN_OBJECT_FUNCTION_TYPE, |
| 59 |
s.getVar("Boolean").getType()); |
| 60 |
assertTypeEquals(DATE_FUNCTION_TYPE, s.getVar("Date").getType()); |
| 61 |
assertTypeEquals(ERROR_FUNCTION_TYPE, s.getVar("Error").getType()); |
| 62 |
assertTypeEquals(EVAL_ERROR_FUNCTION_TYPE, |
| 63 |
s.getVar("EvalError").getType()); |
| 64 |
assertTypeEquals(NUMBER_OBJECT_FUNCTION_TYPE, |
| 65 |
s.getVar("Number").getType()); |
| 66 |
assertTypeEquals(OBJECT_FUNCTION_TYPE, s.getVar("Object").getType()); |
| 67 |
assertTypeEquals(RANGE_ERROR_FUNCTION_TYPE, |
| 68 |
s.getVar("RangeError").getType()); |
| 69 |
assertTypeEquals(REFERENCE_ERROR_FUNCTION_TYPE, |
| 70 |
s.getVar("ReferenceError").getType()); |
| 71 |
assertTypeEquals(REGEXP_FUNCTION_TYPE, s.getVar("RegExp").getType()); |
| 72 |
assertTypeEquals(STRING_OBJECT_FUNCTION_TYPE, |
| 73 |
s.getVar("String").getType()); |
| 74 |
assertTypeEquals(SYNTAX_ERROR_FUNCTION_TYPE, |
| 75 |
s.getVar("SyntaxError").getType()); |
| 76 |
assertTypeEquals(TYPE_ERROR_FUNCTION_TYPE, |
| 77 |
s.getVar("TypeError").getType()); |
| 78 |
assertTypeEquals(URI_ERROR_FUNCTION_TYPE, |
| 79 |
s.getVar("URIError").getType()); |
| 80 |
} |
| 81 |
|
| 82 |
public void testPrivateType() throws Exception { |
| 83 |
testTypes( |
| 84 |
"/** @private {number} */ var x = false;", |
| 85 |
"initializing variable\n" + |
| 86 |
"found : boolean\n" + |
| 87 |
"required: number"); |
| 88 |
} |
| 89 |
|
| 90 |
public void testTypeCheck1() throws Exception { |
| 91 |
testTypes("/**@return {void}*/function foo(){ if (foo()) return; }"); |
| 92 |
} |
| 93 |
|
| 94 |
public void testTypeCheck2() throws Exception { |
| 95 |
testTypes("/**@return {void}*/function foo(){ var x=foo(); x--; }", |
| 96 |
"increment/decrement\n" + |
| 97 |
"found : undefined\n" + |
| 98 |
"required: number"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testTypeCheck4() throws Exception { |
| 102 |
testTypes("/**@return {void}*/function foo(){ !foo(); }"); |
| 103 |
} |
| 104 |
|
| 105 |
public void testTypeCheck5() throws Exception { |
| 106 |
testTypes("/**@return {void}*/function foo(){ var a = +foo(); }", |
| 107 |
"sign operator\n" + |
| 108 |
"found : undefined\n" + |
| 109 |
"required: number"); |
| 110 |
} |
| 111 |
|
| 112 |
public void testTypeCheck6() throws Exception { |
| 113 |
testTypes( |
| 114 |
"/**@return {void}*/function foo(){" + |
| 115 |
"/** @type {undefined|number} */var a;if (a == foo())return;}"); |
| 116 |
} |
| 117 |
|
| 118 |
public void testTypeCheck8() throws Exception { |
| 119 |
testTypes("/**@return {void}*/function foo(){do {} while (foo());}"); |
| 120 |
} |
| 121 |
|
| 122 |
public void testTypeCheck9() throws Exception { |
| 123 |
testTypes("/**@return {void}*/function foo(){while (foo());}"); |
| 124 |
} |
| 125 |
|
| 126 |
public void testTypeCheck10() throws Exception { |
| 127 |
testTypes("/**@return {void}*/function foo(){for (;foo(););}"); |
| 128 |
} |
| 129 |
|
| 130 |
public void testTypeCheck11() throws Exception { |
| 131 |
testTypes("/**@type !Number */var a;" + |
| 132 |
"/**@type !String */var b;" + |
| 133 |
"a = b;", |
| 134 |
"assignment\n" + |
| 135 |
"found : String\n" + |
| 136 |
"required: Number"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testTypeCheck12() throws Exception { |
| 140 |
testTypes("/**@return {!Object}*/function foo(){var a = 3^foo();}", |
| 141 |
"bad right operand to bitwise operator\n" + |
| 142 |
"found : Object\n" + |
| 143 |
"required: (boolean|null|number|string|undefined)"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testTypeCheck13() throws Exception { |
| 147 |
testTypes("/**@type {!Number|!String}*/var i; i=/xx/;", |
| 148 |
"assignment\n" + |
| 149 |
"found : RegExp\n" + |
| 150 |
"required: (Number|String)"); |
| 151 |
} |
| 152 |
|
| 153 |
public void testTypeCheck14() throws Exception { |
| 154 |
testTypes("/**@param opt_a*/function foo(opt_a){}"); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
public void testTypeCheck15() throws Exception { |
| 159 |
testTypes("/**@type {Number|null} */var x;x=null;x=10;", |
| 160 |
"assignment\n" + |
| 161 |
"found : number\n" + |
| 162 |
"required: (Number|null)"); |
| 163 |
} |
| 164 |
|
| 165 |
public void testTypeCheck16() throws Exception { |
| 166 |
testTypes("/**@type {Number|null} */var x='';", |
| 167 |
"initializing variable\n" + |
| 168 |
"found : string\n" + |
| 169 |
"required: (Number|null)"); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public void testTypeCheck17() throws Exception { |
| 174 |
testTypes("/**@return {Number}\n@param {Number} opt_foo */\n" + |
| 175 |
"function a(opt_foo){\nreturn /**@type {Number}*/(opt_foo);\n}"); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
public void testTypeCheck18() throws Exception { |
| 180 |
testTypes("/**@return {RegExp}\n*/\n function a(){return new RegExp();}"); |
| 181 |
} |
| 182 |
|
| 183 |
public void testTypeCheck19() throws Exception { |
| 184 |
testTypes("/**@return {Array}\n*/\n function a(){return new Array();}"); |
| 185 |
} |
| 186 |
|
| 187 |
public void testTypeCheck20() throws Exception { |
| 188 |
testTypes("/**@return {Date}\n*/\n function a(){return new Date();}"); |
| 189 |
} |
| 190 |
|
| 191 |
public void testTypeCheckBasicDowncast() throws Exception { |
| 192 |
testTypes("/** @constructor */function foo() {}\n" + |
| 193 |
"/** @type {Object} */ var bar = new foo();\n"); |
| 194 |
} |
| 195 |
|
| 196 |
public void testTypeCheckNoDowncastToNumber() throws Exception { |
| 197 |
testTypes("/** @constructor */function foo() {}\n" + |
| 198 |
"/** @type {!Number} */ var bar = new foo();\n", |
| 199 |
"initializing variable\n" + |
| 200 |
"found : foo\n" + |
| 201 |
"required: Number"); |
| 202 |
} |
| 203 |
|
| 204 |
public void testTypeCheck21() throws Exception { |
| 205 |
testTypes("/** @type Array.<String> */var foo;"); |
| 206 |
} |
| 207 |
|
| 208 |
public void testTypeCheck22() throws Exception { |
| 209 |
testTypes("/** @param {Element|Object} p */\nfunction foo(p){}\n" + |
| 210 |
"/** @constructor */function Element(){}\n" + |
| 211 |
"/** @type {Element|Object} */var v;\n" + |
| 212 |
"foo(v);\n"); |
| 213 |
} |
| 214 |
|
| 215 |
public void testTypeCheck23() throws Exception { |
| 216 |
testTypes("/** @type {(Object,Null)} */var foo; foo = null;"); |
| 217 |
} |
| 218 |
|
| 219 |
public void testTypeCheck24() throws Exception { |
| 220 |
testTypes("/** @constructor */function MyType(){}\n" + |
| 221 |
"/** @type {(MyType,Null)} */var foo; foo = null;"); |
| 222 |
} |
| 223 |
|
| 224 |
public void testTypeCheckDefaultExterns() throws Exception { |
| 225 |
testTypes("/** @param {string} x */ function f(x) {}" + |
| 226 |
"f([].length);" , |
| 227 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 228 |
"found : number\n" + |
| 229 |
"required: string"); |
| 230 |
} |
| 231 |
|
| 232 |
public void testTypeCheckCustomExterns() throws Exception { |
| 233 |
testTypes( |
| 234 |
DEFAULT_EXTERNS + "/** @type {boolean} */ Array.prototype.oogabooga;", |
| 235 |
"/** @param {string} x */ function f(x) {}" + |
| 236 |
"f([].oogabooga);" , |
| 237 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 238 |
"found : boolean\n" + |
| 239 |
"required: string", false); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
public void testTypeCheckCustomExterns2() throws Exception { |
| 244 |
testTypes( |
| 245 |
DEFAULT_EXTERNS + "/** @enum {string} */ var Enum = {FOO: 1, BAR: 1};", |
| 246 |
"/** @param {Enum} x */ function f(x) {} f(Enum.FOO); f(true);", |
| 247 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 248 |
"found : boolean\n" + |
| 249 |
"required: Enum.<string>", |
| 250 |
false); |
| 251 |
} |
| 252 |
|
| 253 |
public void testTemplatizedArray1() throws Exception { |
| 254 |
testTypes("/** @param {!Array.<number>} a\n" + |
| 255 |
"* @return {string}\n" + |
| 256 |
"*/ var f = function(a) { return a[0]; };", |
| 257 |
"inconsistent return type\n" + |
| 258 |
"found : number\n" + |
| 259 |
"required: string"); |
| 260 |
} |
| 261 |
|
| 262 |
public void testTemplatizedArray2() throws Exception { |
| 263 |
testTypes("/** @param {!Array.<!Array.<number>>} a\n" + |
| 264 |
"* @return {number}\n" + |
| 265 |
"*/ var f = function(a) { return a[0]; };", |
| 266 |
"inconsistent return type\n" + |
| 267 |
"found : Array.<number>\n" + |
| 268 |
"required: number"); |
| 269 |
} |
| 270 |
|
| 271 |
public void testTemplatizedArray3() throws Exception { |
| 272 |
testTypes("/** @param {!Array.<number>} a\n" + |
| 273 |
"* @return {number}\n" + |
| 274 |
"*/ var f = function(a) { a[1] = 0; return a[0]; };"); |
| 275 |
} |
| 276 |
|
| 277 |
public void testTemplatizedArray4() throws Exception { |
| 278 |
testTypes("/** @param {!Array.<number>} a\n" + |
| 279 |
"*/ var f = function(a) { a[0] = 'a'; };", |
| 280 |
"assignment\n" + |
| 281 |
"found : string\n" + |
| 282 |
"required: number"); |
| 283 |
} |
| 284 |
|
| 285 |
public void testTemplatizedArray5() throws Exception { |
| 286 |
testTypes("/** @param {!Array.<*>} a\n" + |
| 287 |
"*/ var f = function(a) { a[0] = 'a'; };"); |
| 288 |
} |
| 289 |
|
| 290 |
public void testTemplatizedArray6() throws Exception { |
| 291 |
testTypes("/** @param {!Array.<*>} a\n" + |
| 292 |
"* @return {string}\n" + |
| 293 |
"*/ var f = function(a) { return a[0]; };", |
| 294 |
"inconsistent return type\n" + |
| 295 |
"found : *\n" + |
| 296 |
"required: string"); |
| 297 |
} |
| 298 |
|
| 299 |
public void testTemplatizedArray7() throws Exception { |
| 300 |
testTypes("/** @param {?Array.<number>} a\n" + |
| 301 |
"* @return {string}\n" + |
| 302 |
"*/ var f = function(a) { return a[0]; };", |
| 303 |
"inconsistent return type\n" + |
| 304 |
"found : number\n" + |
| 305 |
"required: string"); |
| 306 |
} |
| 307 |
|
| 308 |
public void testTemplatizedObject1() throws Exception { |
| 309 |
testTypes("/** @param {!Object.<number>} a\n" + |
| 310 |
"* @return {string}\n" + |
| 311 |
"*/ var f = function(a) { return a[0]; };", |
| 312 |
"inconsistent return type\n" + |
| 313 |
"found : number\n" + |
| 314 |
"required: string"); |
| 315 |
} |
| 316 |
|
| 317 |
public void testTemplatizedObject2() throws Exception { |
| 318 |
testTypes("/** @param {!Object.<string,number>} a\n" + |
| 319 |
"* @return {string}\n" + |
| 320 |
"*/ var f = function(a) { return a['x']; };", |
| 321 |
"inconsistent return type\n" + |
| 322 |
"found : number\n" + |
| 323 |
"required: string"); |
| 324 |
} |
| 325 |
|
| 326 |
public void testTemplatizedObject3() throws Exception { |
| 327 |
testTypes("/** @param {!Object.<number,string>} a\n" + |
| 328 |
"* @return {string}\n" + |
| 329 |
"*/ var f = function(a) { return a['x']; };", |
| 330 |
"restricted index type\n" + |
| 331 |
"found : string\n" + |
| 332 |
"required: number"); |
| 333 |
} |
| 334 |
|
| 335 |
public void testTemplatizedObject4() throws Exception { |
| 336 |
testTypes("/** @enum {string} */ var E = {A: 'a', B: 'b'};\n" + |
| 337 |
"/** @param {!Object.<E,string>} a\n" + |
| 338 |
"* @return {string}\n" + |
| 339 |
"*/ var f = function(a) { return a['x']; };", |
| 340 |
"restricted index type\n" + |
| 341 |
"found : string\n" + |
| 342 |
"required: E.<string>"); |
| 343 |
} |
| 344 |
|
| 345 |
public void testTemplatizedObject5() throws Exception { |
| 346 |
testTypes("/** @constructor */ function F() {" + |
| 347 |
" /** @type {Object.<number, string>} */ this.numbers = {};" + |
| 348 |
"}" + |
| 349 |
"(new F()).numbers['ten'] = '10';", |
| 350 |
"restricted index type\n" + |
| 351 |
"found : string\n" + |
| 352 |
"required: number"); |
| 353 |
} |
| 354 |
|
| 355 |
public void testUnionOfFunctionAndType() throws Exception { |
| 356 |
testTypes("/** @type {null|(function(Number):void)} */ var a;" + |
| 357 |
"/** @type {(function(Number):void)|null} */ var b = null; a = b;"); |
| 358 |
} |
| 359 |
|
| 360 |
public void testOptionalParameterComparedToUndefined() throws Exception { |
| 361 |
testTypes("/**@param opt_a {Number}*/function foo(opt_a)" + |
| 362 |
"{if (opt_a==undefined) var b = 3;}"); |
| 363 |
} |
| 364 |
|
| 365 |
public void testOptionalAllType() throws Exception { |
| 366 |
testTypes("/** @param {*} opt_x */function f(opt_x) { return opt_x }\n" + |
| 367 |
"/** @type {*} */var y;\n" + |
| 368 |
"f(y);"); |
| 369 |
} |
| 370 |
|
| 371 |
public void testOptionalUnknownNamedType() throws Exception { |
| 372 |
testTypes("/** @param {!T} opt_x\n@return {undefined} */\n" + |
| 373 |
"function f(opt_x) { return opt_x; }\n" + |
| 374 |
"/** @constructor */var T = function() {};", |
| 375 |
"inconsistent return type\n" + |
| 376 |
"found : (T|undefined)\n" + |
| 377 |
"required: undefined"); |
| 378 |
} |
| 379 |
|
| 380 |
public void testOptionalArgFunctionParam() throws Exception { |
| 381 |
testTypes("/** @param {function(number=)} a */" + |
| 382 |
"function f(a) {a()};"); |
| 383 |
} |
| 384 |
|
| 385 |
public void testOptionalArgFunctionParam2() throws Exception { |
| 386 |
testTypes("/** @param {function(number=)} a */" + |
| 387 |
"function f(a) {a(3)};"); |
| 388 |
} |
| 389 |
|
| 390 |
public void testOptionalArgFunctionParam3() throws Exception { |
| 391 |
testTypes("/** @param {function(number=)} a */" + |
| 392 |
"function f(a) {a(undefined)};"); |
| 393 |
} |
| 394 |
|
| 395 |
public void testOptionalArgFunctionParam4() throws Exception { |
| 396 |
String expectedWarning = "Function a: called with 2 argument(s). " + |
| 397 |
"Function requires at least 0 argument(s) and no more than 1 " + |
| 398 |
"argument(s)."; |
| 399 |
|
| 400 |
testTypes("/** @param {function(number=)} a */function f(a) {a(3,4)};", |
| 401 |
expectedWarning, false); |
| 402 |
} |
| 403 |
|
| 404 |
public void testOptionalArgFunctionParamError() throws Exception { |
| 405 |
String expectedWarning = |
| 406 |
"Bad type annotation. variable length argument must be last"; |
| 407 |
testTypes("/** @param {function(...[number], number=)} a */" + |
| 408 |
"function f(a) {};", expectedWarning, false); |
| 409 |
} |
| 410 |
|
| 411 |
public void testOptionalNullableArgFunctionParam() throws Exception { |
| 412 |
testTypes("/** @param {function(?number=)} a */" + |
| 413 |
"function f(a) {a()};"); |
| 414 |
} |
| 415 |
|
| 416 |
public void testOptionalNullableArgFunctionParam2() throws Exception { |
| 417 |
testTypes("/** @param {function(?number=)} a */" + |
| 418 |
"function f(a) {a(null)};"); |
| 419 |
} |
| 420 |
|
| 421 |
public void testOptionalNullableArgFunctionParam3() throws Exception { |
| 422 |
testTypes("/** @param {function(?number=)} a */" + |
| 423 |
"function f(a) {a(3)};"); |
| 424 |
} |
| 425 |
|
| 426 |
public void testOptionalArgFunctionReturn() throws Exception { |
| 427 |
testTypes("/** @return {function(number=)} */" + |
| 428 |
"function f() { return function(opt_x) { }; };" + |
| 429 |
"f()()"); |
| 430 |
} |
| 431 |
|
| 432 |
public void testOptionalArgFunctionReturn2() throws Exception { |
| 433 |
testTypes("/** @return {function(Object=)} */" + |
| 434 |
"function f() { return function(opt_x) { }; };" + |
| 435 |
"f()({})"); |
| 436 |
} |
| 437 |
|
| 438 |
public void testBooleanType() throws Exception { |
| 439 |
testTypes("/**@type {boolean} */var x = 1 < 2;"); |
| 440 |
} |
| 441 |
|
| 442 |
public void testBooleanReduction1() throws Exception { |
| 443 |
testTypes("/**@type {string} */var x; x = null || \"a\";"); |
| 444 |
} |
| 445 |
|
| 446 |
public void testBooleanReduction2() throws Exception { |
| 447 |
|
| 448 |
|
| 449 |
testTypes("/** @param {string} s\n @return {string} */" + |
| 450 |
"(function(s) { return ((s == 'a') && s) || 'b'; })"); |
| 451 |
} |
| 452 |
|
| 453 |
public void testBooleanReduction3() throws Exception { |
| 454 |
testTypes("/** @param {string} s\n @return {string?} */" + |
| 455 |
"(function(s) { return s && null && 3; })"); |
| 456 |
} |
| 457 |
|
| 458 |
public void testBooleanReduction4() throws Exception { |
| 459 |
testTypes("/** @param {Object} x\n @return {Object} */" + |
| 460 |
"(function(x) { return null || x || null ; })"); |
| 461 |
} |
| 462 |
|
| 463 |
public void testBooleanReduction5() throws Exception { |
| 464 |
testTypes("/**\n" + |
| 465 |
"* @param {Array|string} x\n" + |
| 466 |
"* @return {string?}\n" + |
| 467 |
"*/\n" + |
| 468 |
"var f = function(x) {\n" + |
| 469 |
"if (!x || typeof x == 'string') {\n" + |
| 470 |
"return x;\n" + |
| 471 |
"}\n" + |
| 472 |
"return null;\n" + |
| 473 |
"};"); |
| 474 |
} |
| 475 |
|
| 476 |
public void testBooleanReduction6() throws Exception { |
| 477 |
testTypes("/**\n" + |
| 478 |
"* @param {Array|string|null} x\n" + |
| 479 |
"* @return {string?}\n" + |
| 480 |
"*/\n" + |
| 481 |
"var f = function(x) {\n" + |
| 482 |
"if (!(x && typeof x != 'string')) {\n" + |
| 483 |
"return x;\n" + |
| 484 |
"}\n" + |
| 485 |
"return null;\n" + |
| 486 |
"};"); |
| 487 |
} |
| 488 |
|
| 489 |
public void testBooleanReduction7() throws Exception { |
| 490 |
testTypes("/** @constructor */var T = function() {};\n" + |
| 491 |
"/**\n" + |
| 492 |
"* @param {Array|T} x\n" + |
| 493 |
"* @return {null}\n" + |
| 494 |
"*/\n" + |
| 495 |
"var f = function(x) {\n" + |
| 496 |
"if (!x) {\n" + |
| 497 |
"return x;\n" + |
| 498 |
"}\n" + |
| 499 |
"return null;\n" + |
| 500 |
"};"); |
| 501 |
} |
| 502 |
|
| 503 |
public void testNullAnd() throws Exception { |
| 504 |
testTypes("/** @type null */var x;\n" + |
| 505 |
"/** @type number */var r = x && x;", |
| 506 |
"initializing variable\n" + |
| 507 |
"found : null\n" + |
| 508 |
"required: number"); |
| 509 |
} |
| 510 |
|
| 511 |
public void testNullOr() throws Exception { |
| 512 |
testTypes("/** @type null */var x;\n" + |
| 513 |
"/** @type number */var r = x || x;", |
| 514 |
"initializing variable\n" + |
| 515 |
"found : null\n" + |
| 516 |
"required: number"); |
| 517 |
} |
| 518 |
|
| 519 |
public void testBooleanPreservation1() throws Exception { |
| 520 |
testTypes("/**@type {string} */var x = \"a\";" + |
| 521 |
"x = ((x == \"a\") && x) || x == \"b\";", |
| 522 |
"assignment\n" + |
| 523 |
"found : (boolean|string)\n" + |
| 524 |
"required: string"); |
| 525 |
} |
| 526 |
|
| 527 |
public void testBooleanPreservation2() throws Exception { |
| 528 |
testTypes("/**@type {string} */var x = \"a\"; x = (x == \"a\") || x;", |
| 529 |
"assignment\n" + |
| 530 |
"found : (boolean|string)\n" + |
| 531 |
"required: string"); |
| 532 |
} |
| 533 |
|
| 534 |
public void testBooleanPreservation3() throws Exception { |
| 535 |
testTypes("/** @param {Function?} x\n @return {boolean?} */" + |
| 536 |
"function f(x) { return x && x == \"a\"; }", |
| 537 |
"condition always evaluates to false\n" + |
| 538 |
"left : Function\n" + |
| 539 |
"right: string"); |
| 540 |
} |
| 541 |
|
| 542 |
public void testBooleanPreservation4() throws Exception { |
| 543 |
testTypes("/** @param {Function?|boolean} x\n @return {boolean} */" + |
| 544 |
"function f(x) { return x && x == \"a\"; }", |
| 545 |
"inconsistent return type\n" + |
| 546 |
"found : (boolean|null)\n" + |
| 547 |
"required: boolean"); |
| 548 |
} |
| 549 |
|
| 550 |
public void testTypeOfReduction1() throws Exception { |
| 551 |
testTypes("/** @param {string|number} x\n @return {string} */ " + |
| 552 |
"function f(x) { return typeof x == 'number' ? String(x) : x; }"); |
| 553 |
} |
| 554 |
|
| 555 |
public void testTypeOfReduction2() throws Exception { |
| 556 |
testTypes("/** @param {string|number} x\n @return {string} */ " + |
| 557 |
"function f(x) { return typeof x != 'string' ? String(x) : x; }"); |
| 558 |
} |
| 559 |
|
| 560 |
public void testTypeOfReduction3() throws Exception { |
| 561 |
testTypes("/** @param {number|null} x\n @return {number} */ " + |
| 562 |
"function f(x) { return typeof x == 'object' ? 1 : x; }"); |
| 563 |
} |
| 564 |
|
| 565 |
public void testTypeOfReduction4() throws Exception { |
| 566 |
testTypes("/** @param {Object|undefined} x\n @return {Object} */ " + |
| 567 |
"function f(x) { return typeof x == 'undefined' ? {} : x; }"); |
| 568 |
} |
| 569 |
|
| 570 |
public void testTypeOfReduction5() throws Exception { |
| 571 |
testTypes("/** @enum {string} */ var E = {A: 'a', B: 'b'};\n" + |
| 572 |
"/** @param {!E|number} x\n @return {string} */ " + |
| 573 |
"function f(x) { return typeof x != 'number' ? x : 'a'; }"); |
| 574 |
} |
| 575 |
|
| 576 |
public void testTypeOfReduction6() throws Exception { |
| 577 |
testTypes("/** @param {number|string} x\n@return {string} */\n" + |
| 578 |
"function f(x) {\n" + |
| 579 |
"return typeof x == 'string' && x.length == 3 ? x : 'a';\n" + |
| 580 |
"}"); |
| 581 |
} |
| 582 |
|
| 583 |
public void testTypeOfReduction7() throws Exception { |
| 584 |
testTypes("/** @return {string} */var f = function(x) { " + |
| 585 |
"return typeof x == 'number' ? x : 'a'; }", |
| 586 |
"inconsistent return type\n" + |
| 587 |
"found : (number|string)\n" + |
| 588 |
"required: string"); |
| 589 |
} |
| 590 |
|
| 591 |
public void testTypeOfReduction8() throws Exception { |
| 592 |
testClosureTypes( |
| 593 |
CLOSURE_DEFS + |
| 594 |
"/** @param {number|string} x\n@return {string} */\n" + |
| 595 |
"function f(x) {\n" + |
| 596 |
"return goog.isString(x) && x.length == 3 ? x : 'a';\n" + |
| 597 |
"}", null); |
| 598 |
} |
| 599 |
|
| 600 |
public void testTypeOfReduction9() throws Exception { |
| 601 |
testClosureTypes( |
| 602 |
CLOSURE_DEFS + |
| 603 |
"/** @param {!Array|string} x\n@return {string} */\n" + |
| 604 |
"function f(x) {\n" + |
| 605 |
"return goog.isArray(x) ? 'a' : x;\n" + |
| 606 |
"}", null); |
| 607 |
} |
| 608 |
|
| 609 |
public void testTypeOfReduction10() throws Exception { |
| 610 |
testClosureTypes( |
| 611 |
CLOSURE_DEFS + |
| 612 |
"/** @param {Array|string} x\n@return {Array} */\n" + |
| 613 |
"function f(x) {\n" + |
| 614 |
"return goog.isArray(x) ? x : [];\n" + |
| 615 |
"}", null); |
| 616 |
} |
| 617 |
|
| 618 |
public void testTypeOfReduction11() throws Exception { |
| 619 |
testClosureTypes( |
| 620 |
CLOSURE_DEFS + |
| 621 |
"/** @param {Array|string} x\n@return {Array} */\n" + |
| 622 |
"function f(x) {\n" + |
| 623 |
"return goog.isObject(x) ? x : [];\n" + |
| 624 |
"}", null); |
| 625 |
} |
| 626 |
|
| 627 |
public void testTypeOfReduction12() throws Exception { |
| 628 |
testTypes("/** @enum {string} */ var E = {A: 'a', B: 'b'};\n" + |
| 629 |
"/** @param {E|Array} x\n @return {Array} */ " + |
| 630 |
"function f(x) { return typeof x == 'object' ? x : []; }"); |
| 631 |
} |
| 632 |
|
| 633 |
public void testTypeOfReduction13() throws Exception { |
| 634 |
testClosureTypes( |
| 635 |
CLOSURE_DEFS + |
| 636 |
"/** @enum {string} */ var E = {A: 'a', B: 'b'};\n" + |
| 637 |
"/** @param {E|Array} x\n@return {Array} */ " + |
| 638 |
"function f(x) { return goog.isObject(x) ? x : []; }", null); |
| 639 |
} |
| 640 |
|
| 641 |
public void testTypeOfReduction14() throws Exception { |
| 642 |
|
| 643 |
testClosureTypes( |
| 644 |
CLOSURE_DEFS + |
| 645 |
"function f(x) { " + |
| 646 |
" return goog.isString(arguments[0]) ? arguments[0] : 0;" + |
| 647 |
"}", null); |
| 648 |
} |
| 649 |
|
| 650 |
public void testTypeOfReduction15() throws Exception { |
| 651 |
|
| 652 |
testClosureTypes( |
| 653 |
CLOSURE_DEFS + |
| 654 |
"function f(x) { " + |
| 655 |
" return typeof arguments[0] == 'string' ? arguments[0] : 0;" + |
| 656 |
"}", null); |
| 657 |
} |
| 658 |
|
| 659 |
public void testTypeOfReduction16() throws Exception { |
| 660 |
testClosureTypes( |
| 661 |
CLOSURE_DEFS + |
| 662 |
"/** @interface */ function I() {}\n" + |
| 663 |
"/**\n" + |
| 664 |
" * @param {*} x\n" + |
| 665 |
" * @return {I}\n" + |
| 666 |
" */\n" + |
| 667 |
"function f(x) { " + |
| 668 |
" if(goog.isObject(x)) {" + |
| 669 |
" return /** @type {I} */(x);" + |
| 670 |
" }" + |
| 671 |
" return null;" + |
| 672 |
"}", null); |
| 673 |
} |
| 674 |
|
| 675 |
public void testQualifiedNameReduction1() throws Exception { |
| 676 |
testTypes("var x = {}; /** @type {string?} */ x.a = 'a';\n" + |
| 677 |
"/** @return {string} */ var f = function() {\n" + |
| 678 |
"return x.a ? x.a : 'a'; }"); |
| 679 |
} |
| 680 |
|
| 681 |
public void testQualifiedNameReduction2() throws Exception { |
| 682 |
testTypes("/** @param {string?} a\n@constructor */ var T = " + |
| 683 |
"function(a) {this.a = a};\n" + |
| 684 |
"/** @return {string} */ T.prototype.f = function() {\n" + |
| 685 |
"return this.a ? this.a : 'a'; }"); |
| 686 |
} |
| 687 |
|
| 688 |
public void testQualifiedNameReduction3() throws Exception { |
| 689 |
testTypes("/** @param {string|Array} a\n@constructor */ var T = " + |
| 690 |
"function(a) {this.a = a};\n" + |
| 691 |
"/** @return {string} */ T.prototype.f = function() {\n" + |
| 692 |
"return typeof this.a == 'string' ? this.a : 'a'; }"); |
| 693 |
} |
| 694 |
|
| 695 |
public void testQualifiedNameReduction4() throws Exception { |
| 696 |
testClosureTypes( |
| 697 |
CLOSURE_DEFS + |
| 698 |
"/** @param {string|Array} a\n@constructor */ var T = " + |
| 699 |
"function(a) {this.a = a};\n" + |
| 700 |
"/** @return {string} */ T.prototype.f = function() {\n" + |
| 701 |
"return goog.isString(this.a) ? this.a : 'a'; }", null); |
| 702 |
} |
| 703 |
|
| 704 |
public void testQualifiedNameReduction5a() throws Exception { |
| 705 |
testTypes("var x = {/** @type {string} */ a:'b' };\n" + |
| 706 |
"/** @return {string} */ var f = function() {\n" + |
| 707 |
"return x.a; }"); |
| 708 |
} |
| 709 |
|
| 710 |
public void testQualifiedNameReduction5b() throws Exception { |
| 711 |
testTypes( |
| 712 |
"var x = {/** @type {number} */ a:12 };\n" + |
| 713 |
"/** @return {string} */\n" + |
| 714 |
"var f = function() {\n" + |
| 715 |
" return x.a;\n" + |
| 716 |
"}", |
| 717 |
"inconsistent return type\n" + |
| 718 |
"found : number\n" + |
| 719 |
"required: string"); |
| 720 |
} |
| 721 |
|
| 722 |
public void testQualifiedNameReduction5c() throws Exception { |
| 723 |
testTypes( |
| 724 |
"/** @return {string} */ var f = function() {\n" + |
| 725 |
"var x = {/** @type {number} */ a:0 };\n" + |
| 726 |
"return (x.a) ? (x.a) : 'a'; }", |
| 727 |
"inconsistent return type\n" + |
| 728 |
"found : (number|string)\n" + |
| 729 |
"required: string"); |
| 730 |
} |
| 731 |
|
| 732 |
public void testQualifiedNameReduction6() throws Exception { |
| 733 |
testTypes( |
| 734 |
"/** @return {string} */ var f = function() {\n" + |
| 735 |
"var x = {/** @return {string?} */ get a() {return 'a'}};\n" + |
| 736 |
"return x.a ? x.a : 'a'; }"); |
| 737 |
} |
| 738 |
|
| 739 |
public void testQualifiedNameReduction7() throws Exception { |
| 740 |
testTypes( |
| 741 |
"/** @return {string} */ var f = function() {\n" + |
| 742 |
"var x = {/** @return {number} */ get a() {return 12}};\n" + |
| 743 |
"return x.a; }", |
| 744 |
"inconsistent return type\n" + |
| 745 |
"found : number\n" + |
| 746 |
"required: string"); |
| 747 |
} |
| 748 |
|
| 749 |
public void testQualifiedNameReduction7a() throws Exception { |
| 750 |
|
| 751 |
testTypes( |
| 752 |
"/** @return {string} */ var f = function() {\n" + |
| 753 |
"var x = {get a() {return 12}};\n" + |
| 754 |
"return x.a; }"); |
| 755 |
} |
| 756 |
|
| 757 |
public void testQualifiedNameReduction8() throws Exception { |
| 758 |
testTypes( |
| 759 |
"/** @return {string} */ var f = function() {\n" + |
| 760 |
"var x = {get a() {return 'a'}};\n" + |
| 761 |
"return x.a ? x.a : 'a'; }"); |
| 762 |
} |
| 763 |
|
| 764 |
public void testQualifiedNameReduction9() throws Exception { |
| 765 |
testTypes( |
| 766 |
"/** @return {string} */ var f = function() {\n" + |
| 767 |
"var x = { /** @param {string} b */ set a(b) {}};\n" + |
| 768 |
"return x.a ? x.a : 'a'; }"); |
| 769 |
} |
| 770 |
|
| 771 |
public void testQualifiedNameReduction10() throws Exception { |
| 772 |
|
| 773 |
|
| 774 |
testTypes( |
| 775 |
"/** @return {string} */ var f = function() {\n" + |
| 776 |
"var x = { /** @param {number} b */ set a(b) {}};\n" + |
| 777 |
"return x.a ? x.a : 'a'; }", |
| 778 |
"inconsistent return type\n" + |
| 779 |
"found : (number|string)\n" + |
| 780 |
"required: string"); |
| 781 |
} |
| 782 |
|
| 783 |
public void testObjLitDef1a() throws Exception { |
| 784 |
testTypes( |
| 785 |
"var x = {/** @type {number} */ a:12 };\n" + |
| 786 |
"x.a = 'a';", |
| 787 |
"assignment to property a of x\n" + |
| 788 |
"found : string\n" + |
| 789 |
"required: number"); |
| 790 |
} |
| 791 |
|
| 792 |
public void testObjLitDef1b() throws Exception { |
| 793 |
testTypes( |
| 794 |
"function f(){" + |
| 795 |
"var x = {/** @type {number} */ a:12 };\n" + |
| 796 |
"x.a = 'a';" + |
| 797 |
"};\n" + |
| 798 |
"f();", |
| 799 |
"assignment to property a of x\n" + |
| 800 |
"found : string\n" + |
| 801 |
"required: number"); |
| 802 |
} |
| 803 |
|
| 804 |
public void testObjLitDef2a() throws Exception { |
| 805 |
testTypes( |
| 806 |
"var x = {/** @param {number} b */ set a(b){} };\n" + |
| 807 |
"x.a = 'a';", |
| 808 |
"assignment to property a of x\n" + |
| 809 |
"found : string\n" + |
| 810 |
"required: number"); |
| 811 |
} |
| 812 |
|
| 813 |
public void testObjLitDef2b() throws Exception { |
| 814 |
testTypes( |
| 815 |
"function f(){" + |
| 816 |
"var x = {/** @param {number} b */ set a(b){} };\n" + |
| 817 |
"x.a = 'a';" + |
| 818 |
"};\n" + |
| 819 |
"f();", |
| 820 |
"assignment to property a of x\n" + |
| 821 |
"found : string\n" + |
| 822 |
"required: number"); |
| 823 |
} |
| 824 |
|
| 825 |
public void testObjLitDef3a() throws Exception { |
| 826 |
testTypes( |
| 827 |
"/** @type {string} */ var y;\n" + |
| 828 |
"var x = {/** @return {number} */ get a(){} };\n" + |
| 829 |
"y = x.a;", |
| 830 |
"assignment\n" + |
| 831 |
"found : number\n" + |
| 832 |
"required: string"); |
| 833 |
} |
| 834 |
|
| 835 |
public void testObjLitDef3b() throws Exception { |
| 836 |
testTypes( |
| 837 |
"/** @type {string} */ var y;\n" + |
| 838 |
"function f(){" + |
| 839 |
"var x = {/** @return {number} */ get a(){} };\n" + |
| 840 |
"y = x.a;" + |
| 841 |
"};\n" + |
| 842 |
"f();", |
| 843 |
"assignment\n" + |
| 844 |
"found : number\n" + |
| 845 |
"required: string"); |
| 846 |
} |
| 847 |
|
| 848 |
public void testObjLitDef4() throws Exception { |
| 849 |
testTypes( |
| 850 |
"var x = {" + |
| 851 |
"/** @return {number} */ a:12 };\n", |
| 852 |
"assignment to property a of {a: function (): number}\n" + |
| 853 |
"found : number\n" + |
| 854 |
"required: function (): number"); |
| 855 |
} |
| 856 |
|
| 857 |
public void testObjLitDef5() throws Exception { |
| 858 |
testTypes( |
| 859 |
"var x = {};\n" + |
| 860 |
"/** @return {number} */ x.a = 12;\n", |
| 861 |
"assignment to property a of x\n" + |
| 862 |
"found : number\n" + |
| 863 |
"required: function (): number"); |
| 864 |
} |
| 865 |
|
| 866 |
public void testObjLitDef6() throws Exception { |
| 867 |
testTypes("var lit = /** @struct */ { 'x': 1 };", |
| 868 |
"Illegal key, the object literal is a struct"); |
| 869 |
} |
| 870 |
|
| 871 |
public void testObjLitDef7() throws Exception { |
| 872 |
testTypes("var lit = /** @dict */ { x: 1 };", |
| 873 |
"Illegal key, the object literal is a dict"); |
| 874 |
} |
| 875 |
|
| 876 |
public void testInstanceOfReduction1() throws Exception { |
| 877 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 878 |
"/** @param {T|string} x\n@return {T} */\n" + |
| 879 |
"var f = function(x) {\n" + |
| 880 |
"if (x instanceof T) { return x; } else { return new T(); }\n" + |
| 881 |
"};"); |
| 882 |
} |
| 883 |
|
| 884 |
public void testInstanceOfReduction2() throws Exception { |
| 885 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 886 |
"/** @param {!T|string} x\n@return {string} */\n" + |
| 887 |
"var f = function(x) {\n" + |
| 888 |
"if (x instanceof T) { return ''; } else { return x; }\n" + |
| 889 |
"};"); |
| 890 |
} |
| 891 |
|
| 892 |
public void testUndeclaredGlobalProperty1() throws Exception { |
| 893 |
testTypes("/** @const */ var x = {}; x.y = null;" + |
| 894 |
"function f(a) { x.y = a; }" + |
| 895 |
"/** @param {string} a */ function g(a) { }" + |
| 896 |
"function h() { g(x.y); }"); |
| 897 |
} |
| 898 |
|
| 899 |
public void testUndeclaredGlobalProperty2() throws Exception { |
| 900 |
testTypes("/** @const */ var x = {}; x.y = null;" + |
| 901 |
"function f() { x.y = 3; }" + |
| 902 |
"/** @param {string} a */ function g(a) { }" + |
| 903 |
"function h() { g(x.y); }", |
| 904 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 905 |
"found : (null|number)\n" + |
| 906 |
"required: string"); |
| 907 |
} |
| 908 |
|
| 909 |
public void testLocallyInferredGlobalProperty1() throws Exception { |
| 910 |
|
| 911 |
testTypes( |
| 912 |
"/** @constructor */ function F() {}" + |
| 913 |
"/** @type {number} */ F.prototype.z;" + |
| 914 |
"/** @const */ var x = {}; /** @type {F} */ x.y;" + |
| 915 |
"function f() { x.y.z = 'abc'; }" + |
| 916 |
"/** @param {number} x */ function g(x) {}" + |
| 917 |
"function h() { g(x.y.z); }", |
| 918 |
"assignment to property z of F\n" + |
| 919 |
"found : string\n" + |
| 920 |
"required: number"); |
| 921 |
} |
| 922 |
|
| 923 |
public void testPropertyInferredPropagation() throws Exception { |
| 924 |
testTypes("/** @return {Object} */function f() { return {}; }\n" + |
| 925 |
"function g() { var x = f(); if (x.p) x.a = 'a'; else x.a = 'b'; }\n" + |
| 926 |
"function h() { var x = f(); x.a = false; }"); |
| 927 |
} |
| 928 |
|
| 929 |
public void testPropertyInference1() throws Exception { |
| 930 |
testTypes( |
| 931 |
"/** @constructor */ function F() { this.x_ = true; }" + |
| 932 |
"/** @return {string} */" + |
| 933 |
"F.prototype.bar = function() { if (this.x_) return this.x_; };", |
| 934 |
"inconsistent return type\n" + |
| 935 |
"found : boolean\n" + |
| 936 |
"required: string"); |
| 937 |
} |
| 938 |
|
| 939 |
public void testPropertyInference2() throws Exception { |
| 940 |
testTypes( |
| 941 |
"/** @constructor */ function F() { this.x_ = true; }" + |
| 942 |
"F.prototype.baz = function() { this.x_ = null; };" + |
| 943 |
"/** @return {string} */" + |
| 944 |
"F.prototype.bar = function() { if (this.x_) return this.x_; };", |
| 945 |
"inconsistent return type\n" + |
| 946 |
"found : boolean\n" + |
| 947 |
"required: string"); |
| 948 |
} |
| 949 |
|
| 950 |
public void testPropertyInference3() throws Exception { |
| 951 |
testTypes( |
| 952 |
"/** @constructor */ function F() { this.x_ = true; }" + |
| 953 |
"F.prototype.baz = function() { this.x_ = 3; };" + |
| 954 |
"/** @return {string} */" + |
| 955 |
"F.prototype.bar = function() { if (this.x_) return this.x_; };", |
| 956 |
"inconsistent return type\n" + |
| 957 |
"found : (boolean|number)\n" + |
| 958 |
"required: string"); |
| 959 |
} |
| 960 |
|
| 961 |
public void testPropertyInference4() throws Exception { |
| 962 |
testTypes( |
| 963 |
"/** @constructor */ function F() { }" + |
| 964 |
"F.prototype.x_ = 3;" + |
| 965 |
"/** @return {string} */" + |
| 966 |
"F.prototype.bar = function() { if (this.x_) return this.x_; };", |
| 967 |
"inconsistent return type\n" + |
| 968 |
"found : number\n" + |
| 969 |
"required: string"); |
| 970 |
} |
| 971 |
|
| 972 |
public void testPropertyInference5() throws Exception { |
| 973 |
testTypes( |
| 974 |
"/** @constructor */ function F() { }" + |
| 975 |
"F.prototype.baz = function() { this.x_ = 3; };" + |
| 976 |
"/** @return {string} */" + |
| 977 |
"F.prototype.bar = function() { if (this.x_) return this.x_; };"); |
| 978 |
} |
| 979 |
|
| 980 |
public void testPropertyInference6() throws Exception { |
| 981 |
testTypes( |
| 982 |
"/** @constructor */ function F() { }" + |
| 983 |
"(new F).x_ = 3;" + |
| 984 |
"/** @return {string} */" + |
| 985 |
"F.prototype.bar = function() { return this.x_; };"); |
| 986 |
} |
| 987 |
|
| 988 |
public void testPropertyInference7() throws Exception { |
| 989 |
testTypes( |
| 990 |
"/** @constructor */ function F() { this.x_ = true; }" + |
| 991 |
"(new F).x_ = 3;" + |
| 992 |
"/** @return {string} */" + |
| 993 |
"F.prototype.bar = function() { return this.x_; };", |
| 994 |
"inconsistent return type\n" + |
| 995 |
"found : boolean\n" + |
| 996 |
"required: string"); |
| 997 |
} |
| 998 |
|
| 999 |
public void testPropertyInference8() throws Exception { |
| 1000 |
testTypes( |
| 1001 |
"/** @constructor */ function F() { " + |
| 1002 |
" /** @type {string} */ this.x_ = 'x';" + |
| 1003 |
"}" + |
| 1004 |
"(new F).x_ = 3;" + |
| 1005 |
"/** @return {string} */" + |
| 1006 |
"F.prototype.bar = function() { return this.x_; };", |
| 1007 |
"assignment to property x_ of F\n" + |
| 1008 |
"found : number\n" + |
| 1009 |
"required: string"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
public void testPropertyInference9() throws Exception { |
| 1013 |
testTypes( |
| 1014 |
"/** @constructor */ function A() {}" + |
| 1015 |
"/** @return {function(): ?} */ function f() { " + |
| 1016 |
" return function() {};" + |
| 1017 |
"}" + |
| 1018 |
"var g = f();" + |
| 1019 |
"/** @type {number} */ g.prototype.bar_ = null;", |
| 1020 |
"assignment\n" + |
| 1021 |
"found : null\n" + |
| 1022 |
"required: number"); |
| 1023 |
} |
| 1024 |
|
| 1025 |
public void testPropertyInference10() throws Exception { |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
testTypes( |
| 1031 |
"/** @constructor */ function A() {}" + |
| 1032 |
"/** @return {function(): ?} */ function f() { " + |
| 1033 |
" return function() {};" + |
| 1034 |
"}" + |
| 1035 |
"var g = f();" + |
| 1036 |
"/** @type {number} */ g.prototype.bar_ = 1;" + |
| 1037 |
"var h = f();" + |
| 1038 |
"/** @type {string} */ h.prototype.bar_ = 1;", |
| 1039 |
"assignment\n" + |
| 1040 |
"found : number\n" + |
| 1041 |
"required: string"); |
| 1042 |
} |
| 1043 |
|
| 1044 |
public void testNoPersistentTypeInferenceForObjectProperties() |
| 1045 |
throws Exception { |
| 1046 |
testTypes("/** @param {Object} o\n@param {string} x */\n" + |
| 1047 |
"function s1(o,x) { o.x = x; }\n" + |
| 1048 |
"/** @param {Object} o\n@return {string} */\n" + |
| 1049 |
"function g1(o) { return typeof o.x == 'undefined' ? '' : o.x; }\n" + |
| 1050 |
"/** @param {Object} o\n@param {number} x */\n" + |
| 1051 |
"function s2(o,x) { o.x = x; }\n" + |
| 1052 |
"/** @param {Object} o\n@return {number} */\n" + |
| 1053 |
"function g2(o) { return typeof o.x == 'undefined' ? 0 : o.x; }"); |
| 1054 |
} |
| 1055 |
|
| 1056 |
public void testNoPersistentTypeInferenceForFunctionProperties() |
| 1057 |
throws Exception { |
| 1058 |
testTypes("/** @param {Function} o\n@param {string} x */\n" + |
| 1059 |
"function s1(o,x) { o.x = x; }\n" + |
| 1060 |
"/** @param {Function} o\n@return {string} */\n" + |
| 1061 |
"function g1(o) { return typeof o.x == 'undefined' ? '' : o.x; }\n" + |
| 1062 |
"/** @param {Function} o\n@param {number} x */\n" + |
| 1063 |
"function s2(o,x) { o.x = x; }\n" + |
| 1064 |
"/** @param {Function} o\n@return {number} */\n" + |
| 1065 |
"function g2(o) { return typeof o.x == 'undefined' ? 0 : o.x; }"); |
| 1066 |
} |
| 1067 |
|
| 1068 |
public void testObjectPropertyTypeInferredInLocalScope1() throws Exception { |
| 1069 |
testTypes("/** @param {!Object} o\n@return {string} */\n" + |
| 1070 |
"function f(o) { o.x = 1; return o.x; }", |
| 1071 |
"inconsistent return type\n" + |
| 1072 |
"found : number\n" + |
| 1073 |
"required: string"); |
| 1074 |
} |
| 1075 |
|
| 1076 |
public void testObjectPropertyTypeInferredInLocalScope2() throws Exception { |
| 1077 |
testTypes("/**@param {!Object} o\n@param {number?} x\n@return {string}*/" + |
| 1078 |
"function f(o, x) { o.x = 'a';\nif (x) {o.x = x;}\nreturn o.x; }", |
| 1079 |
"inconsistent return type\n" + |
| 1080 |
"found : (number|string)\n" + |
| 1081 |
"required: string"); |
| 1082 |
} |
| 1083 |
|
| 1084 |
public void testObjectPropertyTypeInferredInLocalScope3() throws Exception { |
| 1085 |
testTypes("/**@param {!Object} o\n@param {number?} x\n@return {string}*/" + |
| 1086 |
"function f(o, x) { if (x) {o.x = x;} else {o.x = 'a';}\nreturn o.x; }", |
| 1087 |
"inconsistent return type\n" + |
| 1088 |
"found : (number|string)\n" + |
| 1089 |
"required: string"); |
| 1090 |
} |
| 1091 |
|
| 1092 |
public void testMismatchingOverridingInferredPropertyBeforeDeclaredProperty1() |
| 1093 |
throws Exception { |
| 1094 |
testTypes("/** @constructor */var T = function() { this.x = ''; };\n" + |
| 1095 |
"/** @type {number} */ T.prototype.x = 0;", |
| 1096 |
"assignment to property x of T\n" + |
| 1097 |
"found : string\n" + |
| 1098 |
"required: number"); |
| 1099 |
} |
| 1100 |
|
| 1101 |
public void testMismatchingOverridingInferredPropertyBeforeDeclaredProperty2() |
| 1102 |
throws Exception { |
| 1103 |
testTypes("/** @constructor */var T = function() { this.x = ''; };\n" + |
| 1104 |
"/** @type {number} */ T.prototype.x;", |
| 1105 |
"assignment to property x of T\n" + |
| 1106 |
"found : string\n" + |
| 1107 |
"required: number"); |
| 1108 |
} |
| 1109 |
|
| 1110 |
public void testMismatchingOverridingInferredPropertyBeforeDeclaredProperty3() |
| 1111 |
throws Exception { |
| 1112 |
testTypes("/** @type {Object} */ var n = {};\n" + |
| 1113 |
"/** @constructor */ n.T = function() { this.x = ''; };\n" + |
| 1114 |
"/** @type {number} */ n.T.prototype.x = 0;", |
| 1115 |
"assignment to property x of n.T\n" + |
| 1116 |
"found : string\n" + |
| 1117 |
"required: number"); |
| 1118 |
} |
| 1119 |
|
| 1120 |
public void testMismatchingOverridingInferredPropertyBeforeDeclaredProperty4() |
| 1121 |
throws Exception { |
| 1122 |
testTypes("var n = {};\n" + |
| 1123 |
"/** @constructor */ n.T = function() { this.x = ''; };\n" + |
| 1124 |
"/** @type {number} */ n.T.prototype.x = 0;", |
| 1125 |
"assignment to property x of n.T\n" + |
| 1126 |
"found : string\n" + |
| 1127 |
"required: number"); |
| 1128 |
} |
| 1129 |
|
| 1130 |
public void testPropertyUsedBeforeDefinition1() throws Exception { |
| 1131 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 1132 |
"/** @return {string} */" + |
| 1133 |
"T.prototype.f = function() { return this.g(); };\n" + |
| 1134 |
"/** @return {number} */ T.prototype.g = function() { return 1; };\n", |
| 1135 |
"inconsistent return type\n" + |
| 1136 |
"found : number\n" + |
| 1137 |
"required: string"); |
| 1138 |
} |
| 1139 |
|
| 1140 |
public void testPropertyUsedBeforeDefinition2() throws Exception { |
| 1141 |
testTypes("var n = {};\n" + |
| 1142 |
"/** @constructor */ n.T = function() {};\n" + |
| 1143 |
"/** @return {string} */" + |
| 1144 |
"n.T.prototype.f = function() { return this.g(); };\n" + |
| 1145 |
"/** @return {number} */ n.T.prototype.g = function() { return 1; };\n", |
| 1146 |
"inconsistent return type\n" + |
| 1147 |
"found : number\n" + |
| 1148 |
"required: string"); |
| 1149 |
} |
| 1150 |
|
| 1151 |
public void testAdd1() throws Exception { |
| 1152 |
testTypes("/**@return {void}*/function foo(){var a = 'abc'+foo();}"); |
| 1153 |
} |
| 1154 |
|
| 1155 |
public void testAdd2() throws Exception { |
| 1156 |
testTypes("/**@return {void}*/function foo(){var a = foo()+4;}"); |
| 1157 |
} |
| 1158 |
|
| 1159 |
public void testAdd3() throws Exception { |
| 1160 |
testTypes("/** @type {string} */ var a = 'a';" + |
| 1161 |
"/** @type {string} */ var b = 'b';" + |
| 1162 |
"/** @type {string} */ var c = a + b;"); |
| 1163 |
} |
| 1164 |
|
| 1165 |
public void testAdd4() throws Exception { |
| 1166 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1167 |
"/** @type {string} */ var b = 'b';" + |
| 1168 |
"/** @type {string} */ var c = a + b;"); |
| 1169 |
} |
| 1170 |
|
| 1171 |
public void testAdd5() throws Exception { |
| 1172 |
testTypes("/** @type {string} */ var a = 'a';" + |
| 1173 |
"/** @type {number} */ var b = 5;" + |
| 1174 |
"/** @type {string} */ var c = a + b;"); |
| 1175 |
} |
| 1176 |
|
| 1177 |
public void testAdd6() throws Exception { |
| 1178 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1179 |
"/** @type {number} */ var b = 5;" + |
| 1180 |
"/** @type {number} */ var c = a + b;"); |
| 1181 |
} |
| 1182 |
|
| 1183 |
public void testAdd7() throws Exception { |
| 1184 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1185 |
"/** @type {string} */ var b = 'b';" + |
| 1186 |
"/** @type {number} */ var c = a + b;", |
| 1187 |
"initializing variable\n" + |
| 1188 |
"found : string\n" + |
| 1189 |
"required: number"); |
| 1190 |
} |
| 1191 |
|
| 1192 |
public void testAdd8() throws Exception { |
| 1193 |
testTypes("/** @type {string} */ var a = 'a';" + |
| 1194 |
"/** @type {number} */ var b = 5;" + |
| 1195 |
"/** @type {number} */ var c = a + b;", |
| 1196 |
"initializing variable\n" + |
| 1197 |
"found : string\n" + |
| 1198 |
"required: number"); |
| 1199 |
} |
| 1200 |
|
| 1201 |
public void testAdd9() throws Exception { |
| 1202 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1203 |
"/** @type {number} */ var b = 5;" + |
| 1204 |
"/** @type {string} */ var c = a + b;", |
| 1205 |
"initializing variable\n" + |
| 1206 |
"found : number\n" + |
| 1207 |
"required: string"); |
| 1208 |
} |
| 1209 |
|
| 1210 |
public void testAdd10() throws Exception { |
| 1211 |
|
| 1212 |
testTypes( |
| 1213 |
suppressMissingProperty("e", "f") + |
| 1214 |
"/** @type {number} */ var a = 5;" + |
| 1215 |
"/** @type {string} */ var c = a + d.e.f;"); |
| 1216 |
} |
| 1217 |
|
| 1218 |
public void testAdd11() throws Exception { |
| 1219 |
|
| 1220 |
testTypes( |
| 1221 |
suppressMissingProperty("e", "f") + |
| 1222 |
"/** @type {number} */ var a = 5;" + |
| 1223 |
"/** @type {number} */ var c = a + d.e.f;"); |
| 1224 |
} |
| 1225 |
|
| 1226 |
public void testAdd12() throws Exception { |
| 1227 |
testTypes("/** @return {(number,string)} */ function a() { return 5; }" + |
| 1228 |
"/** @type {number} */ var b = 5;" + |
| 1229 |
"/** @type {boolean} */ var c = a() + b;", |
| 1230 |
"initializing variable\n" + |
| 1231 |
"found : (number|string)\n" + |
| 1232 |
"required: boolean"); |
| 1233 |
} |
| 1234 |
|
| 1235 |
public void testAdd13() throws Exception { |
| 1236 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1237 |
"/** @return {(number,string)} */ function b() { return 5; }" + |
| 1238 |
"/** @type {boolean} */ var c = a + b();", |
| 1239 |
"initializing variable\n" + |
| 1240 |
"found : (number|string)\n" + |
| 1241 |
"required: boolean"); |
| 1242 |
} |
| 1243 |
|
| 1244 |
public void testAdd14() throws Exception { |
| 1245 |
testTypes("/** @type {(null,string)} */ var a = null;" + |
| 1246 |
"/** @type {number} */ var b = 5;" + |
| 1247 |
"/** @type {boolean} */ var c = a + b;", |
| 1248 |
"initializing variable\n" + |
| 1249 |
"found : (number|string)\n" + |
| 1250 |
"required: boolean"); |
| 1251 |
} |
| 1252 |
|
| 1253 |
public void testAdd15() throws Exception { |
| 1254 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1255 |
"/** @return {(number,string)} */ function b() { return 5; }" + |
| 1256 |
"/** @type {boolean} */ var c = a + b();", |
| 1257 |
"initializing variable\n" + |
| 1258 |
"found : (number|string)\n" + |
| 1259 |
"required: boolean"); |
| 1260 |
} |
| 1261 |
|
| 1262 |
public void testAdd16() throws Exception { |
| 1263 |
testTypes("/** @type {(undefined,string)} */ var a = undefined;" + |
| 1264 |
"/** @type {number} */ var b = 5;" + |
| 1265 |
"/** @type {boolean} */ var c = a + b;", |
| 1266 |
"initializing variable\n" + |
| 1267 |
"found : (number|string)\n" + |
| 1268 |
"required: boolean"); |
| 1269 |
} |
| 1270 |
|
| 1271 |
public void testAdd17() throws Exception { |
| 1272 |
testTypes("/** @type {number} */ var a = 5;" + |
| 1273 |
"/** @type {(undefined,string)} */ var b = undefined;" + |
| 1274 |
"/** @type {boolean} */ var c = a + b;", |
| 1275 |
"initializing variable\n" + |
| 1276 |
"found : (number|string)\n" + |
| 1277 |
"required: boolean"); |
| 1278 |
} |
| 1279 |
|
| 1280 |
public void testAdd18() throws Exception { |
| 1281 |
testTypes("function f() {};" + |
| 1282 |
"/** @type {string} */ var a = 'a';" + |
| 1283 |
"/** @type {number} */ var c = a + f();", |
| 1284 |
"initializing variable\n" + |
| 1285 |
"found : string\n" + |
| 1286 |
"required: number"); |
| 1287 |
} |
| 1288 |
|
| 1289 |
public void testAdd19() throws Exception { |
| 1290 |
testTypes("/** @param {number} opt_x\n@param {number} opt_y\n" + |
| 1291 |
"@return {number} */ function f(opt_x, opt_y) {" + |
| 1292 |
"return opt_x + opt_y;}"); |
| 1293 |
} |
| 1294 |
|
| 1295 |
public void testAdd20() throws Exception { |
| 1296 |
testTypes("/** @param {!Number} opt_x\n@param {!Number} opt_y\n" + |
| 1297 |
"@return {number} */ function f(opt_x, opt_y) {" + |
| 1298 |
"return opt_x + opt_y;}"); |
| 1299 |
} |
| 1300 |
|
| 1301 |
public void testAdd21() throws Exception { |
| 1302 |
testTypes("/** @param {Number|Boolean} opt_x\n" + |
| 1303 |
"@param {number|boolean} opt_y\n" + |
| 1304 |
"@return {number} */ function f(opt_x, opt_y) {" + |
| 1305 |
"return opt_x + opt_y;}"); |
| 1306 |
} |
| 1307 |
|
| 1308 |
public void testNumericComparison1() throws Exception { |
| 1309 |
testTypes("/**@param {number} a*/ function f(a) {return a < 3;}"); |
| 1310 |
} |
| 1311 |
|
| 1312 |
public void testNumericComparison2() throws Exception { |
| 1313 |
testTypes("/**@param {!Object} a*/ function f(a) {return a < 3;}", |
| 1314 |
"left side of numeric comparison\n" + |
| 1315 |
"found : Object\n" + |
| 1316 |
"required: number"); |
| 1317 |
} |
| 1318 |
|
| 1319 |
public void testNumericComparison3() throws Exception { |
| 1320 |
testTypes("/**@param {string} a*/ function f(a) {return a < 3;}"); |
| 1321 |
} |
| 1322 |
|
| 1323 |
public void testNumericComparison4() throws Exception { |
| 1324 |
testTypes("/**@param {(number,undefined)} a*/ " + |
| 1325 |
"function f(a) {return a < 3;}"); |
| 1326 |
} |
| 1327 |
|
| 1328 |
public void testNumericComparison5() throws Exception { |
| 1329 |
testTypes("/**@param {*} a*/ function f(a) {return a < 3;}", |
| 1330 |
"left side of numeric comparison\n" + |
| 1331 |
"found : *\n" + |
| 1332 |
"required: number"); |
| 1333 |
} |
| 1334 |
|
| 1335 |
public void testNumericComparison6() throws Exception { |
| 1336 |
testTypes("/**@return {void} */ function foo() { if (3 >= foo()) return; }", |
| 1337 |
"right side of numeric comparison\n" + |
| 1338 |
"found : undefined\n" + |
| 1339 |
"required: number"); |
| 1340 |
} |
| 1341 |
|
| 1342 |
public void testStringComparison1() throws Exception { |
| 1343 |
testTypes("/**@param {string} a*/ function f(a) {return a < 'x';}"); |
| 1344 |
} |
| 1345 |
|
| 1346 |
public void testStringComparison2() throws Exception { |
| 1347 |
testTypes("/**@param {Object} a*/ function f(a) {return a < 'x';}"); |
| 1348 |
} |
| 1349 |
|
| 1350 |
public void testStringComparison3() throws Exception { |
| 1351 |
testTypes("/**@param {number} a*/ function f(a) {return a < 'x';}"); |
| 1352 |
} |
| 1353 |
|
| 1354 |
public void testStringComparison4() throws Exception { |
| 1355 |
testTypes("/**@param {string|undefined} a*/ " + |
| 1356 |
"function f(a) {return a < 'x';}"); |
| 1357 |
} |
| 1358 |
|
| 1359 |
public void testStringComparison5() throws Exception { |
| 1360 |
testTypes("/**@param {*} a*/ " + |
| 1361 |
"function f(a) {return a < 'x';}"); |
| 1362 |
} |
| 1363 |
|
| 1364 |
public void testStringComparison6() throws Exception { |
| 1365 |
testTypes("/**@return {void} */ " + |
| 1366 |
"function foo() { if ('a' >= foo()) return; }", |
| 1367 |
"right side of comparison\n" + |
| 1368 |
"found : undefined\n" + |
| 1369 |
"required: string"); |
| 1370 |
} |
| 1371 |
|
| 1372 |
public void testValueOfComparison1() throws Exception { |
| 1373 |
testTypes("/** @constructor */function O() {};" + |
| 1374 |
"/**@override*/O.prototype.valueOf = function() { return 1; };" + |
| 1375 |
"/**@param {!O} a\n@param {!O} b*/ function f(a,b) { return a < b; }"); |
| 1376 |
} |
| 1377 |
|
| 1378 |
public void testValueOfComparison2() throws Exception { |
| 1379 |
testTypes("/** @constructor */function O() {};" + |
| 1380 |
"/**@override*/O.prototype.valueOf = function() { return 1; };" + |
| 1381 |
"/**@param {!O} a\n@param {number} b*/" + |
| 1382 |
"function f(a,b) { return a < b; }"); |
| 1383 |
} |
| 1384 |
|
| 1385 |
public void testValueOfComparison3() throws Exception { |
| 1386 |
testTypes("/** @constructor */function O() {};" + |
| 1387 |
"/**@override*/O.prototype.toString = function() { return 'o'; };" + |
| 1388 |
"/**@param {!O} a\n@param {string} b*/" + |
| 1389 |
"function f(a,b) { return a < b; }"); |
| 1390 |
} |
| 1391 |
|
| 1392 |
public void testGenericRelationalExpression() throws Exception { |
| 1393 |
testTypes("/**@param {*} a\n@param {*} b*/ " + |
| 1394 |
"function f(a,b) {return a < b;}"); |
| 1395 |
} |
| 1396 |
|
| 1397 |
public void testInstanceof1() throws Exception { |
| 1398 |
testTypes("function foo(){" + |
| 1399 |
"if (bar instanceof 3)return;}", |
| 1400 |
"instanceof requires an object\n" + |
| 1401 |
"found : number\n" + |
| 1402 |
"required: Object"); |
| 1403 |
} |
| 1404 |
|
| 1405 |
public void testInstanceof2() throws Exception { |
| 1406 |
testTypes("/**@return {void}*/function foo(){" + |
| 1407 |
"if (foo() instanceof Object)return;}", |
| 1408 |
"deterministic instanceof yields false\n" + |
| 1409 |
"found : undefined\n" + |
| 1410 |
"required: NoObject"); |
| 1411 |
} |
| 1412 |
|
| 1413 |
public void testInstanceof3() throws Exception { |
| 1414 |
testTypes("/**@return {*} */function foo(){" + |
| 1415 |
"if (foo() instanceof Object)return;}"); |
| 1416 |
} |
| 1417 |
|
| 1418 |
public void testInstanceof4() throws Exception { |
| 1419 |
testTypes("/**@return {(Object|number)} */function foo(){" + |
| 1420 |
"if (foo() instanceof Object)return 3;}"); |
| 1421 |
} |
| 1422 |
|
| 1423 |
public void testInstanceof5() throws Exception { |
| 1424 |
|
| 1425 |
testTypes("/** @return {?} */ function foo(){" + |
| 1426 |
"if (foo() instanceof Object)return;}"); |
| 1427 |
} |
| 1428 |
|
| 1429 |
public void testInstanceof6() throws Exception { |
| 1430 |
testTypes("/**@return {(Array|number)} */function foo(){" + |
| 1431 |
"if (foo() instanceof Object)return 3;}"); |
| 1432 |
} |
| 1433 |
|
| 1434 |
public void testInstanceOfReduction3() throws Exception { |
| 1435 |
testTypes( |
| 1436 |
"/** \n" + |
| 1437 |
" * @param {Object} x \n" + |
| 1438 |
" * @param {Function} y \n" + |
| 1439 |
" * @return {boolean} \n" + |
| 1440 |
" */\n" + |
| 1441 |
"var f = function(x, y) {\n" + |
| 1442 |
" return x instanceof y;\n" + |
| 1443 |
"};"); |
| 1444 |
} |
| 1445 |
|
| 1446 |
public void testScoping1() throws Exception { |
| 1447 |
testTypes( |
| 1448 |
"/**@param {string} a*/function foo(a){" + |
| 1449 |
" /**@param {Array|string} a*/function bar(a){" + |
| 1450 |
" if (a instanceof Array)return;" + |
| 1451 |
" }" + |
| 1452 |
"}"); |
| 1453 |
} |
| 1454 |
|
| 1455 |
public void testScoping2() throws Exception { |
| 1456 |
testTypes( |
| 1457 |
"/** @type number */ var a;" + |
| 1458 |
"function Foo() {" + |
| 1459 |
" /** @type string */ var a;" + |
| 1460 |
"}"); |
| 1461 |
} |
| 1462 |
|
| 1463 |
public void testScoping3() throws Exception { |
| 1464 |
testTypes("\n\n/** @type{Number}*/var b;\n/** @type{!String} */var b;", |
| 1465 |
"variable b redefined with type String, original " + |
| 1466 |
"definition at [testcode]:3 with type (Number|null)"); |
| 1467 |
} |
| 1468 |
|
| 1469 |
public void testScoping4() throws Exception { |
| 1470 |
testTypes("/** @type{Number}*/var b; if (true) /** @type{!String} */var b;", |
| 1471 |
"variable b redefined with type String, original " + |
| 1472 |
"definition at [testcode]:1 with type (Number|null)"); |
| 1473 |
} |
| 1474 |
|
| 1475 |
public void testScoping5() throws Exception { |
| 1476 |
|
| 1477 |
|
| 1478 |
testTypes("if (true) var b; var b;"); |
| 1479 |
} |
| 1480 |
|
| 1481 |
public void testScoping6() throws Exception { |
| 1482 |
|
| 1483 |
|
| 1484 |
testTypes("if (true) var b; if (true) var b;"); |
| 1485 |
} |
| 1486 |
|
| 1487 |
public void testScoping7() throws Exception { |
| 1488 |
testTypes("/** @constructor */function A() {" + |
| 1489 |
" /** @type !A */this.a = null;" + |
| 1490 |
"}", |
| 1491 |
"assignment to property a of A\n" + |
| 1492 |
"found : null\n" + |
| 1493 |
"required: A"); |
| 1494 |
} |
| 1495 |
|
| 1496 |
public void testScoping8() throws Exception { |
| 1497 |
testTypes("/** @constructor */function A() {}" + |
| 1498 |
"/** @constructor */function B() {" + |
| 1499 |
" /** @type !A */this.a = null;" + |
| 1500 |
"}", |
| 1501 |
"assignment to property a of B\n" + |
| 1502 |
"found : null\n" + |
| 1503 |
"required: A"); |
| 1504 |
} |
| 1505 |
|
| 1506 |
public void testScoping9() throws Exception { |
| 1507 |
testTypes("/** @constructor */function B() {" + |
| 1508 |
" /** @type !A */this.a = null;" + |
| 1509 |
"}" + |
| 1510 |
"/** @constructor */function A() {}", |
| 1511 |
"assignment to property a of B\n" + |
| 1512 |
"found : null\n" + |
| 1513 |
"required: A"); |
| 1514 |
} |
| 1515 |
|
| 1516 |
public void testScoping10() throws Exception { |
| 1517 |
TypeCheckResult p = parseAndTypeCheckWithScope("var a = function b(){};"); |
| 1518 |
|
| 1519 |
|
| 1520 |
assertTrue(p.scope.isDeclared("a", false)); |
| 1521 |
assertFalse(p.scope.isDeclared("b", false)); |
| 1522 |
|
| 1523 |
|
| 1524 |
assertEquals("function (): undefined", |
| 1525 |
p.scope.getVar("a").getType().toString()); |
| 1526 |
} |
| 1527 |
|
| 1528 |
public void testScoping11() throws Exception { |
| 1529 |
|
| 1530 |
|
| 1531 |
testTypes( |
| 1532 |
"/** @return {number} */var a = function b(){ return b };", |
| 1533 |
"inconsistent return type\n" + |
| 1534 |
"found : function (): number\n" + |
| 1535 |
"required: number"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
public void testScoping12() throws Exception { |
| 1539 |
testTypes( |
| 1540 |
"/** @constructor */ function F() {}" + |
| 1541 |
"/** @type {number} */ F.prototype.bar = 3;" + |
| 1542 |
"/** @param {!F} f */ function g(f) {" + |
| 1543 |
" /** @return {string} */" + |
| 1544 |
" function h() {" + |
| 1545 |
" return f.bar;" + |
| 1546 |
" }" + |
| 1547 |
"}", |
| 1548 |
"inconsistent return type\n" + |
| 1549 |
"found : number\n" + |
| 1550 |
"required: string"); |
| 1551 |
} |
| 1552 |
|
| 1553 |
public void testFunctionArguments1() throws Exception { |
| 1554 |
testFunctionType( |
| 1555 |
"/** @param {number} a\n@return {string} */" + |
| 1556 |
"function f(a) {}", |
| 1557 |
"function (number): string"); |
| 1558 |
} |
| 1559 |
|
| 1560 |
public void testFunctionArguments2() throws Exception { |
| 1561 |
testFunctionType( |
| 1562 |
"/** @param {number} opt_a\n@return {string} */" + |
| 1563 |
"function f(opt_a) {}", |
| 1564 |
"function (number=): string"); |
| 1565 |
} |
| 1566 |
|
| 1567 |
public void testFunctionArguments3() throws Exception { |
| 1568 |
testFunctionType( |
| 1569 |
"/** @param {number} b\n@return {string} */" + |
| 1570 |
"function f(a,b) {}", |
| 1571 |
"function (?, number): string"); |
| 1572 |
} |
| 1573 |
|
| 1574 |
public void testFunctionArguments4() throws Exception { |
| 1575 |
testFunctionType( |
| 1576 |
"/** @param {number} opt_a\n@return {string} */" + |
| 1577 |
"function f(a,opt_a) {}", |
| 1578 |
"function (?, number=): string"); |
| 1579 |
} |
| 1580 |
|
| 1581 |
public void testFunctionArguments5() throws Exception { |
| 1582 |
testTypes( |
| 1583 |
"function a(opt_a,a) {}", |
| 1584 |
"optional arguments must be at the end"); |
| 1585 |
} |
| 1586 |
|
| 1587 |
public void testFunctionArguments6() throws Exception { |
| 1588 |
testTypes( |
| 1589 |
"function a(var_args,a) {}", |
| 1590 |
"variable length argument must be last"); |
| 1591 |
} |
| 1592 |
|
| 1593 |
public void testFunctionArguments7() throws Exception { |
| 1594 |
testTypes( |
| 1595 |
"/** @param {number} opt_a\n@return {string} */" + |
| 1596 |
"function a(a,opt_a,var_args) {}"); |
| 1597 |
} |
| 1598 |
|
| 1599 |
public void testFunctionArguments8() throws Exception { |
| 1600 |
testTypes( |
| 1601 |
"function a(a,opt_a,var_args,b) {}", |
| 1602 |
"variable length argument must be last"); |
| 1603 |
} |
| 1604 |
|
| 1605 |
public void testFunctionArguments9() throws Exception { |
| 1606 |
|
| 1607 |
testTypes( |
| 1608 |
"function a(a,opt_a,var_args,b,c) {}", |
| 1609 |
"variable length argument must be last"); |
| 1610 |
} |
| 1611 |
|
| 1612 |
public void testFunctionArguments10() throws Exception { |
| 1613 |
|
| 1614 |
testTypes( |
| 1615 |
"function a(a,opt_a,b,c) {}", |
| 1616 |
"optional arguments must be at the end"); |
| 1617 |
} |
| 1618 |
|
| 1619 |
public void testFunctionArguments11() throws Exception { |
| 1620 |
testTypes( |
| 1621 |
"function a(a,opt_a,b,c,var_args,d) {}", |
| 1622 |
"optional arguments must be at the end"); |
| 1623 |
} |
| 1624 |
|
| 1625 |
public void testFunctionArguments12() throws Exception { |
| 1626 |
testTypes("/** @param foo {String} */function bar(baz){}", |
| 1627 |
"parameter foo does not appear in bar's parameter list"); |
| 1628 |
} |
| 1629 |
|
| 1630 |
public void testFunctionArguments13() throws Exception { |
| 1631 |
|
| 1632 |
testTypes( |
| 1633 |
"/** @return {boolean} */ function u() { return true; }" + |
| 1634 |
"/** @param {boolean} b\n@return {?boolean} */" + |
| 1635 |
"function f(b) { if (u()) { b = null; } return b; }", |
| 1636 |
"assignment\n" + |
| 1637 |
"found : null\n" + |
| 1638 |
"required: boolean"); |
| 1639 |
} |
| 1640 |
|
| 1641 |
public void testFunctionArguments14() throws Exception { |
| 1642 |
testTypes( |
| 1643 |
"/**\n" + |
| 1644 |
" * @param {string} x\n" + |
| 1645 |
" * @param {number} opt_y\n" + |
| 1646 |
" * @param {boolean} var_args\n" + |
| 1647 |
" */ function f(x, opt_y, var_args) {}" + |
| 1648 |
"f('3'); f('3', 2); f('3', 2, true); f('3', 2, true, false);"); |
| 1649 |
} |
| 1650 |
|
| 1651 |
public void testFunctionArguments15() throws Exception { |
| 1652 |
testTypes( |
| 1653 |
"/** @param {?function(*)} f */" + |
| 1654 |
"function g(f) { f(1, 2); }", |
| 1655 |
"Function f: called with 2 argument(s). " + |
| 1656 |
"Function requires at least 1 argument(s) " + |
| 1657 |
"and no more than 1 argument(s)."); |
| 1658 |
} |
| 1659 |
|
| 1660 |
public void testFunctionArguments16() throws Exception { |
| 1661 |
testTypes( |
| 1662 |
"/** @param {...number} var_args */" + |
| 1663 |
"function g(var_args) {} g(1, true);", |
| 1664 |
"actual parameter 2 of g does not match formal parameter\n" + |
| 1665 |
"found : boolean\n" + |
| 1666 |
"required: (number|undefined)"); |
| 1667 |
} |
| 1668 |
|
| 1669 |
public void testFunctionArguments17() throws Exception { |
| 1670 |
testClosureTypesMultipleWarnings( |
| 1671 |
"/** @param {booool|string} x */" + |
| 1672 |
"function f(x) { g(x) }" + |
| 1673 |
"/** @param {number} x */" + |
| 1674 |
"function g(x) {}", |
| 1675 |
Lists.newArrayList( |
| 1676 |
"Bad type annotation. Unknown type booool", |
| 1677 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 1678 |
"found : (booool|null|string)\n" + |
| 1679 |
"required: number")); |
| 1680 |
} |
| 1681 |
|
| 1682 |
public void testFunctionArguments18() throws Exception { |
| 1683 |
testTypes( |
| 1684 |
"function f(x) {}" + |
| 1685 |
"f(/** @param {number} y */ (function() {}));", |
| 1686 |
"parameter y does not appear in <anonymous>'s parameter list"); |
| 1687 |
} |
| 1688 |
|
| 1689 |
public void testPrintFunctionName1() throws Exception { |
| 1690 |
|
| 1691 |
testTypes( |
| 1692 |
"var goog = {}; goog.run = function(f) {};" + |
| 1693 |
"goog.run();", |
| 1694 |
"Function goog.run: called with 0 argument(s). " + |
| 1695 |
"Function requires at least 1 argument(s) " + |
| 1696 |
"and no more than 1 argument(s)."); |
| 1697 |
} |
| 1698 |
|
| 1699 |
public void testPrintFunctionName2() throws Exception { |
| 1700 |
testTypes( |
| 1701 |
"/** @constructor */ var Foo = function() {}; " + |
| 1702 |
"Foo.prototype.run = function(f) {};" + |
| 1703 |
"(new Foo).run();", |
| 1704 |
"Function Foo.prototype.run: called with 0 argument(s). " + |
| 1705 |
"Function requires at least 1 argument(s) " + |
| 1706 |
"and no more than 1 argument(s)."); |
| 1707 |
} |
| 1708 |
|
| 1709 |
public void testFunctionInference1() throws Exception { |
| 1710 |
testFunctionType( |
| 1711 |
"function f(a) {}", |
| 1712 |
"function (?): undefined"); |
| 1713 |
} |
| 1714 |
|
| 1715 |
public void testFunctionInference2() throws Exception { |
| 1716 |
testFunctionType( |
| 1717 |
"function f(a,b) {}", |
| 1718 |
"function (?, ?): undefined"); |
| 1719 |
} |
| 1720 |
|
| 1721 |
public void testFunctionInference3() throws Exception { |
| 1722 |
testFunctionType( |
| 1723 |
"function f(var_args) {}", |
| 1724 |
"function (...[?]): undefined"); |
| 1725 |
} |
| 1726 |
|
| 1727 |
public void testFunctionInference4() throws Exception { |
| 1728 |
testFunctionType( |
| 1729 |
"function f(a,b,c,var_args) {}", |
| 1730 |
"function (?, ?, ?, ...[?]): undefined"); |
| 1731 |
} |
| 1732 |
|
| 1733 |
public void testFunctionInference5() throws Exception { |
| 1734 |
testFunctionType( |
| 1735 |
"/** @this Date\n@return {string} */function f(a) {}", |
| 1736 |
"function (this:Date, ?): string"); |
| 1737 |
} |
| 1738 |
|
| 1739 |
public void testFunctionInference6() throws Exception { |
| 1740 |
testFunctionType( |
| 1741 |
"/** @this Date\n@return {string} */function f(opt_a) {}", |
| 1742 |
"function (this:Date, ?=): string"); |
| 1743 |
} |
| 1744 |
|
| 1745 |
public void testFunctionInference7() throws Exception { |
| 1746 |
testFunctionType( |
| 1747 |
"/** @this Date */function f(a,b,c,var_args) {}", |
| 1748 |
"function (this:Date, ?, ?, ?, ...[?]): undefined"); |
| 1749 |
} |
| 1750 |
|
| 1751 |
public void testFunctionInference8() throws Exception { |
| 1752 |
testFunctionType( |
| 1753 |
"function f() {}", |
| 1754 |
"function (): undefined"); |
| 1755 |
} |
| 1756 |
|
| 1757 |
public void testFunctionInference9() throws Exception { |
| 1758 |
testFunctionType( |
| 1759 |
"var f = function() {};", |
| 1760 |
"function (): undefined"); |
| 1761 |
} |
| 1762 |
|
| 1763 |
public void testFunctionInference10() throws Exception { |
| 1764 |
testFunctionType( |
| 1765 |
"/** @this Date\n@param {boolean} b\n@return {string} */" + |
| 1766 |
"var f = function(a,b) {};", |
| 1767 |
"function (this:Date, ?, boolean): string"); |
| 1768 |
} |
| 1769 |
|
| 1770 |
public void testFunctionInference11() throws Exception { |
| 1771 |
testFunctionType( |
| 1772 |
"var goog = {};" + |
| 1773 |
"/** @return {number}*/goog.f = function(){};", |
| 1774 |
"goog.f", |
| 1775 |
"function (): number"); |
| 1776 |
} |
| 1777 |
|
| 1778 |
public void testFunctionInference12() throws Exception { |
| 1779 |
testFunctionType( |
| 1780 |
"var goog = {};" + |
| 1781 |
"goog.f = function(){};", |
| 1782 |
"goog.f", |
| 1783 |
"function (): undefined"); |
| 1784 |
} |
| 1785 |
|
| 1786 |
public void testFunctionInference13() throws Exception { |
| 1787 |
testFunctionType( |
| 1788 |
"var goog = {};" + |
| 1789 |
"/** @constructor */ goog.Foo = function(){};" + |
| 1790 |
"/** @param {!goog.Foo} f */function eatFoo(f){};", |
| 1791 |
"eatFoo", |
| 1792 |
"function (goog.Foo): undefined"); |
| 1793 |
} |
| 1794 |
|
| 1795 |
public void testFunctionInference14() throws Exception { |
| 1796 |
testFunctionType( |
| 1797 |
"var goog = {};" + |
| 1798 |
"/** @constructor */ goog.Foo = function(){};" + |
| 1799 |
"/** @return {!goog.Foo} */function eatFoo(){ return new goog.Foo; };", |
| 1800 |
"eatFoo", |
| 1801 |
"function (): goog.Foo"); |
| 1802 |
} |
| 1803 |
|
| 1804 |
public void testFunctionInference15() throws Exception { |
| 1805 |
testFunctionType( |
| 1806 |
"/** @constructor */ function f() {};" + |
| 1807 |
"f.prototype.foo = function(){};", |
| 1808 |
"f.prototype.foo", |
| 1809 |
"function (this:f): undefined"); |
| 1810 |
} |
| 1811 |
|
| 1812 |
public void testFunctionInference16() throws Exception { |
| 1813 |
testFunctionType( |
| 1814 |
"/** @constructor */ function f() {};" + |
| 1815 |
"f.prototype.foo = function(){};", |
| 1816 |
"(new f).foo", |
| 1817 |
"function (this:f): undefined"); |
| 1818 |
} |
| 1819 |
|
| 1820 |
public void testFunctionInference17() throws Exception { |
| 1821 |
testFunctionType( |
| 1822 |
"/** @constructor */ function f() {}" + |
| 1823 |
"function abstractMethod() {}" + |
| 1824 |
"/** @param {number} x */ f.prototype.foo = abstractMethod;", |
| 1825 |
"(new f).foo", |
| 1826 |
"function (this:f, number): ?"); |
| 1827 |
} |
| 1828 |
|
| 1829 |
public void testFunctionInference18() throws Exception { |
| 1830 |
testFunctionType( |
| 1831 |
"var goog = {};" + |
| 1832 |
"/** @this {Date} */ goog.eatWithDate;", |
| 1833 |
"goog.eatWithDate", |
| 1834 |
"function (this:Date): ?"); |
| 1835 |
} |
| 1836 |
|
| 1837 |
public void testFunctionInference19() throws Exception { |
| 1838 |
testFunctionType( |
| 1839 |
"/** @param {string} x */ var f;", |
| 1840 |
"f", |
| 1841 |
"function (string): ?"); |
| 1842 |
} |
| 1843 |
|
| 1844 |
public void testFunctionInference20() throws Exception { |
| 1845 |
testFunctionType( |
| 1846 |
"/** @this {Date} */ var f;", |
| 1847 |
"f", |
| 1848 |
"function (this:Date): ?"); |
| 1849 |
} |
| 1850 |
|
| 1851 |
public void testFunctionInference21() throws Exception { |
| 1852 |
testTypes( |
| 1853 |
"var f = function() { throw 'x' };" + |
| 1854 |
"/** @return {boolean} */ var g = f;"); |
| 1855 |
testFunctionType( |
| 1856 |
"var f = function() { throw 'x' };", |
| 1857 |
"f", |
| 1858 |
"function (): ?"); |
| 1859 |
} |
| 1860 |
|
| 1861 |
public void testFunctionInference22() throws Exception { |
| 1862 |
testTypes( |
| 1863 |
"/** @type {!Function} */ var f = function() { g(this); };" + |
| 1864 |
"/** @param {boolean} x */ var g = function(x) {};"); |
| 1865 |
} |
| 1866 |
|
| 1867 |
public void testFunctionInference23() throws Exception { |
| 1868 |
|
| 1869 |
testTypes( |
| 1870 |
"/** @type {!Function} */ var f = function() {\n" + |
| 1871 |
" /** @type {number} */ this.prop = 3;\n" + |
| 1872 |
"};" + |
| 1873 |
"/**\n" + |
| 1874 |
" * @param {Object} x\n" + |
| 1875 |
" * @return {string}\n" + |
| 1876 |
" */ var g = function(x) { return x.prop; };"); |
| 1877 |
} |
| 1878 |
|
| 1879 |
public void testInnerFunction1() throws Exception { |
| 1880 |
testTypes( |
| 1881 |
"function f() {" + |
| 1882 |
" /** @type {number} */ var x = 3;\n" + |
| 1883 |
" function g() { x = null; }" + |
| 1884 |
" return x;" + |
| 1885 |
"}", |
| 1886 |
"assignment\n" + |
| 1887 |
"found : null\n" + |
| 1888 |
"required: number"); |
| 1889 |
} |
| 1890 |
|
| 1891 |
public void testInnerFunction2() throws Exception { |
| 1892 |
testTypes( |
| 1893 |
"/** @return {number} */\n" + |
| 1894 |
"function f() {" + |
| 1895 |
" var x = null;\n" + |
| 1896 |
" function g() { x = 3; }" + |
| 1897 |
" g();" + |
| 1898 |
" return x;" + |
| 1899 |
"}", |
| 1900 |
"inconsistent return type\n" + |
| 1901 |
"found : (null|number)\n" + |
| 1902 |
"required: number"); |
| 1903 |
} |
| 1904 |
|
| 1905 |
public void testInnerFunction3() throws Exception { |
| 1906 |
testTypes( |
| 1907 |
"var x = null;" + |
| 1908 |
"/** @return {number} */\n" + |
| 1909 |
"function f() {" + |
| 1910 |
" x = 3;\n" + |
| 1911 |
" /** @return {number} */\n" + |
| 1912 |
" function g() { x = true; return x; }" + |
| 1913 |
" return x;" + |
| 1914 |
"}", |
| 1915 |
"inconsistent return type\n" + |
| 1916 |
"found : boolean\n" + |
| 1917 |
"required: number"); |
| 1918 |
} |
| 1919 |
|
| 1920 |
public void testInnerFunction4() throws Exception { |
| 1921 |
testTypes( |
| 1922 |
"var x = null;" + |
| 1923 |
"/** @return {number} */\n" + |
| 1924 |
"function f() {" + |
| 1925 |
" x = '3';\n" + |
| 1926 |
" /** @return {number} */\n" + |
| 1927 |
" function g() { x = 3; return x; }" + |
| 1928 |
" return x;" + |
| 1929 |
"}", |
| 1930 |
"inconsistent return type\n" + |
| 1931 |
"found : string\n" + |
| 1932 |
"required: number"); |
| 1933 |
} |
| 1934 |
|
| 1935 |
public void testInnerFunction5() throws Exception { |
| 1936 |
testTypes( |
| 1937 |
"/** @return {number} */\n" + |
| 1938 |
"function f() {" + |
| 1939 |
" var x = 3;\n" + |
| 1940 |
" /** @return {number} */" + |
| 1941 |
" function g() { var x = 3;x = true; return x; }" + |
| 1942 |
" return x;" + |
| 1943 |
"}", |
| 1944 |
"inconsistent return type\n" + |
| 1945 |
"found : boolean\n" + |
| 1946 |
"required: number"); |
| 1947 |
} |
| 1948 |
|
| 1949 |
public void testInnerFunction6() throws Exception { |
| 1950 |
testClosureTypes( |
| 1951 |
CLOSURE_DEFS + |
| 1952 |
"function f() {" + |
| 1953 |
" var x = 0 || function() {};\n" + |
| 1954 |
" function g() { if (goog.isFunction(x)) { x(1); } }" + |
| 1955 |
" g();" + |
| 1956 |
"}", |
| 1957 |
"Function x: called with 1 argument(s). " + |
| 1958 |
"Function requires at least 0 argument(s) " + |
| 1959 |
"and no more than 0 argument(s)."); |
| 1960 |
} |
| 1961 |
|
| 1962 |
public void testInnerFunction7() throws Exception { |
| 1963 |
testClosureTypes( |
| 1964 |
CLOSURE_DEFS + |
| 1965 |
"function f() {" + |
| 1966 |
" /** @type {number|function()} */" + |
| 1967 |
" var x = 0 || function() {};\n" + |
| 1968 |
" function g() { if (goog.isFunction(x)) { x(1); } }" + |
| 1969 |
" g();" + |
| 1970 |
"}", |
| 1971 |
"Function x: called with 1 argument(s). " + |
| 1972 |
"Function requires at least 0 argument(s) " + |
| 1973 |
"and no more than 0 argument(s)."); |
| 1974 |
} |
| 1975 |
|
| 1976 |
public void testInnerFunction8() throws Exception { |
| 1977 |
testClosureTypes( |
| 1978 |
CLOSURE_DEFS + |
| 1979 |
"function f() {" + |
| 1980 |
" function x() {};\n" + |
| 1981 |
" function g() { if (goog.isFunction(x)) { x(1); } }" + |
| 1982 |
" g();" + |
| 1983 |
"}", |
| 1984 |
"Function x: called with 1 argument(s). " + |
| 1985 |
"Function requires at least 0 argument(s) " + |
| 1986 |
"and no more than 0 argument(s)."); |
| 1987 |
} |
| 1988 |
|
| 1989 |
public void testInnerFunction9() throws Exception { |
| 1990 |
testTypes( |
| 1991 |
"function f() {" + |
| 1992 |
" var x = 3;\n" + |
| 1993 |
" function g() { x = null; };\n" + |
| 1994 |
" function h() { return x == null; }" + |
| 1995 |
" return h();" + |
| 1996 |
"}"); |
| 1997 |
} |
| 1998 |
|
| 1999 |
public void testInnerFunction10() throws Exception { |
| 2000 |
testTypes( |
| 2001 |
"function f() {" + |
| 2002 |
" /** @type {?number} */ var x = null;" + |
| 2003 |
" /** @return {string} */" + |
| 2004 |
" function g() {" + |
| 2005 |
" if (!x) {" + |
| 2006 |
" x = 1;" + |
| 2007 |
" }" + |
| 2008 |
" return x;" + |
| 2009 |
" }" + |
| 2010 |
"}", |
| 2011 |
"inconsistent return type\n" + |
| 2012 |
"found : number\n" + |
| 2013 |
"required: string"); |
| 2014 |
} |
| 2015 |
|
| 2016 |
public void testInnerFunction11() throws Exception { |
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
testTypes( |
| 2022 |
"function f() {" + |
| 2023 |
" /** @type {?number} */ var x = null;" + |
| 2024 |
" /** @return {number} */" + |
| 2025 |
" function g() {" + |
| 2026 |
" x = 1;" + |
| 2027 |
" h();" + |
| 2028 |
" return x;" + |
| 2029 |
" }" + |
| 2030 |
" function h() {" + |
| 2031 |
" x = null;" + |
| 2032 |
" }" + |
| 2033 |
"}"); |
| 2034 |
} |
| 2035 |
|
| 2036 |
public void testAbstractMethodHandling1() throws Exception { |
| 2037 |
testTypes( |
| 2038 |
"/** @type {Function} */ var abstractFn = function() {};" + |
| 2039 |
"abstractFn(1);"); |
| 2040 |
} |
| 2041 |
|
| 2042 |
public void testAbstractMethodHandling2() throws Exception { |
| 2043 |
testTypes( |
| 2044 |
"var abstractFn = function() {};" + |
| 2045 |
"abstractFn(1);", |
| 2046 |
"Function abstractFn: called with 1 argument(s). " + |
| 2047 |
"Function requires at least 0 argument(s) " + |
| 2048 |
"and no more than 0 argument(s)."); |
| 2049 |
} |
| 2050 |
|
| 2051 |
public void testAbstractMethodHandling3() throws Exception { |
| 2052 |
testTypes( |
| 2053 |
"var goog = {};" + |
| 2054 |
"/** @type {Function} */ goog.abstractFn = function() {};" + |
| 2055 |
"goog.abstractFn(1);"); |
| 2056 |
} |
| 2057 |
|
| 2058 |
public void testAbstractMethodHandling4() throws Exception { |
| 2059 |
testTypes( |
| 2060 |
"var goog = {};" + |
| 2061 |
"goog.abstractFn = function() {};" + |
| 2062 |
"goog.abstractFn(1);", |
| 2063 |
"Function goog.abstractFn: called with 1 argument(s). " + |
| 2064 |
"Function requires at least 0 argument(s) " + |
| 2065 |
"and no more than 0 argument(s)."); |
| 2066 |
} |
| 2067 |
|
| 2068 |
public void testAbstractMethodHandling5() throws Exception { |
| 2069 |
testTypes( |
| 2070 |
"/** @type {!Function} */ var abstractFn = function() {};" + |
| 2071 |
"/** @param {number} x */ var f = abstractFn;" + |
| 2072 |
"f('x');", |
| 2073 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2074 |
"found : string\n" + |
| 2075 |
"required: number"); |
| 2076 |
} |
| 2077 |
|
| 2078 |
public void testAbstractMethodHandling6() throws Exception { |
| 2079 |
testTypes( |
| 2080 |
"var goog = {};" + |
| 2081 |
"/** @type {Function} */ goog.abstractFn = function() {};" + |
| 2082 |
"/** @param {number} x */ goog.f = abstractFn;" + |
| 2083 |
"goog.f('x');", |
| 2084 |
"actual parameter 1 of goog.f does not match formal parameter\n" + |
| 2085 |
"found : string\n" + |
| 2086 |
"required: number"); |
| 2087 |
} |
| 2088 |
|
| 2089 |
public void testMethodInference1() throws Exception { |
| 2090 |
testTypes( |
| 2091 |
"/** @constructor */ function F() {}" + |
| 2092 |
"/** @return {number} */ F.prototype.foo = function() { return 3; };" + |
| 2093 |
"/** @constructor \n * @extends {F} */ " + |
| 2094 |
"function G() {}" + |
| 2095 |
"/** @override */ G.prototype.foo = function() { return true; };", |
| 2096 |
"inconsistent return type\n" + |
| 2097 |
"found : boolean\n" + |
| 2098 |
"required: number"); |
| 2099 |
} |
| 2100 |
|
| 2101 |
public void testMethodInference2() throws Exception { |
| 2102 |
testTypes( |
| 2103 |
"var goog = {};" + |
| 2104 |
"/** @constructor */ goog.F = function() {};" + |
| 2105 |
"/** @return {number} */ goog.F.prototype.foo = " + |
| 2106 |
" function() { return 3; };" + |
| 2107 |
"/** @constructor \n * @extends {goog.F} */ " + |
| 2108 |
"goog.G = function() {};" + |
| 2109 |
"/** @override */ goog.G.prototype.foo = function() { return true; };", |
| 2110 |
"inconsistent return type\n" + |
| 2111 |
"found : boolean\n" + |
| 2112 |
"required: number"); |
| 2113 |
} |
| 2114 |
|
| 2115 |
public void testMethodInference3() throws Exception { |
| 2116 |
testTypes( |
| 2117 |
"/** @constructor */ function F() {}" + |
| 2118 |
"/** @param {boolean} x \n * @return {number} */ " + |
| 2119 |
"F.prototype.foo = function(x) { return 3; };" + |
| 2120 |
"/** @constructor \n * @extends {F} */ " + |
| 2121 |
"function G() {}" + |
| 2122 |
"/** @override */ " + |
| 2123 |
"G.prototype.foo = function(x) { return x; };", |
| 2124 |
"inconsistent return type\n" + |
| 2125 |
"found : boolean\n" + |
| 2126 |
"required: number"); |
| 2127 |
} |
| 2128 |
|
| 2129 |
public void testMethodInference4() throws Exception { |
| 2130 |
testTypes( |
| 2131 |
"/** @constructor */ function F() {}" + |
| 2132 |
"/** @param {boolean} x \n * @return {number} */ " + |
| 2133 |
"F.prototype.foo = function(x) { return 3; };" + |
| 2134 |
"/** @constructor \n * @extends {F} */ " + |
| 2135 |
"function G() {}" + |
| 2136 |
"/** @override */ " + |
| 2137 |
"G.prototype.foo = function(y) { return y; };", |
| 2138 |
"inconsistent return type\n" + |
| 2139 |
"found : boolean\n" + |
| 2140 |
"required: number"); |
| 2141 |
} |
| 2142 |
|
| 2143 |
public void testMethodInference5() throws Exception { |
| 2144 |
testTypes( |
| 2145 |
"/** @constructor */ function F() {}" + |
| 2146 |
"/** @param {number} x \n * @return {string} */ " + |
| 2147 |
"F.prototype.foo = function(x) { return 'x'; };" + |
| 2148 |
"/** @constructor \n * @extends {F} */ " + |
| 2149 |
"function G() {}" + |
| 2150 |
"/** @type {number} */ G.prototype.num = 3;" + |
| 2151 |
"/** @override */ " + |
| 2152 |
"G.prototype.foo = function(y) { return this.num + y; };", |
| 2153 |
"inconsistent return type\n" + |
| 2154 |
"found : number\n" + |
| 2155 |
"required: string"); |
| 2156 |
} |
| 2157 |
|
| 2158 |
public void testMethodInference6() throws Exception { |
| 2159 |
testTypes( |
| 2160 |
"/** @constructor */ function F() {}" + |
| 2161 |
"/** @param {number} x */ F.prototype.foo = function(x) { };" + |
| 2162 |
"/** @constructor \n * @extends {F} */ " + |
| 2163 |
"function G() {}" + |
| 2164 |
"/** @override */ G.prototype.foo = function() { };" + |
| 2165 |
"(new G()).foo(1);"); |
| 2166 |
} |
| 2167 |
|
| 2168 |
public void testMethodInference7() throws Exception { |
| 2169 |
testTypes( |
| 2170 |
"/** @constructor */ function F() {}" + |
| 2171 |
"F.prototype.foo = function() { };" + |
| 2172 |
"/** @constructor \n * @extends {F} */ " + |
| 2173 |
"function G() {}" + |
| 2174 |
"/** @override */ G.prototype.foo = function(x, y) { };", |
| 2175 |
"mismatch of the foo property type and the type of the property " + |
| 2176 |
"it overrides from superclass F\n" + |
| 2177 |
"original: function (this:F): undefined\n" + |
| 2178 |
"override: function (this:G, ?, ?): undefined"); |
| 2179 |
} |
| 2180 |
|
| 2181 |
public void testMethodInference8() throws Exception { |
| 2182 |
testTypes( |
| 2183 |
"/** @constructor */ function F() {}" + |
| 2184 |
"F.prototype.foo = function() { };" + |
| 2185 |
"/** @constructor \n * @extends {F} */ " + |
| 2186 |
"function G() {}" + |
| 2187 |
"/** @override */ " + |
| 2188 |
"G.prototype.foo = function(opt_b, var_args) { };" + |
| 2189 |
"(new G()).foo(1, 2, 3);"); |
| 2190 |
} |
| 2191 |
|
| 2192 |
public void testMethodInference9() throws Exception { |
| 2193 |
testTypes( |
| 2194 |
"/** @constructor */ function F() {}" + |
| 2195 |
"F.prototype.foo = function() { };" + |
| 2196 |
"/** @constructor \n * @extends {F} */ " + |
| 2197 |
"function G() {}" + |
| 2198 |
"/** @override */ " + |
| 2199 |
"G.prototype.foo = function(var_args, opt_b) { };", |
| 2200 |
"variable length argument must be last"); |
| 2201 |
} |
| 2202 |
|
| 2203 |
public void testStaticMethodDeclaration1() throws Exception { |
| 2204 |
testTypes( |
| 2205 |
"/** @constructor */ function F() { F.foo(true); }" + |
| 2206 |
"/** @param {number} x */ F.foo = function(x) {};", |
| 2207 |
"actual parameter 1 of F.foo does not match formal parameter\n" + |
| 2208 |
"found : boolean\n" + |
| 2209 |
"required: number"); |
| 2210 |
} |
| 2211 |
|
| 2212 |
public void testStaticMethodDeclaration2() throws Exception { |
| 2213 |
testTypes( |
| 2214 |
"var goog = goog || {}; function f() { goog.foo(true); }" + |
| 2215 |
"/** @param {number} x */ goog.foo = function(x) {};", |
| 2216 |
"actual parameter 1 of goog.foo does not match formal parameter\n" + |
| 2217 |
"found : boolean\n" + |
| 2218 |
"required: number"); |
| 2219 |
} |
| 2220 |
|
| 2221 |
public void testStaticMethodDeclaration3() throws Exception { |
| 2222 |
testTypes( |
| 2223 |
"var goog = goog || {}; function f() { goog.foo(true); }" + |
| 2224 |
"goog.foo = function() {};", |
| 2225 |
"Function goog.foo: called with 1 argument(s). Function requires " + |
| 2226 |
"at least 0 argument(s) and no more than 0 argument(s)."); |
| 2227 |
} |
| 2228 |
|
| 2229 |
public void testDuplicateStaticMethodDecl1() throws Exception { |
| 2230 |
testTypes( |
| 2231 |
"var goog = goog || {};" + |
| 2232 |
"/** @param {number} x */ goog.foo = function(x) {};" + |
| 2233 |
"/** @param {number} x */ goog.foo = function(x) {};", |
| 2234 |
"variable goog.foo redefined with type function (number): undefined, " + |
| 2235 |
"original definition at [testcode]:1 " + |
| 2236 |
"with type function (number): undefined"); |
| 2237 |
} |
| 2238 |
|
| 2239 |
public void testDuplicateStaticMethodDecl2() throws Exception { |
| 2240 |
testTypes( |
| 2241 |
"var goog = goog || {};" + |
| 2242 |
"/** @param {number} x */ goog.foo = function(x) {};" + |
| 2243 |
"/** @param {number} x \n * @suppress {duplicate} */ " + |
| 2244 |
"goog.foo = function(x) {};"); |
| 2245 |
} |
| 2246 |
|
| 2247 |
public void testDuplicateStaticMethodDecl3() throws Exception { |
| 2248 |
testTypes( |
| 2249 |
"var goog = goog || {};" + |
| 2250 |
"goog.foo = function(x) {};" + |
| 2251 |
"goog.foo = function(x) {};"); |
| 2252 |
} |
| 2253 |
|
| 2254 |
public void testDuplicateStaticMethodDecl4() throws Exception { |
| 2255 |
testTypes( |
| 2256 |
"var goog = goog || {};" + |
| 2257 |
"/** @type {Function} */ goog.foo = function(x) {};" + |
| 2258 |
"goog.foo = function(x) {};"); |
| 2259 |
} |
| 2260 |
|
| 2261 |
public void testDuplicateStaticMethodDecl5() throws Exception { |
| 2262 |
testTypes( |
| 2263 |
"var goog = goog || {};" + |
| 2264 |
"goog.foo = function(x) {};" + |
| 2265 |
"/** @return {undefined} */ goog.foo = function(x) {};", |
| 2266 |
"variable goog.foo redefined with type function (?): undefined, " + |
| 2267 |
"original definition at [testcode]:1 with type " + |
| 2268 |
"function (?): undefined"); |
| 2269 |
} |
| 2270 |
|
| 2271 |
public void testDuplicateStaticMethodDecl6() throws Exception { |
| 2272 |
|
| 2273 |
|
| 2274 |
testTypes( |
| 2275 |
"var goog = goog || {};" + |
| 2276 |
"goog.foo = function(x) {};" + |
| 2277 |
"/**\n" + |
| 2278 |
" * @suppress {duplicate}\n" + |
| 2279 |
" * @return {undefined}\n" + |
| 2280 |
" */\n" + |
| 2281 |
"goog.foo = " + |
| 2282 |
" /** @type {!Function} */ (function(x) {});"); |
| 2283 |
} |
| 2284 |
|
| 2285 |
public void testDuplicateStaticPropertyDecl1() throws Exception { |
| 2286 |
testTypes( |
| 2287 |
"var goog = goog || {};" + |
| 2288 |
"/** @type {Foo} */ goog.foo;" + |
| 2289 |
"/** @type {Foo} */ goog.foo;" + |
| 2290 |
"/** @constructor */ function Foo() {}"); |
| 2291 |
} |
| 2292 |
|
| 2293 |
public void testDuplicateStaticPropertyDecl2() throws Exception { |
| 2294 |
testTypes( |
| 2295 |
"var goog = goog || {};" + |
| 2296 |
"/** @type {Foo} */ goog.foo;" + |
| 2297 |
"/** @type {Foo} \n * @suppress {duplicate} */ goog.foo;" + |
| 2298 |
"/** @constructor */ function Foo() {}"); |
| 2299 |
} |
| 2300 |
|
| 2301 |
public void testDuplicateStaticPropertyDecl3() throws Exception { |
| 2302 |
testTypes( |
| 2303 |
"var goog = goog || {};" + |
| 2304 |
"/** @type {!Foo} */ goog.foo;" + |
| 2305 |
"/** @type {string} */ goog.foo;" + |
| 2306 |
"/** @constructor */ function Foo() {}", |
| 2307 |
"variable goog.foo redefined with type string, " + |
| 2308 |
"original definition at [testcode]:1 with type Foo"); |
| 2309 |
} |
| 2310 |
|
| 2311 |
public void testDuplicateStaticPropertyDecl4() throws Exception { |
| 2312 |
testClosureTypesMultipleWarnings( |
| 2313 |
"var goog = goog || {};" + |
| 2314 |
"/** @type {!Foo} */ goog.foo;" + |
| 2315 |
"/** @type {string} */ goog.foo = 'x';" + |
| 2316 |
"/** @constructor */ function Foo() {}", |
| 2317 |
Lists.newArrayList( |
| 2318 |
"assignment to property foo of goog\n" + |
| 2319 |
"found : string\n" + |
| 2320 |
"required: Foo", |
| 2321 |
"variable goog.foo redefined with type string, " + |
| 2322 |
"original definition at [testcode]:1 with type Foo")); |
| 2323 |
} |
| 2324 |
|
| 2325 |
public void testDuplicateStaticPropertyDecl5() throws Exception { |
| 2326 |
testClosureTypesMultipleWarnings( |
| 2327 |
"var goog = goog || {};" + |
| 2328 |
"/** @type {!Foo} */ goog.foo;" + |
| 2329 |
"/** @type {string}\n * @suppress {duplicate} */ goog.foo = 'x';" + |
| 2330 |
"/** @constructor */ function Foo() {}", |
| 2331 |
Lists.newArrayList( |
| 2332 |
"assignment to property foo of goog\n" + |
| 2333 |
"found : string\n" + |
| 2334 |
"required: Foo", |
| 2335 |
"variable goog.foo redefined with type string, " + |
| 2336 |
"original definition at [testcode]:1 with type Foo")); |
| 2337 |
} |
| 2338 |
|
| 2339 |
public void testDuplicateStaticPropertyDecl6() throws Exception { |
| 2340 |
testTypes( |
| 2341 |
"var goog = goog || {};" + |
| 2342 |
"/** @type {string} */ goog.foo = 'y';" + |
| 2343 |
"/** @type {string}\n * @suppress {duplicate} */ goog.foo = 'x';"); |
| 2344 |
} |
| 2345 |
|
| 2346 |
public void testDuplicateStaticPropertyDecl7() throws Exception { |
| 2347 |
testTypes( |
| 2348 |
"var goog = goog || {};" + |
| 2349 |
"/** @param {string} x */ goog.foo;" + |
| 2350 |
"/** @type {function(string)} */ goog.foo;"); |
| 2351 |
} |
| 2352 |
|
| 2353 |
public void testDuplicateStaticPropertyDecl8() throws Exception { |
| 2354 |
testTypes( |
| 2355 |
"var goog = goog || {};" + |
| 2356 |
"/** @return {EventCopy} */ goog.foo;" + |
| 2357 |
"/** @constructor */ function EventCopy() {}" + |
| 2358 |
"/** @return {EventCopy} */ goog.foo;"); |
| 2359 |
} |
| 2360 |
|
| 2361 |
public void testDuplicateStaticPropertyDecl9() throws Exception { |
| 2362 |
testTypes( |
| 2363 |
"var goog = goog || {};" + |
| 2364 |
"/** @return {EventCopy} */ goog.foo;" + |
| 2365 |
"/** @return {EventCopy} */ goog.foo;" + |
| 2366 |
"/** @constructor */ function EventCopy() {}"); |
| 2367 |
} |
| 2368 |
|
| 2369 |
public void testDuplicateStaticPropertyDec20() throws Exception { |
| 2370 |
testTypes( |
| 2371 |
"/**\n" + |
| 2372 |
" * @fileoverview\n" + |
| 2373 |
" * @suppress {duplicate}\n" + |
| 2374 |
" */" + |
| 2375 |
"var goog = goog || {};" + |
| 2376 |
"/** @type {string} */ goog.foo = 'y';" + |
| 2377 |
"/** @type {string} */ goog.foo = 'x';"); |
| 2378 |
} |
| 2379 |
|
| 2380 |
public void testDuplicateLocalVarDecl() throws Exception { |
| 2381 |
testClosureTypesMultipleWarnings( |
| 2382 |
"/** @param {number} x */\n" + |
| 2383 |
"function f(x) { /** @type {string} */ var x = ''; }", |
| 2384 |
Lists.newArrayList( |
| 2385 |
"variable x redefined with type string, original definition" + |
| 2386 |
" at [testcode]:2 with type number", |
| 2387 |
"initializing variable\n" + |
| 2388 |
"found : string\n" + |
| 2389 |
"required: number")); |
| 2390 |
} |
| 2391 |
|
| 2392 |
public void testDuplicateInstanceMethod1() throws Exception { |
| 2393 |
|
| 2394 |
|
| 2395 |
testTypes( |
| 2396 |
"/** @constructor */ function F() {}" + |
| 2397 |
"F.prototype.bar = function() {};" + |
| 2398 |
"F.prototype.bar = function() {};"); |
| 2399 |
} |
| 2400 |
|
| 2401 |
public void testDuplicateInstanceMethod2() throws Exception { |
| 2402 |
testTypes( |
| 2403 |
"/** @constructor */ function F() {}" + |
| 2404 |
"/** jsdoc */ F.prototype.bar = function() {};" + |
| 2405 |
"/** jsdoc */ F.prototype.bar = function() {};", |
| 2406 |
"variable F.prototype.bar redefined with type " + |
| 2407 |
"function (this:F): undefined, original definition at " + |
| 2408 |
"[testcode]:1 with type function (this:F): undefined"); |
| 2409 |
} |
| 2410 |
|
| 2411 |
public void testDuplicateInstanceMethod3() throws Exception { |
| 2412 |
testTypes( |
| 2413 |
"/** @constructor */ function F() {}" + |
| 2414 |
"F.prototype.bar = function() {};" + |
| 2415 |
"/** jsdoc */ F.prototype.bar = function() {};", |
| 2416 |
"variable F.prototype.bar redefined with type " + |
| 2417 |
"function (this:F): undefined, original definition at " + |
| 2418 |
"[testcode]:1 with type function (this:F): undefined"); |
| 2419 |
} |
| 2420 |
|
| 2421 |
public void testDuplicateInstanceMethod4() throws Exception { |
| 2422 |
testTypes( |
| 2423 |
"/** @constructor */ function F() {}" + |
| 2424 |
"/** jsdoc */ F.prototype.bar = function() {};" + |
| 2425 |
"F.prototype.bar = function() {};"); |
| 2426 |
} |
| 2427 |
|
| 2428 |
public void testDuplicateInstanceMethod5() throws Exception { |
| 2429 |
testTypes( |
| 2430 |
"/** @constructor */ function F() {}" + |
| 2431 |
"/** jsdoc \n * @return {number} */ F.prototype.bar = function() {" + |
| 2432 |
" return 3;" + |
| 2433 |
"};" + |
| 2434 |
"/** jsdoc \n * @suppress {duplicate} */ " + |
| 2435 |
"F.prototype.bar = function() { return ''; };", |
| 2436 |
"inconsistent return type\n" + |
| 2437 |
"found : string\n" + |
| 2438 |
"required: number"); |
| 2439 |
} |
| 2440 |
|
| 2441 |
public void testDuplicateInstanceMethod6() throws Exception { |
| 2442 |
testTypes( |
| 2443 |
"/** @constructor */ function F() {}" + |
| 2444 |
"/** jsdoc \n * @return {number} */ F.prototype.bar = function() {" + |
| 2445 |
" return 3;" + |
| 2446 |
"};" + |
| 2447 |
"/** jsdoc \n * @return {string} * \n @suppress {duplicate} */ " + |
| 2448 |
"F.prototype.bar = function() { return ''; };", |
| 2449 |
"assignment to property bar of F.prototype\n" + |
| 2450 |
"found : function (this:F): string\n" + |
| 2451 |
"required: function (this:F): number"); |
| 2452 |
} |
| 2453 |
|
| 2454 |
public void testStubFunctionDeclaration1() throws Exception { |
| 2455 |
testFunctionType( |
| 2456 |
"/** @constructor */ function f() {};" + |
| 2457 |
"/** @param {number} x \n * @param {string} y \n" + |
| 2458 |
" * @return {number} */ f.prototype.foo;", |
| 2459 |
"(new f).foo", |
| 2460 |
"function (this:f, number, string): number"); |
| 2461 |
} |
| 2462 |
|
| 2463 |
public void testStubFunctionDeclaration2() throws Exception { |
| 2464 |
testExternFunctionType( |
| 2465 |
|
| 2466 |
"/** @constructor */ function f() {};" + |
| 2467 |
"/** @constructor \n * @extends {f} */ f.subclass;", |
| 2468 |
"f.subclass", |
| 2469 |
"function (new:f.subclass): ?"); |
| 2470 |
} |
| 2471 |
|
| 2472 |
public void testStubFunctionDeclaration3() throws Exception { |
| 2473 |
testFunctionType( |
| 2474 |
"/** @constructor */ function f() {};" + |
| 2475 |
"/** @return {undefined} */ f.foo;", |
| 2476 |
"f.foo", |
| 2477 |
"function (): undefined"); |
| 2478 |
} |
| 2479 |
|
| 2480 |
public void testStubFunctionDeclaration4() throws Exception { |
| 2481 |
testFunctionType( |
| 2482 |
"/** @constructor */ function f() { " + |
| 2483 |
" /** @return {number} */ this.foo;" + |
| 2484 |
"}", |
| 2485 |
"(new f).foo", |
| 2486 |
"function (this:f): number"); |
| 2487 |
} |
| 2488 |
|
| 2489 |
public void testStubFunctionDeclaration5() throws Exception { |
| 2490 |
testFunctionType( |
| 2491 |
"/** @constructor */ function f() { " + |
| 2492 |
" /** @type {Function} */ this.foo;" + |
| 2493 |
"}", |
| 2494 |
"(new f).foo", |
| 2495 |
createNullableType(U2U_CONSTRUCTOR_TYPE).toString()); |
| 2496 |
} |
| 2497 |
|
| 2498 |
public void testStubFunctionDeclaration6() throws Exception { |
| 2499 |
testFunctionType( |
| 2500 |
"/** @constructor */ function f() {} " + |
| 2501 |
"/** @type {Function} */ f.prototype.foo;", |
| 2502 |
"(new f).foo", |
| 2503 |
createNullableType(U2U_CONSTRUCTOR_TYPE).toString()); |
| 2504 |
} |
| 2505 |
|
| 2506 |
public void testStubFunctionDeclaration7() throws Exception { |
| 2507 |
testFunctionType( |
| 2508 |
"/** @constructor */ function f() {} " + |
| 2509 |
"/** @type {Function} */ f.prototype.foo = function() {};", |
| 2510 |
"(new f).foo", |
| 2511 |
createNullableType(U2U_CONSTRUCTOR_TYPE).toString()); |
| 2512 |
} |
| 2513 |
|
| 2514 |
public void testStubFunctionDeclaration8() throws Exception { |
| 2515 |
testFunctionType( |
| 2516 |
"/** @type {Function} */ var f = function() {}; ", |
| 2517 |
"f", |
| 2518 |
createNullableType(U2U_CONSTRUCTOR_TYPE).toString()); |
| 2519 |
} |
| 2520 |
|
| 2521 |
public void testStubFunctionDeclaration9() throws Exception { |
| 2522 |
testFunctionType( |
| 2523 |
"/** @type {function():number} */ var f; ", |
| 2524 |
"f", |
| 2525 |
"function (): number"); |
| 2526 |
} |
| 2527 |
|
| 2528 |
public void testStubFunctionDeclaration10() throws Exception { |
| 2529 |
testFunctionType( |
| 2530 |
"/** @type {function(number):number} */ var f = function(x) {};", |
| 2531 |
"f", |
| 2532 |
"function (number): number"); |
| 2533 |
} |
| 2534 |
|
| 2535 |
public void testNestedFunctionInference1() throws Exception { |
| 2536 |
String nestedAssignOfFooAndBar = |
| 2537 |
"/** @constructor */ function f() {};" + |
| 2538 |
"f.prototype.foo = f.prototype.bar = function(){};"; |
| 2539 |
testFunctionType(nestedAssignOfFooAndBar, "(new f).bar", |
| 2540 |
"function (this:f): undefined"); |
| 2541 |
} |
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
private void testFunctionType(String functionDef, String functionType) |
| 2548 |
throws Exception { |
| 2549 |
testFunctionType(functionDef, "f", functionType); |
| 2550 |
} |
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
private void testFunctionType(String functionDef, String functionName, |
| 2557 |
String functionType) throws Exception { |
| 2558 |
|
| 2559 |
testTypes( |
| 2560 |
functionDef + |
| 2561 |
"/** @type number */var a=" + functionName + ";", |
| 2562 |
"initializing variable\n" + |
| 2563 |
"found : " + functionType + "\n" + |
| 2564 |
"required: number"); |
| 2565 |
} |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
private void testExternFunctionType(String functionDef, String functionName, |
| 2573 |
String functionType) throws Exception { |
| 2574 |
testTypes( |
| 2575 |
functionDef, |
| 2576 |
"/** @type number */var a=" + functionName + ";", |
| 2577 |
"initializing variable\n" + |
| 2578 |
"found : " + functionType + "\n" + |
| 2579 |
"required: number", false); |
| 2580 |
} |
| 2581 |
|
| 2582 |
public void testTypeRedefinition() throws Exception { |
| 2583 |
testClosureTypesMultipleWarnings("a={};/**@enum {string}*/ a.A = {ZOR:'b'};" |
| 2584 |
+ "/** @constructor */ a.A = function() {}", |
| 2585 |
Lists.newArrayList( |
| 2586 |
"variable a.A redefined with type function (new:a.A): undefined, " + |
| 2587 |
"original definition at [testcode]:1 with type enum{a.A}", |
| 2588 |
"assignment to property A of a\n" + |
| 2589 |
"found : function (new:a.A): undefined\n" + |
| 2590 |
"required: enum{a.A}")); |
| 2591 |
} |
| 2592 |
|
| 2593 |
public void testIn1() throws Exception { |
| 2594 |
testTypes("'foo' in Object"); |
| 2595 |
} |
| 2596 |
|
| 2597 |
public void testIn2() throws Exception { |
| 2598 |
testTypes("3 in Object"); |
| 2599 |
} |
| 2600 |
|
| 2601 |
public void testIn3() throws Exception { |
| 2602 |
testTypes("undefined in Object"); |
| 2603 |
} |
| 2604 |
|
| 2605 |
public void testIn4() throws Exception { |
| 2606 |
testTypes("Date in Object", |
| 2607 |
"left side of 'in'\n" + |
| 2608 |
"found : function (new:Date, ?=, ?=, ?=, ?=, ?=, ?=, ?=): string\n" + |
| 2609 |
"required: string"); |
| 2610 |
} |
| 2611 |
|
| 2612 |
public void testIn5() throws Exception { |
| 2613 |
testTypes("'x' in null", |
| 2614 |
"'in' requires an object\n" + |
| 2615 |
"found : null\n" + |
| 2616 |
"required: Object"); |
| 2617 |
} |
| 2618 |
|
| 2619 |
public void testIn6() throws Exception { |
| 2620 |
testTypes( |
| 2621 |
"/** @param {number} x */" + |
| 2622 |
"function g(x) {}" + |
| 2623 |
"g(1 in {});", |
| 2624 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 2625 |
"found : boolean\n" + |
| 2626 |
"required: number"); |
| 2627 |
} |
| 2628 |
|
| 2629 |
public void testIn7() throws Exception { |
| 2630 |
|
| 2631 |
testTypes( |
| 2632 |
"/**\n" + |
| 2633 |
" * @param {number} x\n" + |
| 2634 |
" * @return {number}\n" + |
| 2635 |
" */\n" + |
| 2636 |
"function g(x) { return 5; }" + |
| 2637 |
"function f() {" + |
| 2638 |
" var x = {};" + |
| 2639 |
" x.foo = '3';" + |
| 2640 |
" return g(x.foo) in {};" + |
| 2641 |
"}", |
| 2642 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 2643 |
"found : string\n" + |
| 2644 |
"required: number"); |
| 2645 |
} |
| 2646 |
|
| 2647 |
public void testForIn1() throws Exception { |
| 2648 |
testTypes( |
| 2649 |
"/** @param {boolean} x */ function f(x) {}" + |
| 2650 |
"for (var k in {}) {" + |
| 2651 |
" f(k);" + |
| 2652 |
"}", |
| 2653 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2654 |
"found : string\n" + |
| 2655 |
"required: boolean"); |
| 2656 |
} |
| 2657 |
|
| 2658 |
public void testForIn2() throws Exception { |
| 2659 |
testTypes( |
| 2660 |
"/** @param {boolean} x */ function f(x) {}" + |
| 2661 |
"/** @enum {string} */ var E = {FOO: 'bar'};" + |
| 2662 |
"/** @type {Object.<E, string>} */ var obj = {};" + |
| 2663 |
"var k = null;" + |
| 2664 |
"for (k in obj) {" + |
| 2665 |
" f(k);" + |
| 2666 |
"}", |
| 2667 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2668 |
"found : E.<string>\n" + |
| 2669 |
"required: boolean"); |
| 2670 |
} |
| 2671 |
|
| 2672 |
public void testForIn3() throws Exception { |
| 2673 |
testTypes( |
| 2674 |
"/** @param {boolean} x */ function f(x) {}" + |
| 2675 |
"/** @type {Object.<number>} */ var obj = {};" + |
| 2676 |
"for (var k in obj) {" + |
| 2677 |
" f(obj[k]);" + |
| 2678 |
"}", |
| 2679 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2680 |
"found : number\n" + |
| 2681 |
"required: boolean"); |
| 2682 |
} |
| 2683 |
|
| 2684 |
public void testForIn4() throws Exception { |
| 2685 |
testTypes( |
| 2686 |
"/** @param {boolean} x */ function f(x) {}" + |
| 2687 |
"/** @enum {string} */ var E = {FOO: 'bar'};" + |
| 2688 |
"/** @type {Object.<E, Array>} */ var obj = {};" + |
| 2689 |
"for (var k in obj) {" + |
| 2690 |
" f(obj[k]);" + |
| 2691 |
"}", |
| 2692 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2693 |
"found : (Array|null)\n" + |
| 2694 |
"required: boolean"); |
| 2695 |
} |
| 2696 |
|
| 2697 |
public void testForIn5() throws Exception { |
| 2698 |
testTypes( |
| 2699 |
"/** @param {boolean} x */ function f(x) {}" + |
| 2700 |
"/** @constructor */ var E = function(){};" + |
| 2701 |
"/** @type {Object.<E, number>} */ var obj = {};" + |
| 2702 |
"for (var k in obj) {" + |
| 2703 |
" f(k);" + |
| 2704 |
"}", |
| 2705 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2706 |
"found : string\n" + |
| 2707 |
"required: boolean"); |
| 2708 |
} |
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
public void testComparison2() throws Exception { |
| 2721 |
testTypes("/**@type number*/var a;" + |
| 2722 |
"/**@type !Date */var b;" + |
| 2723 |
"if (a!==b) {}", |
| 2724 |
"condition always evaluates to true\n" + |
| 2725 |
"left : number\n" + |
| 2726 |
"right: Date"); |
| 2727 |
} |
| 2728 |
|
| 2729 |
public void testComparison3() throws Exception { |
| 2730 |
|
| 2731 |
testTypes("/** @type {(Object,undefined)} */var a;" + |
| 2732 |
"var b = a == null"); |
| 2733 |
} |
| 2734 |
|
| 2735 |
public void testComparison4() throws Exception { |
| 2736 |
testTypes("/** @type {(!Object,undefined)} */var a;" + |
| 2737 |
"/** @type {!Object} */var b;" + |
| 2738 |
"var c = a == b"); |
| 2739 |
} |
| 2740 |
|
| 2741 |
public void testComparison5() throws Exception { |
| 2742 |
testTypes("/** @type null */var a;" + |
| 2743 |
"/** @type null */var b;" + |
| 2744 |
"a == b", |
| 2745 |
"condition always evaluates to true\n" + |
| 2746 |
"left : null\n" + |
| 2747 |
"right: null"); |
| 2748 |
} |
| 2749 |
|
| 2750 |
public void testComparison6() throws Exception { |
| 2751 |
testTypes("/** @type null */var a;" + |
| 2752 |
"/** @type null */var b;" + |
| 2753 |
"a != b", |
| 2754 |
"condition always evaluates to false\n" + |
| 2755 |
"left : null\n" + |
| 2756 |
"right: null"); |
| 2757 |
} |
| 2758 |
|
| 2759 |
public void testComparison7() throws Exception { |
| 2760 |
testTypes("var a;" + |
| 2761 |
"var b;" + |
| 2762 |
"a == b", |
| 2763 |
"condition always evaluates to true\n" + |
| 2764 |
"left : undefined\n" + |
| 2765 |
"right: undefined"); |
| 2766 |
} |
| 2767 |
|
| 2768 |
public void testComparison8() throws Exception { |
| 2769 |
testTypes("/** @type {Array.<string>} */ var a = [];" + |
| 2770 |
"a[0] == null || a[1] == undefined"); |
| 2771 |
} |
| 2772 |
|
| 2773 |
public void testComparison9() throws Exception { |
| 2774 |
testTypes("/** @type {Array.<undefined>} */ var a = [];" + |
| 2775 |
"a[0] == null", |
| 2776 |
"condition always evaluates to true\n" + |
| 2777 |
"left : undefined\n" + |
| 2778 |
"right: null"); |
| 2779 |
} |
| 2780 |
|
| 2781 |
public void testComparison10() throws Exception { |
| 2782 |
testTypes("/** @type {Array.<undefined>} */ var a = [];" + |
| 2783 |
"a[0] === null"); |
| 2784 |
} |
| 2785 |
|
| 2786 |
public void testComparison11() throws Exception { |
| 2787 |
testTypes( |
| 2788 |
"(function(){}) == 'x'", |
| 2789 |
"condition always evaluates to false\n" + |
| 2790 |
"left : function (): undefined\n" + |
| 2791 |
"right: string"); |
| 2792 |
} |
| 2793 |
|
| 2794 |
public void testComparison12() throws Exception { |
| 2795 |
testTypes( |
| 2796 |
"(function(){}) == 3", |
| 2797 |
"condition always evaluates to false\n" + |
| 2798 |
"left : function (): undefined\n" + |
| 2799 |
"right: number"); |
| 2800 |
} |
| 2801 |
|
| 2802 |
public void testComparison13() throws Exception { |
| 2803 |
testTypes( |
| 2804 |
"(function(){}) == false", |
| 2805 |
"condition always evaluates to false\n" + |
| 2806 |
"left : function (): undefined\n" + |
| 2807 |
"right: boolean"); |
| 2808 |
} |
| 2809 |
|
| 2810 |
public void testComparison14() throws Exception { |
| 2811 |
testTypes("/** @type {function((Array|string), Object): number} */" + |
| 2812 |
"function f(x, y) { return x === y; }", |
| 2813 |
"inconsistent return type\n" + |
| 2814 |
"found : boolean\n" + |
| 2815 |
"required: number"); |
| 2816 |
} |
| 2817 |
|
| 2818 |
public void testComparison15() throws Exception { |
| 2819 |
testClosureTypes( |
| 2820 |
CLOSURE_DEFS + |
| 2821 |
"/** @constructor */ function F() {}" + |
| 2822 |
"/**\n" + |
| 2823 |
" * @param {number} x\n" + |
| 2824 |
" * @constructor\n" + |
| 2825 |
" * @extends {F}\n" + |
| 2826 |
" */\n" + |
| 2827 |
"function G(x) {}\n" + |
| 2828 |
"goog.inherits(G, F);\n" + |
| 2829 |
"/**\n" + |
| 2830 |
" * @param {number} x\n" + |
| 2831 |
" * @constructor\n" + |
| 2832 |
" * @extends {G}\n" + |
| 2833 |
" */\n" + |
| 2834 |
"function H(x) {}\n" + |
| 2835 |
"goog.inherits(H, G);\n" + |
| 2836 |
"/** @param {G} x */" + |
| 2837 |
"function f(x) { return x.constructor === H; }", |
| 2838 |
null); |
| 2839 |
} |
| 2840 |
|
| 2841 |
public void testDeleteOperator1() throws Exception { |
| 2842 |
testTypes( |
| 2843 |
"var x = {};" + |
| 2844 |
"/** @return {string} */ function f() { return delete x['a']; }", |
| 2845 |
"inconsistent return type\n" + |
| 2846 |
"found : boolean\n" + |
| 2847 |
"required: string"); |
| 2848 |
} |
| 2849 |
|
| 2850 |
public void testDeleteOperator2() throws Exception { |
| 2851 |
testTypes( |
| 2852 |
"var obj = {};" + |
| 2853 |
"/** \n" + |
| 2854 |
" * @param {string} x\n" + |
| 2855 |
" * @return {Object} */ function f(x) { return obj; }" + |
| 2856 |
"/** @param {?number} x */ function g(x) {" + |
| 2857 |
" if (x) { delete f(x)['a']; }" + |
| 2858 |
"}", |
| 2859 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 2860 |
"found : number\n" + |
| 2861 |
"required: string"); |
| 2862 |
} |
| 2863 |
|
| 2864 |
public void testEnumStaticMethod1() throws Exception { |
| 2865 |
testTypes( |
| 2866 |
"/** @enum */ var Foo = {AAA: 1};" + |
| 2867 |
"/** @param {number} x */ Foo.method = function(x) {};" + |
| 2868 |
"Foo.method(true);", |
| 2869 |
"actual parameter 1 of Foo.method does not match formal parameter\n" + |
| 2870 |
"found : boolean\n" + |
| 2871 |
"required: number"); |
| 2872 |
} |
| 2873 |
|
| 2874 |
public void testEnumStaticMethod2() throws Exception { |
| 2875 |
testTypes( |
| 2876 |
"/** @enum */ var Foo = {AAA: 1};" + |
| 2877 |
"/** @param {number} x */ Foo.method = function(x) {};" + |
| 2878 |
"function f() { Foo.method(true); }", |
| 2879 |
"actual parameter 1 of Foo.method does not match formal parameter\n" + |
| 2880 |
"found : boolean\n" + |
| 2881 |
"required: number"); |
| 2882 |
} |
| 2883 |
|
| 2884 |
public void testEnum1() throws Exception { |
| 2885 |
testTypes("/**@enum*/var a={BB:1,CC:2};\n" + |
| 2886 |
"/**@type {a}*/var d;d=a.BB;"); |
| 2887 |
} |
| 2888 |
|
| 2889 |
public void testEnum2() throws Exception { |
| 2890 |
testTypes("/**@enum*/var a={b:1}", |
| 2891 |
"enum key b must be a syntactic constant"); |
| 2892 |
} |
| 2893 |
|
| 2894 |
public void testEnum3() throws Exception { |
| 2895 |
testTypes("/**@enum*/var a={BB:1,BB:2}", |
| 2896 |
"variable a.BB redefined with type a.<number>, " + |
| 2897 |
"original definition at [testcode]:1 with type a.<number>"); |
| 2898 |
} |
| 2899 |
|
| 2900 |
public void testEnum4() throws Exception { |
| 2901 |
testTypes("/**@enum*/var a={BB:'string'}", |
| 2902 |
"assignment to property BB of enum{a}\n" + |
| 2903 |
"found : string\n" + |
| 2904 |
"required: number"); |
| 2905 |
} |
| 2906 |
|
| 2907 |
public void testEnum5() throws Exception { |
| 2908 |
testTypes("/**@enum {String}*/var a={BB:'string'}", |
| 2909 |
"assignment to property BB of enum{a}\n" + |
| 2910 |
"found : string\n" + |
| 2911 |
"required: (String|null)"); |
| 2912 |
} |
| 2913 |
|
| 2914 |
public void testEnum6() throws Exception { |
| 2915 |
testTypes("/**@enum*/var a={BB:1,CC:2};\n/**@type {!Array}*/var d;d=a.BB;", |
| 2916 |
"assignment\n" + |
| 2917 |
"found : a.<number>\n" + |
| 2918 |
"required: Array"); |
| 2919 |
} |
| 2920 |
|
| 2921 |
public void testEnum7() throws Exception { |
| 2922 |
testTypes("/** @enum */var a={AA:1,BB:2,CC:3};" + |
| 2923 |
"/** @type a */var b=a.D;", |
| 2924 |
"element D does not exist on this enum"); |
| 2925 |
} |
| 2926 |
|
| 2927 |
public void testEnum8() throws Exception { |
| 2928 |
testClosureTypesMultipleWarnings("/** @enum */var a=8;", |
| 2929 |
Lists.newArrayList( |
| 2930 |
"enum initializer must be an object literal or an enum", |
| 2931 |
"initializing variable\n" + |
| 2932 |
"found : number\n" + |
| 2933 |
"required: enum{a}")); |
| 2934 |
} |
| 2935 |
|
| 2936 |
public void testEnum9() throws Exception { |
| 2937 |
testClosureTypesMultipleWarnings( |
| 2938 |
"var goog = {};" + |
| 2939 |
"/** @enum */goog.a=8;", |
| 2940 |
Lists.newArrayList( |
| 2941 |
"assignment to property a of goog\n" + |
| 2942 |
"found : number\n" + |
| 2943 |
"required: enum{goog.a}", |
| 2944 |
"enum initializer must be an object literal or an enum")); |
| 2945 |
} |
| 2946 |
|
| 2947 |
public void testEnum10() throws Exception { |
| 2948 |
testTypes( |
| 2949 |
"/** @enum {number} */" + |
| 2950 |
"goog.K = { A : 3 };"); |
| 2951 |
} |
| 2952 |
|
| 2953 |
public void testEnum11() throws Exception { |
| 2954 |
testTypes( |
| 2955 |
"/** @enum {number} */" + |
| 2956 |
"goog.K = { 502 : 3 };"); |
| 2957 |
} |
| 2958 |
|
| 2959 |
public void testEnum12() throws Exception { |
| 2960 |
testTypes( |
| 2961 |
"/** @enum {number} */ var a = {};" + |
| 2962 |
"/** @enum */ var b = a;"); |
| 2963 |
} |
| 2964 |
|
| 2965 |
public void testEnum13() throws Exception { |
| 2966 |
testTypes( |
| 2967 |
"/** @enum {number} */ var a = {};" + |
| 2968 |
"/** @enum {string} */ var b = a;", |
| 2969 |
"incompatible enum element types\n" + |
| 2970 |
"found : number\n" + |
| 2971 |
"required: string"); |
| 2972 |
} |
| 2973 |
|
| 2974 |
public void testEnum14() throws Exception { |
| 2975 |
testTypes( |
| 2976 |
"/** @enum {number} */ var a = {FOO:5};" + |
| 2977 |
"/** @enum */ var b = a;" + |
| 2978 |
"var c = b.FOO;"); |
| 2979 |
} |
| 2980 |
|
| 2981 |
public void testEnum15() throws Exception { |
| 2982 |
testTypes( |
| 2983 |
"/** @enum {number} */ var a = {FOO:5};" + |
| 2984 |
"/** @enum */ var b = a;" + |
| 2985 |
"var c = b.BAR;", |
| 2986 |
"element BAR does not exist on this enum"); |
| 2987 |
} |
| 2988 |
|
| 2989 |
public void testEnum16() throws Exception { |
| 2990 |
testTypes("var goog = {};" + |
| 2991 |
"/**@enum*/goog .a={BB:1,BB:2}", |
| 2992 |
"variable goog.a.BB redefined with type goog.a.<number>, " + |
| 2993 |
"original definition at [testcode]:1 with type goog.a.<number>"); |
| 2994 |
} |
| 2995 |
|
| 2996 |
public void testEnum17() throws Exception { |
| 2997 |
testTypes("var goog = {};" + |
| 2998 |
"/**@enum*/goog.a={BB:'string'}", |
| 2999 |
"assignment to property BB of enum{goog.a}\n" + |
| 3000 |
"found : string\n" + |
| 3001 |
"required: number"); |
| 3002 |
} |
| 3003 |
|
| 3004 |
public void testEnum18() throws Exception { |
| 3005 |
testTypes("/**@enum*/ var E = {A: 1, B: 2};" + |
| 3006 |
"/** @param {!E} x\n@return {number} */\n" + |
| 3007 |
"var f = function(x) { return x; };"); |
| 3008 |
} |
| 3009 |
|
| 3010 |
public void testEnum19() throws Exception { |
| 3011 |
testTypes("/**@enum*/ var E = {A: 1, B: 2};" + |
| 3012 |
"/** @param {number} x\n@return {!E} */\n" + |
| 3013 |
"var f = function(x) { return x; };", |
| 3014 |
"inconsistent return type\n" + |
| 3015 |
"found : number\n" + |
| 3016 |
"required: E.<number>"); |
| 3017 |
} |
| 3018 |
|
| 3019 |
public void testEnum20() throws Exception { |
| 3020 |
testTypes("/**@enum*/ var E = {A: 1, B: 2}; var x = []; x[E.A] = 0;"); |
| 3021 |
} |
| 3022 |
|
| 3023 |
public void testEnum21() throws Exception { |
| 3024 |
Node n = parseAndTypeCheck( |
| 3025 |
"/** @enum {string} */ var E = {A : 'a', B : 'b'};\n" + |
| 3026 |
"/** @param {!E} x\n@return {!E} */ function f(x) { return x; }"); |
| 3027 |
Node nodeX = n.getLastChild().getLastChild().getLastChild().getLastChild(); |
| 3028 |
JSType typeE = nodeX.getJSType(); |
| 3029 |
assertFalse(typeE.isObject()); |
| 3030 |
assertFalse(typeE.isNullable()); |
| 3031 |
} |
| 3032 |
|
| 3033 |
public void testEnum22() throws Exception { |
| 3034 |
testTypes("/**@enum*/ var E = {A: 1, B: 2};" + |
| 3035 |
"/** @param {E} x \n* @return {number} */ function f(x) {return x}"); |
| 3036 |
} |
| 3037 |
|
| 3038 |
public void testEnum23() throws Exception { |
| 3039 |
testTypes("/**@enum*/ var E = {A: 1, B: 2};" + |
| 3040 |
"/** @param {E} x \n* @return {string} */ function f(x) {return x}", |
| 3041 |
"inconsistent return type\n" + |
| 3042 |
"found : E.<number>\n" + |
| 3043 |
"required: string"); |
| 3044 |
} |
| 3045 |
|
| 3046 |
public void testEnum24() throws Exception { |
| 3047 |
testTypes("/**@enum {Object} */ var E = {A: {}};" + |
| 3048 |
"/** @param {E} x \n* @return {!Object} */ function f(x) {return x}", |
| 3049 |
"inconsistent return type\n" + |
| 3050 |
"found : E.<(Object|null)>\n" + |
| 3051 |
"required: Object"); |
| 3052 |
} |
| 3053 |
|
| 3054 |
public void testEnum25() throws Exception { |
| 3055 |
testTypes("/**@enum {!Object} */ var E = {A: {}};" + |
| 3056 |
"/** @param {E} x \n* @return {!Object} */ function f(x) {return x}"); |
| 3057 |
} |
| 3058 |
|
| 3059 |
public void testEnum26() throws Exception { |
| 3060 |
testTypes("var a = {}; /**@enum*/ a.B = {A: 1, B: 2};" + |
| 3061 |
"/** @param {a.B} x \n* @return {number} */ function f(x) {return x}"); |
| 3062 |
} |
| 3063 |
|
| 3064 |
public void testEnum27() throws Exception { |
| 3065 |
|
| 3066 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3067 |
"function f(x) { return A == x; }"); |
| 3068 |
} |
| 3069 |
|
| 3070 |
public void testEnum28() throws Exception { |
| 3071 |
|
| 3072 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3073 |
"function f(x) { return A.B == x; }"); |
| 3074 |
} |
| 3075 |
|
| 3076 |
public void testEnum29() throws Exception { |
| 3077 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3078 |
"/** @return {number} */ function f() { return A; }", |
| 3079 |
"inconsistent return type\n" + |
| 3080 |
"found : enum{A}\n" + |
| 3081 |
"required: number"); |
| 3082 |
} |
| 3083 |
|
| 3084 |
public void testEnum30() throws Exception { |
| 3085 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3086 |
"/** @return {number} */ function f() { return A.B; }"); |
| 3087 |
} |
| 3088 |
|
| 3089 |
public void testEnum31() throws Exception { |
| 3090 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3091 |
"/** @return {A} */ function f() { return A; }", |
| 3092 |
"inconsistent return type\n" + |
| 3093 |
"found : enum{A}\n" + |
| 3094 |
"required: A.<number>"); |
| 3095 |
} |
| 3096 |
|
| 3097 |
public void testEnum32() throws Exception { |
| 3098 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3099 |
"/** @return {A} */ function f() { return A.B; }"); |
| 3100 |
} |
| 3101 |
|
| 3102 |
public void testEnum34() throws Exception { |
| 3103 |
testTypes("/** @enum */ var A = {B: 1, C: 2}; " + |
| 3104 |
"/** @param {number} x */ function f(x) { return x == A.B; }"); |
| 3105 |
} |
| 3106 |
|
| 3107 |
public void testEnum35() throws Exception { |
| 3108 |
testTypes("var a = a || {}; /** @enum */ a.b = {C: 1, D: 2};" + |
| 3109 |
"/** @return {a.b} */ function f() { return a.b.C; }"); |
| 3110 |
} |
| 3111 |
|
| 3112 |
public void testEnum36() throws Exception { |
| 3113 |
testTypes("var a = a || {}; /** @enum */ a.b = {C: 1, D: 2};" + |
| 3114 |
"/** @return {!a.b} */ function f() { return 1; }", |
| 3115 |
"inconsistent return type\n" + |
| 3116 |
"found : number\n" + |
| 3117 |
"required: a.b.<number>"); |
| 3118 |
} |
| 3119 |
|
| 3120 |
public void testEnum37() throws Exception { |
| 3121 |
testTypes( |
| 3122 |
"var goog = goog || {};" + |
| 3123 |
"/** @enum {number} */ goog.a = {};" + |
| 3124 |
"/** @enum */ var b = goog.a;"); |
| 3125 |
} |
| 3126 |
|
| 3127 |
public void testEnum38() throws Exception { |
| 3128 |
testTypes( |
| 3129 |
"/** @enum {MyEnum} */ var MyEnum = {};" + |
| 3130 |
"/** @param {MyEnum} x */ function f(x) {}", |
| 3131 |
"Parse error. Cycle detected in inheritance chain " + |
| 3132 |
"of type MyEnum"); |
| 3133 |
} |
| 3134 |
|
| 3135 |
public void testEnum39() throws Exception { |
| 3136 |
testTypes( |
| 3137 |
"/** @enum {Number} */ var MyEnum = {FOO: new Number(1)};" + |
| 3138 |
"/** @param {MyEnum} x \n * @return {number} */" + |
| 3139 |
"function f(x) { return x == MyEnum.FOO && MyEnum.FOO == x; }", |
| 3140 |
"inconsistent return type\n" + |
| 3141 |
"found : boolean\n" + |
| 3142 |
"required: number"); |
| 3143 |
} |
| 3144 |
|
| 3145 |
public void testEnum40() throws Exception { |
| 3146 |
testTypes( |
| 3147 |
"/** @enum {Number} */ var MyEnum = {FOO: new Number(1)};" + |
| 3148 |
"/** @param {number} x \n * @return {number} */" + |
| 3149 |
"function f(x) { return x == MyEnum.FOO && MyEnum.FOO == x; }", |
| 3150 |
"inconsistent return type\n" + |
| 3151 |
"found : boolean\n" + |
| 3152 |
"required: number"); |
| 3153 |
} |
| 3154 |
|
| 3155 |
public void testEnum41() throws Exception { |
| 3156 |
testTypes( |
| 3157 |
"/** @enum {number} */ var MyEnum = {/** @const */ FOO: 1};" + |
| 3158 |
"/** @return {string} */" + |
| 3159 |
"function f() { return MyEnum.FOO; }", |
| 3160 |
"inconsistent return type\n" + |
| 3161 |
"found : MyEnum.<number>\n" + |
| 3162 |
"required: string"); |
| 3163 |
} |
| 3164 |
|
| 3165 |
public void testEnum42() throws Exception { |
| 3166 |
testTypes( |
| 3167 |
"/** @param {number} x */ function f(x) {}" + |
| 3168 |
"/** @enum {Object} */ var MyEnum = {FOO: {newProperty: 1, b: 2}};" + |
| 3169 |
"f(MyEnum.FOO.newProperty);"); |
| 3170 |
} |
| 3171 |
|
| 3172 |
public void testAliasedEnum1() throws Exception { |
| 3173 |
testTypes( |
| 3174 |
"/** @enum */ var YourEnum = {FOO: 3};" + |
| 3175 |
"/** @enum */ var MyEnum = YourEnum;" + |
| 3176 |
"/** @param {MyEnum} x */ function f(x) {} f(MyEnum.FOO);"); |
| 3177 |
} |
| 3178 |
|
| 3179 |
public void testAliasedEnum2() throws Exception { |
| 3180 |
testTypes( |
| 3181 |
"/** @enum */ var YourEnum = {FOO: 3};" + |
| 3182 |
"/** @enum */ var MyEnum = YourEnum;" + |
| 3183 |
"/** @param {YourEnum} x */ function f(x) {} f(MyEnum.FOO);"); |
| 3184 |
} |
| 3185 |
|
| 3186 |
public void testAliasedEnum3() throws Exception { |
| 3187 |
testTypes( |
| 3188 |
"/** @enum */ var YourEnum = {FOO: 3};" + |
| 3189 |
"/** @enum */ var MyEnum = YourEnum;" + |
| 3190 |
"/** @param {MyEnum} x */ function f(x) {} f(YourEnum.FOO);"); |
| 3191 |
} |
| 3192 |
|
| 3193 |
public void testAliasedEnum4() throws Exception { |
| 3194 |
testTypes( |
| 3195 |
"/** @enum */ var YourEnum = {FOO: 3};" + |
| 3196 |
"/** @enum */ var MyEnum = YourEnum;" + |
| 3197 |
"/** @param {YourEnum} x */ function f(x) {} f(YourEnum.FOO);"); |
| 3198 |
} |
| 3199 |
|
| 3200 |
public void testAliasedEnum5() throws Exception { |
| 3201 |
testTypes( |
| 3202 |
"/** @enum */ var YourEnum = {FOO: 3};" + |
| 3203 |
"/** @enum */ var MyEnum = YourEnum;" + |
| 3204 |
"/** @param {string} x */ function f(x) {} f(MyEnum.FOO);", |
| 3205 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 3206 |
"found : YourEnum.<number>\n" + |
| 3207 |
"required: string"); |
| 3208 |
} |
| 3209 |
|
| 3210 |
public void testBackwardsEnumUse1() throws Exception { |
| 3211 |
testTypes( |
| 3212 |
"/** @return {string} */ function f() { return MyEnum.FOO; }" + |
| 3213 |
"/** @enum {string} */ var MyEnum = {FOO: 'x'};"); |
| 3214 |
} |
| 3215 |
|
| 3216 |
public void testBackwardsEnumUse2() throws Exception { |
| 3217 |
testTypes( |
| 3218 |
"/** @return {number} */ function f() { return MyEnum.FOO; }" + |
| 3219 |
"/** @enum {string} */ var MyEnum = {FOO: 'x'};", |
| 3220 |
"inconsistent return type\n" + |
| 3221 |
"found : MyEnum.<string>\n" + |
| 3222 |
"required: number"); |
| 3223 |
} |
| 3224 |
|
| 3225 |
public void testBackwardsEnumUse3() throws Exception { |
| 3226 |
testTypes( |
| 3227 |
"/** @return {string} */ function f() { return MyEnum.FOO; }" + |
| 3228 |
"/** @enum {string} */ var YourEnum = {FOO: 'x'};" + |
| 3229 |
"/** @enum {string} */ var MyEnum = YourEnum;"); |
| 3230 |
} |
| 3231 |
|
| 3232 |
public void testBackwardsEnumUse4() throws Exception { |
| 3233 |
testTypes( |
| 3234 |
"/** @return {number} */ function f() { return MyEnum.FOO; }" + |
| 3235 |
"/** @enum {string} */ var YourEnum = {FOO: 'x'};" + |
| 3236 |
"/** @enum {string} */ var MyEnum = YourEnum;", |
| 3237 |
"inconsistent return type\n" + |
| 3238 |
"found : YourEnum.<string>\n" + |
| 3239 |
"required: number"); |
| 3240 |
} |
| 3241 |
|
| 3242 |
public void testBackwardsEnumUse5() throws Exception { |
| 3243 |
testTypes( |
| 3244 |
"/** @return {string} */ function f() { return MyEnum.BAR; }" + |
| 3245 |
"/** @enum {string} */ var YourEnum = {FOO: 'x'};" + |
| 3246 |
"/** @enum {string} */ var MyEnum = YourEnum;", |
| 3247 |
"element BAR does not exist on this enum"); |
| 3248 |
} |
| 3249 |
|
| 3250 |
public void testBackwardsTypedefUse2() throws Exception { |
| 3251 |
testTypes( |
| 3252 |
"/** @this {MyTypedef} */ function f() {}" + |
| 3253 |
"/** @typedef {!(Date|Array)} */ var MyTypedef;"); |
| 3254 |
} |
| 3255 |
|
| 3256 |
public void testBackwardsTypedefUse4() throws Exception { |
| 3257 |
testTypes( |
| 3258 |
"/** @return {MyTypedef} */ function f() { return null; }" + |
| 3259 |
"/** @typedef {string} */ var MyTypedef;", |
| 3260 |
"inconsistent return type\n" + |
| 3261 |
"found : null\n" + |
| 3262 |
"required: string"); |
| 3263 |
} |
| 3264 |
|
| 3265 |
public void testBackwardsTypedefUse6() throws Exception { |
| 3266 |
testTypes( |
| 3267 |
"/** @return {goog.MyTypedef} */ function f() { return null; }" + |
| 3268 |
"var goog = {};" + |
| 3269 |
"/** @typedef {string} */ goog.MyTypedef;", |
| 3270 |
"inconsistent return type\n" + |
| 3271 |
"found : null\n" + |
| 3272 |
"required: string"); |
| 3273 |
} |
| 3274 |
|
| 3275 |
public void testBackwardsTypedefUse7() throws Exception { |
| 3276 |
testTypes( |
| 3277 |
"/** @return {goog.MyTypedef} */ function f() { return null; }" + |
| 3278 |
"var goog = {};" + |
| 3279 |
"/** @typedef {Object} */ goog.MyTypedef;"); |
| 3280 |
} |
| 3281 |
|
| 3282 |
public void testBackwardsTypedefUse8() throws Exception { |
| 3283 |
|
| 3284 |
|
| 3285 |
testTypes( |
| 3286 |
"/** @param {!Array} x */ function g(x) {}" + |
| 3287 |
"/** @this {goog.MyTypedef} */ function f() { g(this); }" + |
| 3288 |
"var goog = {};" + |
| 3289 |
"/** @typedef {(Array|null|undefined)} */ goog.MyTypedef;"); |
| 3290 |
} |
| 3291 |
|
| 3292 |
public void testBackwardsTypedefUse9() throws Exception { |
| 3293 |
testTypes( |
| 3294 |
"/** @param {!Array} x */ function g(x) {}" + |
| 3295 |
"/** @this {goog.MyTypedef} */ function f() { g(this); }" + |
| 3296 |
"var goog = {};" + |
| 3297 |
"/** @typedef {(Error|null|undefined)} */ goog.MyTypedef;", |
| 3298 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 3299 |
"found : Error\n" + |
| 3300 |
"required: Array"); |
| 3301 |
} |
| 3302 |
|
| 3303 |
public void testBackwardsTypedefUse10() throws Exception { |
| 3304 |
testTypes( |
| 3305 |
"/** @param {goog.MyEnum} x */ function g(x) {}" + |
| 3306 |
"var goog = {};" + |
| 3307 |
"/** @enum {goog.MyTypedef} */ goog.MyEnum = {FOO: 1};" + |
| 3308 |
"/** @typedef {number} */ goog.MyTypedef;" + |
| 3309 |
"g(1);", |
| 3310 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 3311 |
"found : number\n" + |
| 3312 |
"required: goog.MyEnum.<number>"); |
| 3313 |
} |
| 3314 |
|
| 3315 |
public void testBackwardsConstructor1() throws Exception { |
| 3316 |
testTypes( |
| 3317 |
"function f() { (new Foo(true)); }" + |
| 3318 |
"/** \n * @constructor \n * @param {number} x */" + |
| 3319 |
"var Foo = function(x) {};", |
| 3320 |
"actual parameter 1 of Foo does not match formal parameter\n" + |
| 3321 |
"found : boolean\n" + |
| 3322 |
"required: number"); |
| 3323 |
} |
| 3324 |
|
| 3325 |
public void testBackwardsConstructor2() throws Exception { |
| 3326 |
testTypes( |
| 3327 |
"function f() { (new Foo(true)); }" + |
| 3328 |
"/** \n * @constructor \n * @param {number} x */" + |
| 3329 |
"var YourFoo = function(x) {};" + |
| 3330 |
"/** \n * @constructor \n * @param {number} x */" + |
| 3331 |
"var Foo = YourFoo;", |
| 3332 |
"actual parameter 1 of Foo does not match formal parameter\n" + |
| 3333 |
"found : boolean\n" + |
| 3334 |
"required: number"); |
| 3335 |
} |
| 3336 |
|
| 3337 |
public void testMinimalConstructorAnnotation() throws Exception { |
| 3338 |
testTypes("/** @constructor */function Foo(){}"); |
| 3339 |
} |
| 3340 |
|
| 3341 |
public void testGoodExtends1() throws Exception { |
| 3342 |
|
| 3343 |
testTypes("/** @constructor */function base() {}\n" + |
| 3344 |
"/** @constructor\n * @extends {base} */function derived() {}\n"); |
| 3345 |
} |
| 3346 |
|
| 3347 |
public void testGoodExtends2() throws Exception { |
| 3348 |
testTypes("/** @constructor\n * @extends base */function derived() {}\n" + |
| 3349 |
"/** @constructor */function base() {}\n"); |
| 3350 |
} |
| 3351 |
|
| 3352 |
public void testGoodExtends3() throws Exception { |
| 3353 |
testTypes("/** @constructor\n * @extends {Object} */function base() {}\n" + |
| 3354 |
"/** @constructor\n * @extends {base} */function derived() {}\n"); |
| 3355 |
} |
| 3356 |
|
| 3357 |
public void testGoodExtends4() throws Exception { |
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
Node n = parseAndTypeCheck( |
| 3362 |
"var goog = {};\n" + |
| 3363 |
"/** @constructor */goog.Base = function(){};\n" + |
| 3364 |
"/** @constructor\n" + |
| 3365 |
" * @extends {goog.Base} */goog.Derived = function(){};\n"); |
| 3366 |
Node subTypeName = n.getLastChild().getLastChild().getFirstChild(); |
| 3367 |
assertEquals("goog.Derived", subTypeName.getQualifiedName()); |
| 3368 |
|
| 3369 |
FunctionType subCtorType = |
| 3370 |
(FunctionType) subTypeName.getNext().getJSType(); |
| 3371 |
assertEquals("goog.Derived", subCtorType.getInstanceType().toString()); |
| 3372 |
|
| 3373 |
JSType superType = subCtorType.getPrototype().getImplicitPrototype(); |
| 3374 |
assertEquals("goog.Base", superType.toString()); |
| 3375 |
} |
| 3376 |
|
| 3377 |
public void testGoodExtends5() throws Exception { |
| 3378 |
|
| 3379 |
testTypes("/** @constructor */function base() {}\n" + |
| 3380 |
"/** @extends {base}\n * @constructor */function derived() {}\n"); |
| 3381 |
} |
| 3382 |
|
| 3383 |
public void testGoodExtends6() throws Exception { |
| 3384 |
testFunctionType( |
| 3385 |
CLOSURE_DEFS + |
| 3386 |
"/** @constructor */function base() {}\n" + |
| 3387 |
"/** @return {number} */ " + |
| 3388 |
" base.prototype.foo = function() { return 1; };\n" + |
| 3389 |
"/** @extends {base}\n * @constructor */function derived() {}\n" + |
| 3390 |
"goog.inherits(derived, base);", |
| 3391 |
"derived.superClass_.foo", |
| 3392 |
"function (this:base): number"); |
| 3393 |
} |
| 3394 |
|
| 3395 |
public void testGoodExtends7() throws Exception { |
| 3396 |
testFunctionType( |
| 3397 |
"Function.prototype.inherits = function(x) {};" + |
| 3398 |
"/** @constructor */function base() {}\n" + |
| 3399 |
"/** @extends {base}\n * @constructor */function derived() {}\n" + |
| 3400 |
"derived.inherits(base);", |
| 3401 |
"(new derived).constructor", |
| 3402 |
"function (new:derived, ...[?]): ?"); |
| 3403 |
} |
| 3404 |
|
| 3405 |
public void testGoodExtends8() throws Exception { |
| 3406 |
testTypes("/** @constructor \n @extends {Base} */ function Sub() {}" + |
| 3407 |
"/** @return {number} */ function f() { return (new Sub()).foo; }" + |
| 3408 |
"/** @constructor */ function Base() {}" + |
| 3409 |
"/** @type {boolean} */ Base.prototype.foo = true;", |
| 3410 |
"inconsistent return type\n" + |
| 3411 |
"found : boolean\n" + |
| 3412 |
"required: number"); |
| 3413 |
} |
| 3414 |
|
| 3415 |
public void testGoodExtends9() throws Exception { |
| 3416 |
testTypes( |
| 3417 |
"/** @constructor */ function Super() {}" + |
| 3418 |
"Super.prototype.foo = function() {};" + |
| 3419 |
"/** @constructor \n * @extends {Super} */ function Sub() {}" + |
| 3420 |
"Sub.prototype = new Super();" + |
| 3421 |
"/** @override */ Sub.prototype.foo = function() {};"); |
| 3422 |
} |
| 3423 |
|
| 3424 |
public void testGoodExtends10() throws Exception { |
| 3425 |
testTypes( |
| 3426 |
"/** @constructor */ function Super() {}" + |
| 3427 |
"/** @constructor \n * @extends {Super} */ function Sub() {}" + |
| 3428 |
"Sub.prototype = new Super();" + |
| 3429 |
"/** @return {Super} */ function foo() { return new Sub(); }"); |
| 3430 |
} |
| 3431 |
|
| 3432 |
public void testGoodExtends11() throws Exception { |
| 3433 |
testTypes( |
| 3434 |
"/** @constructor */ function Super() {}" + |
| 3435 |
"/** @param {boolean} x */ Super.prototype.foo = function(x) {};" + |
| 3436 |
"/** @constructor \n * @extends {Super} */ function Sub() {}" + |
| 3437 |
"Sub.prototype = new Super();" + |
| 3438 |
"(new Sub()).foo(0);", |
| 3439 |
"actual parameter 1 of Super.prototype.foo " + |
| 3440 |
"does not match formal parameter\n" + |
| 3441 |
"found : number\n" + |
| 3442 |
"required: boolean"); |
| 3443 |
} |
| 3444 |
|
| 3445 |
public void testGoodExtends12() throws Exception { |
| 3446 |
testTypes( |
| 3447 |
"/** @constructor \n * @extends {Super} */ function Sub() {}" + |
| 3448 |
"/** @constructor \n * @extends {Sub} */ function Sub2() {}" + |
| 3449 |
"/** @constructor */ function Super() {}" + |
| 3450 |
"/** @param {Super} x */ function foo(x) {}" + |
| 3451 |
"foo(new Sub2());"); |
| 3452 |
} |
| 3453 |
|
| 3454 |
public void testGoodExtends13() throws Exception { |
| 3455 |
testTypes( |
| 3456 |
"/** @constructor \n * @extends {B} */ function C() {}" + |
| 3457 |
"/** @constructor \n * @extends {D} */ function E() {}" + |
| 3458 |
"/** @constructor \n * @extends {C} */ function D() {}" + |
| 3459 |
"/** @constructor \n * @extends {A} */ function B() {}" + |
| 3460 |
"/** @constructor */ function A() {}" + |
| 3461 |
"/** @param {number} x */ function f(x) {} f(new E());", |
| 3462 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 3463 |
"found : E\n" + |
| 3464 |
"required: number"); |
| 3465 |
} |
| 3466 |
|
| 3467 |
public void testGoodExtends14() throws Exception { |
| 3468 |
testTypes( |
| 3469 |
CLOSURE_DEFS + |
| 3470 |
"/** @param {Function} f */ function g(f) {" + |
| 3471 |
" /** @constructor */ function NewType() {};" + |
| 3472 |
" goog.inherits(NewType, f);" + |
| 3473 |
" (new NewType());" + |
| 3474 |
"}"); |
| 3475 |
} |
| 3476 |
|
| 3477 |
public void testGoodExtends15() throws Exception { |
| 3478 |
testTypes( |
| 3479 |
CLOSURE_DEFS + |
| 3480 |
"/** @constructor */ function OldType() {}" + |
| 3481 |
"/** @param {?function(new:OldType)} f */ function g(f) {" + |
| 3482 |
" /**\n" + |
| 3483 |
" * @constructor\n" + |
| 3484 |
" * @extends {OldType}\n" + |
| 3485 |
" */\n" + |
| 3486 |
" function NewType() {};" + |
| 3487 |
" goog.inherits(NewType, f);" + |
| 3488 |
" NewType.prototype.method = function() {" + |
| 3489 |
" NewType.superClass_.foo.call(this);" + |
| 3490 |
" };" + |
| 3491 |
"}", |
| 3492 |
"Property foo never defined on OldType.prototype"); |
| 3493 |
} |
| 3494 |
|
| 3495 |
public void testGoodExtends16() throws Exception { |
| 3496 |
testTypes( |
| 3497 |
CLOSURE_DEFS + |
| 3498 |
"/** @param {Function} f */ function g(f) {" + |
| 3499 |
" /** @constructor */ function NewType() {};" + |
| 3500 |
" goog.inherits(f, NewType);" + |
| 3501 |
" (new NewType());" + |
| 3502 |
"}"); |
| 3503 |
} |
| 3504 |
|
| 3505 |
public void testGoodExtends17() throws Exception { |
| 3506 |
testFunctionType( |
| 3507 |
"Function.prototype.inherits = function(x) {};" + |
| 3508 |
"/** @constructor */function base() {}\n" + |
| 3509 |
"/** @param {number} x */ base.prototype.bar = function(x) {};\n" + |
| 3510 |
"/** @extends {base}\n * @constructor */function derived() {}\n" + |
| 3511 |
"derived.inherits(base);", |
| 3512 |
"(new derived).constructor.prototype.bar", |
| 3513 |
"function (this:base, number): undefined"); |
| 3514 |
} |
| 3515 |
|
| 3516 |
public void testBadExtends1() throws Exception { |
| 3517 |
testTypes("/** @constructor */function base() {}\n" + |
| 3518 |
"/** @constructor\n * @extends {not_base} */function derived() {}\n", |
| 3519 |
"Bad type annotation. Unknown type not_base"); |
| 3520 |
} |
| 3521 |
|
| 3522 |
public void testBadExtends2() throws Exception { |
| 3523 |
testTypes("/** @constructor */function base() {\n" + |
| 3524 |
"/** @type {!Number}*/\n" + |
| 3525 |
"this.baseMember = new Number(4);\n" + |
| 3526 |
"}\n" + |
| 3527 |
"/** @constructor\n" + |
| 3528 |
" * @extends {base} */function derived() {}\n" + |
| 3529 |
"/** @param {!String} x*/\n" + |
| 3530 |
"function foo(x){ }\n" + |
| 3531 |
"/** @type {!derived}*/var y;\n" + |
| 3532 |
"foo(y.baseMember);\n", |
| 3533 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 3534 |
"found : Number\n" + |
| 3535 |
"required: String"); |
| 3536 |
} |
| 3537 |
|
| 3538 |
public void testBadExtends3() throws Exception { |
| 3539 |
testTypes("/** @extends {Object} */function base() {}", |
| 3540 |
"@extends used without @constructor or @interface for base"); |
| 3541 |
} |
| 3542 |
|
| 3543 |
public void testBadExtends4() throws Exception { |
| 3544 |
|
| 3545 |
|
| 3546 |
testTypes( |
| 3547 |
"/** @constructor \n * @extends {bad} */ function Sub() {}" + |
| 3548 |
"/** @constructor \n * @extends {Sub} */ function Sub2() {}" + |
| 3549 |
"/** @param {Sub} x */ function foo(x) {}" + |
| 3550 |
"foo(new Sub2());", |
| 3551 |
"Bad type annotation. Unknown type bad"); |
| 3552 |
} |
| 3553 |
|
| 3554 |
public void testLateExtends() throws Exception { |
| 3555 |
testTypes( |
| 3556 |
CLOSURE_DEFS + |
| 3557 |
"/** @constructor */ function Foo() {}\n" + |
| 3558 |
"Foo.prototype.foo = function() {};\n" + |
| 3559 |
"/** @constructor */function Bar() {}\n" + |
| 3560 |
"goog.inherits(Foo, Bar);\n", |
| 3561 |
"Missing @extends tag on type Foo"); |
| 3562 |
} |
| 3563 |
|
| 3564 |
public void testSuperclassMatch() throws Exception { |
| 3565 |
compiler.getOptions().setCodingConvention(new GoogleCodingConvention()); |
| 3566 |
testTypes("/** @constructor */ var Foo = function() {};\n" + |
| 3567 |
"/** @constructor \n @extends Foo */ var Bar = function() {};\n" + |
| 3568 |
"Bar.inherits = function(x){};" + |
| 3569 |
"Bar.inherits(Foo);\n"); |
| 3570 |
} |
| 3571 |
|
| 3572 |
public void testSuperclassMatchWithMixin() throws Exception { |
| 3573 |
compiler.getOptions().setCodingConvention(new GoogleCodingConvention()); |
| 3574 |
testTypes("/** @constructor */ var Foo = function() {};\n" + |
| 3575 |
"/** @constructor */ var Baz = function() {};\n" + |
| 3576 |
"/** @constructor \n @extends Foo */ var Bar = function() {};\n" + |
| 3577 |
"Bar.inherits = function(x){};" + |
| 3578 |
"Bar.mixin = function(y){};" + |
| 3579 |
"Bar.inherits(Foo);\n" + |
| 3580 |
"Bar.mixin(Baz);\n"); |
| 3581 |
} |
| 3582 |
|
| 3583 |
public void testSuperclassMismatch1() throws Exception { |
| 3584 |
compiler.getOptions().setCodingConvention(new GoogleCodingConvention()); |
| 3585 |
testTypes("/** @constructor */ var Foo = function() {};\n" + |
| 3586 |
"/** @constructor \n @extends Object */ var Bar = function() {};\n" + |
| 3587 |
"Bar.inherits = function(x){};" + |
| 3588 |
"Bar.inherits(Foo);\n", |
| 3589 |
"Missing @extends tag on type Bar"); |
| 3590 |
} |
| 3591 |
|
| 3592 |
public void testSuperclassMismatch2() throws Exception { |
| 3593 |
compiler.getOptions().setCodingConvention(new GoogleCodingConvention()); |
| 3594 |
testTypes("/** @constructor */ var Foo = function(){};\n" + |
| 3595 |
"/** @constructor */ var Bar = function(){};\n" + |
| 3596 |
"Bar.inherits = function(x){};" + |
| 3597 |
"Bar.inherits(Foo);", |
| 3598 |
"Missing @extends tag on type Bar"); |
| 3599 |
} |
| 3600 |
|
| 3601 |
public void testSuperClassDefinedAfterSubClass1() throws Exception { |
| 3602 |
testTypes( |
| 3603 |
"/** @constructor \n * @extends {Base} */ function A() {}" + |
| 3604 |
"/** @constructor \n * @extends {Base} */ function B() {}" + |
| 3605 |
"/** @constructor */ function Base() {}" + |
| 3606 |
"/** @param {A|B} x \n * @return {B|A} */ " + |
| 3607 |
"function foo(x) { return x; }"); |
| 3608 |
} |
| 3609 |
|
| 3610 |
public void testSuperClassDefinedAfterSubClass2() throws Exception { |
| 3611 |
testTypes( |
| 3612 |
"/** @constructor \n * @extends {Base} */ function A() {}" + |
| 3613 |
"/** @constructor \n * @extends {Base} */ function B() {}" + |
| 3614 |
"/** @param {A|B} x \n * @return {B|A} */ " + |
| 3615 |
"function foo(x) { return x; }" + |
| 3616 |
"/** @constructor */ function Base() {}"); |
| 3617 |
} |
| 3618 |
|
| 3619 |
public void testDirectPrototypeAssignment1() throws Exception { |
| 3620 |
testTypes( |
| 3621 |
"/** @constructor */ function Base() {}" + |
| 3622 |
"Base.prototype.foo = 3;" + |
| 3623 |
"/** @constructor \n * @extends {Base} */ function A() {}" + |
| 3624 |
"A.prototype = new Base();" + |
| 3625 |
"/** @return {string} */ function foo() { return (new A).foo; }", |
| 3626 |
"inconsistent return type\n" + |
| 3627 |
"found : number\n" + |
| 3628 |
"required: string"); |
| 3629 |
} |
| 3630 |
|
| 3631 |
public void testDirectPrototypeAssignment2() throws Exception { |
| 3632 |
|
| 3633 |
|
| 3634 |
testTypes( |
| 3635 |
"/** @constructor */ function Base() {}" + |
| 3636 |
"/** @constructor \n * @extends {Base} */ function A() {}" + |
| 3637 |
"A.prototype = new Base();" + |
| 3638 |
"A.prototype.foo = 3;" + |
| 3639 |
"/** @return {string} */ function foo() { return (new Base).foo; }"); |
| 3640 |
} |
| 3641 |
|
| 3642 |
public void testDirectPrototypeAssignment3() throws Exception { |
| 3643 |
|
| 3644 |
|
| 3645 |
testTypes( |
| 3646 |
"/** @constructor */ var MainWidgetCreator = function() {};" + |
| 3647 |
"/** @param {Function} ctor */" + |
| 3648 |
"function createMainWidget(ctor) {" + |
| 3649 |
" /** @constructor */ function tempCtor() {};" + |
| 3650 |
" tempCtor.prototype = ctor.prototype;" + |
| 3651 |
" MainWidgetCreator.superClass_ = ctor.prototype;" + |
| 3652 |
" MainWidgetCreator.prototype = new tempCtor();" + |
| 3653 |
"}"); |
| 3654 |
} |
| 3655 |
|
| 3656 |
public void testGoodImplements1() throws Exception { |
| 3657 |
testTypes("/** @interface */function Disposable() {}\n" + |
| 3658 |
"/** @implements {Disposable}\n * @constructor */function f() {}"); |
| 3659 |
} |
| 3660 |
|
| 3661 |
public void testGoodImplements2() throws Exception { |
| 3662 |
testTypes("/** @interface */function Base1() {}\n" + |
| 3663 |
"/** @interface */function Base2() {}\n" + |
| 3664 |
"/** @constructor\n" + |
| 3665 |
" * @implements {Base1}\n" + |
| 3666 |
" * @implements {Base2}\n" + |
| 3667 |
" */ function derived() {}"); |
| 3668 |
} |
| 3669 |
|
| 3670 |
public void testGoodImplements3() throws Exception { |
| 3671 |
testTypes("/** @interface */function Disposable() {}\n" + |
| 3672 |
"/** @constructor \n @implements {Disposable} */function f() {}"); |
| 3673 |
} |
| 3674 |
|
| 3675 |
public void testGoodImplements4() throws Exception { |
| 3676 |
testTypes("var goog = {};" + |
| 3677 |
"/** @type {!Function} */" + |
| 3678 |
"goog.abstractMethod = function() {};" + |
| 3679 |
"/** @interface */\n" + |
| 3680 |
"goog.Disposable = goog.abstractMethod;" + |
| 3681 |
"goog.Disposable.prototype.dispose = goog.abstractMethod;" + |
| 3682 |
"/** @implements {goog.Disposable}\n * @constructor */" + |
| 3683 |
"goog.SubDisposable = function() {};" + |
| 3684 |
"/** @inheritDoc */ " + |
| 3685 |
"goog.SubDisposable.prototype.dispose = function() {};"); |
| 3686 |
} |
| 3687 |
|
| 3688 |
public void testGoodImplements5() throws Exception { |
| 3689 |
testTypes( |
| 3690 |
"/** @interface */\n" + |
| 3691 |
"goog.Disposable = function() {};" + |
| 3692 |
"/** @type {Function} */" + |
| 3693 |
"goog.Disposable.prototype.dispose = function() {};" + |
| 3694 |
"/** @implements {goog.Disposable}\n * @constructor */" + |
| 3695 |
"goog.SubDisposable = function() {};" + |
| 3696 |
"/** @param {number} key \n @override */ " + |
| 3697 |
"goog.SubDisposable.prototype.dispose = function(key) {};"); |
| 3698 |
} |
| 3699 |
|
| 3700 |
public void testGoodImplements6() throws Exception { |
| 3701 |
testTypes( |
| 3702 |
"var myNullFunction = function() {};" + |
| 3703 |
"/** @interface */\n" + |
| 3704 |
"goog.Disposable = function() {};" + |
| 3705 |
"/** @return {number} */" + |
| 3706 |
"goog.Disposable.prototype.dispose = myNullFunction;" + |
| 3707 |
"/** @implements {goog.Disposable}\n * @constructor */" + |
| 3708 |
"goog.SubDisposable = function() {};" + |
| 3709 |
"/** @return {number} \n @override */ " + |
| 3710 |
"goog.SubDisposable.prototype.dispose = function() { return 0; };"); |
| 3711 |
} |
| 3712 |
|
| 3713 |
public void testGoodImplements7() throws Exception { |
| 3714 |
testTypes( |
| 3715 |
"var myNullFunction = function() {};" + |
| 3716 |
"/** @interface */\n" + |
| 3717 |
"goog.Disposable = function() {};" + |
| 3718 |
"/** @return {number} */" + |
| 3719 |
"goog.Disposable.prototype.dispose = function() {};" + |
| 3720 |
"/** @implements {goog.Disposable}\n * @constructor */" + |
| 3721 |
"goog.SubDisposable = function() {};" + |
| 3722 |
"/** @return {number} \n @override */ " + |
| 3723 |
"goog.SubDisposable.prototype.dispose = function() { return 0; };"); |
| 3724 |
} |
| 3725 |
|
| 3726 |
public void testBadImplements1() throws Exception { |
| 3727 |
testTypes("/** @interface */function Base1() {}\n" + |
| 3728 |
"/** @interface */function Base2() {}\n" + |
| 3729 |
"/** @constructor\n" + |
| 3730 |
" * @implements {nonExistent}\n" + |
| 3731 |
" * @implements {Base2}\n" + |
| 3732 |
" */ function derived() {}", |
| 3733 |
"Bad type annotation. Unknown type nonExistent"); |
| 3734 |
} |
| 3735 |
|
| 3736 |
public void testBadImplements2() throws Exception { |
| 3737 |
testTypes("/** @interface */function Disposable() {}\n" + |
| 3738 |
"/** @implements {Disposable}\n */function f() {}", |
| 3739 |
"@implements used without @constructor for f"); |
| 3740 |
} |
| 3741 |
|
| 3742 |
public void testBadImplements3() throws Exception { |
| 3743 |
testTypes( |
| 3744 |
"var goog = {};" + |
| 3745 |
"/** @type {!Function} */ goog.abstractMethod = function(){};" + |
| 3746 |
"/** @interface */ var Disposable = goog.abstractMethod;" + |
| 3747 |
"Disposable.prototype.method = goog.abstractMethod;" + |
| 3748 |
"/** @implements {Disposable}\n * @constructor */function f() {}", |
| 3749 |
"property method on interface Disposable is not implemented by type f"); |
| 3750 |
} |
| 3751 |
|
| 3752 |
public void testBadImplements4() throws Exception { |
| 3753 |
testTypes("/** @interface */function Disposable() {}\n" + |
| 3754 |
"/** @implements {Disposable}\n * @interface */function f() {}", |
| 3755 |
"f cannot implement this type; an interface can only extend, " + |
| 3756 |
"but not implement interfaces"); |
| 3757 |
} |
| 3758 |
|
| 3759 |
public void testBadImplements5() throws Exception { |
| 3760 |
testTypes("/** @interface */function Disposable() {}\n" + |
| 3761 |
"/** @type {number} */ Disposable.prototype.bar = function() {};", |
| 3762 |
"assignment to property bar of Disposable.prototype\n" + |
| 3763 |
"found : function (): undefined\n" + |
| 3764 |
"required: number"); |
| 3765 |
} |
| 3766 |
|
| 3767 |
public void testBadImplements6() throws Exception { |
| 3768 |
testClosureTypesMultipleWarnings( |
| 3769 |
"/** @interface */function Disposable() {}\n" + |
| 3770 |
"/** @type {function()} */ Disposable.prototype.bar = 3;", |
| 3771 |
Lists.newArrayList( |
| 3772 |
"assignment to property bar of Disposable.prototype\n" + |
| 3773 |
"found : number\n" + |
| 3774 |
"required: function (): ?", |
| 3775 |
"interface members can only be empty property declarations, " + |
| 3776 |
"empty functions, or goog.abstractMethod")); |
| 3777 |
} |
| 3778 |
|
| 3779 |
public void testConstructorClassTemplate() throws Exception { |
| 3780 |
testTypes("/** @constructor \n @classTemplate S,T */ function A() {}\n"); |
| 3781 |
} |
| 3782 |
|
| 3783 |
|
| 3784 |
public void disable_testBadClassTemplate() throws Exception { |
| 3785 |
testTypes("/** @classTemplate T */function A() {}", |
| 3786 |
"Bad type annotation. Unknown type nonExistent"); |
| 3787 |
} |
| 3788 |
|
| 3789 |
public void testInterfaceExtends() throws Exception { |
| 3790 |
testTypes("/** @interface */function A() {}\n" + |
| 3791 |
"/** @interface \n * @extends {A} */function B() {}\n" + |
| 3792 |
"/** @constructor\n" + |
| 3793 |
" * @implements {B}\n" + |
| 3794 |
" */ function derived() {}"); |
| 3795 |
} |
| 3796 |
|
| 3797 |
public void testBadInterfaceExtends1() throws Exception { |
| 3798 |
testTypes("/** @interface \n * @extends {nonExistent} */function A() {}", |
| 3799 |
"Bad type annotation. Unknown type nonExistent"); |
| 3800 |
} |
| 3801 |
|
| 3802 |
public void testBadInterfaceExtendsNonExistentInterfaces() throws Exception { |
| 3803 |
String js = "/** @interface \n" + |
| 3804 |
" * @extends {nonExistent1} \n" + |
| 3805 |
" * @extends {nonExistent2} \n" + |
| 3806 |
" */function A() {}"; |
| 3807 |
String[] expectedWarnings = { |
| 3808 |
"Bad type annotation. Unknown type nonExistent1", |
| 3809 |
"Bad type annotation. Unknown type nonExistent2" |
| 3810 |
}; |
| 3811 |
testTypes(js, expectedWarnings); |
| 3812 |
} |
| 3813 |
|
| 3814 |
public void testBadInterfaceExtends2() throws Exception { |
| 3815 |
testTypes("/** @constructor */function A() {}\n" + |
| 3816 |
"/** @interface \n * @extends {A} */function B() {}", |
| 3817 |
"B cannot extend this type; interfaces can only extend interfaces"); |
| 3818 |
} |
| 3819 |
|
| 3820 |
public void testBadInterfaceExtends3() throws Exception { |
| 3821 |
testTypes("/** @interface */function A() {}\n" + |
| 3822 |
"/** @constructor \n * @extends {A} */function B() {}", |
| 3823 |
"B cannot extend this type; constructors can only extend constructors"); |
| 3824 |
} |
| 3825 |
|
| 3826 |
public void testBadInterfaceExtends4() throws Exception { |
| 3827 |
|
| 3828 |
|
| 3829 |
|
| 3830 |
testTypes("/** @interface */function A() {}\n" + |
| 3831 |
"/** @constructor */function B() {}\n" + |
| 3832 |
"B.prototype = A;"); |
| 3833 |
} |
| 3834 |
|
| 3835 |
public void testBadInterfaceExtends5() throws Exception { |
| 3836 |
|
| 3837 |
|
| 3838 |
|
| 3839 |
testTypes("/** @constructor */function A() {}\n" + |
| 3840 |
"/** @interface */function B() {}\n" + |
| 3841 |
"B.prototype = A;"); |
| 3842 |
} |
| 3843 |
|
| 3844 |
public void testBadImplementsAConstructor() throws Exception { |
| 3845 |
testTypes("/** @constructor */function A() {}\n" + |
| 3846 |
"/** @constructor \n * @implements {A} */function B() {}", |
| 3847 |
"can only implement interfaces"); |
| 3848 |
} |
| 3849 |
|
| 3850 |
public void testBadImplementsNonInterfaceType() throws Exception { |
| 3851 |
testTypes("/** @constructor \n * @implements {Boolean} */function B() {}", |
| 3852 |
"can only implement interfaces"); |
| 3853 |
} |
| 3854 |
|
| 3855 |
public void testBadImplementsNonObjectType() throws Exception { |
| 3856 |
testTypes("/** @constructor \n * @implements {string} */function S() {}", |
| 3857 |
"can only implement interfaces"); |
| 3858 |
} |
| 3859 |
|
| 3860 |
public void testInterfaceAssignment1() throws Exception { |
| 3861 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3862 |
"/** @constructor\n@implements {I} */var T = function() {};\n" + |
| 3863 |
"var t = new T();\n" + |
| 3864 |
"/** @type {!I} */var i = t;"); |
| 3865 |
} |
| 3866 |
|
| 3867 |
public void testInterfaceAssignment2() throws Exception { |
| 3868 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3869 |
"/** @constructor */var T = function() {};\n" + |
| 3870 |
"var t = new T();\n" + |
| 3871 |
"/** @type {!I} */var i = t;", |
| 3872 |
"initializing variable\n" + |
| 3873 |
"found : T\n" + |
| 3874 |
"required: I"); |
| 3875 |
} |
| 3876 |
|
| 3877 |
public void testInterfaceAssignment3() throws Exception { |
| 3878 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3879 |
"/** @constructor\n@implements {I} */var T = function() {};\n" + |
| 3880 |
"var t = new T();\n" + |
| 3881 |
"/** @type {I|number} */var i = t;"); |
| 3882 |
} |
| 3883 |
|
| 3884 |
public void testInterfaceAssignment4() throws Exception { |
| 3885 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3886 |
"/** @interface */var I2 = function() {};\n" + |
| 3887 |
"/** @constructor\n@implements {I1} */var T = function() {};\n" + |
| 3888 |
"var t = new T();\n" + |
| 3889 |
"/** @type {I1|I2} */var i = t;"); |
| 3890 |
} |
| 3891 |
|
| 3892 |
public void testInterfaceAssignment5() throws Exception { |
| 3893 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3894 |
"/** @interface */var I2 = function() {};\n" + |
| 3895 |
"/** @constructor\n@implements {I1}\n@implements {I2}*/" + |
| 3896 |
"var T = function() {};\n" + |
| 3897 |
"var t = new T();\n" + |
| 3898 |
"/** @type {I1} */var i1 = t;\n" + |
| 3899 |
"/** @type {I2} */var i2 = t;\n"); |
| 3900 |
} |
| 3901 |
|
| 3902 |
public void testInterfaceAssignment6() throws Exception { |
| 3903 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3904 |
"/** @interface */var I2 = function() {};\n" + |
| 3905 |
"/** @constructor\n@implements {I1} */var T = function() {};\n" + |
| 3906 |
"/** @type {!I1} */var i1 = new T();\n" + |
| 3907 |
"/** @type {!I2} */var i2 = i1;\n", |
| 3908 |
"initializing variable\n" + |
| 3909 |
"found : I1\n" + |
| 3910 |
"required: I2"); |
| 3911 |
} |
| 3912 |
|
| 3913 |
public void testInterfaceAssignment7() throws Exception { |
| 3914 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3915 |
"/** @interface\n@extends {I1}*/var I2 = function() {};\n" + |
| 3916 |
"/** @constructor\n@implements {I2}*/var T = function() {};\n" + |
| 3917 |
"var t = new T();\n" + |
| 3918 |
"/** @type {I1} */var i1 = t;\n" + |
| 3919 |
"/** @type {I2} */var i2 = t;\n" + |
| 3920 |
"i1 = i2;\n"); |
| 3921 |
} |
| 3922 |
|
| 3923 |
public void testInterfaceAssignment8() throws Exception { |
| 3924 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3925 |
"/** @type {I} */var i;\n" + |
| 3926 |
"/** @type {Object} */var o = i;\n" + |
| 3927 |
"new Object().prototype = i.prototype;"); |
| 3928 |
} |
| 3929 |
|
| 3930 |
public void testInterfaceAssignment9() throws Exception { |
| 3931 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3932 |
"/** @return {I?} */function f() { return null; }\n" + |
| 3933 |
"/** @type {!I} */var i = f();\n", |
| 3934 |
"initializing variable\n" + |
| 3935 |
"found : (I|null)\n" + |
| 3936 |
"required: I"); |
| 3937 |
} |
| 3938 |
|
| 3939 |
public void testInterfaceAssignment10() throws Exception { |
| 3940 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3941 |
"/** @interface */var I2 = function() {};\n" + |
| 3942 |
"/** @constructor\n@implements {I2} */var T = function() {};\n" + |
| 3943 |
"/** @return {!I1|!I2} */function f() { return new T(); }\n" + |
| 3944 |
"/** @type {!I1} */var i1 = f();\n", |
| 3945 |
"initializing variable\n" + |
| 3946 |
"found : (I1|I2)\n" + |
| 3947 |
"required: I1"); |
| 3948 |
} |
| 3949 |
|
| 3950 |
public void testInterfaceAssignment11() throws Exception { |
| 3951 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 3952 |
"/** @interface */var I2 = function() {};\n" + |
| 3953 |
"/** @constructor */var T = function() {};\n" + |
| 3954 |
"/** @return {!I1|!I2|!T} */function f() { return new T(); }\n" + |
| 3955 |
"/** @type {!I1} */var i1 = f();\n", |
| 3956 |
"initializing variable\n" + |
| 3957 |
"found : (I1|I2|T)\n" + |
| 3958 |
"required: I1"); |
| 3959 |
} |
| 3960 |
|
| 3961 |
public void testInterfaceAssignment12() throws Exception { |
| 3962 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3963 |
"/** @constructor\n@implements{I}*/var T1 = function() {};\n" + |
| 3964 |
"/** @constructor\n@extends {T1}*/var T2 = function() {};\n" + |
| 3965 |
"/** @return {I} */function f() { return new T2(); }"); |
| 3966 |
} |
| 3967 |
|
| 3968 |
public void testInterfaceAssignment13() throws Exception { |
| 3969 |
testTypes("/** @interface */var I = function() {};\n" + |
| 3970 |
"/** @constructor\n@implements {I}*/var T = function() {};\n" + |
| 3971 |
"/** @constructor */function Super() {};\n" + |
| 3972 |
"/** @return {I} */Super.prototype.foo = " + |
| 3973 |
"function() { return new T(); };\n" + |
| 3974 |
"/** @constructor\n@extends {Super} */function Sub() {}\n" + |
| 3975 |
"/** @override\n@return {T} */Sub.prototype.foo = " + |
| 3976 |
"function() { return new T(); };\n"); |
| 3977 |
} |
| 3978 |
|
| 3979 |
public void testGetprop1() throws Exception { |
| 3980 |
testTypes("/** @return {void}*/function foo(){foo().bar;}", |
| 3981 |
"No properties on this expression\n" + |
| 3982 |
"found : undefined\n" + |
| 3983 |
"required: Object"); |
| 3984 |
} |
| 3985 |
|
| 3986 |
public void testGetprop2() throws Exception { |
| 3987 |
testTypes("var x = null; x.alert();", |
| 3988 |
"No properties on this expression\n" + |
| 3989 |
"found : null\n" + |
| 3990 |
"required: Object"); |
| 3991 |
} |
| 3992 |
|
| 3993 |
public void testGetprop3() throws Exception { |
| 3994 |
testTypes( |
| 3995 |
"/** @constructor */ " + |
| 3996 |
"function Foo() { /** @type {?Object} */ this.x = null; }" + |
| 3997 |
"Foo.prototype.initX = function() { this.x = {foo: 1}; };" + |
| 3998 |
"Foo.prototype.bar = function() {" + |
| 3999 |
" if (this.x == null) { this.initX(); alert(this.x.foo); }" + |
| 4000 |
"};"); |
| 4001 |
} |
| 4002 |
|
| 4003 |
public void testGetprop4() throws Exception { |
| 4004 |
testTypes("var x = null; x.prop = 3;", |
| 4005 |
"No properties on this expression\n" + |
| 4006 |
"found : null\n" + |
| 4007 |
"required: Object"); |
| 4008 |
} |
| 4009 |
|
| 4010 |
public void testSetprop1() throws Exception { |
| 4011 |
|
| 4012 |
testTypes("/**\n" + |
| 4013 |
" * @constructor\n" + |
| 4014 |
" * @struct\n" + |
| 4015 |
" */\n" + |
| 4016 |
"function Foo() { this.x = 123; }"); |
| 4017 |
} |
| 4018 |
|
| 4019 |
public void testSetprop2() throws Exception { |
| 4020 |
|
| 4021 |
testTypes("/**\n" + |
| 4022 |
" * @constructor\n" + |
| 4023 |
" * @struct\n" + |
| 4024 |
" */\n" + |
| 4025 |
"function Foo() {}\n" + |
| 4026 |
"(new Foo()).x = 123;", |
| 4027 |
"Cannot add a property to a struct instance " + |
| 4028 |
"after it is constructed."); |
| 4029 |
} |
| 4030 |
|
| 4031 |
public void testSetprop3() throws Exception { |
| 4032 |
|
| 4033 |
testTypes("/**\n" + |
| 4034 |
" * @constructor\n" + |
| 4035 |
" * @struct\n" + |
| 4036 |
" */\n" + |
| 4037 |
"function Foo() {}\n" + |
| 4038 |
"(function() { (new Foo()).x = 123; })();", |
| 4039 |
"Cannot add a property to a struct instance " + |
| 4040 |
"after it is constructed."); |
| 4041 |
} |
| 4042 |
|
| 4043 |
public void testSetprop4() throws Exception { |
| 4044 |
|
| 4045 |
testTypes("/**\n" + |
| 4046 |
" * @constructor\n" + |
| 4047 |
" * @struct\n" + |
| 4048 |
" */\n" + |
| 4049 |
"function Foo() { this.x = 123; }\n" + |
| 4050 |
"(new Foo()).x = \"asdf\";"); |
| 4051 |
} |
| 4052 |
|
| 4053 |
public void testSetprop5() throws Exception { |
| 4054 |
|
| 4055 |
testTypes("/**\n" + |
| 4056 |
" * @constructor\n" + |
| 4057 |
" * @struct\n" + |
| 4058 |
" */\n" + |
| 4059 |
"function Foo() {}\n" + |
| 4060 |
"(true ? new Foo() : {}).x = 123;", |
| 4061 |
"Cannot add a property to a struct instance " + |
| 4062 |
"after it is constructed."); |
| 4063 |
} |
| 4064 |
|
| 4065 |
public void testSetprop6() throws Exception { |
| 4066 |
|
| 4067 |
testTypes("/**\n" + |
| 4068 |
" * @constructor\n" + |
| 4069 |
" * @struct\n" + |
| 4070 |
" */\n" + |
| 4071 |
"function Foo() {}\n" + |
| 4072 |
"/**\n" + |
| 4073 |
" * @constructor\n" + |
| 4074 |
" * @param{Foo} f\n" + |
| 4075 |
" */\n" + |
| 4076 |
"function Bar(f) { f.x = 123; }", |
| 4077 |
"Cannot add a property to a struct instance " + |
| 4078 |
"after it is constructed."); |
| 4079 |
} |
| 4080 |
|
| 4081 |
public void testSetprop7() throws Exception { |
| 4082 |
|
| 4083 |
testTypes("/**\n" + |
| 4084 |
" * @constructor\n" + |
| 4085 |
" * @struct\n" + |
| 4086 |
" */\n" + |
| 4087 |
"function Foo() {\n" + |
| 4088 |
" var t = this;\n" + |
| 4089 |
" t.x = 123;\n" + |
| 4090 |
"}", |
| 4091 |
"Cannot add a property to a struct instance " + |
| 4092 |
"after it is constructed."); |
| 4093 |
} |
| 4094 |
|
| 4095 |
public void testSetprop8() throws Exception { |
| 4096 |
|
| 4097 |
testTypes("/**\n" + |
| 4098 |
" * @constructor\n" + |
| 4099 |
" * @struct\n" + |
| 4100 |
" */\n" + |
| 4101 |
"function Foo() {}\n" + |
| 4102 |
"(new Foo()).x--;", |
| 4103 |
new String[] { |
| 4104 |
"Property x never defined on Foo", |
| 4105 |
"Cannot add a property to a struct instance " + |
| 4106 |
"after it is constructed." |
| 4107 |
}); |
| 4108 |
} |
| 4109 |
|
| 4110 |
public void testSetprop9() throws Exception { |
| 4111 |
|
| 4112 |
testTypes("/**\n" + |
| 4113 |
" * @constructor\n" + |
| 4114 |
" * @struct\n" + |
| 4115 |
" */\n" + |
| 4116 |
"function Foo() {}\n" + |
| 4117 |
"(new Foo()).x += 123;", |
| 4118 |
new String[] { |
| 4119 |
"Property x never defined on Foo", |
| 4120 |
"Cannot add a property to a struct instance " + |
| 4121 |
"after it is constructed." |
| 4122 |
}); |
| 4123 |
} |
| 4124 |
|
| 4125 |
public void testSetprop10() throws Exception { |
| 4126 |
|
| 4127 |
testTypes("/** \n" + |
| 4128 |
" * @constructor \n" + |
| 4129 |
" * @struct \n" + |
| 4130 |
" */ \n" + |
| 4131 |
"function Square(side) { \n" + |
| 4132 |
" this.side = side; \n" + |
| 4133 |
"} \n" + |
| 4134 |
"Square.prototype = /** @struct */ {\n" + |
| 4135 |
" area: function() { return this.side * this.side; }\n" + |
| 4136 |
"};\n" + |
| 4137 |
"Square.prototype.id = function(x) { return x; };"); |
| 4138 |
} |
| 4139 |
|
| 4140 |
public void testSetprop11() throws Exception { |
| 4141 |
testTypes("/**\n" + |
| 4142 |
" * @constructor\n" + |
| 4143 |
" * @struct\n" + |
| 4144 |
" */\n" + |
| 4145 |
"function Foo() {}\n" + |
| 4146 |
"/** @constructor */\n" + |
| 4147 |
"function Bar() {}\n" + |
| 4148 |
"Bar.prototype = new Foo();\n" + |
| 4149 |
"Bar.prototype.someprop = 123;"); |
| 4150 |
} |
| 4151 |
|
| 4152 |
public void testSetprop12() throws Exception { |
| 4153 |
|
| 4154 |
testTypes("/**\n" + |
| 4155 |
" * @constructor\n" + |
| 4156 |
" * @struct\n" + |
| 4157 |
" */\n" + |
| 4158 |
"function Foo() {}\n" + |
| 4159 |
"Foo.someprop = 123;"); |
| 4160 |
} |
| 4161 |
|
| 4162 |
public void testGetpropDict1() throws Exception { |
| 4163 |
testTypes("/**\n" + |
| 4164 |
" * @constructor\n" + |
| 4165 |
" * @dict\n" + |
| 4166 |
" */" + |
| 4167 |
"function Dict1(){ this['prop'] = 123; }" + |
| 4168 |
"/** @param{Dict1} x */" + |
| 4169 |
"function takesDict(x) { return x.prop; }", |
| 4170 |
"Cannot do '.' access on a dict"); |
| 4171 |
} |
| 4172 |
|
| 4173 |
public void testGetpropDict2() throws Exception { |
| 4174 |
testTypes("/**\n" + |
| 4175 |
" * @constructor\n" + |
| 4176 |
" * @dict\n" + |
| 4177 |
" */" + |
| 4178 |
"function Dict1(){ this['prop'] = 123; }" + |
| 4179 |
"/**\n" + |
| 4180 |
" * @constructor\n" + |
| 4181 |
" * @extends {Dict1}\n" + |
| 4182 |
" */" + |
| 4183 |
"function Dict1kid(){ this['prop'] = 123; }" + |
| 4184 |
"/** @param{Dict1kid} x */" + |
| 4185 |
"function takesDict(x) { return x.prop; }", |
| 4186 |
"Cannot do '.' access on a dict"); |
| 4187 |
} |
| 4188 |
|
| 4189 |
public void testGetpropDict3() throws Exception { |
| 4190 |
testTypes("/**\n" + |
| 4191 |
" * @constructor\n" + |
| 4192 |
" * @dict\n" + |
| 4193 |
" */" + |
| 4194 |
"function Dict1() { this['prop'] = 123; }" + |
| 4195 |
"/** @constructor */" + |
| 4196 |
"function NonDict() { this.prop = 321; }" + |
| 4197 |
"/** @param{(NonDict|Dict1)} x */" + |
| 4198 |
"function takesDict(x) { return x.prop; }", |
| 4199 |
"Cannot do '.' access on a dict"); |
| 4200 |
} |
| 4201 |
|
| 4202 |
public void testGetpropDict4() throws Exception { |
| 4203 |
testTypes("/**\n" + |
| 4204 |
" * @constructor\n" + |
| 4205 |
" * @dict\n" + |
| 4206 |
" */" + |
| 4207 |
"function Dict1() { this['prop'] = 123; }" + |
| 4208 |
"/**\n" + |
| 4209 |
" * @constructor\n" + |
| 4210 |
" * @struct\n" + |
| 4211 |
" */" + |
| 4212 |
"function Struct1() { this.prop = 123; }" + |
| 4213 |
"/** @param{(Struct1|Dict1)} x */" + |
| 4214 |
"function takesNothing(x) { return x.prop; }", |
| 4215 |
"Cannot do '.' access on a dict"); |
| 4216 |
} |
| 4217 |
|
| 4218 |
public void testGetpropDict5() throws Exception { |
| 4219 |
testTypes("/**\n" + |
| 4220 |
" * @constructor\n" + |
| 4221 |
" * @dict\n" + |
| 4222 |
" */" + |
| 4223 |
"function Dict1(){ this.prop = 123; }", |
| 4224 |
"Cannot do '.' access on a dict"); |
| 4225 |
} |
| 4226 |
|
| 4227 |
public void testGetpropDict6() throws Exception { |
| 4228 |
testTypes("/**\n" + |
| 4229 |
" * @constructor\n" + |
| 4230 |
" * @dict\n" + |
| 4231 |
" */\n" + |
| 4232 |
"function Foo() {}\n" + |
| 4233 |
"function Bar() {}\n" + |
| 4234 |
"Bar.prototype = new Foo();\n" + |
| 4235 |
"Bar.prototype.someprop = 123;\n", |
| 4236 |
"Cannot do '.' access on a dict"); |
| 4237 |
} |
| 4238 |
|
| 4239 |
public void testGetpropDict7() throws Exception { |
| 4240 |
testTypes("(/** @dict */ {'x': 123}).x = 321;", |
| 4241 |
"Cannot do '.' access on a dict"); |
| 4242 |
} |
| 4243 |
|
| 4244 |
public void testGetelemStruct1() throws Exception { |
| 4245 |
testTypes("/**\n" + |
| 4246 |
" * @constructor\n" + |
| 4247 |
" * @struct\n" + |
| 4248 |
" */" + |
| 4249 |
"function Struct1(){ this.prop = 123; }" + |
| 4250 |
"/** @param{Struct1} x */" + |
| 4251 |
"function takesStruct(x) {" + |
| 4252 |
" var z = x;" + |
| 4253 |
" return z['prop'];" + |
| 4254 |
"}", |
| 4255 |
"Cannot do '[]' access on a struct"); |
| 4256 |
} |
| 4257 |
|
| 4258 |
public void testGetelemStruct2() throws Exception { |
| 4259 |
testTypes("/**\n" + |
| 4260 |
" * @constructor\n" + |
| 4261 |
" * @struct\n" + |
| 4262 |
" */" + |
| 4263 |
"function Struct1(){ this.prop = 123; }" + |
| 4264 |
"/**\n" + |
| 4265 |
" * @constructor\n" + |
| 4266 |
" * @extends {Struct1}" + |
| 4267 |
" */" + |
| 4268 |
"function Struct1kid(){ this.prop = 123; }" + |
| 4269 |
"/** @param{Struct1kid} x */" + |
| 4270 |
"function takesStruct2(x) { return x['prop']; }", |
| 4271 |
"Cannot do '[]' access on a struct"); |
| 4272 |
} |
| 4273 |
|
| 4274 |
public void testGetelemStruct3() throws Exception { |
| 4275 |
testTypes("/**\n" + |
| 4276 |
" * @constructor\n" + |
| 4277 |
" * @struct\n" + |
| 4278 |
" */" + |
| 4279 |
"function Struct1(){ this.prop = 123; }" + |
| 4280 |
"/**\n" + |
| 4281 |
" * @constructor\n" + |
| 4282 |
" * @extends {Struct1}\n" + |
| 4283 |
" */" + |
| 4284 |
"function Struct1kid(){ this.prop = 123; }" + |
| 4285 |
"var x = (new Struct1kid())['prop'];", |
| 4286 |
"Cannot do '[]' access on a struct"); |
| 4287 |
} |
| 4288 |
|
| 4289 |
public void testGetelemStruct4() throws Exception { |
| 4290 |
testTypes("/**\n" + |
| 4291 |
" * @constructor\n" + |
| 4292 |
" * @struct\n" + |
| 4293 |
" */" + |
| 4294 |
"function Struct1() { this.prop = 123; }" + |
| 4295 |
"/** @constructor */" + |
| 4296 |
"function NonStruct() { this.prop = 321; }" + |
| 4297 |
"/** @param{(NonStruct|Struct1)} x */" + |
| 4298 |
"function takesStruct(x) { return x['prop']; }", |
| 4299 |
"Cannot do '[]' access on a struct"); |
| 4300 |
} |
| 4301 |
|
| 4302 |
public void testGetelemStruct5() throws Exception { |
| 4303 |
testTypes("/**\n" + |
| 4304 |
" * @constructor\n" + |
| 4305 |
" * @struct\n" + |
| 4306 |
" */" + |
| 4307 |
"function Struct1() { this.prop = 123; }" + |
| 4308 |
"/**\n" + |
| 4309 |
" * @constructor\n" + |
| 4310 |
" * @dict\n" + |
| 4311 |
" */" + |
| 4312 |
"function Dict1() { this['prop'] = 123; }" + |
| 4313 |
"/** @param{(Struct1|Dict1)} x */" + |
| 4314 |
"function takesNothing(x) { return x['prop']; }", |
| 4315 |
"Cannot do '[]' access on a struct"); |
| 4316 |
} |
| 4317 |
|
| 4318 |
public void testGetelemStruct6() throws Exception { |
| 4319 |
|
| 4320 |
testTypes("/** @interface */ function Foo(){}\n" + |
| 4321 |
"/**\n" + |
| 4322 |
" * @constructor\n" + |
| 4323 |
" * @struct\n" + |
| 4324 |
" * @implements {Foo}\n" + |
| 4325 |
" */" + |
| 4326 |
"function Bar(){ this.x = 123; }\n" + |
| 4327 |
"var z = /** @type {Foo} */(new Bar)['x'];"); |
| 4328 |
} |
| 4329 |
|
| 4330 |
public void testGetelemStruct7() throws Exception { |
| 4331 |
testTypes("/**\n" + |
| 4332 |
" * @constructor\n" + |
| 4333 |
" * @struct\n" + |
| 4334 |
" */\n" + |
| 4335 |
"function Foo() {}\n" + |
| 4336 |
"/** @constructor */\n" + |
| 4337 |
"function Bar() {}\n" + |
| 4338 |
"Bar.prototype = new Foo();\n" + |
| 4339 |
"Bar.prototype['someprop'] = 123;\n", |
| 4340 |
"Cannot do '[]' access on a struct"); |
| 4341 |
} |
| 4342 |
|
| 4343 |
public void testInOnStruct() throws Exception { |
| 4344 |
testTypes("/**\n" + |
| 4345 |
" * @constructor\n" + |
| 4346 |
" * @struct\n" + |
| 4347 |
" */" + |
| 4348 |
"function Foo() {}\n" + |
| 4349 |
"if ('prop' in (new Foo())) {}", |
| 4350 |
"Cannot use the IN operator with structs"); |
| 4351 |
} |
| 4352 |
|
| 4353 |
public void testForinOnStruct() throws Exception { |
| 4354 |
testTypes("/**\n" + |
| 4355 |
" * @constructor\n" + |
| 4356 |
" * @struct\n" + |
| 4357 |
" */" + |
| 4358 |
"function Foo() {}\n" + |
| 4359 |
"for (var prop in (new Foo())) {}", |
| 4360 |
"Cannot use the IN operator with structs"); |
| 4361 |
} |
| 4362 |
|
| 4363 |
public void testArrayAccess1() throws Exception { |
| 4364 |
testTypes("var a = []; var b = a['hi'];"); |
| 4365 |
} |
| 4366 |
|
| 4367 |
public void testArrayAccess2() throws Exception { |
| 4368 |
testTypes("var a = []; var b = a[[1,2]];", |
| 4369 |
"array access\n" + |
| 4370 |
"found : Array\n" + |
| 4371 |
"required: number"); |
| 4372 |
} |
| 4373 |
|
| 4374 |
public void testArrayAccess3() throws Exception { |
| 4375 |
testTypes("var bar = [];" + |
| 4376 |
"/** @return {void} */function baz(){};" + |
| 4377 |
"var foo = bar[baz()];", |
| 4378 |
"array access\n" + |
| 4379 |
"found : undefined\n" + |
| 4380 |
"required: number"); |
| 4381 |
} |
| 4382 |
|
| 4383 |
public void testArrayAccess4() throws Exception { |
| 4384 |
testTypes("/**@return {!Array}*/function foo(){};var bar = foo()[foo()];", |
| 4385 |
"array access\n" + |
| 4386 |
"found : Array\n" + |
| 4387 |
"required: number"); |
| 4388 |
} |
| 4389 |
|
| 4390 |
public void testArrayAccess6() throws Exception { |
| 4391 |
testTypes("var bar = null[1];", |
| 4392 |
"only arrays or objects can be accessed\n" + |
| 4393 |
"found : null\n" + |
| 4394 |
"required: Object"); |
| 4395 |
} |
| 4396 |
|
| 4397 |
public void testArrayAccess7() throws Exception { |
| 4398 |
testTypes("var bar = void 0; bar[0];", |
| 4399 |
"only arrays or objects can be accessed\n" + |
| 4400 |
"found : undefined\n" + |
| 4401 |
"required: Object"); |
| 4402 |
} |
| 4403 |
|
| 4404 |
public void testArrayAccess8() throws Exception { |
| 4405 |
|
| 4406 |
|
| 4407 |
testTypes("var bar = void 0; bar[0]; bar[1];", |
| 4408 |
"only arrays or objects can be accessed\n" + |
| 4409 |
"found : undefined\n" + |
| 4410 |
"required: Object"); |
| 4411 |
} |
| 4412 |
|
| 4413 |
public void testArrayAccess9() throws Exception { |
| 4414 |
testTypes("/** @return {?Array} */ function f() { return []; }" + |
| 4415 |
"f()[{}]", |
| 4416 |
"array access\n" + |
| 4417 |
"found : {}\n" + |
| 4418 |
"required: number"); |
| 4419 |
} |
| 4420 |
|
| 4421 |
public void testPropAccess() throws Exception { |
| 4422 |
testTypes("/** @param {*} x */var f = function(x) {\n" + |
| 4423 |
"var o = String(x);\n" + |
| 4424 |
"if (typeof o['a'] != 'undefined') { return o['a']; }\n" + |
| 4425 |
"return null;\n" + |
| 4426 |
"};"); |
| 4427 |
} |
| 4428 |
|
| 4429 |
public void testPropAccess2() throws Exception { |
| 4430 |
testTypes("var bar = void 0; bar.baz;", |
| 4431 |
"No properties on this expression\n" + |
| 4432 |
"found : undefined\n" + |
| 4433 |
"required: Object"); |
| 4434 |
} |
| 4435 |
|
| 4436 |
public void testPropAccess3() throws Exception { |
| 4437 |
|
| 4438 |
|
| 4439 |
testTypes("var bar = void 0; bar.baz; bar.bax;", |
| 4440 |
"No properties on this expression\n" + |
| 4441 |
"found : undefined\n" + |
| 4442 |
"required: Object"); |
| 4443 |
} |
| 4444 |
|
| 4445 |
public void testPropAccess4() throws Exception { |
| 4446 |
testTypes("/** @param {*} x */ function f(x) { return x['hi']; }"); |
| 4447 |
} |
| 4448 |
|
| 4449 |
public void testSwitchCase1() throws Exception { |
| 4450 |
testTypes("/**@type number*/var a;" + |
| 4451 |
"/**@type string*/var b;" + |
| 4452 |
"switch(a){case b:;}", |
| 4453 |
"case expression doesn't match switch\n" + |
| 4454 |
"found : string\n" + |
| 4455 |
"required: number"); |
| 4456 |
} |
| 4457 |
|
| 4458 |
public void testSwitchCase2() throws Exception { |
| 4459 |
testTypes("var a = null; switch (typeof a) { case 'foo': }"); |
| 4460 |
} |
| 4461 |
|
| 4462 |
public void testVar1() throws Exception { |
| 4463 |
TypeCheckResult p = |
| 4464 |
parseAndTypeCheckWithScope("/** @type {(string,null)} */var a = null"); |
| 4465 |
|
| 4466 |
assertTypeEquals(createUnionType(STRING_TYPE, NULL_TYPE), |
| 4467 |
p.scope.getVar("a").getType()); |
| 4468 |
} |
| 4469 |
|
| 4470 |
public void testVar2() throws Exception { |
| 4471 |
testTypes("/** @type {Function} */ var a = function(){}"); |
| 4472 |
} |
| 4473 |
|
| 4474 |
public void testVar3() throws Exception { |
| 4475 |
TypeCheckResult p = parseAndTypeCheckWithScope("var a = 3;"); |
| 4476 |
|
| 4477 |
assertTypeEquals(NUMBER_TYPE, p.scope.getVar("a").getType()); |
| 4478 |
} |
| 4479 |
|
| 4480 |
public void testVar4() throws Exception { |
| 4481 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 4482 |
"var a = 3; a = 'string';"); |
| 4483 |
|
| 4484 |
assertTypeEquals(createUnionType(STRING_TYPE, NUMBER_TYPE), |
| 4485 |
p.scope.getVar("a").getType()); |
| 4486 |
} |
| 4487 |
|
| 4488 |
public void testVar5() throws Exception { |
| 4489 |
testTypes("var goog = {};" + |
| 4490 |
"/** @type string */goog.foo = 'hello';" + |
| 4491 |
"/** @type number */var a = goog.foo;", |
| 4492 |
"initializing variable\n" + |
| 4493 |
"found : string\n" + |
| 4494 |
"required: number"); |
| 4495 |
} |
| 4496 |
|
| 4497 |
public void testVar6() throws Exception { |
| 4498 |
testTypes( |
| 4499 |
"function f() {" + |
| 4500 |
" return function() {" + |
| 4501 |
" /** @type {!Date} */" + |
| 4502 |
" var a = 7;" + |
| 4503 |
" };" + |
| 4504 |
"}", |
| 4505 |
"initializing variable\n" + |
| 4506 |
"found : number\n" + |
| 4507 |
"required: Date"); |
| 4508 |
} |
| 4509 |
|
| 4510 |
public void testVar7() throws Exception { |
| 4511 |
testTypes("/** @type number */var a, b;", |
| 4512 |
"declaration of multiple variables with shared type information"); |
| 4513 |
} |
| 4514 |
|
| 4515 |
public void testVar8() throws Exception { |
| 4516 |
testTypes("var a, b;"); |
| 4517 |
} |
| 4518 |
|
| 4519 |
public void testVar9() throws Exception { |
| 4520 |
testTypes("/** @enum */var a;", |
| 4521 |
"enum initializer must be an object literal or an enum"); |
| 4522 |
} |
| 4523 |
|
| 4524 |
public void testVar10() throws Exception { |
| 4525 |
testTypes("/** @type !Number */var foo = 'abc';", |
| 4526 |
"initializing variable\n" + |
| 4527 |
"found : string\n" + |
| 4528 |
"required: Number"); |
| 4529 |
} |
| 4530 |
|
| 4531 |
public void testVar11() throws Exception { |
| 4532 |
testTypes("var /** @type !Date */foo = 'abc';", |
| 4533 |
"initializing variable\n" + |
| 4534 |
"found : string\n" + |
| 4535 |
"required: Date"); |
| 4536 |
} |
| 4537 |
|
| 4538 |
public void testVar12() throws Exception { |
| 4539 |
testTypes("var /** @type !Date */foo = 'abc', " + |
| 4540 |
"/** @type !RegExp */bar = 5;", |
| 4541 |
new String[] { |
| 4542 |
"initializing variable\n" + |
| 4543 |
"found : string\n" + |
| 4544 |
"required: Date", |
| 4545 |
"initializing variable\n" + |
| 4546 |
"found : number\n" + |
| 4547 |
"required: RegExp"}); |
| 4548 |
} |
| 4549 |
|
| 4550 |
public void testVar13() throws Exception { |
| 4551 |
|
| 4552 |
testTypes("var /** @type number */a,a;"); |
| 4553 |
} |
| 4554 |
|
| 4555 |
public void testVar14() throws Exception { |
| 4556 |
testTypes("/** @return {number} */ function f() { var x; return x; }", |
| 4557 |
"inconsistent return type\n" + |
| 4558 |
"found : undefined\n" + |
| 4559 |
"required: number"); |
| 4560 |
} |
| 4561 |
|
| 4562 |
public void testVar15() throws Exception { |
| 4563 |
testTypes("/** @return {number} */" + |
| 4564 |
"function f() { var x = x || {}; return x; }", |
| 4565 |
"inconsistent return type\n" + |
| 4566 |
"found : {}\n" + |
| 4567 |
"required: number"); |
| 4568 |
} |
| 4569 |
|
| 4570 |
public void testAssign1() throws Exception { |
| 4571 |
testTypes("var goog = {};" + |
| 4572 |
"/** @type number */goog.foo = 'hello';", |
| 4573 |
"assignment to property foo of goog\n" + |
| 4574 |
"found : string\n" + |
| 4575 |
"required: number"); |
| 4576 |
} |
| 4577 |
|
| 4578 |
public void testAssign2() throws Exception { |
| 4579 |
testTypes("var goog = {};" + |
| 4580 |
"/** @type number */goog.foo = 3;" + |
| 4581 |
"goog.foo = 'hello';", |
| 4582 |
"assignment to property foo of goog\n" + |
| 4583 |
"found : string\n" + |
| 4584 |
"required: number"); |
| 4585 |
} |
| 4586 |
|
| 4587 |
public void testAssign3() throws Exception { |
| 4588 |
testTypes("var goog = {};" + |
| 4589 |
"/** @type number */goog.foo = 3;" + |
| 4590 |
"goog.foo = 4;"); |
| 4591 |
} |
| 4592 |
|
| 4593 |
public void testAssign4() throws Exception { |
| 4594 |
testTypes("var goog = {};" + |
| 4595 |
"goog.foo = 3;" + |
| 4596 |
"goog.foo = 'hello';"); |
| 4597 |
} |
| 4598 |
|
| 4599 |
public void testAssignInference() throws Exception { |
| 4600 |
testTypes( |
| 4601 |
"/**" + |
| 4602 |
" * @param {Array} x" + |
| 4603 |
" * @return {number}" + |
| 4604 |
" */" + |
| 4605 |
"function f(x) {" + |
| 4606 |
" var y = null;" + |
| 4607 |
" y = x[0];" + |
| 4608 |
" if (y == null) { return 4; } else { return 6; }" + |
| 4609 |
"}"); |
| 4610 |
} |
| 4611 |
|
| 4612 |
public void testOr1() throws Exception { |
| 4613 |
testTypes("/** @type number */var a;" + |
| 4614 |
"/** @type number */var b;" + |
| 4615 |
"a + b || undefined;"); |
| 4616 |
} |
| 4617 |
|
| 4618 |
public void testOr2() throws Exception { |
| 4619 |
testTypes("/** @type number */var a;" + |
| 4620 |
"/** @type number */var b;" + |
| 4621 |
"/** @type number */var c = a + b || undefined;", |
| 4622 |
"initializing variable\n" + |
| 4623 |
"found : (number|undefined)\n" + |
| 4624 |
"required: number"); |
| 4625 |
} |
| 4626 |
|
| 4627 |
public void testOr3() throws Exception { |
| 4628 |
testTypes("/** @type {(number, undefined)} */var a;" + |
| 4629 |
"/** @type number */var c = a || 3;"); |
| 4630 |
} |
| 4631 |
|
| 4632 |
|
| 4633 |
|
| 4634 |
|
| 4635 |
|
| 4636 |
|
| 4637 |
public void testOr4() throws Exception { |
| 4638 |
testTypes("/**@type {number} */var x;x=null || \"a\";", |
| 4639 |
"assignment\n" + |
| 4640 |
"found : string\n" + |
| 4641 |
"required: number"); |
| 4642 |
} |
| 4643 |
|
| 4644 |
|
| 4645 |
|
| 4646 |
|
| 4647 |
public void testOr5() throws Exception { |
| 4648 |
testTypes("/**@type {number} */var x;x=undefined || \"a\";", |
| 4649 |
"assignment\n" + |
| 4650 |
"found : string\n" + |
| 4651 |
"required: number"); |
| 4652 |
} |
| 4653 |
|
| 4654 |
public void testAnd1() throws Exception { |
| 4655 |
testTypes("/** @type number */var a;" + |
| 4656 |
"/** @type number */var b;" + |
| 4657 |
"a + b && undefined;"); |
| 4658 |
} |
| 4659 |
|
| 4660 |
public void testAnd2() throws Exception { |
| 4661 |
testTypes("/** @type number */var a;" + |
| 4662 |
"/** @type number */var b;" + |
| 4663 |
"/** @type number */var c = a + b && undefined;", |
| 4664 |
"initializing variable\n" + |
| 4665 |
"found : (number|undefined)\n" + |
| 4666 |
"required: number"); |
| 4667 |
} |
| 4668 |
|
| 4669 |
public void testAnd3() throws Exception { |
| 4670 |
testTypes("/** @type {(!Array, undefined)} */var a;" + |
| 4671 |
"/** @type number */var c = a && undefined;", |
| 4672 |
"initializing variable\n" + |
| 4673 |
"found : undefined\n" + |
| 4674 |
"required: number"); |
| 4675 |
} |
| 4676 |
|
| 4677 |
public void testAnd4() throws Exception { |
| 4678 |
testTypes("/** @param {number} x */function f(x){};\n" + |
| 4679 |
"/** @type null */var x; /** @type {number?} */var y;\n" + |
| 4680 |
"if (x && y) { f(y) }"); |
| 4681 |
} |
| 4682 |
|
| 4683 |
public void testAnd5() throws Exception { |
| 4684 |
testTypes("/** @param {number} x\n@param {string} y*/function f(x,y){};\n" + |
| 4685 |
"/** @type {number?} */var x; /** @type {string?} */var y;\n" + |
| 4686 |
"if (x && y) { f(x, y) }"); |
| 4687 |
} |
| 4688 |
|
| 4689 |
public void testAnd6() throws Exception { |
| 4690 |
testTypes("/** @param {number} x */function f(x){};\n" + |
| 4691 |
"/** @type {number|undefined} */var x;\n" + |
| 4692 |
"if (x && f(x)) { f(x) }"); |
| 4693 |
} |
| 4694 |
|
| 4695 |
public void testAnd7() throws Exception { |
| 4696 |
|
| 4697 |
|
| 4698 |
|
| 4699 |
|
| 4700 |
testTypes("/** @type null */var x; if (x && x) {}"); |
| 4701 |
} |
| 4702 |
|
| 4703 |
public void testHook() throws Exception { |
| 4704 |
testTypes("/**@return {void}*/function foo(){ var x=foo()?a:b; }"); |
| 4705 |
} |
| 4706 |
|
| 4707 |
public void testHookRestrictsType1() throws Exception { |
| 4708 |
testTypes("/** @return {(string,null)} */" + |
| 4709 |
"function f() { return null;}" + |
| 4710 |
"/** @type {(string,null)} */ var a = f();" + |
| 4711 |
"/** @type string */" + |
| 4712 |
"var b = a ? a : 'default';"); |
| 4713 |
} |
| 4714 |
|
| 4715 |
public void testHookRestrictsType2() throws Exception { |
| 4716 |
testTypes("/** @type {String} */" + |
| 4717 |
"var a = null;" + |
| 4718 |
"/** @type null */" + |
| 4719 |
"var b = a ? null : a;"); |
| 4720 |
} |
| 4721 |
|
| 4722 |
public void testHookRestrictsType3() throws Exception { |
| 4723 |
testTypes("/** @type {String} */" + |
| 4724 |
"var a;" + |
| 4725 |
"/** @type null */" + |
| 4726 |
"var b = (!a) ? a : null;"); |
| 4727 |
} |
| 4728 |
|
| 4729 |
public void testHookRestrictsType4() throws Exception { |
| 4730 |
testTypes("/** @type {(boolean,undefined)} */" + |
| 4731 |
"var a;" + |
| 4732 |
"/** @type boolean */" + |
| 4733 |
"var b = a != null ? a : true;"); |
| 4734 |
} |
| 4735 |
|
| 4736 |
public void testHookRestrictsType5() throws Exception { |
| 4737 |
testTypes("/** @type {(boolean,undefined)} */" + |
| 4738 |
"var a;" + |
| 4739 |
"/** @type {(undefined)} */" + |
| 4740 |
"var b = a == null ? a : undefined;"); |
| 4741 |
} |
| 4742 |
|
| 4743 |
public void testHookRestrictsType6() throws Exception { |
| 4744 |
testTypes("/** @type {(number,null,undefined)} */" + |
| 4745 |
"var a;" + |
| 4746 |
"/** @type {number} */" + |
| 4747 |
"var b = a == null ? 5 : a;"); |
| 4748 |
} |
| 4749 |
|
| 4750 |
public void testHookRestrictsType7() throws Exception { |
| 4751 |
testTypes("/** @type {(number,null,undefined)} */" + |
| 4752 |
"var a;" + |
| 4753 |
"/** @type {number} */" + |
| 4754 |
"var b = a == undefined ? 5 : a;"); |
| 4755 |
} |
| 4756 |
|
| 4757 |
public void testWhileRestrictsType1() throws Exception { |
| 4758 |
testTypes("/** @param {null} x */ function g(x) {}" + |
| 4759 |
"/** @param {number?} x */\n" + |
| 4760 |
"function f(x) {\n" + |
| 4761 |
"while (x) {\n" + |
| 4762 |
"if (g(x)) { x = 1; }\n" + |
| 4763 |
"x = x-1;\n}\n}", |
| 4764 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 4765 |
"found : number\n" + |
| 4766 |
"required: null"); |
| 4767 |
} |
| 4768 |
|
| 4769 |
public void testWhileRestrictsType2() throws Exception { |
| 4770 |
testTypes("/** @param {number?} x\n@return {number}*/\n" + |
| 4771 |
"function f(x) {\n/** @type {number} */var y = 0;" + |
| 4772 |
"while (x) {\n" + |
| 4773 |
"y = x;\n" + |
| 4774 |
"x = x-1;\n}\n" + |
| 4775 |
"return y;}"); |
| 4776 |
} |
| 4777 |
|
| 4778 |
public void testHigherOrderFunctions1() throws Exception { |
| 4779 |
testTypes( |
| 4780 |
"/** @type {function(number)} */var f;" + |
| 4781 |
"f(true);", |
| 4782 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 4783 |
"found : boolean\n" + |
| 4784 |
"required: number"); |
| 4785 |
} |
| 4786 |
|
| 4787 |
public void testHigherOrderFunctions2() throws Exception { |
| 4788 |
testTypes( |
| 4789 |
"/** @type {function():!Date} */var f;" + |
| 4790 |
"/** @type boolean */var a = f();", |
| 4791 |
"initializing variable\n" + |
| 4792 |
"found : Date\n" + |
| 4793 |
"required: boolean"); |
| 4794 |
} |
| 4795 |
|
| 4796 |
public void testHigherOrderFunctions3() throws Exception { |
| 4797 |
testTypes( |
| 4798 |
"/** @type {function(this:Error):Date} */var f; new f", |
| 4799 |
"cannot instantiate non-constructor"); |
| 4800 |
} |
| 4801 |
|
| 4802 |
public void testHigherOrderFunctions4() throws Exception { |
| 4803 |
testTypes( |
| 4804 |
"/** @type {function(this:Error,...[number]):Date} */var f; new f", |
| 4805 |
"cannot instantiate non-constructor"); |
| 4806 |
} |
| 4807 |
|
| 4808 |
public void testHigherOrderFunctions5() throws Exception { |
| 4809 |
testTypes( |
| 4810 |
"/** @param {number} x */ function g(x) {}" + |
| 4811 |
"/** @type {function(new:Error,...[number]):Date} */ var f;" + |
| 4812 |
"g(new f());", |
| 4813 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 4814 |
"found : Error\n" + |
| 4815 |
"required: number"); |
| 4816 |
} |
| 4817 |
|
| 4818 |
public void testConstructorAlias1() throws Exception { |
| 4819 |
testTypes( |
| 4820 |
"/** @constructor */ var Foo = function() {};" + |
| 4821 |
"/** @type {number} */ Foo.prototype.bar = 3;" + |
| 4822 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4823 |
"/** @return {string} */ function foo() { " + |
| 4824 |
" return (new FooAlias()).bar; }", |
| 4825 |
"inconsistent return type\n" + |
| 4826 |
"found : number\n" + |
| 4827 |
"required: string"); |
| 4828 |
} |
| 4829 |
|
| 4830 |
public void testConstructorAlias2() throws Exception { |
| 4831 |
testTypes( |
| 4832 |
"/** @constructor */ var Foo = function() {};" + |
| 4833 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4834 |
"/** @type {number} */ FooAlias.prototype.bar = 3;" + |
| 4835 |
"/** @return {string} */ function foo() { " + |
| 4836 |
" return (new Foo()).bar; }", |
| 4837 |
"inconsistent return type\n" + |
| 4838 |
"found : number\n" + |
| 4839 |
"required: string"); |
| 4840 |
} |
| 4841 |
|
| 4842 |
public void testConstructorAlias3() throws Exception { |
| 4843 |
testTypes( |
| 4844 |
"/** @constructor */ var Foo = function() {};" + |
| 4845 |
"/** @type {number} */ Foo.prototype.bar = 3;" + |
| 4846 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4847 |
"/** @return {string} */ function foo() { " + |
| 4848 |
" return (new FooAlias()).bar; }", |
| 4849 |
"inconsistent return type\n" + |
| 4850 |
"found : number\n" + |
| 4851 |
"required: string"); |
| 4852 |
} |
| 4853 |
|
| 4854 |
public void testConstructorAlias4() throws Exception { |
| 4855 |
testTypes( |
| 4856 |
"/** @constructor */ var Foo = function() {};" + |
| 4857 |
"var FooAlias = Foo;" + |
| 4858 |
"/** @type {number} */ FooAlias.prototype.bar = 3;" + |
| 4859 |
"/** @return {string} */ function foo() { " + |
| 4860 |
" return (new Foo()).bar; }", |
| 4861 |
"inconsistent return type\n" + |
| 4862 |
"found : number\n" + |
| 4863 |
"required: string"); |
| 4864 |
} |
| 4865 |
|
| 4866 |
public void testConstructorAlias5() throws Exception { |
| 4867 |
testTypes( |
| 4868 |
"/** @constructor */ var Foo = function() {};" + |
| 4869 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4870 |
"/** @return {FooAlias} */ function foo() { " + |
| 4871 |
" return new Foo(); }"); |
| 4872 |
} |
| 4873 |
|
| 4874 |
public void testConstructorAlias6() throws Exception { |
| 4875 |
testTypes( |
| 4876 |
"/** @constructor */ var Foo = function() {};" + |
| 4877 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4878 |
"/** @return {Foo} */ function foo() { " + |
| 4879 |
" return new FooAlias(); }"); |
| 4880 |
} |
| 4881 |
|
| 4882 |
public void testConstructorAlias7() throws Exception { |
| 4883 |
testTypes( |
| 4884 |
"var goog = {};" + |
| 4885 |
"/** @constructor */ goog.Foo = function() {};" + |
| 4886 |
"/** @constructor */ goog.FooAlias = goog.Foo;" + |
| 4887 |
"/** @return {number} */ function foo() { " + |
| 4888 |
" return new goog.FooAlias(); }", |
| 4889 |
"inconsistent return type\n" + |
| 4890 |
"found : goog.Foo\n" + |
| 4891 |
"required: number"); |
| 4892 |
} |
| 4893 |
|
| 4894 |
public void testConstructorAlias8() throws Exception { |
| 4895 |
testTypes( |
| 4896 |
"var goog = {};" + |
| 4897 |
"/**\n * @param {number} x \n * @constructor */ " + |
| 4898 |
"goog.Foo = function(x) {};" + |
| 4899 |
"/**\n * @param {number} x \n * @constructor */ " + |
| 4900 |
"goog.FooAlias = goog.Foo;" + |
| 4901 |
"/** @return {number} */ function foo() { " + |
| 4902 |
" return new goog.FooAlias(1); }", |
| 4903 |
"inconsistent return type\n" + |
| 4904 |
"found : goog.Foo\n" + |
| 4905 |
"required: number"); |
| 4906 |
} |
| 4907 |
|
| 4908 |
public void testConstructorAlias9() throws Exception { |
| 4909 |
testTypes( |
| 4910 |
"var goog = {};" + |
| 4911 |
"/**\n * @param {number} x \n * @constructor */ " + |
| 4912 |
"goog.Foo = function(x) {};" + |
| 4913 |
"/** @constructor */ goog.FooAlias = goog.Foo;" + |
| 4914 |
"/** @return {number} */ function foo() { " + |
| 4915 |
" return new goog.FooAlias(1); }", |
| 4916 |
"inconsistent return type\n" + |
| 4917 |
"found : goog.Foo\n" + |
| 4918 |
"required: number"); |
| 4919 |
} |
| 4920 |
|
| 4921 |
public void testConstructorAlias10() throws Exception { |
| 4922 |
testTypes( |
| 4923 |
"/**\n * @param {number} x \n * @constructor */ " + |
| 4924 |
"var Foo = function(x) {};" + |
| 4925 |
"/** @constructor */ var FooAlias = Foo;" + |
| 4926 |
"/** @return {number} */ function foo() { " + |
| 4927 |
" return new FooAlias(1); }", |
| 4928 |
"inconsistent return type\n" + |
| 4929 |
"found : Foo\n" + |
| 4930 |
"required: number"); |
| 4931 |
} |
| 4932 |
|
| 4933 |
public void testClosure1() throws Exception { |
| 4934 |
testClosureTypes( |
| 4935 |
CLOSURE_DEFS + |
| 4936 |
"/** @type {string|undefined} */var a;" + |
| 4937 |
"/** @type string */" + |
| 4938 |
"var b = goog.isDef(a) ? a : 'default';", |
| 4939 |
null); |
| 4940 |
} |
| 4941 |
|
| 4942 |
public void testClosure2() throws Exception { |
| 4943 |
testClosureTypes( |
| 4944 |
CLOSURE_DEFS + |
| 4945 |
"/** @type {string?} */var a;" + |
| 4946 |
"/** @type string */" + |
| 4947 |
"var b = goog.isNull(a) ? 'default' : a;", |
| 4948 |
null); |
| 4949 |
} |
| 4950 |
|
| 4951 |
public void testClosure3() throws Exception { |
| 4952 |
testClosureTypes( |
| 4953 |
CLOSURE_DEFS + |
| 4954 |
"/** @type {string|null|undefined} */var a;" + |
| 4955 |
"/** @type string */" + |
| 4956 |
"var b = goog.isDefAndNotNull(a) ? a : 'default';", |
| 4957 |
null); |
| 4958 |
} |
| 4959 |
|
| 4960 |
public void testClosure4() throws Exception { |
| 4961 |
testClosureTypes( |
| 4962 |
CLOSURE_DEFS + |
| 4963 |
"/** @type {string|undefined} */var a;" + |
| 4964 |
"/** @type string */" + |
| 4965 |
"var b = !goog.isDef(a) ? 'default' : a;", |
| 4966 |
null); |
| 4967 |
} |
| 4968 |
|
| 4969 |
public void testClosure5() throws Exception { |
| 4970 |
testClosureTypes( |
| 4971 |
CLOSURE_DEFS + |
| 4972 |
"/** @type {string?} */var a;" + |
| 4973 |
"/** @type string */" + |
| 4974 |
"var b = !goog.isNull(a) ? a : 'default';", |
| 4975 |
null); |
| 4976 |
} |
| 4977 |
|
| 4978 |
public void testClosure6() throws Exception { |
| 4979 |
testClosureTypes( |
| 4980 |
CLOSURE_DEFS + |
| 4981 |
"/** @type {string|null|undefined} */var a;" + |
| 4982 |
"/** @type string */" + |
| 4983 |
"var b = !goog.isDefAndNotNull(a) ? 'default' : a;", |
| 4984 |
null); |
| 4985 |
} |
| 4986 |
|
| 4987 |
public void testClosure7() throws Exception { |
| 4988 |
testClosureTypes( |
| 4989 |
CLOSURE_DEFS + |
| 4990 |
"/** @type {string|null|undefined} */ var a = foo();" + |
| 4991 |
"/** @type {number} */" + |
| 4992 |
"var b = goog.asserts.assert(a);", |
| 4993 |
"initializing variable\n" + |
| 4994 |
"found : string\n" + |
| 4995 |
"required: number"); |
| 4996 |
} |
| 4997 |
|
| 4998 |
public void testReturn1() throws Exception { |
| 4999 |
testTypes("/**@return {void}*/function foo(){ return 3; }", |
| 5000 |
"inconsistent return type\n" + |
| 5001 |
"found : number\n" + |
| 5002 |
"required: undefined"); |
| 5003 |
} |
| 5004 |
|
| 5005 |
public void testReturn2() throws Exception { |
| 5006 |
testTypes("/**@return {!Number}*/function foo(){ return; }", |
| 5007 |
"inconsistent return type\n" + |
| 5008 |
"found : undefined\n" + |
| 5009 |
"required: Number"); |
| 5010 |
} |
| 5011 |
|
| 5012 |
public void testReturn3() throws Exception { |
| 5013 |
testTypes("/**@return {!Number}*/function foo(){ return 'abc'; }", |
| 5014 |
"inconsistent return type\n" + |
| 5015 |
"found : string\n" + |
| 5016 |
"required: Number"); |
| 5017 |
} |
| 5018 |
|
| 5019 |
public void testReturn4() throws Exception { |
| 5020 |
testTypes("/**@return {!Number}\n*/\n function a(){return new Array();}", |
| 5021 |
"inconsistent return type\n" + |
| 5022 |
"found : Array\n" + |
| 5023 |
"required: Number"); |
| 5024 |
} |
| 5025 |
|
| 5026 |
public void testReturn5() throws Exception { |
| 5027 |
testTypes("/** @param {number} n\n" + |
| 5028 |
"@constructor */function n(n){return};"); |
| 5029 |
} |
| 5030 |
|
| 5031 |
public void testReturn6() throws Exception { |
| 5032 |
testTypes( |
| 5033 |
"/** @param {number} opt_a\n@return {string} */" + |
| 5034 |
"function a(opt_a) { return opt_a }", |
| 5035 |
"inconsistent return type\n" + |
| 5036 |
"found : (number|undefined)\n" + |
| 5037 |
"required: string"); |
| 5038 |
} |
| 5039 |
|
| 5040 |
public void testReturn7() throws Exception { |
| 5041 |
testTypes("/** @constructor */var A = function() {};\n" + |
| 5042 |
"/** @constructor */var B = function() {};\n" + |
| 5043 |
"/** @return {!B} */A.f = function() { return 1; };", |
| 5044 |
"inconsistent return type\n" + |
| 5045 |
"found : number\n" + |
| 5046 |
"required: B"); |
| 5047 |
} |
| 5048 |
|
| 5049 |
public void testReturn8() throws Exception { |
| 5050 |
testTypes("/** @constructor */var A = function() {};\n" + |
| 5051 |
"/** @constructor */var B = function() {};\n" + |
| 5052 |
"/** @return {!B} */A.prototype.f = function() { return 1; };", |
| 5053 |
"inconsistent return type\n" + |
| 5054 |
"found : number\n" + |
| 5055 |
"required: B"); |
| 5056 |
} |
| 5057 |
|
| 5058 |
public void testInferredReturn1() throws Exception { |
| 5059 |
testTypes( |
| 5060 |
"function f() {} /** @param {number} x */ function g(x) {}" + |
| 5061 |
"g(f());", |
| 5062 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 5063 |
"found : undefined\n" + |
| 5064 |
"required: number"); |
| 5065 |
} |
| 5066 |
|
| 5067 |
public void testInferredReturn2() throws Exception { |
| 5068 |
testTypes( |
| 5069 |
"/** @constructor */ function Foo() {}" + |
| 5070 |
"Foo.prototype.bar = function() {}; " + |
| 5071 |
"/** @param {number} x */ function g(x) {}" + |
| 5072 |
"g((new Foo()).bar());", |
| 5073 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 5074 |
"found : undefined\n" + |
| 5075 |
"required: number"); |
| 5076 |
} |
| 5077 |
|
| 5078 |
public void testInferredReturn3() throws Exception { |
| 5079 |
testTypes( |
| 5080 |
"/** @constructor */ function Foo() {}" + |
| 5081 |
"Foo.prototype.bar = function() {}; " + |
| 5082 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5083 |
"/** @return {number} \n * @override */ " + |
| 5084 |
"SubFoo.prototype.bar = function() { return 3; }; ", |
| 5085 |
"mismatch of the bar property type and the type of the property " + |
| 5086 |
"it overrides from superclass Foo\n" + |
| 5087 |
"original: function (this:Foo): undefined\n" + |
| 5088 |
"override: function (this:SubFoo): number"); |
| 5089 |
} |
| 5090 |
|
| 5091 |
public void testInferredReturn4() throws Exception { |
| 5092 |
|
| 5093 |
|
| 5094 |
|
| 5095 |
testTypes( |
| 5096 |
"var x = function() {};" + |
| 5097 |
"x = /** @type {function(): number} */ (function() { return 3; });", |
| 5098 |
"assignment\n" + |
| 5099 |
"found : function (): number\n" + |
| 5100 |
"required: function (): undefined"); |
| 5101 |
} |
| 5102 |
|
| 5103 |
public void testInferredReturn5() throws Exception { |
| 5104 |
|
| 5105 |
testTypes( |
| 5106 |
"/** @return {string} */" + |
| 5107 |
"function f() {" + |
| 5108 |
" var x = function() {};" + |
| 5109 |
" x = /** @type {function(): number} */ (function() { return 3; });" + |
| 5110 |
" return x();" + |
| 5111 |
"}", |
| 5112 |
"inconsistent return type\n" + |
| 5113 |
"found : number\n" + |
| 5114 |
"required: string"); |
| 5115 |
} |
| 5116 |
|
| 5117 |
public void testInferredReturn6() throws Exception { |
| 5118 |
testTypes( |
| 5119 |
"/** @return {string} */" + |
| 5120 |
"function f() {" + |
| 5121 |
" var x = function() {};" + |
| 5122 |
" if (f()) " + |
| 5123 |
" x = /** @type {function(): number} */ " + |
| 5124 |
" (function() { return 3; });" + |
| 5125 |
" return x();" + |
| 5126 |
"}", |
| 5127 |
"inconsistent return type\n" + |
| 5128 |
"found : (number|undefined)\n" + |
| 5129 |
"required: string"); |
| 5130 |
} |
| 5131 |
|
| 5132 |
public void testInferredReturn7() throws Exception { |
| 5133 |
testTypes( |
| 5134 |
"/** @constructor */ function Foo() {}" + |
| 5135 |
"/** @param {number} x */ Foo.prototype.bar = function(x) {};" + |
| 5136 |
"Foo.prototype.bar = function(x) { return 3; };", |
| 5137 |
"inconsistent return type\n" + |
| 5138 |
"found : number\n" + |
| 5139 |
"required: undefined"); |
| 5140 |
} |
| 5141 |
|
| 5142 |
public void testInferredReturn8() throws Exception { |
| 5143 |
reportMissingOverrides = CheckLevel.OFF; |
| 5144 |
testTypes( |
| 5145 |
"/** @constructor */ function Foo() {}" + |
| 5146 |
"/** @param {number} x */ Foo.prototype.bar = function(x) {};" + |
| 5147 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5148 |
"/** @param {number} x */ SubFoo.prototype.bar = " + |
| 5149 |
" function(x) { return 3; }", |
| 5150 |
"inconsistent return type\n" + |
| 5151 |
"found : number\n" + |
| 5152 |
"required: undefined"); |
| 5153 |
} |
| 5154 |
|
| 5155 |
public void testInferredParam1() throws Exception { |
| 5156 |
testTypes( |
| 5157 |
"/** @constructor */ function Foo() {}" + |
| 5158 |
"/** @param {number} x */ Foo.prototype.bar = function(x) {};" + |
| 5159 |
"/** @param {string} x */ function f(x) {}" + |
| 5160 |
"Foo.prototype.bar = function(y) { f(y); };", |
| 5161 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5162 |
"found : number\n" + |
| 5163 |
"required: string"); |
| 5164 |
} |
| 5165 |
|
| 5166 |
public void testInferredParam2() throws Exception { |
| 5167 |
reportMissingOverrides = CheckLevel.OFF; |
| 5168 |
testTypes( |
| 5169 |
"/** @param {string} x */ function f(x) {}" + |
| 5170 |
"/** @constructor */ function Foo() {}" + |
| 5171 |
"/** @param {number} x */ Foo.prototype.bar = function(x) {};" + |
| 5172 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5173 |
"/** @return {void} */ SubFoo.prototype.bar = " + |
| 5174 |
" function(x) { f(x); }", |
| 5175 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5176 |
"found : number\n" + |
| 5177 |
"required: string"); |
| 5178 |
} |
| 5179 |
|
| 5180 |
public void testInferredParam3() throws Exception { |
| 5181 |
reportMissingOverrides = CheckLevel.OFF; |
| 5182 |
testTypes( |
| 5183 |
"/** @param {string} x */ function f(x) {}" + |
| 5184 |
"/** @constructor */ function Foo() {}" + |
| 5185 |
"/** @param {number=} x */ Foo.prototype.bar = function(x) {};" + |
| 5186 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5187 |
"/** @return {void} */ SubFoo.prototype.bar = " + |
| 5188 |
" function(x) { f(x); }; (new SubFoo()).bar();", |
| 5189 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5190 |
"found : (number|undefined)\n" + |
| 5191 |
"required: string"); |
| 5192 |
} |
| 5193 |
|
| 5194 |
public void testInferredParam4() throws Exception { |
| 5195 |
reportMissingOverrides = CheckLevel.OFF; |
| 5196 |
testTypes( |
| 5197 |
"/** @param {string} x */ function f(x) {}" + |
| 5198 |
"/** @constructor */ function Foo() {}" + |
| 5199 |
"/** @param {...number} x */ Foo.prototype.bar = function(x) {};" + |
| 5200 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5201 |
"/** @return {void} */ SubFoo.prototype.bar = " + |
| 5202 |
" function(x) { f(x); }; (new SubFoo()).bar();", |
| 5203 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5204 |
"found : (number|undefined)\n" + |
| 5205 |
"required: string"); |
| 5206 |
} |
| 5207 |
|
| 5208 |
public void testInferredParam5() throws Exception { |
| 5209 |
reportMissingOverrides = CheckLevel.OFF; |
| 5210 |
testTypes( |
| 5211 |
"/** @param {string} x */ function f(x) {}" + |
| 5212 |
"/** @constructor */ function Foo() {}" + |
| 5213 |
"/** @param {...number} x */ Foo.prototype.bar = function(x) {};" + |
| 5214 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5215 |
"/** @param {number=} x \n * @param {...number} y */ " + |
| 5216 |
"SubFoo.prototype.bar = " + |
| 5217 |
" function(x, y) { f(x); }; (new SubFoo()).bar();", |
| 5218 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5219 |
"found : (number|undefined)\n" + |
| 5220 |
"required: string"); |
| 5221 |
} |
| 5222 |
|
| 5223 |
public void testInferredParam6() throws Exception { |
| 5224 |
reportMissingOverrides = CheckLevel.OFF; |
| 5225 |
testTypes( |
| 5226 |
"/** @param {string} x */ function f(x) {}" + |
| 5227 |
"/** @constructor */ function Foo() {}" + |
| 5228 |
"/** @param {number=} x */ Foo.prototype.bar = function(x) {};" + |
| 5229 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5230 |
"/** @param {number=} x \n * @param {number=} y */ " + |
| 5231 |
"SubFoo.prototype.bar = " + |
| 5232 |
" function(x, y) { f(y); };", |
| 5233 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5234 |
"found : (number|undefined)\n" + |
| 5235 |
"required: string"); |
| 5236 |
} |
| 5237 |
|
| 5238 |
public void testInferredParam7() throws Exception { |
| 5239 |
testTypes( |
| 5240 |
"/** @param {string} x */ function f(x) {}" + |
| 5241 |
"var bar = /** @type {function(number=,number=)} */ (" + |
| 5242 |
" function(x, y) { f(y); });", |
| 5243 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5244 |
"found : (number|undefined)\n" + |
| 5245 |
"required: string"); |
| 5246 |
} |
| 5247 |
|
| 5248 |
public void testOverriddenParams1() throws Exception { |
| 5249 |
testTypes( |
| 5250 |
"/** @constructor */ function Foo() {}" + |
| 5251 |
"/** @param {...?} var_args */" + |
| 5252 |
"Foo.prototype.bar = function(var_args) {};" + |
| 5253 |
"/**\n" + |
| 5254 |
" * @constructor\n" + |
| 5255 |
" * @extends {Foo}\n" + |
| 5256 |
" */ function SubFoo() {}" + |
| 5257 |
"/**\n" + |
| 5258 |
" * @param {number} x\n" + |
| 5259 |
" * @override\n" + |
| 5260 |
" */" + |
| 5261 |
"SubFoo.prototype.bar = function(x) {};"); |
| 5262 |
} |
| 5263 |
|
| 5264 |
public void testOverriddenParams2() throws Exception { |
| 5265 |
testTypes( |
| 5266 |
"/** @constructor */ function Foo() {}" + |
| 5267 |
"/** @type {function(...[?])} */" + |
| 5268 |
"Foo.prototype.bar = function(var_args) {};" + |
| 5269 |
"/**\n" + |
| 5270 |
" * @constructor\n" + |
| 5271 |
" * @extends {Foo}\n" + |
| 5272 |
" */ function SubFoo() {}" + |
| 5273 |
"/**\n" + |
| 5274 |
" * @type {function(number)}\n" + |
| 5275 |
" * @override\n" + |
| 5276 |
" */" + |
| 5277 |
"SubFoo.prototype.bar = function(x) {};"); |
| 5278 |
} |
| 5279 |
|
| 5280 |
public void testOverriddenParams3() throws Exception { |
| 5281 |
testTypes( |
| 5282 |
"/** @constructor */ function Foo() {}" + |
| 5283 |
"/** @param {...number} var_args */" + |
| 5284 |
"Foo.prototype.bar = function(var_args) { };" + |
| 5285 |
"/**\n" + |
| 5286 |
" * @constructor\n" + |
| 5287 |
" * @extends {Foo}\n" + |
| 5288 |
" */ function SubFoo() {}" + |
| 5289 |
"/**\n" + |
| 5290 |
" * @param {number} x\n" + |
| 5291 |
" * @override\n" + |
| 5292 |
" */" + |
| 5293 |
"SubFoo.prototype.bar = function(x) {};", |
| 5294 |
"mismatch of the bar property type and the type of the " + |
| 5295 |
"property it overrides from superclass Foo\n" + |
| 5296 |
"original: function (this:Foo, ...[number]): undefined\n" + |
| 5297 |
"override: function (this:SubFoo, number): undefined"); |
| 5298 |
} |
| 5299 |
|
| 5300 |
public void testOverriddenParams4() throws Exception { |
| 5301 |
testTypes( |
| 5302 |
"/** @constructor */ function Foo() {}" + |
| 5303 |
"/** @type {function(...[number])} */" + |
| 5304 |
"Foo.prototype.bar = function(var_args) {};" + |
| 5305 |
"/**\n" + |
| 5306 |
" * @constructor\n" + |
| 5307 |
" * @extends {Foo}\n" + |
| 5308 |
" */ function SubFoo() {}" + |
| 5309 |
"/**\n" + |
| 5310 |
" * @type {function(number)}\n" + |
| 5311 |
" * @override\n" + |
| 5312 |
" */" + |
| 5313 |
"SubFoo.prototype.bar = function(x) {};", |
| 5314 |
"mismatch of the bar property type and the type of the " + |
| 5315 |
"property it overrides from superclass Foo\n" + |
| 5316 |
"original: function (...[number]): ?\n" + |
| 5317 |
"override: function (number): ?"); |
| 5318 |
} |
| 5319 |
|
| 5320 |
public void testOverriddenParams5() throws Exception { |
| 5321 |
testTypes( |
| 5322 |
"/** @constructor */ function Foo() {}" + |
| 5323 |
"/** @param {number} x */" + |
| 5324 |
"Foo.prototype.bar = function(x) { };" + |
| 5325 |
"/**\n" + |
| 5326 |
" * @constructor\n" + |
| 5327 |
" * @extends {Foo}\n" + |
| 5328 |
" */ function SubFoo() {}" + |
| 5329 |
"/**\n" + |
| 5330 |
" * @override\n" + |
| 5331 |
" */" + |
| 5332 |
"SubFoo.prototype.bar = function() {};" + |
| 5333 |
"(new SubFoo()).bar();"); |
| 5334 |
} |
| 5335 |
|
| 5336 |
public void testOverriddenParams6() throws Exception { |
| 5337 |
testTypes( |
| 5338 |
"/** @constructor */ function Foo() {}" + |
| 5339 |
"/** @param {number} x */" + |
| 5340 |
"Foo.prototype.bar = function(x) { };" + |
| 5341 |
"/**\n" + |
| 5342 |
" * @constructor\n" + |
| 5343 |
" * @extends {Foo}\n" + |
| 5344 |
" */ function SubFoo() {}" + |
| 5345 |
"/**\n" + |
| 5346 |
" * @override\n" + |
| 5347 |
" */" + |
| 5348 |
"SubFoo.prototype.bar = function() {};" + |
| 5349 |
"(new SubFoo()).bar(true);", |
| 5350 |
"actual parameter 1 of SubFoo.prototype.bar " + |
| 5351 |
"does not match formal parameter\n" + |
| 5352 |
"found : boolean\n" + |
| 5353 |
"required: number"); |
| 5354 |
} |
| 5355 |
|
| 5356 |
public void testOverriddenReturn1() throws Exception { |
| 5357 |
testTypes( |
| 5358 |
"/** @constructor */ function Foo() {}" + |
| 5359 |
"/** @return {Object} */ Foo.prototype.bar = " + |
| 5360 |
" function() { return {}; };" + |
| 5361 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5362 |
"/** @return {SubFoo}\n * @override */ SubFoo.prototype.bar = " + |
| 5363 |
" function() { return new Foo(); }", |
| 5364 |
"inconsistent return type\n" + |
| 5365 |
"found : Foo\n" + |
| 5366 |
"required: (SubFoo|null)"); |
| 5367 |
} |
| 5368 |
|
| 5369 |
public void testOverriddenReturn2() throws Exception { |
| 5370 |
testTypes( |
| 5371 |
"/** @constructor */ function Foo() {}" + |
| 5372 |
"/** @return {SubFoo} */ Foo.prototype.bar = " + |
| 5373 |
" function() { return new SubFoo(); };" + |
| 5374 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 5375 |
"/** @return {Foo} x\n * @override */ SubFoo.prototype.bar = " + |
| 5376 |
" function() { return new SubFoo(); }", |
| 5377 |
"mismatch of the bar property type and the type of the " + |
| 5378 |
"property it overrides from superclass Foo\n" + |
| 5379 |
"original: function (this:Foo): (SubFoo|null)\n" + |
| 5380 |
"override: function (this:SubFoo): (Foo|null)"); |
| 5381 |
} |
| 5382 |
|
| 5383 |
public void testThis1() throws Exception { |
| 5384 |
testTypes("var goog = {};" + |
| 5385 |
"/** @constructor */goog.A = function(){};" + |
| 5386 |
"/** @return {number} */" + |
| 5387 |
"goog.A.prototype.n = function() { return this };", |
| 5388 |
"inconsistent return type\n" + |
| 5389 |
"found : goog.A\n" + |
| 5390 |
"required: number"); |
| 5391 |
} |
| 5392 |
|
| 5393 |
public void testOverriddenProperty1() throws Exception { |
| 5394 |
testTypes( |
| 5395 |
"/** @constructor */ function Foo() {}" + |
| 5396 |
"/** @type {Object} */" + |
| 5397 |
"Foo.prototype.bar = {};" + |
| 5398 |
"/**\n" + |
| 5399 |
" * @constructor\n" + |
| 5400 |
" * @extends {Foo}\n" + |
| 5401 |
" */ function SubFoo() {}" + |
| 5402 |
"/**\n" + |
| 5403 |
" * @type {Array}\n" + |
| 5404 |
" * @override\n" + |
| 5405 |
" */" + |
| 5406 |
"SubFoo.prototype.bar = [];"); |
| 5407 |
} |
| 5408 |
|
| 5409 |
public void testOverriddenProperty2() throws Exception { |
| 5410 |
testTypes( |
| 5411 |
"/** @constructor */ function Foo() {" + |
| 5412 |
" /** @type {Object} */" + |
| 5413 |
" this.bar = {};" + |
| 5414 |
"}" + |
| 5415 |
"/**\n" + |
| 5416 |
" * @constructor\n" + |
| 5417 |
" * @extends {Foo}\n" + |
| 5418 |
" */ function SubFoo() {}" + |
| 5419 |
"/**\n" + |
| 5420 |
" * @type {Array}\n" + |
| 5421 |
" * @override\n" + |
| 5422 |
" */" + |
| 5423 |
"SubFoo.prototype.bar = [];"); |
| 5424 |
} |
| 5425 |
|
| 5426 |
public void testOverriddenProperty3() throws Exception { |
| 5427 |
testTypes( |
| 5428 |
"/** @constructor */ function Foo() {" + |
| 5429 |
"}" + |
| 5430 |
"/** @type {string} */ Foo.prototype.data;" + |
| 5431 |
"/**\n" + |
| 5432 |
" * @constructor\n" + |
| 5433 |
" * @extends {Foo}\n" + |
| 5434 |
" */ function SubFoo() {}" + |
| 5435 |
"/** @type {string|Object} \n @override */ " + |
| 5436 |
"SubFoo.prototype.data = null;", |
| 5437 |
"mismatch of the data property type and the type " + |
| 5438 |
"of the property it overrides from superclass Foo\n" + |
| 5439 |
"original: string\n" + |
| 5440 |
"override: (Object|null|string)"); |
| 5441 |
} |
| 5442 |
|
| 5443 |
public void testOverriddenProperty4() throws Exception { |
| 5444 |
|
| 5445 |
testTypes( |
| 5446 |
"/** @constructor */ function Foo() {}" + |
| 5447 |
"Foo.prototype.bar = null;" + |
| 5448 |
"/**\n" + |
| 5449 |
" * @constructor\n" + |
| 5450 |
" * @extends {Foo}\n" + |
| 5451 |
" */ function SubFoo() {}" + |
| 5452 |
"SubFoo.prototype.bar = 3;"); |
| 5453 |
} |
| 5454 |
|
| 5455 |
public void testOverriddenProperty5() throws Exception { |
| 5456 |
|
| 5457 |
testTypes( |
| 5458 |
"/** @constructor */ function Foo() {}" + |
| 5459 |
"Foo.prototype.bar = null;" + |
| 5460 |
"/**\n" + |
| 5461 |
" * @constructor\n" + |
| 5462 |
" * @extends {Foo}\n" + |
| 5463 |
" */ function SubFoo() {}" + |
| 5464 |
"/** @override */ SubFoo.prototype.bar = 3;"); |
| 5465 |
} |
| 5466 |
|
| 5467 |
public void testOverriddenProperty6() throws Exception { |
| 5468 |
|
| 5469 |
|
| 5470 |
testTypes( |
| 5471 |
"/** @constructor */ function Foo() {}" + |
| 5472 |
"/** @type {?number} */ Foo.prototype.bar = null;" + |
| 5473 |
"/**\n" + |
| 5474 |
" * @constructor\n" + |
| 5475 |
" * @extends {Foo}\n" + |
| 5476 |
" */ function SubFoo() {}" + |
| 5477 |
"SubFoo.prototype.bar = 3;"); |
| 5478 |
} |
| 5479 |
|
| 5480 |
public void testThis2() throws Exception { |
| 5481 |
testTypes("var goog = {};" + |
| 5482 |
"/** @constructor */goog.A = function(){" + |
| 5483 |
" this.foo = null;" + |
| 5484 |
"};" + |
| 5485 |
"/** @return {number} */" + |
| 5486 |
"goog.A.prototype.n = function() { return this.foo };", |
| 5487 |
"inconsistent return type\n" + |
| 5488 |
"found : null\n" + |
| 5489 |
"required: number"); |
| 5490 |
} |
| 5491 |
|
| 5492 |
public void testThis3() throws Exception { |
| 5493 |
testTypes("var goog = {};" + |
| 5494 |
"/** @constructor */goog.A = function(){" + |
| 5495 |
" this.foo = null;" + |
| 5496 |
" this.foo = 5;" + |
| 5497 |
"};"); |
| 5498 |
} |
| 5499 |
|
| 5500 |
public void testThis4() throws Exception { |
| 5501 |
testTypes("var goog = {};" + |
| 5502 |
"/** @constructor */goog.A = function(){" + |
| 5503 |
" /** @type {string?} */this.foo = null;" + |
| 5504 |
"};" + |
| 5505 |
"/** @return {number} */goog.A.prototype.n = function() {" + |
| 5506 |
" return this.foo };", |
| 5507 |
"inconsistent return type\n" + |
| 5508 |
"found : (null|string)\n" + |
| 5509 |
"required: number"); |
| 5510 |
} |
| 5511 |
|
| 5512 |
public void testThis5() throws Exception { |
| 5513 |
testTypes("/** @this Date\n@return {number}*/function h() { return this }", |
| 5514 |
"inconsistent return type\n" + |
| 5515 |
"found : Date\n" + |
| 5516 |
"required: number"); |
| 5517 |
} |
| 5518 |
|
| 5519 |
public void testThis6() throws Exception { |
| 5520 |
testTypes("var goog = {};" + |
| 5521 |
"/** @constructor\n@return {!Date} */" + |
| 5522 |
"goog.A = function(){ return this };", |
| 5523 |
"inconsistent return type\n" + |
| 5524 |
"found : goog.A\n" + |
| 5525 |
"required: Date"); |
| 5526 |
} |
| 5527 |
|
| 5528 |
public void testThis7() throws Exception { |
| 5529 |
testTypes("/** @constructor */function A(){};" + |
| 5530 |
"/** @return {number} */A.prototype.n = function() { return this };", |
| 5531 |
"inconsistent return type\n" + |
| 5532 |
"found : A\n" + |
| 5533 |
"required: number"); |
| 5534 |
} |
| 5535 |
|
| 5536 |
public void testThis8() throws Exception { |
| 5537 |
testTypes("/** @constructor */function A(){" + |
| 5538 |
" /** @type {string?} */this.foo = null;" + |
| 5539 |
"};" + |
| 5540 |
"/** @return {number} */A.prototype.n = function() {" + |
| 5541 |
" return this.foo };", |
| 5542 |
"inconsistent return type\n" + |
| 5543 |
"found : (null|string)\n" + |
| 5544 |
"required: number"); |
| 5545 |
} |
| 5546 |
|
| 5547 |
public void testThis9() throws Exception { |
| 5548 |
|
| 5549 |
testTypes("/** @constructor */function A(){};" + |
| 5550 |
"A.prototype.foo = 3;" + |
| 5551 |
"/** @return {string} */ A.bar = function() { return this.foo; };"); |
| 5552 |
} |
| 5553 |
|
| 5554 |
public void testThis10() throws Exception { |
| 5555 |
|
| 5556 |
testTypes("/** @constructor */function A(){};" + |
| 5557 |
"A.prototype.foo = 3;" + |
| 5558 |
"/** @this {A}\n@return {string} */" + |
| 5559 |
"A.bar = function() { return this.foo; };", |
| 5560 |
"inconsistent return type\n" + |
| 5561 |
"found : number\n" + |
| 5562 |
"required: string"); |
| 5563 |
} |
| 5564 |
|
| 5565 |
public void testThis11() throws Exception { |
| 5566 |
testTypes( |
| 5567 |
"/** @param {number} x */ function f(x) {}" + |
| 5568 |
"/** @constructor */ function Ctor() {" + |
| 5569 |
" /** @this {Date} */" + |
| 5570 |
" this.method = function() {" + |
| 5571 |
" f(this);" + |
| 5572 |
" };" + |
| 5573 |
"}", |
| 5574 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5575 |
"found : Date\n" + |
| 5576 |
"required: number"); |
| 5577 |
} |
| 5578 |
|
| 5579 |
public void testThis12() throws Exception { |
| 5580 |
testTypes( |
| 5581 |
"/** @param {number} x */ function f(x) {}" + |
| 5582 |
"/** @constructor */ function Ctor() {}" + |
| 5583 |
"Ctor.prototype['method'] = function() {" + |
| 5584 |
" f(this);" + |
| 5585 |
"}", |
| 5586 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5587 |
"found : Ctor\n" + |
| 5588 |
"required: number"); |
| 5589 |
} |
| 5590 |
|
| 5591 |
public void testThis13() throws Exception { |
| 5592 |
testTypes( |
| 5593 |
"/** @param {number} x */ function f(x) {}" + |
| 5594 |
"/** @constructor */ function Ctor() {}" + |
| 5595 |
"Ctor.prototype = {" + |
| 5596 |
" method: function() {" + |
| 5597 |
" f(this);" + |
| 5598 |
" }" + |
| 5599 |
"};", |
| 5600 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5601 |
"found : Ctor\n" + |
| 5602 |
"required: number"); |
| 5603 |
} |
| 5604 |
|
| 5605 |
public void testThis14() throws Exception { |
| 5606 |
testTypes( |
| 5607 |
"/** @param {number} x */ function f(x) {}" + |
| 5608 |
"f(this.Object);", |
| 5609 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5610 |
"found : function (new:Object, *=): ?\n" + |
| 5611 |
"required: number"); |
| 5612 |
} |
| 5613 |
|
| 5614 |
public void testThisTypeOfFunction1() throws Exception { |
| 5615 |
testTypes( |
| 5616 |
"/** @type {function(this:Object)} */ function f() {}" + |
| 5617 |
"f();"); |
| 5618 |
} |
| 5619 |
|
| 5620 |
public void testThisTypeOfFunction2() throws Exception { |
| 5621 |
testTypes( |
| 5622 |
"/** @constructor */ function F() {}" + |
| 5623 |
"/** @type {function(this:F)} */ function f() {}" + |
| 5624 |
"f();", |
| 5625 |
"\"function (this:F): ?\" must be called with a \"this\" type"); |
| 5626 |
} |
| 5627 |
|
| 5628 |
public void testThisTypeOfFunction3() throws Exception { |
| 5629 |
testTypes( |
| 5630 |
"/** @constructor */ function F() {}" + |
| 5631 |
"F.prototype.bar = function() {};" + |
| 5632 |
"var f = (new F()).bar; f();", |
| 5633 |
"\"function (this:F): undefined\" must be called with a \"this\" type"); |
| 5634 |
} |
| 5635 |
|
| 5636 |
public void testThisTypeOfFunction4() throws Exception { |
| 5637 |
testTypes( |
| 5638 |
"/** @constructor */ function F() {}" + |
| 5639 |
"F.prototype.moveTo = function(x, y) {};" + |
| 5640 |
"F.prototype.lineTo = function(x, y) {};" + |
| 5641 |
"function demo() {" + |
| 5642 |
" var path = new F();" + |
| 5643 |
" var points = [[1,1], [2,2]];" + |
| 5644 |
" for (var i = 0; i < points.length; i++) {" + |
| 5645 |
" (i == 0 ? path.moveTo : path.lineTo)(" + |
| 5646 |
" points[i][0], points[i][1]);" + |
| 5647 |
" }" + |
| 5648 |
"}", |
| 5649 |
"\"function (this:F, ?, ?): undefined\" " + |
| 5650 |
"must be called with a \"this\" type"); |
| 5651 |
} |
| 5652 |
|
| 5653 |
public void testGlobalThis1() throws Exception { |
| 5654 |
testTypes("/** @constructor */ function Window() {}" + |
| 5655 |
"/** @param {string} msg */ " + |
| 5656 |
"Window.prototype.alert = function(msg) {};" + |
| 5657 |
"this.alert(3);", |
| 5658 |
"actual parameter 1 of Window.prototype.alert " + |
| 5659 |
"does not match formal parameter\n" + |
| 5660 |
"found : number\n" + |
| 5661 |
"required: string"); |
| 5662 |
} |
| 5663 |
|
| 5664 |
public void testGlobalThis2() throws Exception { |
| 5665 |
|
| 5666 |
testTypes("/** @constructor */ function Bindow() {}" + |
| 5667 |
"/** @param {string} msg */ " + |
| 5668 |
"Bindow.prototype.alert = function(msg) {};" + |
| 5669 |
"this.alert = 3;" + |
| 5670 |
"(new Bindow()).alert(this.alert)"); |
| 5671 |
} |
| 5672 |
|
| 5673 |
|
| 5674 |
public void testGlobalThis2b() throws Exception { |
| 5675 |
testTypes("/** @constructor */ function Bindow() {}" + |
| 5676 |
"/** @param {string} msg */ " + |
| 5677 |
"Bindow.prototype.alert = function(msg) {};" + |
| 5678 |
"/** @return {number} */ this.alert = function() { return 3; };" + |
| 5679 |
"(new Bindow()).alert(this.alert())", |
| 5680 |
"actual parameter 1 of Bindow.prototype.alert " + |
| 5681 |
"does not match formal parameter\n" + |
| 5682 |
"found : number\n" + |
| 5683 |
"required: string"); |
| 5684 |
} |
| 5685 |
|
| 5686 |
public void testGlobalThis3() throws Exception { |
| 5687 |
testTypes( |
| 5688 |
"/** @param {string} msg */ " + |
| 5689 |
"function alert(msg) {};" + |
| 5690 |
"this.alert(3);", |
| 5691 |
"actual parameter 1 of global this.alert " + |
| 5692 |
"does not match formal parameter\n" + |
| 5693 |
"found : number\n" + |
| 5694 |
"required: string"); |
| 5695 |
} |
| 5696 |
|
| 5697 |
public void testGlobalThis4() throws Exception { |
| 5698 |
testTypes( |
| 5699 |
"/** @param {string} msg */ " + |
| 5700 |
"var alert = function(msg) {};" + |
| 5701 |
"this.alert(3);", |
| 5702 |
"actual parameter 1 of global this.alert " + |
| 5703 |
"does not match formal parameter\n" + |
| 5704 |
"found : number\n" + |
| 5705 |
"required: string"); |
| 5706 |
} |
| 5707 |
|
| 5708 |
public void testGlobalThis5() throws Exception { |
| 5709 |
testTypes( |
| 5710 |
"function f() {" + |
| 5711 |
" /** @param {string} msg */ " + |
| 5712 |
" var alert = function(msg) {};" + |
| 5713 |
"}" + |
| 5714 |
"this.alert(3);", |
| 5715 |
"Property alert never defined on global this"); |
| 5716 |
} |
| 5717 |
|
| 5718 |
public void testGlobalThis6() throws Exception { |
| 5719 |
testTypes( |
| 5720 |
"/** @param {string} msg */ " + |
| 5721 |
"var alert = function(msg) {};" + |
| 5722 |
"var x = 3;" + |
| 5723 |
"x = 'msg';" + |
| 5724 |
"this.alert(this.x);"); |
| 5725 |
} |
| 5726 |
|
| 5727 |
public void testGlobalThis7() throws Exception { |
| 5728 |
testTypes( |
| 5729 |
"/** @constructor */ function Window() {}" + |
| 5730 |
"/** @param {Window} msg */ " + |
| 5731 |
"var foo = function(msg) {};" + |
| 5732 |
"foo(this);"); |
| 5733 |
} |
| 5734 |
|
| 5735 |
public void testGlobalThis8() throws Exception { |
| 5736 |
testTypes( |
| 5737 |
"/** @constructor */ function Window() {}" + |
| 5738 |
"/** @param {number} msg */ " + |
| 5739 |
"var foo = function(msg) {};" + |
| 5740 |
"foo(this);", |
| 5741 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 5742 |
"found : global this\n" + |
| 5743 |
"required: number"); |
| 5744 |
} |
| 5745 |
|
| 5746 |
public void testGlobalThis9() throws Exception { |
| 5747 |
testTypes( |
| 5748 |
|
| 5749 |
|
| 5750 |
"function Window() {}" + |
| 5751 |
"Window.prototype.alert = function() {};" + |
| 5752 |
"this.alert();", |
| 5753 |
"Property alert never defined on global this"); |
| 5754 |
} |
| 5755 |
|
| 5756 |
public void testControlFlowRestrictsType1() throws Exception { |
| 5757 |
testTypes("/** @return {String?} */ function f() { return null; }" + |
| 5758 |
"/** @type {String?} */ var a = f();" + |
| 5759 |
"/** @type String */ var b = new String('foo');" + |
| 5760 |
"/** @type null */ var c = null;" + |
| 5761 |
"if (a) {" + |
| 5762 |
" b = a;" + |
| 5763 |
"} else {" + |
| 5764 |
" c = a;" + |
| 5765 |
"}"); |
| 5766 |
} |
| 5767 |
|
| 5768 |
public void testControlFlowRestrictsType2() throws Exception { |
| 5769 |
testTypes("/** @return {(string,null)} */ function f() { return null; }" + |
| 5770 |
"/** @type {(string,null)} */ var a = f();" + |
| 5771 |
"/** @type string */ var b = 'foo';" + |
| 5772 |
"/** @type null */ var c = null;" + |
| 5773 |
"if (a) {" + |
| 5774 |
" b = a;" + |
| 5775 |
"} else {" + |
| 5776 |
" c = a;" + |
| 5777 |
"}", |
| 5778 |
"assignment\n" + |
| 5779 |
"found : (null|string)\n" + |
| 5780 |
"required: null"); |
| 5781 |
} |
| 5782 |
|
| 5783 |
public void testControlFlowRestrictsType3() throws Exception { |
| 5784 |
testTypes("/** @type {(string,void)} */" + |
| 5785 |
"var a;" + |
| 5786 |
"/** @type string */" + |
| 5787 |
"var b = 'foo';" + |
| 5788 |
"if (a) {" + |
| 5789 |
" b = a;" + |
| 5790 |
"}"); |
| 5791 |
} |
| 5792 |
|
| 5793 |
public void testControlFlowRestrictsType4() throws Exception { |
| 5794 |
testTypes("/** @param {string} a */ function f(a){}" + |
| 5795 |
"/** @type {(string,undefined)} */ var a;" + |
| 5796 |
"a && f(a);"); |
| 5797 |
} |
| 5798 |
|
| 5799 |
public void testControlFlowRestrictsType5() throws Exception { |
| 5800 |
testTypes("/** @param {undefined} a */ function f(a){}" + |
| 5801 |
"/** @type {(!Array,undefined)} */ var a;" + |
| 5802 |
"a || f(a);"); |
| 5803 |
} |
| 5804 |
|
| 5805 |
public void testControlFlowRestrictsType6() throws Exception { |
| 5806 |
testTypes("/** @param {undefined} x */ function f(x) {}" + |
| 5807 |
"/** @type {(string,undefined)} */ var a;" + |
| 5808 |
"a && f(a);", |
| 5809 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5810 |
"found : string\n" + |
| 5811 |
"required: undefined"); |
| 5812 |
} |
| 5813 |
|
| 5814 |
public void testControlFlowRestrictsType7() throws Exception { |
| 5815 |
testTypes("/** @param {undefined} x */ function f(x) {}" + |
| 5816 |
"/** @type {(string,undefined)} */ var a;" + |
| 5817 |
"a && f(a);", |
| 5818 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5819 |
"found : string\n" + |
| 5820 |
"required: undefined"); |
| 5821 |
} |
| 5822 |
|
| 5823 |
public void testControlFlowRestrictsType8() throws Exception { |
| 5824 |
testTypes("/** @param {undefined} a */ function f(a){}" + |
| 5825 |
"/** @type {(!Array,undefined)} */ var a;" + |
| 5826 |
"if (a || f(a)) {}"); |
| 5827 |
} |
| 5828 |
|
| 5829 |
public void testControlFlowRestrictsType9() throws Exception { |
| 5830 |
testTypes("/** @param {number?} x\n * @return {number}*/\n" + |
| 5831 |
"var f = function(x) {\n" + |
| 5832 |
"if (!x || x == 1) { return 1; } else { return x; }\n" + |
| 5833 |
"};"); |
| 5834 |
} |
| 5835 |
|
| 5836 |
public void testControlFlowRestrictsType10() throws Exception { |
| 5837 |
|
| 5838 |
|
| 5839 |
testTypes("/** @param {number} x */ function f(x) {}" + |
| 5840 |
"function g() {" + |
| 5841 |
" var y = null;" + |
| 5842 |
" for (var i = 0; i < 10; i++) {" + |
| 5843 |
" f(y);" + |
| 5844 |
" if (y != null) {" + |
| 5845 |
" // y is None the first time it goes through this branch\n" + |
| 5846 |
" } else {" + |
| 5847 |
" y = {};" + |
| 5848 |
" }" + |
| 5849 |
" }" + |
| 5850 |
"};", |
| 5851 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 5852 |
"found : (null|{})\n" + |
| 5853 |
"required: number"); |
| 5854 |
} |
| 5855 |
|
| 5856 |
public void testControlFlowRestrictsType11() throws Exception { |
| 5857 |
testTypes("/** @param {boolean} x */ function f(x) {}" + |
| 5858 |
"function g() {" + |
| 5859 |
" var y = null;" + |
| 5860 |
" if (y != null) {" + |
| 5861 |
" for (var i = 0; i < 10; i++) {" + |
| 5862 |
" f(y);" + |
| 5863 |
" }" + |
| 5864 |
" }" + |
| 5865 |
"};", |
| 5866 |
"condition always evaluates to false\n" + |
| 5867 |
"left : null\n" + |
| 5868 |
"right: null"); |
| 5869 |
} |
| 5870 |
|
| 5871 |
public void testSwitchCase3() throws Exception { |
| 5872 |
testTypes("/** @type String */" + |
| 5873 |
"var a = new String('foo');" + |
| 5874 |
"switch (a) { case 'A': }"); |
| 5875 |
} |
| 5876 |
|
| 5877 |
public void testSwitchCase4() throws Exception { |
| 5878 |
testTypes("/** @type {(string,Null)} */" + |
| 5879 |
"var a = 'foo';" + |
| 5880 |
"switch (a) { case 'A':break; case null:break; }"); |
| 5881 |
} |
| 5882 |
|
| 5883 |
public void testSwitchCase5() throws Exception { |
| 5884 |
testTypes("/** @type {(String,Null)} */" + |
| 5885 |
"var a = new String('foo');" + |
| 5886 |
"switch (a) { case 'A':break; case null:break; }"); |
| 5887 |
} |
| 5888 |
|
| 5889 |
public void testSwitchCase6() throws Exception { |
| 5890 |
testTypes("/** @type {(Number,Null)} */" + |
| 5891 |
"var a = new Number(5);" + |
| 5892 |
"switch (a) { case 5:break; case null:break; }"); |
| 5893 |
} |
| 5894 |
|
| 5895 |
public void testSwitchCase7() throws Exception { |
| 5896 |
|
| 5897 |
testTypes( |
| 5898 |
"/**\n" + |
| 5899 |
" * @param {number} x\n" + |
| 5900 |
" * @return {number}\n" + |
| 5901 |
" */\n" + |
| 5902 |
"function g(x) { return 5; }" + |
| 5903 |
"function f() {" + |
| 5904 |
" var x = {};" + |
| 5905 |
" x.foo = '3';" + |
| 5906 |
" switch (3) { case g(x.foo): return 3; }" + |
| 5907 |
"}", |
| 5908 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 5909 |
"found : string\n" + |
| 5910 |
"required: number"); |
| 5911 |
} |
| 5912 |
|
| 5913 |
public void testSwitchCase8() throws Exception { |
| 5914 |
|
| 5915 |
testTypes( |
| 5916 |
"/**\n" + |
| 5917 |
" * @param {number} x\n" + |
| 5918 |
" * @return {number}\n" + |
| 5919 |
" */\n" + |
| 5920 |
"function g(x) { return 5; }" + |
| 5921 |
"function f() {" + |
| 5922 |
" var x = {};" + |
| 5923 |
" x.foo = '3';" + |
| 5924 |
" switch (g(x.foo)) { case 3: return 3; }" + |
| 5925 |
"}", |
| 5926 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 5927 |
"found : string\n" + |
| 5928 |
"required: number"); |
| 5929 |
} |
| 5930 |
|
| 5931 |
public void testNoTypeCheck1() throws Exception { |
| 5932 |
testTypes("/** @notypecheck */function foo() { new 4 }"); |
| 5933 |
} |
| 5934 |
|
| 5935 |
public void testNoTypeCheck2() throws Exception { |
| 5936 |
testTypes("/** @notypecheck */var foo = function() { new 4 }"); |
| 5937 |
} |
| 5938 |
|
| 5939 |
public void testNoTypeCheck3() throws Exception { |
| 5940 |
testTypes("/** @notypecheck */var foo = function bar() { new 4 }"); |
| 5941 |
} |
| 5942 |
|
| 5943 |
public void testNoTypeCheck4() throws Exception { |
| 5944 |
testTypes("var foo;" + |
| 5945 |
"/** @notypecheck */foo = function() { new 4 }"); |
| 5946 |
} |
| 5947 |
|
| 5948 |
public void testNoTypeCheck5() throws Exception { |
| 5949 |
testTypes("var foo;" + |
| 5950 |
"foo = /** @notypecheck */function() { new 4 }"); |
| 5951 |
} |
| 5952 |
|
| 5953 |
public void testNoTypeCheck6() throws Exception { |
| 5954 |
testTypes("var foo;" + |
| 5955 |
"/** @notypecheck */foo = function bar() { new 4 }"); |
| 5956 |
} |
| 5957 |
|
| 5958 |
public void testNoTypeCheck7() throws Exception { |
| 5959 |
testTypes("var foo;" + |
| 5960 |
"foo = /** @notypecheck */function bar() { new 4 }"); |
| 5961 |
} |
| 5962 |
|
| 5963 |
public void testNoTypeCheck8() throws Exception { |
| 5964 |
testTypes("/** @fileoverview \n * @notypecheck */ var foo;" + |
| 5965 |
"var bar = 3; /** @param {string} x */ function f(x) {} f(bar);"); |
| 5966 |
} |
| 5967 |
|
| 5968 |
public void testNoTypeCheck9() throws Exception { |
| 5969 |
testTypes("/** @notypecheck */ function g() { }" + |
| 5970 |
" /** @type {string} */ var a = 1", |
| 5971 |
"initializing variable\n" + |
| 5972 |
"found : number\n" + |
| 5973 |
"required: string" |
| 5974 |
); |
| 5975 |
} |
| 5976 |
|
| 5977 |
public void testNoTypeCheck10() throws Exception { |
| 5978 |
testTypes("/** @notypecheck */ function g() { }" + |
| 5979 |
" function h() {/** @type {string} */ var a = 1}", |
| 5980 |
"initializing variable\n" + |
| 5981 |
"found : number\n" + |
| 5982 |
"required: string" |
| 5983 |
); |
| 5984 |
} |
| 5985 |
|
| 5986 |
public void testNoTypeCheck11() throws Exception { |
| 5987 |
testTypes("/** @notypecheck */ function g() { }" + |
| 5988 |
"/** @notypecheck */ function h() {/** @type {string} */ var a = 1}" |
| 5989 |
); |
| 5990 |
} |
| 5991 |
|
| 5992 |
public void testNoTypeCheck12() throws Exception { |
| 5993 |
testTypes("/** @notypecheck */ function g() { }" + |
| 5994 |
"function h() {/** @type {string}\n * @notypecheck\n*/ var a = 1}" |
| 5995 |
); |
| 5996 |
} |
| 5997 |
|
| 5998 |
public void testNoTypeCheck13() throws Exception { |
| 5999 |
testTypes("/** @notypecheck */ function g() { }" + |
| 6000 |
"function h() {/** @type {string}\n * @notypecheck\n*/ var a = 1;" + |
| 6001 |
"/** @type {string}*/ var b = 1}", |
| 6002 |
"initializing variable\n" + |
| 6003 |
"found : number\n" + |
| 6004 |
"required: string" |
| 6005 |
); |
| 6006 |
} |
| 6007 |
|
| 6008 |
public void testNoTypeCheck14() throws Exception { |
| 6009 |
testTypes("/** @fileoverview \n * @notypecheck */ function g() { }" + |
| 6010 |
"g(1,2,3)"); |
| 6011 |
} |
| 6012 |
|
| 6013 |
public void testImplicitCast() throws Exception { |
| 6014 |
testTypes("/** @constructor */ function Element() {};\n" + |
| 6015 |
"/** @type {string}\n" + |
| 6016 |
" * @implicitCast */" + |
| 6017 |
"Element.prototype.innerHTML;", |
| 6018 |
"(new Element).innerHTML = new Array();", null, false); |
| 6019 |
} |
| 6020 |
|
| 6021 |
public void testImplicitCastSubclassAccess() throws Exception { |
| 6022 |
testTypes("/** @constructor */ function Element() {};\n" + |
| 6023 |
"/** @type {string}\n" + |
| 6024 |
" * @implicitCast */" + |
| 6025 |
"Element.prototype.innerHTML;" + |
| 6026 |
"/** @constructor \n @extends Element */" + |
| 6027 |
"function DIVElement() {};", |
| 6028 |
"(new DIVElement).innerHTML = new Array();", |
| 6029 |
null, false); |
| 6030 |
} |
| 6031 |
|
| 6032 |
public void testImplicitCastNotInExterns() throws Exception { |
| 6033 |
testTypes("/** @constructor */ function Element() {};\n" + |
| 6034 |
"/** @type {string}\n" + |
| 6035 |
" * @implicitCast */" + |
| 6036 |
"Element.prototype.innerHTML;" + |
| 6037 |
"(new Element).innerHTML = new Array();", |
| 6038 |
new String[] { |
| 6039 |
"Illegal annotation on innerHTML. @implicitCast may only be " + |
| 6040 |
"used in externs.", |
| 6041 |
"assignment to property innerHTML of Element\n" + |
| 6042 |
"found : Array\n" + |
| 6043 |
"required: string"}); |
| 6044 |
} |
| 6045 |
|
| 6046 |
public void testNumberNode() throws Exception { |
| 6047 |
Node n = typeCheck(Node.newNumber(0)); |
| 6048 |
|
| 6049 |
assertTypeEquals(NUMBER_TYPE, n.getJSType()); |
| 6050 |
} |
| 6051 |
|
| 6052 |
public void testStringNode() throws Exception { |
| 6053 |
Node n = typeCheck(Node.newString("hello")); |
| 6054 |
|
| 6055 |
assertTypeEquals(STRING_TYPE, n.getJSType()); |
| 6056 |
} |
| 6057 |
|
| 6058 |
public void testBooleanNodeTrue() throws Exception { |
| 6059 |
Node trueNode = typeCheck(new Node(Token.TRUE)); |
| 6060 |
|
| 6061 |
assertTypeEquals(BOOLEAN_TYPE, trueNode.getJSType()); |
| 6062 |
} |
| 6063 |
|
| 6064 |
public void testBooleanNodeFalse() throws Exception { |
| 6065 |
Node falseNode = typeCheck(new Node(Token.FALSE)); |
| 6066 |
|
| 6067 |
assertTypeEquals(BOOLEAN_TYPE, falseNode.getJSType()); |
| 6068 |
} |
| 6069 |
|
| 6070 |
public void testUndefinedNode() throws Exception { |
| 6071 |
Node p = new Node(Token.ADD); |
| 6072 |
Node n = Node.newString(Token.NAME, "undefined"); |
| 6073 |
p.addChildToBack(n); |
| 6074 |
p.addChildToBack(Node.newNumber(5)); |
| 6075 |
typeCheck(p); |
| 6076 |
|
| 6077 |
assertTypeEquals(VOID_TYPE, n.getJSType()); |
| 6078 |
} |
| 6079 |
|
| 6080 |
public void testNumberAutoboxing() throws Exception { |
| 6081 |
testTypes("/** @type Number */var a = 4;", |
| 6082 |
"initializing variable\n" + |
| 6083 |
"found : number\n" + |
| 6084 |
"required: (Number|null)"); |
| 6085 |
} |
| 6086 |
|
| 6087 |
public void testNumberUnboxing() throws Exception { |
| 6088 |
testTypes("/** @type number */var a = new Number(4);", |
| 6089 |
"initializing variable\n" + |
| 6090 |
"found : Number\n" + |
| 6091 |
"required: number"); |
| 6092 |
} |
| 6093 |
|
| 6094 |
public void testStringAutoboxing() throws Exception { |
| 6095 |
testTypes("/** @type String */var a = 'hello';", |
| 6096 |
"initializing variable\n" + |
| 6097 |
"found : string\n" + |
| 6098 |
"required: (String|null)"); |
| 6099 |
} |
| 6100 |
|
| 6101 |
public void testStringUnboxing() throws Exception { |
| 6102 |
testTypes("/** @type string */var a = new String('hello');", |
| 6103 |
"initializing variable\n" + |
| 6104 |
"found : String\n" + |
| 6105 |
"required: string"); |
| 6106 |
} |
| 6107 |
|
| 6108 |
public void testBooleanAutoboxing() throws Exception { |
| 6109 |
testTypes("/** @type Boolean */var a = true;", |
| 6110 |
"initializing variable\n" + |
| 6111 |
"found : boolean\n" + |
| 6112 |
"required: (Boolean|null)"); |
| 6113 |
} |
| 6114 |
|
| 6115 |
public void testBooleanUnboxing() throws Exception { |
| 6116 |
testTypes("/** @type boolean */var a = new Boolean(false);", |
| 6117 |
"initializing variable\n" + |
| 6118 |
"found : Boolean\n" + |
| 6119 |
"required: boolean"); |
| 6120 |
} |
| 6121 |
|
| 6122 |
public void testIIFE1() throws Exception { |
| 6123 |
testTypes( |
| 6124 |
"var namespace = {};" + |
| 6125 |
"/** @type {number} */ namespace.prop = 3;" + |
| 6126 |
"(function(ns) {" + |
| 6127 |
" ns.prop = true;" + |
| 6128 |
"})(namespace);", |
| 6129 |
"assignment to property prop of ns\n" + |
| 6130 |
"found : boolean\n" + |
| 6131 |
"required: number"); |
| 6132 |
} |
| 6133 |
|
| 6134 |
public void testIIFE2() throws Exception { |
| 6135 |
testTypes( |
| 6136 |
"/** @constructor */ function Foo() {}" + |
| 6137 |
"(function(ctor) {" + |
| 6138 |
" /** @type {boolean} */ ctor.prop = true;" + |
| 6139 |
"})(Foo);" + |
| 6140 |
"/** @return {number} */ function f() { return Foo.prop; }", |
| 6141 |
"inconsistent return type\n" + |
| 6142 |
"found : boolean\n" + |
| 6143 |
"required: number"); |
| 6144 |
} |
| 6145 |
|
| 6146 |
public void testIIFE3() throws Exception { |
| 6147 |
testTypes( |
| 6148 |
"/** @constructor */ function Foo() {}" + |
| 6149 |
"(function(ctor) {" + |
| 6150 |
" /** @type {boolean} */ ctor.prop = true;" + |
| 6151 |
"})(Foo);" + |
| 6152 |
"/** @param {number} x */ function f(x) {}" + |
| 6153 |
"f(Foo.prop);", |
| 6154 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 6155 |
"found : boolean\n" + |
| 6156 |
"required: number"); |
| 6157 |
} |
| 6158 |
|
| 6159 |
public void testIIFE4() throws Exception { |
| 6160 |
testTypes( |
| 6161 |
"/** @const */ var namespace = {};" + |
| 6162 |
"(function(ns) {" + |
| 6163 |
" /**\n" + |
| 6164 |
" * @constructor\n" + |
| 6165 |
" * @param {number} x\n" + |
| 6166 |
" */\n" + |
| 6167 |
" ns.Ctor = function(x) {};" + |
| 6168 |
"})(namespace);" + |
| 6169 |
"new namespace.Ctor(true);", |
| 6170 |
"actual parameter 1 of namespace.Ctor " + |
| 6171 |
"does not match formal parameter\n" + |
| 6172 |
"found : boolean\n" + |
| 6173 |
"required: number"); |
| 6174 |
} |
| 6175 |
|
| 6176 |
public void testIIFE5() throws Exception { |
| 6177 |
|
| 6178 |
|
| 6179 |
|
| 6180 |
testTypes( |
| 6181 |
"/** @const */ var namespace = {};" + |
| 6182 |
"(function(ns) {" + |
| 6183 |
" /**\n" + |
| 6184 |
" * @constructor\n" + |
| 6185 |
" */\n" + |
| 6186 |
" ns.Ctor = function() {};" + |
| 6187 |
" /** @type {boolean} */ ns.Ctor.prototype.bar = true;" + |
| 6188 |
"})(namespace);" + |
| 6189 |
"/** @param {namespace.Ctor} x\n" + |
| 6190 |
" * @return {number} */ function f(x) { return x.bar; }", |
| 6191 |
"Bad type annotation. Unknown type namespace.Ctor"); |
| 6192 |
} |
| 6193 |
|
| 6194 |
public void testNotIIFE1() throws Exception { |
| 6195 |
testTypes( |
| 6196 |
"/** @param {number} x */ function f(x) {}" + |
| 6197 |
"/** @param {...?} x */ function g(x) {}" + |
| 6198 |
"g(function(y) { f(y); }, true);"); |
| 6199 |
} |
| 6200 |
|
| 6201 |
public void testIssue61() throws Exception { |
| 6202 |
testTypes( |
| 6203 |
"var ns = {};" + |
| 6204 |
"(function() {" + |
| 6205 |
" /** @param {string} b */" + |
| 6206 |
" ns.a = function(b) {};" + |
| 6207 |
"})();" + |
| 6208 |
"function d() {" + |
| 6209 |
" ns.a(123);" + |
| 6210 |
"}", |
| 6211 |
"actual parameter 1 of ns.a does not match formal parameter\n" + |
| 6212 |
"found : number\n" + |
| 6213 |
"required: string"); |
| 6214 |
} |
| 6215 |
|
| 6216 |
public void testIssue61b() throws Exception { |
| 6217 |
testTypes( |
| 6218 |
"var ns = {};" + |
| 6219 |
"(function() {" + |
| 6220 |
" /** @param {string} b */" + |
| 6221 |
" ns.a = function(b) {};" + |
| 6222 |
"})();" + |
| 6223 |
"ns.a(123);", |
| 6224 |
"actual parameter 1 of ns.a does not match formal parameter\n" + |
| 6225 |
"found : number\n" + |
| 6226 |
"required: string"); |
| 6227 |
} |
| 6228 |
|
| 6229 |
public void testIssue86() throws Exception { |
| 6230 |
testTypes( |
| 6231 |
"/** @interface */ function I() {}" + |
| 6232 |
"/** @return {number} */ I.prototype.get = function(){};" + |
| 6233 |
"/** @constructor \n * @implements {I} */ function F() {}" + |
| 6234 |
"/** @override */ F.prototype.get = function() { return true; };", |
| 6235 |
"inconsistent return type\n" + |
| 6236 |
"found : boolean\n" + |
| 6237 |
"required: number"); |
| 6238 |
} |
| 6239 |
|
| 6240 |
public void testIssue124() throws Exception { |
| 6241 |
testTypes( |
| 6242 |
"var t = null;" + |
| 6243 |
"function test() {" + |
| 6244 |
" if (t != null) { t = null; }" + |
| 6245 |
" t = 1;" + |
| 6246 |
"}"); |
| 6247 |
} |
| 6248 |
|
| 6249 |
public void testIssue124b() throws Exception { |
| 6250 |
testTypes( |
| 6251 |
"var t = null;" + |
| 6252 |
"function test() {" + |
| 6253 |
" if (t != null) { t = null; }" + |
| 6254 |
" t = undefined;" + |
| 6255 |
"}", |
| 6256 |
"condition always evaluates to false\n" + |
| 6257 |
"left : (null|undefined)\n" + |
| 6258 |
"right: null"); |
| 6259 |
} |
| 6260 |
|
| 6261 |
public void testIssue259() throws Exception { |
| 6262 |
testTypes( |
| 6263 |
"/** @param {number} x */ function f(x) {}" + |
| 6264 |
"/** @constructor */" + |
| 6265 |
"var Clock = function() {" + |
| 6266 |
" /** @constructor */" + |
| 6267 |
" this.Date = function() {};" + |
| 6268 |
" f(new this.Date());" + |
| 6269 |
"};", |
| 6270 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 6271 |
"found : this.Date\n" + |
| 6272 |
"required: number"); |
| 6273 |
} |
| 6274 |
|
| 6275 |
public void testIssue301() throws Exception { |
| 6276 |
testTypes( |
| 6277 |
"Array.indexOf = function() {};" + |
| 6278 |
"var s = 'hello';" + |
| 6279 |
"alert(s.toLowerCase.indexOf('1'));", |
| 6280 |
"Property indexOf never defined on String.prototype.toLowerCase"); |
| 6281 |
} |
| 6282 |
|
| 6283 |
public void testIssue368() throws Exception { |
| 6284 |
testTypes( |
| 6285 |
"/** @constructor */ function Foo(){}" + |
| 6286 |
"/**\n" + |
| 6287 |
" * @param {number} one\n" + |
| 6288 |
" * @param {string} two\n" + |
| 6289 |
" */\n" + |
| 6290 |
"Foo.prototype.add = function(one, two) {};" + |
| 6291 |
"/**\n" + |
| 6292 |
" * @constructor\n" + |
| 6293 |
" * @extends {Foo}\n" + |
| 6294 |
" */\n" + |
| 6295 |
"function Bar(){}" + |
| 6296 |
"/** @override */\n" + |
| 6297 |
"Bar.prototype.add = function(ignored) {};" + |
| 6298 |
"(new Bar()).add(1, 2);", |
| 6299 |
"actual parameter 2 of Bar.prototype.add does not match formal parameter\n" + |
| 6300 |
"found : number\n" + |
| 6301 |
"required: string"); |
| 6302 |
} |
| 6303 |
|
| 6304 |
public void testIssue380() throws Exception { |
| 6305 |
testTypes( |
| 6306 |
"/** @type { function(string): {innerHTML: string} } */\n" + |
| 6307 |
"document.getElementById;\n" + |
| 6308 |
"var list = /** @type {!Array.<string>} */ ['hello', 'you'];\n" + |
| 6309 |
"list.push('?');\n" + |
| 6310 |
"document.getElementById('node').innerHTML = list.toString();", |
| 6311 |
|
| 6312 |
"Type annotations are not allowed here. " + |
| 6313 |
"Are you missing parentheses?"); |
| 6314 |
} |
| 6315 |
|
| 6316 |
public void testIssue483() throws Exception { |
| 6317 |
testTypes( |
| 6318 |
"/** @constructor */ function C() {" + |
| 6319 |
" /** @type {?Array} */ this.a = [];" + |
| 6320 |
"}" + |
| 6321 |
"C.prototype.f = function() {" + |
| 6322 |
" if (this.a.length > 0) {" + |
| 6323 |
" g(this.a);" + |
| 6324 |
" }" + |
| 6325 |
"};" + |
| 6326 |
"/** @param {number} a */ function g(a) {}", |
| 6327 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 6328 |
"found : Array\n" + |
| 6329 |
"required: number"); |
| 6330 |
} |
| 6331 |
|
| 6332 |
public void testIssue537a() throws Exception { |
| 6333 |
testTypes( |
| 6334 |
"/** @constructor */ function Foo() {}" + |
| 6335 |
"Foo.prototype = {method: function() {}};" + |
| 6336 |
"/**\n" + |
| 6337 |
" * @constructor\n" + |
| 6338 |
" * @extends {Foo}\n" + |
| 6339 |
" */\n" + |
| 6340 |
"function Bar() {" + |
| 6341 |
" Foo.call(this);" + |
| 6342 |
" if (this.baz()) this.method(1);" + |
| 6343 |
"}" + |
| 6344 |
"Bar.prototype = {" + |
| 6345 |
" baz: function() {" + |
| 6346 |
" return true;" + |
| 6347 |
" }" + |
| 6348 |
"};" + |
| 6349 |
"Bar.prototype.__proto__ = Foo.prototype;", |
| 6350 |
"Function Foo.prototype.method: called with 1 argument(s). " + |
| 6351 |
"Function requires at least 0 argument(s) " + |
| 6352 |
"and no more than 0 argument(s)."); |
| 6353 |
} |
| 6354 |
|
| 6355 |
public void testIssue537b() throws Exception { |
| 6356 |
testTypes( |
| 6357 |
"/** @constructor */ function Foo() {}" + |
| 6358 |
"Foo.prototype = {method: function() {}};" + |
| 6359 |
"/**\n" + |
| 6360 |
" * @constructor\n" + |
| 6361 |
" * @extends {Foo}\n" + |
| 6362 |
" */\n" + |
| 6363 |
"function Bar() {" + |
| 6364 |
" Foo.call(this);" + |
| 6365 |
" if (this.baz(1)) this.method();" + |
| 6366 |
"}" + |
| 6367 |
"Bar.prototype = {" + |
| 6368 |
" baz: function() {" + |
| 6369 |
" return true;" + |
| 6370 |
" }" + |
| 6371 |
"};" + |
| 6372 |
"Bar.prototype.__proto__ = Foo.prototype;", |
| 6373 |
"Function Bar.prototype.baz: called with 1 argument(s). " + |
| 6374 |
"Function requires at least 0 argument(s) " + |
| 6375 |
"and no more than 0 argument(s)."); |
| 6376 |
} |
| 6377 |
|
| 6378 |
public void testIssue537c() throws Exception { |
| 6379 |
testTypes( |
| 6380 |
"/** @constructor */ function Foo() {}" + |
| 6381 |
"/**\n" + |
| 6382 |
" * @constructor\n" + |
| 6383 |
" * @extends {Foo}\n" + |
| 6384 |
" */\n" + |
| 6385 |
"function Bar() {" + |
| 6386 |
" Foo.call(this);" + |
| 6387 |
" if (this.baz2()) alert(1);" + |
| 6388 |
"}" + |
| 6389 |
"Bar.prototype = {" + |
| 6390 |
" baz: function() {" + |
| 6391 |
" return true;" + |
| 6392 |
" }" + |
| 6393 |
"};" + |
| 6394 |
"Bar.prototype.__proto__ = Foo.prototype;", |
| 6395 |
"Property baz2 never defined on Bar"); |
| 6396 |
} |
| 6397 |
|
| 6398 |
public void testIssue537d() throws Exception { |
| 6399 |
testTypes( |
| 6400 |
"/** @constructor */ function Foo() {}" + |
| 6401 |
"Foo.prototype = {" + |
| 6402 |
" /** @return {Bar} */ x: function() { new Bar(); }," + |
| 6403 |
" /** @return {Foo} */ y: function() { new Bar(); }" + |
| 6404 |
"};" + |
| 6405 |
"/**\n" + |
| 6406 |
" * @constructor\n" + |
| 6407 |
" * @extends {Foo}\n" + |
| 6408 |
" */\n" + |
| 6409 |
"function Bar() {" + |
| 6410 |
" this.xy = 3;" + |
| 6411 |
"}" + |
| 6412 |
"/** @return {Bar} */ function f() { return new Bar(); }" + |
| 6413 |
"/** @return {Foo} */ function g() { return new Bar(); }" + |
| 6414 |
"Bar.prototype = {" + |
| 6415 |
" /** @return {Bar} */ x: function() { new Bar(); }," + |
| 6416 |
" /** @return {Foo} */ y: function() { new Bar(); }" + |
| 6417 |
"};" + |
| 6418 |
"Bar.prototype.__proto__ = Foo.prototype;"); |
| 6419 |
} |
| 6420 |
|
| 6421 |
public void testIssue586() throws Exception { |
| 6422 |
testTypes( |
| 6423 |
"/** @constructor */" + |
| 6424 |
"var MyClass = function() {};" + |
| 6425 |
"/** @param {boolean} success */" + |
| 6426 |
"MyClass.prototype.fn = function(success) {};" + |
| 6427 |
"MyClass.prototype.test = function() {" + |
| 6428 |
" this.fn();" + |
| 6429 |
" this.fn = function() {};" + |
| 6430 |
"};", |
| 6431 |
"Function MyClass.prototype.fn: called with 0 argument(s). " + |
| 6432 |
"Function requires at least 1 argument(s) " + |
| 6433 |
"and no more than 1 argument(s)."); |
| 6434 |
} |
| 6435 |
|
| 6436 |
public void testIssue635() throws Exception { |
| 6437 |
|
| 6438 |
testTypes( |
| 6439 |
"/** @constructor */" + |
| 6440 |
"function F() {}" + |
| 6441 |
"F.prototype.bar = function() { this.baz(); };" + |
| 6442 |
"F.prototype.baz = function() {};" + |
| 6443 |
"/** @constructor */" + |
| 6444 |
"function G() {}" + |
| 6445 |
"G.prototype.bar = F.prototype.bar;"); |
| 6446 |
} |
| 6447 |
|
| 6448 |
public void testIssue635b() throws Exception { |
| 6449 |
testTypes( |
| 6450 |
"/** @constructor */" + |
| 6451 |
"function F() {}" + |
| 6452 |
"/** @constructor */" + |
| 6453 |
"function G() {}" + |
| 6454 |
"/** @type {function(new:G)} */ var x = F;", |
| 6455 |
"initializing variable\n" + |
| 6456 |
"found : function (new:F): undefined\n" + |
| 6457 |
"required: function (new:G): ?"); |
| 6458 |
} |
| 6459 |
|
| 6460 |
public void testIssue669() throws Exception { |
| 6461 |
testTypes( |
| 6462 |
"/** @return {{prop1: (Object|undefined)}} */" + |
| 6463 |
"function f(a) {" + |
| 6464 |
" var results;" + |
| 6465 |
" if (a) {" + |
| 6466 |
" results = {};" + |
| 6467 |
" results.prop1 = {a: 3};" + |
| 6468 |
" } else {" + |
| 6469 |
" results = {prop2: 3};" + |
| 6470 |
" }" + |
| 6471 |
" return results;" + |
| 6472 |
"}"); |
| 6473 |
} |
| 6474 |
|
| 6475 |
public void testIssue688() throws Exception { |
| 6476 |
testTypes( |
| 6477 |
"/** @const */ var SOME_DEFAULT =\n" + |
| 6478 |
" /** @type {TwoNumbers} */ ({first: 1, second: 2});\n" + |
| 6479 |
"/**\n" + |
| 6480 |
"* Class defining an interface with two numbers.\n" + |
| 6481 |
"* @interface\n" + |
| 6482 |
"*/\n" + |
| 6483 |
"function TwoNumbers() {}\n" + |
| 6484 |
"/** @type number */\n" + |
| 6485 |
"TwoNumbers.prototype.first;\n" + |
| 6486 |
"/** @type number */\n" + |
| 6487 |
"TwoNumbers.prototype.second;\n" + |
| 6488 |
"/** @return {number} */ function f() { return SOME_DEFAULT; }", |
| 6489 |
"inconsistent return type\n" + |
| 6490 |
"found : (TwoNumbers|null)\n" + |
| 6491 |
"required: number"); |
| 6492 |
} |
| 6493 |
|
| 6494 |
public void testIssue700() throws Exception { |
| 6495 |
testTypes( |
| 6496 |
"/**\n" + |
| 6497 |
" * @param {{text: string}} opt_data\n" + |
| 6498 |
" * @return {string}\n" + |
| 6499 |
" */\n" + |
| 6500 |
"function temp1(opt_data) {\n" + |
| 6501 |
" return opt_data.text;\n" + |
| 6502 |
"}\n" + |
| 6503 |
"\n" + |
| 6504 |
"/**\n" + |
| 6505 |
" * @param {{activity: (boolean|number|string|null|Object)}} opt_data\n" + |
| 6506 |
" * @return {string}\n" + |
| 6507 |
" */\n" + |
| 6508 |
"function temp2(opt_data) {\n" + |
| 6509 |
" /** @notypecheck */\n" + |
| 6510 |
" function __inner() {\n" + |
| 6511 |
" return temp1(opt_data.activity);\n" + |
| 6512 |
" }\n" + |
| 6513 |
" return __inner();\n" + |
| 6514 |
"}\n" + |
| 6515 |
"\n" + |
| 6516 |
"/**\n" + |
| 6517 |
" * @param {{n: number, text: string, b: boolean}} opt_data\n" + |
| 6518 |
" * @return {string}\n" + |
| 6519 |
" */\n" + |
| 6520 |
"function temp3(opt_data) {\n" + |
| 6521 |
" return 'n: ' + opt_data.n + ', t: ' + opt_data.text + '.';\n" + |
| 6522 |
"}\n" + |
| 6523 |
"\n" + |
| 6524 |
"function callee() {\n" + |
| 6525 |
" var output = temp3({\n" + |
| 6526 |
" n: 0,\n" + |
| 6527 |
" text: 'a string',\n" + |
| 6528 |
" b: true\n" + |
| 6529 |
" })\n" + |
| 6530 |
" alert(output);\n" + |
| 6531 |
"}\n" + |
| 6532 |
"\n" + |
| 6533 |
"callee();"); |
| 6534 |
} |
| 6535 |
|
| 6536 |
public void testIssue725() throws Exception { |
| 6537 |
testTypes( |
| 6538 |
"/** @typedef {{name: string}} */ var RecordType1;" + |
| 6539 |
"/** @typedef {{name2: string}} */ var RecordType2;" + |
| 6540 |
"/** @param {RecordType1} rec */ function f(rec) {" + |
| 6541 |
" alert(rec.name2);" + |
| 6542 |
"}", |
| 6543 |
"Property name2 never defined on rec"); |
| 6544 |
} |
| 6545 |
|
| 6546 |
public void testIssue726() throws Exception { |
| 6547 |
testTypes( |
| 6548 |
"/** @constructor */ function Foo() {}" + |
| 6549 |
"/** @param {number} x */ Foo.prototype.bar = function(x) {};" + |
| 6550 |
"/** @return {!Function} */ " + |
| 6551 |
"Foo.prototype.getDeferredBar = function() { " + |
| 6552 |
" var self = this;" + |
| 6553 |
" return function() {" + |
| 6554 |
" self.bar(true);" + |
| 6555 |
" };" + |
| 6556 |
"};", |
| 6557 |
"actual parameter 1 of Foo.prototype.bar does not match formal parameter\n" + |
| 6558 |
"found : boolean\n" + |
| 6559 |
"required: number"); |
| 6560 |
} |
| 6561 |
|
| 6562 |
public void testIssue765() throws Exception { |
| 6563 |
testTypes( |
| 6564 |
"/** @constructor */" + |
| 6565 |
"var AnotherType = function (parent) {" + |
| 6566 |
" /** @param {string} stringParameter Description... */" + |
| 6567 |
" this.doSomething = function (stringParameter) {};" + |
| 6568 |
"};" + |
| 6569 |
"/** @constructor */" + |
| 6570 |
"var YetAnotherType = function () {" + |
| 6571 |
" this.field = new AnotherType(self);" + |
| 6572 |
" this.testfun=function(stringdata) {" + |
| 6573 |
" this.field.doSomething(null);" + |
| 6574 |
" };" + |
| 6575 |
"};", |
| 6576 |
"actual parameter 1 of AnotherType.doSomething " + |
| 6577 |
"does not match formal parameter\n" + |
| 6578 |
"found : null\n" + |
| 6579 |
"required: string"); |
| 6580 |
} |
| 6581 |
|
| 6582 |
public void testIssue783() throws Exception { |
| 6583 |
testTypes( |
| 6584 |
"/** @constructor */" + |
| 6585 |
"var Type = function () {" + |
| 6586 |
" /** @type {Type} */" + |
| 6587 |
" this.me_ = this;" + |
| 6588 |
"};" + |
| 6589 |
"Type.prototype.doIt = function() {" + |
| 6590 |
" var me = this.me_;" + |
| 6591 |
" for (var i = 0; i < me.unknownProp; i++) {}" + |
| 6592 |
"};", |
| 6593 |
"Property unknownProp never defined on Type"); |
| 6594 |
} |
| 6595 |
|
| 6596 |
public void testIssue791() throws Exception { |
| 6597 |
testTypes( |
| 6598 |
"/** @param {{func: function()}} obj */" + |
| 6599 |
"function test1(obj) {}" + |
| 6600 |
"var fnStruc1 = {};" + |
| 6601 |
"fnStruc1.func = function() {};" + |
| 6602 |
"test1(fnStruc1);"); |
| 6603 |
} |
| 6604 |
|
| 6605 |
public void testIssue810() throws Exception { |
| 6606 |
testTypes( |
| 6607 |
"/** @constructor */" + |
| 6608 |
"var Type = function () {" + |
| 6609 |
"};" + |
| 6610 |
"Type.prototype.doIt = function(obj) {" + |
| 6611 |
" this.prop = obj.unknownProp;" + |
| 6612 |
"};", |
| 6613 |
"Property unknownProp never defined on obj"); |
| 6614 |
} |
| 6615 |
|
| 6616 |
|
| 6617 |
|
| 6618 |
|
| 6619 |
|
| 6620 |
|
| 6621 |
public void testBug592170() throws Exception { |
| 6622 |
testTypes( |
| 6623 |
"/** @param {Function} opt_f ... */" + |
| 6624 |
"function foo(opt_f) {" + |
| 6625 |
" /** @type {Function} */" + |
| 6626 |
" return opt_f || function () {};" + |
| 6627 |
"}", |
| 6628 |
"Type annotations are not allowed here. Are you missing parentheses?"); |
| 6629 |
} |
| 6630 |
|
| 6631 |
|
| 6632 |
|
| 6633 |
|
| 6634 |
|
| 6635 |
|
| 6636 |
public void testBug901455() throws Exception { |
| 6637 |
testTypes("/** @return {(number,undefined)} */ function a() { return 3; }" + |
| 6638 |
"var b = undefined === a()"); |
| 6639 |
testTypes("/** @return {(number,undefined)} */ function a() { return 3; }" + |
| 6640 |
"var b = a() === undefined"); |
| 6641 |
} |
| 6642 |
|
| 6643 |
|
| 6644 |
|
| 6645 |
|
| 6646 |
public void testBug908701() throws Exception { |
| 6647 |
testTypes("/** @type {String} */var s = new String('foo');" + |
| 6648 |
"var b = s.match(/a/) != null;"); |
| 6649 |
} |
| 6650 |
|
| 6651 |
|
| 6652 |
|
| 6653 |
|
| 6654 |
public void testBug908625() throws Exception { |
| 6655 |
testTypes("/** @constructor */function A(){}" + |
| 6656 |
"/** @constructor\n * @extends A */function B(){}" + |
| 6657 |
"/** @param {B} b" + |
| 6658 |
"\n @return {(A,undefined)} */function foo(b){return b}"); |
| 6659 |
} |
| 6660 |
|
| 6661 |
|
| 6662 |
|
| 6663 |
|
| 6664 |
|
| 6665 |
public void testBug911118() throws Exception { |
| 6666 |
|
| 6667 |
Scope s = parseAndTypeCheckWithScope("var a = function(){};").scope; |
| 6668 |
JSType type = s.getVar("a").getType(); |
| 6669 |
assertEquals("function (): undefined", type.toString()); |
| 6670 |
|
| 6671 |
|
| 6672 |
testTypes("function nullFunction() {};" + |
| 6673 |
"var foo = nullFunction;" + |
| 6674 |
"foo = function() {};" + |
| 6675 |
"foo();"); |
| 6676 |
} |
| 6677 |
|
| 6678 |
public void testBug909000() throws Exception { |
| 6679 |
testTypes("/** @constructor */function A(){}\n" + |
| 6680 |
"/** @param {!A} a\n" + |
| 6681 |
"@return {boolean}*/\n" + |
| 6682 |
"function y(a) { return a }", |
| 6683 |
"inconsistent return type\n" + |
| 6684 |
"found : A\n" + |
| 6685 |
"required: boolean"); |
| 6686 |
} |
| 6687 |
|
| 6688 |
public void testBug930117() throws Exception { |
| 6689 |
testTypes( |
| 6690 |
"/** @param {boolean} x */function f(x){}" + |
| 6691 |
"f(null);", |
| 6692 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 6693 |
"found : null\n" + |
| 6694 |
"required: boolean"); |
| 6695 |
} |
| 6696 |
|
| 6697 |
public void testBug1484445() throws Exception { |
| 6698 |
testTypes( |
| 6699 |
"/** @constructor */ function Foo() {}" + |
| 6700 |
"/** @type {number?} */ Foo.prototype.bar = null;" + |
| 6701 |
"/** @type {number?} */ Foo.prototype.baz = null;" + |
| 6702 |
"/** @param {Foo} foo */" + |
| 6703 |
"function f(foo) {" + |
| 6704 |
" while (true) {" + |
| 6705 |
" if (foo.bar == null && foo.baz == null) {" + |
| 6706 |
" foo.bar;" + |
| 6707 |
" }" + |
| 6708 |
" }" + |
| 6709 |
"}"); |
| 6710 |
} |
| 6711 |
|
| 6712 |
public void testBug1859535() throws Exception { |
| 6713 |
testTypes( |
| 6714 |
"/**\n" + |
| 6715 |
" * @param {Function} childCtor Child class.\n" + |
| 6716 |
" * @param {Function} parentCtor Parent class.\n" + |
| 6717 |
" */" + |
| 6718 |
"var inherits = function(childCtor, parentCtor) {" + |
| 6719 |
" /** @constructor */" + |
| 6720 |
" function tempCtor() {};" + |
| 6721 |
" tempCtor.prototype = parentCtor.prototype;" + |
| 6722 |
" childCtor.superClass_ = parentCtor.prototype;" + |
| 6723 |
" childCtor.prototype = new tempCtor();" + |
| 6724 |
" /** @override */ childCtor.prototype.constructor = childCtor;" + |
| 6725 |
"};" + |
| 6726 |
"/**" + |
| 6727 |
" * @param {Function} constructor\n" + |
| 6728 |
" * @param {Object} var_args\n" + |
| 6729 |
" * @return {Object}\n" + |
| 6730 |
" */" + |
| 6731 |
"var factory = function(constructor, var_args) {" + |
| 6732 |
" /** @constructor */" + |
| 6733 |
" var tempCtor = function() {};" + |
| 6734 |
" tempCtor.prototype = constructor.prototype;" + |
| 6735 |
" var obj = new tempCtor();" + |
| 6736 |
" constructor.apply(obj, arguments);" + |
| 6737 |
" return obj;" + |
| 6738 |
"};"); |
| 6739 |
} |
| 6740 |
|
| 6741 |
public void testBug1940591() throws Exception { |
| 6742 |
testTypes( |
| 6743 |
"/** @type {Object} */" + |
| 6744 |
"var a = {};\n" + |
| 6745 |
"/** @type {number} */\n" + |
| 6746 |
"a.name = 0;\n" + |
| 6747 |
"/**\n" + |
| 6748 |
" * @param {Function} x anything.\n" + |
| 6749 |
" */\n" + |
| 6750 |
"a.g = function(x) { x.name = 'a'; }"); |
| 6751 |
} |
| 6752 |
|
| 6753 |
public void testBug1942972() throws Exception { |
| 6754 |
testTypes( |
| 6755 |
"var google = {\n" + |
| 6756 |
" gears: {\n" + |
| 6757 |
" factory: {},\n" + |
| 6758 |
" workerPool: {}\n" + |
| 6759 |
" }\n" + |
| 6760 |
"};\n" + |
| 6761 |
"\n" + |
| 6762 |
"google.gears = {factory: {}};\n"); |
| 6763 |
} |
| 6764 |
|
| 6765 |
public void testBug1943776() throws Exception { |
| 6766 |
testTypes( |
| 6767 |
"/** @return {{foo: Array}} */" + |
| 6768 |
"function bar() {" + |
| 6769 |
" return {foo: []};" + |
| 6770 |
"}"); |
| 6771 |
} |
| 6772 |
|
| 6773 |
public void testBug1987544() throws Exception { |
| 6774 |
testTypes( |
| 6775 |
"/** @param {string} x */ function foo(x) {}" + |
| 6776 |
"var duration;" + |
| 6777 |
"if (true && !(duration = 3)) {" + |
| 6778 |
" foo(duration);" + |
| 6779 |
"}", |
| 6780 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 6781 |
"found : number\n" + |
| 6782 |
"required: string"); |
| 6783 |
} |
| 6784 |
|
| 6785 |
public void testBug1940769() throws Exception { |
| 6786 |
testTypes( |
| 6787 |
"/** @return {!Object} */ " + |
| 6788 |
"function proto(obj) { return obj.prototype; }" + |
| 6789 |
"/** @constructor */ function Map() {}" + |
| 6790 |
"/**\n" + |
| 6791 |
" * @constructor\n" + |
| 6792 |
" * @extends {Map}\n" + |
| 6793 |
" */" + |
| 6794 |
"function Map2() { Map.call(this); };" + |
| 6795 |
"Map2.prototype = proto(Map);"); |
| 6796 |
} |
| 6797 |
|
| 6798 |
public void testBug2335992() throws Exception { |
| 6799 |
testTypes( |
| 6800 |
"/** @return {*} */ function f() { return 3; }" + |
| 6801 |
"var x = f();" + |
| 6802 |
"/** @type {string} */" + |
| 6803 |
"x.y = 3;", |
| 6804 |
"assignment\n" + |
| 6805 |
"found : number\n" + |
| 6806 |
"required: string"); |
| 6807 |
} |
| 6808 |
|
| 6809 |
public void testBug2341812() throws Exception { |
| 6810 |
testTypes( |
| 6811 |
"/** @interface */" + |
| 6812 |
"function EventTarget() {}" + |
| 6813 |
"/** @constructor \n * @implements {EventTarget} */" + |
| 6814 |
"function Node() {}" + |
| 6815 |
"/** @type {number} */ Node.prototype.index;" + |
| 6816 |
"/** @param {EventTarget} x \n * @return {string} */" + |
| 6817 |
"function foo(x) { return x.index; }"); |
| 6818 |
} |
| 6819 |
|
| 6820 |
public void testBug7701884() throws Exception { |
| 6821 |
testTypes( |
| 6822 |
"/**\n" + |
| 6823 |
" * @param {Array.<T>} x\n" + |
| 6824 |
" * @param {function(T)} y\n" + |
| 6825 |
" * @template T\n" + |
| 6826 |
" */\n" + |
| 6827 |
"var forEach = function(x, y) {\n" + |
| 6828 |
" for (var i = 0; i < x.length; i++) y(x[i]);\n" + |
| 6829 |
"};" + |
| 6830 |
"/** @param {number} x */" + |
| 6831 |
"function f(x) {}" + |
| 6832 |
"/** @param {?} x */" + |
| 6833 |
"function h(x) {" + |
| 6834 |
" var top = null;" + |
| 6835 |
" forEach(x, function(z) { top = z; });" + |
| 6836 |
" if (top) f(top);" + |
| 6837 |
"}"); |
| 6838 |
} |
| 6839 |
|
| 6840 |
public void testBug8017789() throws Exception { |
| 6841 |
testTypes( |
| 6842 |
"/** @param {(map|function())} isResult */" + |
| 6843 |
"var f = function(isResult) {" + |
| 6844 |
" while (true)" + |
| 6845 |
" isResult['t'];" + |
| 6846 |
"};" + |
| 6847 |
"/** @typedef {Object.<string, number>} */" + |
| 6848 |
"var map;"); |
| 6849 |
} |
| 6850 |
|
| 6851 |
public void testTypedefBeforeUse() throws Exception { |
| 6852 |
testTypes( |
| 6853 |
"/** @typedef {Object.<string, number>} */" + |
| 6854 |
"var map;" + |
| 6855 |
"/** @param {(map|function())} isResult */" + |
| 6856 |
"var f = function(isResult) {" + |
| 6857 |
" while (true)" + |
| 6858 |
" isResult['t'];" + |
| 6859 |
"};"); |
| 6860 |
} |
| 6861 |
|
| 6862 |
public void testScopedConstructors1() throws Exception { |
| 6863 |
testTypes( |
| 6864 |
"function foo1() { " + |
| 6865 |
" /** @constructor */ function Bar() { " + |
| 6866 |
" /** @type {number} */ this.x = 3;" + |
| 6867 |
" }" + |
| 6868 |
"}" + |
| 6869 |
"function foo2() { " + |
| 6870 |
" /** @constructor */ function Bar() { " + |
| 6871 |
" /** @type {string} */ this.x = 'y';" + |
| 6872 |
" }" + |
| 6873 |
" /** " + |
| 6874 |
" * @param {Bar} b\n" + |
| 6875 |
" * @return {number}\n" + |
| 6876 |
" */" + |
| 6877 |
" function baz(b) { return b.x; }" + |
| 6878 |
"}", |
| 6879 |
"inconsistent return type\n" + |
| 6880 |
"found : string\n" + |
| 6881 |
"required: number"); |
| 6882 |
} |
| 6883 |
|
| 6884 |
public void testScopedConstructors2() throws Exception { |
| 6885 |
testTypes( |
| 6886 |
"/** @param {Function} f */" + |
| 6887 |
"function foo1(f) {" + |
| 6888 |
" /** @param {Function} g */" + |
| 6889 |
" f.prototype.bar = function(g) {};" + |
| 6890 |
"}"); |
| 6891 |
} |
| 6892 |
|
| 6893 |
public void testQualifiedNameInference1() throws Exception { |
| 6894 |
testTypes( |
| 6895 |
"/** @constructor */ function Foo() {}" + |
| 6896 |
"/** @type {number?} */ Foo.prototype.bar = null;" + |
| 6897 |
"/** @type {number?} */ Foo.prototype.baz = null;" + |
| 6898 |
"/** @param {Foo} foo */" + |
| 6899 |
"function f(foo) {" + |
| 6900 |
" while (true) {" + |
| 6901 |
" if (!foo.baz) break; " + |
| 6902 |
" foo.bar = null;" + |
| 6903 |
" }" + |
| 6904 |
|
| 6905 |
" return foo.bar == null;" + |
| 6906 |
"}"); |
| 6907 |
} |
| 6908 |
|
| 6909 |
public void testQualifiedNameInference2() throws Exception { |
| 6910 |
testTypes( |
| 6911 |
"var x = {};" + |
| 6912 |
"x.y = c;" + |
| 6913 |
"function f(a, b) {" + |
| 6914 |
" if (a) {" + |
| 6915 |
" if (b) " + |
| 6916 |
" x.y = 2;" + |
| 6917 |
" else " + |
| 6918 |
" x.y = 1;" + |
| 6919 |
" }" + |
| 6920 |
" return x.y == null;" + |
| 6921 |
"}"); |
| 6922 |
} |
| 6923 |
|
| 6924 |
public void testQualifiedNameInference3() throws Exception { |
| 6925 |
testTypes( |
| 6926 |
"var x = {};" + |
| 6927 |
"x.y = c;" + |
| 6928 |
"function f(a, b) {" + |
| 6929 |
" if (a) {" + |
| 6930 |
" if (b) " + |
| 6931 |
" x.y = 2;" + |
| 6932 |
" else " + |
| 6933 |
" x.y = 1;" + |
| 6934 |
" }" + |
| 6935 |
" return x.y == null;" + |
| 6936 |
"} function g() { x.y = null; }"); |
| 6937 |
} |
| 6938 |
|
| 6939 |
public void testQualifiedNameInference4() throws Exception { |
| 6940 |
testTypes( |
| 6941 |
"/** @param {string} x */ function f(x) {}\n" + |
| 6942 |
"/**\n" + |
| 6943 |
" * @param {?string} x \n" + |
| 6944 |
" * @constructor\n" + |
| 6945 |
" */" + |
| 6946 |
"function Foo(x) { this.x_ = x; }\n" + |
| 6947 |
"Foo.prototype.bar = function() {" + |
| 6948 |
" if (this.x_) { f(this.x_); }" + |
| 6949 |
"};"); |
| 6950 |
} |
| 6951 |
|
| 6952 |
public void testQualifiedNameInference5() throws Exception { |
| 6953 |
testTypes( |
| 6954 |
"var ns = {}; " + |
| 6955 |
"(function() { " + |
| 6956 |
" /** @param {number} x */ ns.foo = function(x) {}; })();" + |
| 6957 |
"(function() { ns.foo(true); })();", |
| 6958 |
"actual parameter 1 of ns.foo does not match formal parameter\n" + |
| 6959 |
"found : boolean\n" + |
| 6960 |
"required: number"); |
| 6961 |
} |
| 6962 |
|
| 6963 |
public void testQualifiedNameInference6() throws Exception { |
| 6964 |
testTypes( |
| 6965 |
"/** @const */ var ns = {}; " + |
| 6966 |
"/** @param {number} x */ ns.foo = function(x) {};" + |
| 6967 |
"(function() { " + |
| 6968 |
" ns.foo = function(x) {};" + |
| 6969 |
" ns.foo(true); " + |
| 6970 |
"})();", |
| 6971 |
"actual parameter 1 of ns.foo does not match formal parameter\n" + |
| 6972 |
"found : boolean\n" + |
| 6973 |
"required: number"); |
| 6974 |
} |
| 6975 |
|
| 6976 |
public void testQualifiedNameInference7() throws Exception { |
| 6977 |
testTypes( |
| 6978 |
"var ns = {}; " + |
| 6979 |
"(function() { " + |
| 6980 |
" /** @constructor \n * @param {number} x */ " + |
| 6981 |
" ns.Foo = function(x) {};" + |
| 6982 |
" /** @param {ns.Foo} x */ function f(x) {}" + |
| 6983 |
" f(new ns.Foo(true));" + |
| 6984 |
"})();", |
| 6985 |
"actual parameter 1 of ns.Foo does not match formal parameter\n" + |
| 6986 |
"found : boolean\n" + |
| 6987 |
"required: number"); |
| 6988 |
} |
| 6989 |
|
| 6990 |
public void testQualifiedNameInference8() throws Exception { |
| 6991 |
|
| 6992 |
|
| 6993 |
testClosureTypesMultipleWarnings( |
| 6994 |
"var ns = {}; " + |
| 6995 |
"(function() { " + |
| 6996 |
" /** @constructor \n * @param {number} x */ " + |
| 6997 |
" ns.Foo = function(x) {};" + |
| 6998 |
"})();" + |
| 6999 |
"/** @param {ns.Foo} x */ function f(x) {}" + |
| 7000 |
"f(new ns.Foo(true));", |
| 7001 |
Lists.newArrayList( |
| 7002 |
"Bad type annotation. Unknown type ns.Foo", |
| 7003 |
"actual parameter 1 of ns.Foo does not match formal parameter\n" + |
| 7004 |
"found : boolean\n" + |
| 7005 |
"required: number")); |
| 7006 |
} |
| 7007 |
|
| 7008 |
public void testQualifiedNameInference9() throws Exception { |
| 7009 |
testTypes( |
| 7010 |
"var ns = {}; " + |
| 7011 |
"ns.ns2 = {}; " + |
| 7012 |
"(function() { " + |
| 7013 |
" /** @constructor \n * @param {number} x */ " + |
| 7014 |
" ns.ns2.Foo = function(x) {};" + |
| 7015 |
" /** @param {ns.ns2.Foo} x */ function f(x) {}" + |
| 7016 |
" f(new ns.ns2.Foo(true));" + |
| 7017 |
"})();", |
| 7018 |
"actual parameter 1 of ns.ns2.Foo does not match formal parameter\n" + |
| 7019 |
"found : boolean\n" + |
| 7020 |
"required: number"); |
| 7021 |
} |
| 7022 |
|
| 7023 |
public void testQualifiedNameInference10() throws Exception { |
| 7024 |
testTypes( |
| 7025 |
"var ns = {}; " + |
| 7026 |
"ns.ns2 = {}; " + |
| 7027 |
"(function() { " + |
| 7028 |
" /** @interface */ " + |
| 7029 |
" ns.ns2.Foo = function() {};" + |
| 7030 |
" /** @constructor \n * @implements {ns.ns2.Foo} */ " + |
| 7031 |
" function F() {}" + |
| 7032 |
" (new F());" + |
| 7033 |
"})();"); |
| 7034 |
} |
| 7035 |
|
| 7036 |
public void testQualifiedNameInference11() throws Exception { |
| 7037 |
testTypes( |
| 7038 |
"/** @constructor */ function Foo() {}" + |
| 7039 |
"function f() {" + |
| 7040 |
" var x = new Foo();" + |
| 7041 |
" x.onload = function() {" + |
| 7042 |
" x.onload = null;" + |
| 7043 |
" };" + |
| 7044 |
"}"); |
| 7045 |
} |
| 7046 |
|
| 7047 |
public void testQualifiedNameInference12() throws Exception { |
| 7048 |
|
| 7049 |
|
| 7050 |
testTypes( |
| 7051 |
"/** @param {function(this:Object)} x */ function f(x) {}" + |
| 7052 |
"/** @constructor */ function Foo() {" + |
| 7053 |
" /** @type {number} */ this.bar = 3;" + |
| 7054 |
" f(function() { this.bar = true; });" + |
| 7055 |
"}"); |
| 7056 |
} |
| 7057 |
|
| 7058 |
public void testQualifiedNameInference13() throws Exception { |
| 7059 |
testTypes( |
| 7060 |
"/** @constructor */ function Foo() {}" + |
| 7061 |
"function f(z) {" + |
| 7062 |
" var x = new Foo();" + |
| 7063 |
" if (z) {" + |
| 7064 |
" x.onload = function() {};" + |
| 7065 |
" } else {" + |
| 7066 |
" x.onload = null;" + |
| 7067 |
" };" + |
| 7068 |
"}"); |
| 7069 |
} |
| 7070 |
|
| 7071 |
public void testSheqRefinedScope() throws Exception { |
| 7072 |
Node n = parseAndTypeCheck( |
| 7073 |
"/** @constructor */function A() {}\n" + |
| 7074 |
"/** @constructor \n @extends A */ function B() {}\n" + |
| 7075 |
"/** @return {number} */\n" + |
| 7076 |
"B.prototype.p = function() { return 1; }\n" + |
| 7077 |
"/** @param {A} a\n @param {B} b */\n" + |
| 7078 |
"function f(a, b) {\n" + |
| 7079 |
" b.p();\n" + |
| 7080 |
" if (a === b) {\n" + |
| 7081 |
" b.p();\n" + |
| 7082 |
" }\n" + |
| 7083 |
"}"); |
| 7084 |
Node nodeC = n.getLastChild().getLastChild().getLastChild().getLastChild() |
| 7085 |
.getLastChild().getLastChild(); |
| 7086 |
JSType typeC = nodeC.getJSType(); |
| 7087 |
assertTrue(typeC.isNumber()); |
| 7088 |
|
| 7089 |
Node nodeB = nodeC.getFirstChild().getFirstChild(); |
| 7090 |
JSType typeB = nodeB.getJSType(); |
| 7091 |
assertEquals("B", typeB.toString()); |
| 7092 |
} |
| 7093 |
|
| 7094 |
public void testAssignToUntypedVariable() throws Exception { |
| 7095 |
Node n = parseAndTypeCheck("var z; z = 1;"); |
| 7096 |
|
| 7097 |
Node assign = n.getLastChild().getFirstChild(); |
| 7098 |
Node node = assign.getFirstChild(); |
| 7099 |
assertFalse(node.getJSType().isUnknownType()); |
| 7100 |
assertEquals("number", node.getJSType().toString()); |
| 7101 |
} |
| 7102 |
|
| 7103 |
public void testAssignToUntypedProperty() throws Exception { |
| 7104 |
Node n = parseAndTypeCheck( |
| 7105 |
"/** @constructor */ function Foo() {}\n" + |
| 7106 |
"Foo.prototype.a = 1;" + |
| 7107 |
"(new Foo).a;"); |
| 7108 |
|
| 7109 |
Node node = n.getLastChild().getFirstChild(); |
| 7110 |
assertFalse(node.getJSType().isUnknownType()); |
| 7111 |
assertTrue(node.getJSType().isNumber()); |
| 7112 |
} |
| 7113 |
|
| 7114 |
public void testNew1() throws Exception { |
| 7115 |
testTypes("new 4", TypeCheck.NOT_A_CONSTRUCTOR); |
| 7116 |
} |
| 7117 |
|
| 7118 |
public void testNew2() throws Exception { |
| 7119 |
testTypes("var Math = {}; new Math()", TypeCheck.NOT_A_CONSTRUCTOR); |
| 7120 |
} |
| 7121 |
|
| 7122 |
public void testNew3() throws Exception { |
| 7123 |
testTypes("new Date()"); |
| 7124 |
} |
| 7125 |
|
| 7126 |
public void testNew4() throws Exception { |
| 7127 |
testTypes("/** @constructor */function A(){}; new A();"); |
| 7128 |
} |
| 7129 |
|
| 7130 |
public void testNew5() throws Exception { |
| 7131 |
testTypes("function A(){}; new A();", TypeCheck.NOT_A_CONSTRUCTOR); |
| 7132 |
} |
| 7133 |
|
| 7134 |
public void testNew6() throws Exception { |
| 7135 |
TypeCheckResult p = |
| 7136 |
parseAndTypeCheckWithScope("/** @constructor */function A(){};" + |
| 7137 |
"var a = new A();"); |
| 7138 |
|
| 7139 |
JSType aType = p.scope.getVar("a").getType(); |
| 7140 |
assertTrue(aType instanceof ObjectType); |
| 7141 |
ObjectType aObjectType = (ObjectType) aType; |
| 7142 |
assertEquals("A", aObjectType.getConstructor().getReferenceName()); |
| 7143 |
} |
| 7144 |
|
| 7145 |
public void testNew7() throws Exception { |
| 7146 |
testTypes("/** @param {Function} opt_constructor */" + |
| 7147 |
"function foo(opt_constructor) {" + |
| 7148 |
"if (opt_constructor) { new opt_constructor; }" + |
| 7149 |
"}"); |
| 7150 |
} |
| 7151 |
|
| 7152 |
public void testNew8() throws Exception { |
| 7153 |
testTypes("/** @param {Function} opt_constructor */" + |
| 7154 |
"function foo(opt_constructor) {" + |
| 7155 |
"new opt_constructor;" + |
| 7156 |
"}"); |
| 7157 |
} |
| 7158 |
|
| 7159 |
public void testNew9() throws Exception { |
| 7160 |
testTypes("/** @param {Function} opt_constructor */" + |
| 7161 |
"function foo(opt_constructor) {" + |
| 7162 |
"new (opt_constructor || Array);" + |
| 7163 |
"}"); |
| 7164 |
} |
| 7165 |
|
| 7166 |
public void testNew10() throws Exception { |
| 7167 |
testTypes("var goog = {};" + |
| 7168 |
"/** @param {Function} opt_constructor */" + |
| 7169 |
"goog.Foo = function (opt_constructor) {" + |
| 7170 |
"new (opt_constructor || Array);" + |
| 7171 |
"}"); |
| 7172 |
} |
| 7173 |
|
| 7174 |
public void testNew11() throws Exception { |
| 7175 |
testTypes("/** @param {Function} c1 */" + |
| 7176 |
"function f(c1) {" + |
| 7177 |
" var c2 = function(){};" + |
| 7178 |
" c1.prototype = new c2;" + |
| 7179 |
"}", TypeCheck.NOT_A_CONSTRUCTOR); |
| 7180 |
} |
| 7181 |
|
| 7182 |
public void testNew12() throws Exception { |
| 7183 |
TypeCheckResult p = parseAndTypeCheckWithScope("var a = new Array();"); |
| 7184 |
Var a = p.scope.getVar("a"); |
| 7185 |
|
| 7186 |
assertTypeEquals(ARRAY_TYPE, a.getType()); |
| 7187 |
} |
| 7188 |
|
| 7189 |
public void testNew13() throws Exception { |
| 7190 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 7191 |
"/** @constructor */function FooBar(){};" + |
| 7192 |
"var a = new FooBar();"); |
| 7193 |
Var a = p.scope.getVar("a"); |
| 7194 |
|
| 7195 |
assertTrue(a.getType() instanceof ObjectType); |
| 7196 |
assertEquals("FooBar", a.getType().toString()); |
| 7197 |
} |
| 7198 |
|
| 7199 |
public void testNew14() throws Exception { |
| 7200 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 7201 |
"/** @constructor */var FooBar = function(){};" + |
| 7202 |
"var a = new FooBar();"); |
| 7203 |
Var a = p.scope.getVar("a"); |
| 7204 |
|
| 7205 |
assertTrue(a.getType() instanceof ObjectType); |
| 7206 |
assertEquals("FooBar", a.getType().toString()); |
| 7207 |
} |
| 7208 |
|
| 7209 |
public void testNew15() throws Exception { |
| 7210 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 7211 |
"var goog = {};" + |
| 7212 |
"/** @constructor */goog.A = function(){};" + |
| 7213 |
"var a = new goog.A();"); |
| 7214 |
Var a = p.scope.getVar("a"); |
| 7215 |
|
| 7216 |
assertTrue(a.getType() instanceof ObjectType); |
| 7217 |
assertEquals("goog.A", a.getType().toString()); |
| 7218 |
} |
| 7219 |
|
| 7220 |
public void testNew16() throws Exception { |
| 7221 |
testTypes( |
| 7222 |
"/** \n" + |
| 7223 |
" * @param {string} x \n" + |
| 7224 |
" * @constructor \n" + |
| 7225 |
" */" + |
| 7226 |
"function Foo(x) {}" + |
| 7227 |
"function g() { new Foo(1); }", |
| 7228 |
"actual parameter 1 of Foo does not match formal parameter\n" + |
| 7229 |
"found : number\n" + |
| 7230 |
"required: string"); |
| 7231 |
} |
| 7232 |
|
| 7233 |
public void testNew17() throws Exception { |
| 7234 |
testTypes("var goog = {}; goog.x = 3; new goog.x", |
| 7235 |
"cannot instantiate non-constructor"); |
| 7236 |
} |
| 7237 |
|
| 7238 |
public void testNew18() throws Exception { |
| 7239 |
testTypes("var goog = {};" + |
| 7240 |
"/** @constructor */ goog.F = function() {};" + |
| 7241 |
"/** @constructor */ goog.G = goog.F;"); |
| 7242 |
} |
| 7243 |
|
| 7244 |
public void testName1() throws Exception { |
| 7245 |
assertTypeEquals(VOID_TYPE, testNameNode("undefined")); |
| 7246 |
} |
| 7247 |
|
| 7248 |
public void testName2() throws Exception { |
| 7249 |
assertTypeEquals(OBJECT_FUNCTION_TYPE, testNameNode("Object")); |
| 7250 |
} |
| 7251 |
|
| 7252 |
public void testName3() throws Exception { |
| 7253 |
assertTypeEquals(ARRAY_FUNCTION_TYPE, testNameNode("Array")); |
| 7254 |
} |
| 7255 |
|
| 7256 |
public void testName4() throws Exception { |
| 7257 |
assertTypeEquals(DATE_FUNCTION_TYPE, testNameNode("Date")); |
| 7258 |
} |
| 7259 |
|
| 7260 |
public void testName5() throws Exception { |
| 7261 |
assertTypeEquals(REGEXP_FUNCTION_TYPE, testNameNode("RegExp")); |
| 7262 |
} |
| 7263 |
|
| 7264 |
|
| 7265 |
|
| 7266 |
|
| 7267 |
private JSType testNameNode(String name) { |
| 7268 |
Node node = Node.newString(Token.NAME, name); |
| 7269 |
Node parent = new Node(Token.SCRIPT, node); |
| 7270 |
parent.setInputId(new InputId("code")); |
| 7271 |
|
| 7272 |
Node externs = new Node(Token.SCRIPT); |
| 7273 |
externs.setInputId(new InputId("externs")); |
| 7274 |
|
| 7275 |
Node externAndJsRoot = new Node(Token.BLOCK, externs, parent); |
| 7276 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 7277 |
|
| 7278 |
makeTypeCheck().processForTesting(null, parent); |
| 7279 |
return node.getJSType(); |
| 7280 |
} |
| 7281 |
|
| 7282 |
public void testBitOperation1() throws Exception { |
| 7283 |
testTypes("/**@return {void}*/function foo(){ ~foo(); }", |
| 7284 |
"operator ~ cannot be applied to undefined"); |
| 7285 |
} |
| 7286 |
|
| 7287 |
public void testBitOperation2() throws Exception { |
| 7288 |
testTypes("/**@return {void}*/function foo(){var a = foo()<<3;}", |
| 7289 |
"operator << cannot be applied to undefined"); |
| 7290 |
} |
| 7291 |
|
| 7292 |
public void testBitOperation3() throws Exception { |
| 7293 |
testTypes("/**@return {void}*/function foo(){var a = 3<<foo();}", |
| 7294 |
"operator << cannot be applied to undefined"); |
| 7295 |
} |
| 7296 |
|
| 7297 |
public void testBitOperation4() throws Exception { |
| 7298 |
testTypes("/**@return {void}*/function foo(){var a = foo()>>>3;}", |
| 7299 |
"operator >>> cannot be applied to undefined"); |
| 7300 |
} |
| 7301 |
|
| 7302 |
public void testBitOperation5() throws Exception { |
| 7303 |
testTypes("/**@return {void}*/function foo(){var a = 3>>>foo();}", |
| 7304 |
"operator >>> cannot be applied to undefined"); |
| 7305 |
} |
| 7306 |
|
| 7307 |
public void testBitOperation6() throws Exception { |
| 7308 |
testTypes("/**@return {!Object}*/function foo(){var a = foo()&3;}", |
| 7309 |
"bad left operand to bitwise operator\n" + |
| 7310 |
"found : Object\n" + |
| 7311 |
"required: (boolean|null|number|string|undefined)"); |
| 7312 |
} |
| 7313 |
|
| 7314 |
public void testBitOperation7() throws Exception { |
| 7315 |
testTypes("var x = null; x |= undefined; x &= 3; x ^= '3'; x |= true;"); |
| 7316 |
} |
| 7317 |
|
| 7318 |
public void testBitOperation8() throws Exception { |
| 7319 |
testTypes("var x = void 0; x |= new Number(3);"); |
| 7320 |
} |
| 7321 |
|
| 7322 |
public void testBitOperation9() throws Exception { |
| 7323 |
testTypes("var x = void 0; x |= {};", |
| 7324 |
"bad right operand to bitwise operator\n" + |
| 7325 |
"found : {}\n" + |
| 7326 |
"required: (boolean|null|number|string|undefined)"); |
| 7327 |
} |
| 7328 |
|
| 7329 |
public void testCall1() throws Exception { |
| 7330 |
testTypes("3();", "number expressions are not callable"); |
| 7331 |
} |
| 7332 |
|
| 7333 |
public void testCall2() throws Exception { |
| 7334 |
testTypes("/** @param {!Number} foo*/function bar(foo){ bar('abc'); }", |
| 7335 |
"actual parameter 1 of bar does not match formal parameter\n" + |
| 7336 |
"found : string\n" + |
| 7337 |
"required: Number"); |
| 7338 |
} |
| 7339 |
|
| 7340 |
public void testCall3() throws Exception { |
| 7341 |
|
| 7342 |
|
| 7343 |
testTypes("/** @type {Function|undefined} */var opt_f;" + |
| 7344 |
"/** @type {some.unknown.type} */var f1;" + |
| 7345 |
"var f2 = opt_f || f1;" + |
| 7346 |
"f2();", |
| 7347 |
"Bad type annotation. Unknown type some.unknown.type"); |
| 7348 |
} |
| 7349 |
|
| 7350 |
public void testCall4() throws Exception { |
| 7351 |
testTypes("/**@param {!RegExp} a*/var foo = function bar(a){ bar('abc'); }", |
| 7352 |
"actual parameter 1 of bar does not match formal parameter\n" + |
| 7353 |
"found : string\n" + |
| 7354 |
"required: RegExp"); |
| 7355 |
} |
| 7356 |
|
| 7357 |
public void testCall5() throws Exception { |
| 7358 |
testTypes("/**@param {!RegExp} a*/var foo = function bar(a){ foo('abc'); }", |
| 7359 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 7360 |
"found : string\n" + |
| 7361 |
"required: RegExp"); |
| 7362 |
} |
| 7363 |
|
| 7364 |
public void testCall6() throws Exception { |
| 7365 |
testTypes("/** @param {!Number} foo*/function bar(foo){}" + |
| 7366 |
"bar('abc');", |
| 7367 |
"actual parameter 1 of bar does not match formal parameter\n" + |
| 7368 |
"found : string\n" + |
| 7369 |
"required: Number"); |
| 7370 |
} |
| 7371 |
|
| 7372 |
public void testCall7() throws Exception { |
| 7373 |
testTypes("/** @param {!RegExp} a*/var foo = function bar(a){};" + |
| 7374 |
"foo('abc');", |
| 7375 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 7376 |
"found : string\n" + |
| 7377 |
"required: RegExp"); |
| 7378 |
} |
| 7379 |
|
| 7380 |
public void testCall8() throws Exception { |
| 7381 |
testTypes("/** @type {Function|number} */var f;f();", |
| 7382 |
"(Function|number) expressions are " + |
| 7383 |
"not callable"); |
| 7384 |
} |
| 7385 |
|
| 7386 |
public void testCall9() throws Exception { |
| 7387 |
testTypes( |
| 7388 |
"var goog = {};" + |
| 7389 |
"/** @constructor */ goog.Foo = function() {};" + |
| 7390 |
"/** @param {!goog.Foo} a */ var bar = function(a){};" + |
| 7391 |
"bar('abc');", |
| 7392 |
"actual parameter 1 of bar does not match formal parameter\n" + |
| 7393 |
"found : string\n" + |
| 7394 |
"required: goog.Foo"); |
| 7395 |
} |
| 7396 |
|
| 7397 |
public void testCall10() throws Exception { |
| 7398 |
testTypes("/** @type {Function} */var f;f();"); |
| 7399 |
} |
| 7400 |
|
| 7401 |
public void testCall11() throws Exception { |
| 7402 |
testTypes("var f = new Function(); f();"); |
| 7403 |
} |
| 7404 |
|
| 7405 |
public void testFunctionCall1() throws Exception { |
| 7406 |
testTypes( |
| 7407 |
"/** @param {number} x */ var foo = function(x) {};" + |
| 7408 |
"foo.call(null, 3);"); |
| 7409 |
} |
| 7410 |
|
| 7411 |
public void testFunctionCall2() throws Exception { |
| 7412 |
testTypes( |
| 7413 |
"/** @param {number} x */ var foo = function(x) {};" + |
| 7414 |
"foo.call(null, 'bar');", |
| 7415 |
"actual parameter 2 of foo.call does not match formal parameter\n" + |
| 7416 |
"found : string\n" + |
| 7417 |
"required: number"); |
| 7418 |
} |
| 7419 |
|
| 7420 |
public void testFunctionCall3() throws Exception { |
| 7421 |
testTypes( |
| 7422 |
"/** @param {number} x \n * @constructor */ " + |
| 7423 |
"var Foo = function(x) { this.bar.call(null, x); };" + |
| 7424 |
"/** @type {function(number)} */ Foo.prototype.bar;"); |
| 7425 |
} |
| 7426 |
|
| 7427 |
public void testFunctionCall4() throws Exception { |
| 7428 |
testTypes( |
| 7429 |
"/** @param {string} x \n * @constructor */ " + |
| 7430 |
"var Foo = function(x) { this.bar.call(null, x); };" + |
| 7431 |
"/** @type {function(number)} */ Foo.prototype.bar;", |
| 7432 |
"actual parameter 2 of this.bar.call " + |
| 7433 |
"does not match formal parameter\n" + |
| 7434 |
"found : string\n" + |
| 7435 |
"required: number"); |
| 7436 |
} |
| 7437 |
|
| 7438 |
public void testFunctionCall5() throws Exception { |
| 7439 |
testTypes( |
| 7440 |
"/** @param {Function} handler \n * @constructor */ " + |
| 7441 |
"var Foo = function(handler) { handler.call(this, x); };"); |
| 7442 |
} |
| 7443 |
|
| 7444 |
public void testFunctionCall6() throws Exception { |
| 7445 |
testTypes( |
| 7446 |
"/** @param {Function} handler \n * @constructor */ " + |
| 7447 |
"var Foo = function(handler) { handler.apply(this, x); };"); |
| 7448 |
} |
| 7449 |
|
| 7450 |
public void testFunctionCall7() throws Exception { |
| 7451 |
testTypes( |
| 7452 |
"/** @param {Function} handler \n * @param {Object} opt_context */ " + |
| 7453 |
"var Foo = function(handler, opt_context) { " + |
| 7454 |
" handler.call(opt_context, x);" + |
| 7455 |
"};"); |
| 7456 |
} |
| 7457 |
|
| 7458 |
public void testFunctionCall8() throws Exception { |
| 7459 |
testTypes( |
| 7460 |
"/** @param {Function} handler \n * @param {Object} opt_context */ " + |
| 7461 |
"var Foo = function(handler, opt_context) { " + |
| 7462 |
" handler.apply(opt_context, x);" + |
| 7463 |
"};"); |
| 7464 |
} |
| 7465 |
|
| 7466 |
public void testFunctionBind1() throws Exception { |
| 7467 |
testTypes( |
| 7468 |
"/** @type {function(string, number): boolean} */" + |
| 7469 |
"function f(x, y) { return true; }" + |
| 7470 |
"f.bind(null, 3);", |
| 7471 |
"actual parameter 2 of f.bind does not match formal parameter\n" + |
| 7472 |
"found : number\n" + |
| 7473 |
"required: string"); |
| 7474 |
} |
| 7475 |
|
| 7476 |
public void testFunctionBind2() throws Exception { |
| 7477 |
testTypes( |
| 7478 |
"/** @type {function(number): boolean} */" + |
| 7479 |
"function f(x) { return true; }" + |
| 7480 |
"f(f.bind(null, 3)());", |
| 7481 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7482 |
"found : boolean\n" + |
| 7483 |
"required: number"); |
| 7484 |
} |
| 7485 |
|
| 7486 |
public void testFunctionBind3() throws Exception { |
| 7487 |
testTypes( |
| 7488 |
"/** @type {function(number, string): boolean} */" + |
| 7489 |
"function f(x, y) { return true; }" + |
| 7490 |
"f.bind(null, 3)(true);", |
| 7491 |
"actual parameter 1 of function does not match formal parameter\n" + |
| 7492 |
"found : boolean\n" + |
| 7493 |
"required: string"); |
| 7494 |
} |
| 7495 |
|
| 7496 |
public void testFunctionBind4() throws Exception { |
| 7497 |
testTypes( |
| 7498 |
"/** @param {...number} x */" + |
| 7499 |
"function f(x) {}" + |
| 7500 |
"f.bind(null, 3, 3, 3)(true);", |
| 7501 |
"actual parameter 1 of function does not match formal parameter\n" + |
| 7502 |
"found : boolean\n" + |
| 7503 |
"required: (number|undefined)"); |
| 7504 |
} |
| 7505 |
|
| 7506 |
public void testFunctionBind5() throws Exception { |
| 7507 |
testTypes( |
| 7508 |
"/** @param {...number} x */" + |
| 7509 |
"function f(x) {}" + |
| 7510 |
"f.bind(null, true)(3, 3, 3);", |
| 7511 |
"actual parameter 2 of f.bind does not match formal parameter\n" + |
| 7512 |
"found : boolean\n" + |
| 7513 |
"required: (number|undefined)"); |
| 7514 |
} |
| 7515 |
|
| 7516 |
public void testGoogBind1() throws Exception { |
| 7517 |
testClosureTypes( |
| 7518 |
"var goog = {}; goog.bind = function(var_args) {};" + |
| 7519 |
"/** @type {function(number): boolean} */" + |
| 7520 |
"function f(x, y) { return true; }" + |
| 7521 |
"f(goog.bind(f, null, 'x')());", |
| 7522 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7523 |
"found : boolean\n" + |
| 7524 |
"required: number"); |
| 7525 |
} |
| 7526 |
|
| 7527 |
public void testGoogBind2() throws Exception { |
| 7528 |
|
| 7529 |
|
| 7530 |
testClosureTypes( |
| 7531 |
"var goog = {}; goog.bind = function(var_args) {};" + |
| 7532 |
"/** @type {function(boolean): boolean} */" + |
| 7533 |
"function f(x, y) { return true; }" + |
| 7534 |
"f(goog.bind(f, null, 'x')());", |
| 7535 |
null); |
| 7536 |
} |
| 7537 |
|
| 7538 |
public void testCast2() throws Exception { |
| 7539 |
|
| 7540 |
testTypes("/** @constructor */function base() {}\n" + |
| 7541 |
"/** @constructor\n @extends {base} */function derived() {}\n" + |
| 7542 |
"/** @type {base} */ var baz = new derived();\n"); |
| 7543 |
} |
| 7544 |
|
| 7545 |
public void testCast3() throws Exception { |
| 7546 |
|
| 7547 |
testTypes("/** @constructor */function base() {}\n" + |
| 7548 |
"/** @constructor @extends {base} */function derived() {}\n" + |
| 7549 |
"/** @type {!derived} */ var baz = new base();\n", |
| 7550 |
"initializing variable\n" + |
| 7551 |
"found : base\n" + |
| 7552 |
"required: derived"); |
| 7553 |
} |
| 7554 |
|
| 7555 |
public void testCast3a() throws Exception { |
| 7556 |
|
| 7557 |
testTypes("/** @constructor */function Base() {}\n" + |
| 7558 |
"/** @constructor @extends {Base} */function Derived() {}\n" + |
| 7559 |
"var baseInstance = new Base();" + |
| 7560 |
"/** @type {!Derived} */ var baz = baseInstance;\n", |
| 7561 |
"initializing variable\n" + |
| 7562 |
"found : Base\n" + |
| 7563 |
"required: Derived"); |
| 7564 |
} |
| 7565 |
|
| 7566 |
public void testCast4() throws Exception { |
| 7567 |
|
| 7568 |
testTypes("/** @constructor */function base() {}\n" + |
| 7569 |
"/** @constructor\n * @extends {base} */function derived() {}\n" + |
| 7570 |
"/** @type {!derived} */ var baz = " + |
| 7571 |
"/** @type {!derived} */(new base());\n"); |
| 7572 |
} |
| 7573 |
|
| 7574 |
public void testCast5() throws Exception { |
| 7575 |
|
| 7576 |
testTypes("/** @constructor */function foo() {}\n" + |
| 7577 |
"/** @constructor */function bar() {}\n" + |
| 7578 |
"var baz = /** @type {!foo} */(new bar);\n", |
| 7579 |
"invalid cast - must be a subtype or supertype\n" + |
| 7580 |
"from: bar\n" + |
| 7581 |
"to : foo"); |
| 7582 |
} |
| 7583 |
|
| 7584 |
public void testCast5a() throws Exception { |
| 7585 |
|
| 7586 |
testTypes("/** @constructor */function foo() {}\n" + |
| 7587 |
"/** @constructor */function bar() {}\n" + |
| 7588 |
"var barInstance = new bar;\n" + |
| 7589 |
"var baz = /** @type {!foo} */(barInstance);\n", |
| 7590 |
"invalid cast - must be a subtype or supertype\n" + |
| 7591 |
"from: bar\n" + |
| 7592 |
"to : foo"); |
| 7593 |
} |
| 7594 |
|
| 7595 |
public void testCast6() throws Exception { |
| 7596 |
|
| 7597 |
testTypes("/** @constructor */function foo() {}\n" + |
| 7598 |
"/** @constructor \n @extends foo */function bar() {}\n" + |
| 7599 |
"var baz = /** @type {!bar} */(new bar);\n" + |
| 7600 |
"var baz = /** @type {!foo} */(new foo);\n" + |
| 7601 |
"var baz = /** @type {bar} */(new bar);\n" + |
| 7602 |
"var baz = /** @type {foo} */(new foo);\n" + |
| 7603 |
"var baz = /** @type {!foo} */(new bar);\n" + |
| 7604 |
"var baz = /** @type {!bar} */(new foo);\n" + |
| 7605 |
"var baz = /** @type {foo} */(new bar);\n" + |
| 7606 |
"var baz = /** @type {bar} */(new foo);\n"); |
| 7607 |
} |
| 7608 |
|
| 7609 |
public void testCast7() throws Exception { |
| 7610 |
testTypes("var x = /** @type {foo} */ (new Object());", |
| 7611 |
"Bad type annotation. Unknown type foo"); |
| 7612 |
} |
| 7613 |
|
| 7614 |
public void testCast8() throws Exception { |
| 7615 |
testTypes("function f() { return /** @type {foo} */ (new Object()); }", |
| 7616 |
"Bad type annotation. Unknown type foo"); |
| 7617 |
} |
| 7618 |
|
| 7619 |
public void testCast9() throws Exception { |
| 7620 |
testTypes("var foo = {};" + |
| 7621 |
"function f() { return /** @type {foo} */ (new Object()); }", |
| 7622 |
"Bad type annotation. Unknown type foo"); |
| 7623 |
} |
| 7624 |
|
| 7625 |
public void testCast10() throws Exception { |
| 7626 |
testTypes("var foo = function() {};" + |
| 7627 |
"function f() { return /** @type {foo} */ (new Object()); }", |
| 7628 |
"Bad type annotation. Unknown type foo"); |
| 7629 |
} |
| 7630 |
|
| 7631 |
public void testCast11() throws Exception { |
| 7632 |
testTypes("var goog = {}; goog.foo = {};" + |
| 7633 |
"function f() { return /** @type {goog.foo} */ (new Object()); }", |
| 7634 |
"Bad type annotation. Unknown type goog.foo"); |
| 7635 |
} |
| 7636 |
|
| 7637 |
public void testCast12() throws Exception { |
| 7638 |
testTypes("var goog = {}; goog.foo = function() {};" + |
| 7639 |
"function f() { return /** @type {goog.foo} */ (new Object()); }", |
| 7640 |
"Bad type annotation. Unknown type goog.foo"); |
| 7641 |
} |
| 7642 |
|
| 7643 |
public void testCast13() throws Exception { |
| 7644 |
|
| 7645 |
|
| 7646 |
testClosureTypes("var goog = {}; " + |
| 7647 |
"goog.addDependency('zzz.js', ['goog.foo'], []);" + |
| 7648 |
"goog.foo = function() {};" + |
| 7649 |
"function f() { return /** @type {goog.foo} */ (new Object()); }", |
| 7650 |
"Bad type annotation. Unknown type goog.foo"); |
| 7651 |
} |
| 7652 |
|
| 7653 |
public void testCast14() throws Exception { |
| 7654 |
|
| 7655 |
|
| 7656 |
testClosureTypes("var goog = {}; " + |
| 7657 |
"goog.addDependency('zzz.js', ['goog.bar'], []);" + |
| 7658 |
"function f() { return /** @type {goog.bar} */ (new Object()); }", |
| 7659 |
null); |
| 7660 |
} |
| 7661 |
|
| 7662 |
public void testCast15() throws Exception { |
| 7663 |
|
| 7664 |
|
| 7665 |
|
| 7666 |
|
| 7667 |
|
| 7668 |
|
| 7669 |
|
| 7670 |
testTypes( |
| 7671 |
"for (var i = 0; i < 10; i++) {" + |
| 7672 |
"var x = /** @type {Object|number} */ ({foo: 3});" + |
| 7673 |
"/** @param {number} x */ function f(x) {}" + |
| 7674 |
"f(x.foo);" + |
| 7675 |
"f([].foo);" + |
| 7676 |
"}", |
| 7677 |
"Property foo never defined on Array"); |
| 7678 |
} |
| 7679 |
|
| 7680 |
public void testCast16() throws Exception { |
| 7681 |
|
| 7682 |
testTypes( |
| 7683 |
"for (var i = 0; i < 10; i++) {" + |
| 7684 |
"var x = /** @type {Object|number} */ (" + |
| 7685 |
" {/** @type {string} */ foo: 3});" + |
| 7686 |
"}", |
| 7687 |
"assignment to property foo of Object\n" + |
| 7688 |
"found : number\n" + |
| 7689 |
"required: string"); |
| 7690 |
} |
| 7691 |
|
| 7692 |
public void testCast17a() throws Exception { |
| 7693 |
|
| 7694 |
testTypes("/** @constructor */ function Foo() {} \n" + |
| 7695 |
"/** @type {Foo} */ var x = /** @type {Foo} */ (y)"); |
| 7696 |
|
| 7697 |
testTypes("/** @constructor */ function Foo() {} \n" + |
| 7698 |
"/** @type {Foo} */ var x = (/** @type {Foo} */ y)"); |
| 7699 |
} |
| 7700 |
|
| 7701 |
public void testCast17b() throws Exception { |
| 7702 |
|
| 7703 |
testTypes("/** @constructor */ function Foo() {} \n" + |
| 7704 |
"/** @type {Foo} */ var x = /** @type {Foo} */ ({})"); |
| 7705 |
} |
| 7706 |
|
| 7707 |
public void testCast18() throws Exception { |
| 7708 |
|
| 7709 |
|
| 7710 |
testTypes("/** @constructor */ function Foo() {} \n" + |
| 7711 |
"/** @type {Foo} */ var x = (/** @type {Foo} */ {})", |
| 7712 |
"Type annotations are not allowed here. " + |
| 7713 |
"Are you missing parentheses?"); |
| 7714 |
|
| 7715 |
|
| 7716 |
testTypes("/** @constructor */ function Foo() {} \n" + |
| 7717 |
"/** @type {Foo} */ var x = /** @type {Foo} */ {}", |
| 7718 |
"Type annotations are not allowed here. " + |
| 7719 |
"Are you missing parentheses?"); |
| 7720 |
} |
| 7721 |
|
| 7722 |
public void testCast19() throws Exception { |
| 7723 |
testTypes( |
| 7724 |
"var x = 'string';\n" + |
| 7725 |
"/** @type {number} */\n" + |
| 7726 |
"var y = /** @type {number} */(x);", |
| 7727 |
"invalid cast - must be a subtype or supertype\n" + |
| 7728 |
"from: string\n" + |
| 7729 |
"to : number"); |
| 7730 |
} |
| 7731 |
|
| 7732 |
public void testCast20() throws Exception { |
| 7733 |
testTypes( |
| 7734 |
"/** @enum {boolean|null} */\n" + |
| 7735 |
"var X = {" + |
| 7736 |
" AA: true," + |
| 7737 |
" BB: false," + |
| 7738 |
" CC: null" + |
| 7739 |
"};\n" + |
| 7740 |
"var y = /** @type {X} */(true);"); |
| 7741 |
} |
| 7742 |
|
| 7743 |
public void testCast21() throws Exception { |
| 7744 |
testTypes( |
| 7745 |
"/** @enum {boolean|null} */\n" + |
| 7746 |
"var X = {" + |
| 7747 |
" AA: true," + |
| 7748 |
" BB: false," + |
| 7749 |
" CC: null" + |
| 7750 |
"};\n" + |
| 7751 |
"var value = true;\n" + |
| 7752 |
"var y = /** @type {X} */(value);"); |
| 7753 |
} |
| 7754 |
|
| 7755 |
public void testCast22() throws Exception { |
| 7756 |
testTypes( |
| 7757 |
"var x = null;\n" + |
| 7758 |
"var y = /** @type {number} */(x);", |
| 7759 |
"invalid cast - must be a subtype or supertype\n" + |
| 7760 |
"from: null\n" + |
| 7761 |
"to : number"); |
| 7762 |
} |
| 7763 |
|
| 7764 |
public void testCast23() throws Exception { |
| 7765 |
testTypes( |
| 7766 |
"var x = null;\n" + |
| 7767 |
"var y = /** @type {Number} */(x);"); |
| 7768 |
} |
| 7769 |
|
| 7770 |
public void testCast24() throws Exception { |
| 7771 |
testTypes( |
| 7772 |
"var x = undefined;\n" + |
| 7773 |
"var y = /** @type {number} */(x);", |
| 7774 |
"invalid cast - must be a subtype or supertype\n" + |
| 7775 |
"from: undefined\n" + |
| 7776 |
"to : number"); |
| 7777 |
} |
| 7778 |
|
| 7779 |
public void testCast25() throws Exception { |
| 7780 |
testTypes( |
| 7781 |
"var x = undefined;\n" + |
| 7782 |
"var y = /** @type {number|undefined} */(x);"); |
| 7783 |
} |
| 7784 |
|
| 7785 |
public void testCast26() throws Exception { |
| 7786 |
testTypes( |
| 7787 |
"function fn(dir) {\n" + |
| 7788 |
" var node = dir ? 1 : 2;\n" + |
| 7789 |
" fn(/** @type {number} */ (node));\n" + |
| 7790 |
"}"); |
| 7791 |
} |
| 7792 |
|
| 7793 |
public void testCast27() throws Exception { |
| 7794 |
|
| 7795 |
testTypes( |
| 7796 |
"/** @interface */ function I() {}\n" + |
| 7797 |
"/** @constructor */ function C() {}\n" + |
| 7798 |
"var x = new C();\n" + |
| 7799 |
"var y = /** @type {I} */(x);"); |
| 7800 |
} |
| 7801 |
|
| 7802 |
public void testCast27a() throws Exception { |
| 7803 |
|
| 7804 |
testTypes( |
| 7805 |
"/** @interface */ function I() {}\n" + |
| 7806 |
"/** @constructor */ function C() {}\n" + |
| 7807 |
"/** @type {C} */ var x ;\n" + |
| 7808 |
"var y = /** @type {I} */(x);"); |
| 7809 |
} |
| 7810 |
|
| 7811 |
public void testCast28() throws Exception { |
| 7812 |
|
| 7813 |
testTypes( |
| 7814 |
"/** @interface */ function I() {}\n" + |
| 7815 |
"/** @constructor */ function C() {}\n" + |
| 7816 |
"/** @type {!I} */ var x;\n" + |
| 7817 |
"var y = /** @type {C} */(x);"); |
| 7818 |
} |
| 7819 |
|
| 7820 |
public void testCast28a() throws Exception { |
| 7821 |
|
| 7822 |
testTypes( |
| 7823 |
"/** @interface */ function I() {}\n" + |
| 7824 |
"/** @constructor */ function C() {}\n" + |
| 7825 |
"/** @type {I} */ var x;\n" + |
| 7826 |
"var y = /** @type {C} */(x);"); |
| 7827 |
} |
| 7828 |
|
| 7829 |
public void testCast29a() throws Exception { |
| 7830 |
|
| 7831 |
testTypes( |
| 7832 |
"/** @constructor */ function C() {}\n" + |
| 7833 |
"var x = new C();\n" + |
| 7834 |
"var y = /** @type {{remoteJids: Array, sessionId: string}} */(x);"); |
| 7835 |
} |
| 7836 |
|
| 7837 |
public void testCast29b() throws Exception { |
| 7838 |
|
| 7839 |
testTypes( |
| 7840 |
"/** @constructor */ function C() {}\n" + |
| 7841 |
"/** @type {C} */ var x;\n" + |
| 7842 |
"var y = /** @type {{prop1: Array, prop2: string}} */(x);"); |
| 7843 |
} |
| 7844 |
|
| 7845 |
public void testCast29c() throws Exception { |
| 7846 |
|
| 7847 |
testTypes( |
| 7848 |
"/** @constructor */ function C() {}\n" + |
| 7849 |
"/** @type {{remoteJids: Array, sessionId: string}} */ var x ;\n" + |
| 7850 |
"var y = /** @type {C} */(x);"); |
| 7851 |
} |
| 7852 |
|
| 7853 |
public void testCast30() throws Exception { |
| 7854 |
|
| 7855 |
testTypes( |
| 7856 |
"/** @constructor */ function C() {}\n" + |
| 7857 |
"/** @type {function():string} */ var x ;\n" + |
| 7858 |
"var y = /** @type {function():?} */(x);"); |
| 7859 |
} |
| 7860 |
|
| 7861 |
public void testCast31() throws Exception { |
| 7862 |
|
| 7863 |
testTypes( |
| 7864 |
"/** @constructor */ function C() {}\n" + |
| 7865 |
"/** @type {function(*)} */ var x ;\n" + |
| 7866 |
"var y = /** @type {function(string)} */(x);"); |
| 7867 |
} |
| 7868 |
|
| 7869 |
public void testCast32() throws Exception { |
| 7870 |
testTypes( |
| 7871 |
"/** @constructor */ function C() {}\n" + |
| 7872 |
"/** @type {Object} */ var x ;\n" + |
| 7873 |
"var y = /** @type {null|{length:number}} */(x);"); |
| 7874 |
} |
| 7875 |
|
| 7876 |
public void testCast33() throws Exception { |
| 7877 |
|
| 7878 |
|
| 7879 |
testTypes( |
| 7880 |
"/** @constructor */ function C() {}\n" + |
| 7881 |
"/** @type {null|undefined} */ var x ;\n" + |
| 7882 |
"var y = /** @type {string?|undefined} */(x);"); |
| 7883 |
testTypes( |
| 7884 |
"/** @constructor */ function C() {}\n" + |
| 7885 |
"/** @type {null|undefined} */ var x ;\n" + |
| 7886 |
"var y = /** @type {string|undefined} */(x);"); |
| 7887 |
testTypes( |
| 7888 |
"/** @constructor */ function C() {}\n" + |
| 7889 |
"/** @type {null|undefined} */ var x ;\n" + |
| 7890 |
"var y = /** @type {string?} */(x);"); |
| 7891 |
testTypes( |
| 7892 |
"/** @constructor */ function C() {}\n" + |
| 7893 |
"/** @type {null|undefined} */ var x ;\n" + |
| 7894 |
"var y = /** @type {null} */(x);"); |
| 7895 |
} |
| 7896 |
|
| 7897 |
public void testCast34a() throws Exception { |
| 7898 |
testTypes( |
| 7899 |
"/** @constructor */ function C() {}\n" + |
| 7900 |
"/** @type {Object} */ var x ;\n" + |
| 7901 |
"var y = /** @type {Function} */(x);"); |
| 7902 |
} |
| 7903 |
|
| 7904 |
public void testCast34b() throws Exception { |
| 7905 |
testTypes( |
| 7906 |
"/** @constructor */ function C() {}\n" + |
| 7907 |
"/** @type {Function} */ var x ;\n" + |
| 7908 |
"var y = /** @type {Object} */(x);"); |
| 7909 |
} |
| 7910 |
|
| 7911 |
public void testNestedCasts() throws Exception { |
| 7912 |
testTypes("/** @constructor */var T = function() {};\n" + |
| 7913 |
"/** @constructor */var V = function() {};\n" + |
| 7914 |
"/**\n" + |
| 7915 |
"* @param {boolean} b\n" + |
| 7916 |
"* @return {T|V}\n" + |
| 7917 |
"*/\n" + |
| 7918 |
"function f(b) { return b ? new T() : new V(); }\n" + |
| 7919 |
"/**\n" + |
| 7920 |
"* @param {boolean} b\n" + |
| 7921 |
"* @return {boolean|undefined}\n" + |
| 7922 |
"*/\n" + |
| 7923 |
"function g(b) { return b ? true : undefined; }\n" + |
| 7924 |
"/** @return {T} */\n" + |
| 7925 |
"function h() {\n" + |
| 7926 |
"return /** @type {T} */ (f(/** @type {boolean} */ (g(true))));\n" + |
| 7927 |
"}"); |
| 7928 |
} |
| 7929 |
|
| 7930 |
public void testNativeCast1() throws Exception { |
| 7931 |
testTypes( |
| 7932 |
"/** @param {number} x */ function f(x) {}" + |
| 7933 |
"f(String(true));", |
| 7934 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7935 |
"found : string\n" + |
| 7936 |
"required: number"); |
| 7937 |
} |
| 7938 |
|
| 7939 |
public void testNativeCast2() throws Exception { |
| 7940 |
testTypes( |
| 7941 |
"/** @param {string} x */ function f(x) {}" + |
| 7942 |
"f(Number(true));", |
| 7943 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7944 |
"found : number\n" + |
| 7945 |
"required: string"); |
| 7946 |
} |
| 7947 |
|
| 7948 |
public void testNativeCast3() throws Exception { |
| 7949 |
testTypes( |
| 7950 |
"/** @param {number} x */ function f(x) {}" + |
| 7951 |
"f(Boolean(''));", |
| 7952 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7953 |
"found : boolean\n" + |
| 7954 |
"required: number"); |
| 7955 |
} |
| 7956 |
|
| 7957 |
public void testNativeCast4() throws Exception { |
| 7958 |
testTypes( |
| 7959 |
"/** @param {number} x */ function f(x) {}" + |
| 7960 |
"f(Error(''));", |
| 7961 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 7962 |
"found : Error\n" + |
| 7963 |
"required: number"); |
| 7964 |
} |
| 7965 |
|
| 7966 |
public void testBadConstructorCall() throws Exception { |
| 7967 |
testTypes( |
| 7968 |
"/** @constructor */ function Foo() {}" + |
| 7969 |
"Foo();", |
| 7970 |
"Constructor function (new:Foo): undefined should be called " + |
| 7971 |
"with the \"new\" keyword"); |
| 7972 |
} |
| 7973 |
|
| 7974 |
public void testTypeof() throws Exception { |
| 7975 |
testTypes("/**@return {void}*/function foo(){ var a = typeof foo(); }"); |
| 7976 |
} |
| 7977 |
|
| 7978 |
public void testTypeof2() throws Exception { |
| 7979 |
testTypes("function f(){ if (typeof 123 == 'numbr') return 321; }", |
| 7980 |
"unknown type: numbr"); |
| 7981 |
} |
| 7982 |
|
| 7983 |
public void testTypeof3() throws Exception { |
| 7984 |
testTypes("function f() {" + |
| 7985 |
"return (typeof 123 == 'number' ||" + |
| 7986 |
"typeof 123 == 'string' ||" + |
| 7987 |
"typeof 123 == 'boolean' ||" + |
| 7988 |
"typeof 123 == 'undefined' ||" + |
| 7989 |
"typeof 123 == 'function' ||" + |
| 7990 |
"typeof 123 == 'object' ||" + |
| 7991 |
"typeof 123 == 'unknown'); }"); |
| 7992 |
} |
| 7993 |
|
| 7994 |
public void testConstructorType1() throws Exception { |
| 7995 |
testTypes("/**@constructor*/function Foo(){}" + |
| 7996 |
"/**@type{!Foo}*/var f = new Date();", |
| 7997 |
"initializing variable\n" + |
| 7998 |
"found : Date\n" + |
| 7999 |
"required: Foo"); |
| 8000 |
} |
| 8001 |
|
| 8002 |
public void testConstructorType2() throws Exception { |
| 8003 |
testTypes("/**@constructor*/function Foo(){\n" + |
| 8004 |
"/**@type{Number}*/this.bar = new Number(5);\n" + |
| 8005 |
"}\n" + |
| 8006 |
"/**@type{Foo}*/var f = new Foo();\n" + |
| 8007 |
"/**@type{Number}*/var n = f.bar;"); |
| 8008 |
} |
| 8009 |
|
| 8010 |
public void testConstructorType3() throws Exception { |
| 8011 |
|
| 8012 |
|
| 8013 |
testTypes("/**@type{Foo}*/var f = new Foo();\n" + |
| 8014 |
"/**@type{Number}*/var n = f.bar;" + |
| 8015 |
"/**@constructor*/function Foo(){\n" + |
| 8016 |
"/**@type{Number}*/this.bar = new Number(5);\n" + |
| 8017 |
"}\n"); |
| 8018 |
} |
| 8019 |
|
| 8020 |
public void testConstructorType4() throws Exception { |
| 8021 |
testTypes("/**@constructor*/function Foo(){\n" + |
| 8022 |
"/**@type{!Number}*/this.bar = new Number(5);\n" + |
| 8023 |
"}\n" + |
| 8024 |
"/**@type{!Foo}*/var f = new Foo();\n" + |
| 8025 |
"/**@type{!String}*/var n = f.bar;", |
| 8026 |
"initializing variable\n" + |
| 8027 |
"found : Number\n" + |
| 8028 |
"required: String"); |
| 8029 |
} |
| 8030 |
|
| 8031 |
public void testConstructorType5() throws Exception { |
| 8032 |
testTypes("/**@constructor*/function Foo(){}\n" + |
| 8033 |
"if (Foo){}\n"); |
| 8034 |
} |
| 8035 |
|
| 8036 |
public void testConstructorType6() throws Exception { |
| 8037 |
testTypes("/** @constructor */\n" + |
| 8038 |
"function bar() {}\n" + |
| 8039 |
"function _foo() {\n" + |
| 8040 |
" /** @param {bar} x */\n" + |
| 8041 |
" function f(x) {}\n" + |
| 8042 |
"}"); |
| 8043 |
} |
| 8044 |
|
| 8045 |
public void testConstructorType7() throws Exception { |
| 8046 |
TypeCheckResult p = |
| 8047 |
parseAndTypeCheckWithScope("/** @constructor */function A(){};"); |
| 8048 |
|
| 8049 |
JSType type = p.scope.getVar("A").getType(); |
| 8050 |
assertTrue(type instanceof FunctionType); |
| 8051 |
FunctionType fType = (FunctionType) type; |
| 8052 |
assertEquals("A", fType.getReferenceName()); |
| 8053 |
} |
| 8054 |
|
| 8055 |
public void testConstructorType8() throws Exception { |
| 8056 |
testTypes( |
| 8057 |
"var ns = {};" + |
| 8058 |
"ns.create = function() { return function() {}; };" + |
| 8059 |
"/** @constructor */ ns.Foo = ns.create();" + |
| 8060 |
"ns.Foo.prototype = {x: 0, y: 0};" + |
| 8061 |
"/**\n" + |
| 8062 |
" * @param {ns.Foo} foo\n" + |
| 8063 |
" * @return {string}\n" + |
| 8064 |
" */\n" + |
| 8065 |
"function f(foo) {" + |
| 8066 |
" return foo.x;" + |
| 8067 |
"}", |
| 8068 |
"inconsistent return type\n" + |
| 8069 |
"found : number\n" + |
| 8070 |
"required: string"); |
| 8071 |
} |
| 8072 |
|
| 8073 |
public void testConstructorType9() throws Exception { |
| 8074 |
testTypes( |
| 8075 |
"var ns = {};" + |
| 8076 |
"ns.create = function() { return function() {}; };" + |
| 8077 |
"ns.extend = function(x) { return x; };" + |
| 8078 |
"/** @constructor */ ns.Foo = ns.create();" + |
| 8079 |
"ns.Foo.prototype = ns.extend({x: 0, y: 0});" + |
| 8080 |
"/**\n" + |
| 8081 |
" * @param {ns.Foo} foo\n" + |
| 8082 |
" * @return {string}\n" + |
| 8083 |
" */\n" + |
| 8084 |
"function f(foo) {" + |
| 8085 |
" return foo.x;" + |
| 8086 |
"}"); |
| 8087 |
} |
| 8088 |
|
| 8089 |
public void testConstructorType10() throws Exception { |
| 8090 |
testTypes("/** @constructor */" + |
| 8091 |
"function NonStr() {}" + |
| 8092 |
"/**\n" + |
| 8093 |
" * @constructor\n" + |
| 8094 |
" * @struct\n" + |
| 8095 |
" * @extends{NonStr}\n" + |
| 8096 |
" */" + |
| 8097 |
"function NonStrKid() {}", |
| 8098 |
"NonStrKid cannot extend this type; " + |
| 8099 |
"structs can only extend structs"); |
| 8100 |
} |
| 8101 |
|
| 8102 |
public void testConstructorType11() throws Exception { |
| 8103 |
testTypes("/** @constructor */" + |
| 8104 |
"function NonDict() {}" + |
| 8105 |
"/**\n" + |
| 8106 |
" * @constructor\n" + |
| 8107 |
" * @dict\n" + |
| 8108 |
" * @extends{NonDict}\n" + |
| 8109 |
" */" + |
| 8110 |
"function NonDictKid() {}", |
| 8111 |
"NonDictKid cannot extend this type; " + |
| 8112 |
"dicts can only extend dicts"); |
| 8113 |
} |
| 8114 |
|
| 8115 |
public void testConstructorType12() throws Exception { |
| 8116 |
testTypes("/**\n" + |
| 8117 |
" * @constructor\n" + |
| 8118 |
" * @struct\n" + |
| 8119 |
" */\n" + |
| 8120 |
"function Bar() {}\n" + |
| 8121 |
"Bar.prototype = {};\n", |
| 8122 |
"Bar cannot extend this type; " + |
| 8123 |
"structs can only extend structs"); |
| 8124 |
} |
| 8125 |
|
| 8126 |
public void testBadStruct() throws Exception { |
| 8127 |
testTypes("/** @struct */function Struct1() {}", |
| 8128 |
"@struct used without @constructor for Struct1"); |
| 8129 |
} |
| 8130 |
|
| 8131 |
public void testBadDict() throws Exception { |
| 8132 |
testTypes("/** @dict */function Dict1() {}", |
| 8133 |
"@dict used without @constructor for Dict1"); |
| 8134 |
} |
| 8135 |
|
| 8136 |
public void testAnonymousPrototype1() throws Exception { |
| 8137 |
testTypes( |
| 8138 |
"var ns = {};" + |
| 8139 |
"/** @constructor */ ns.Foo = function() {" + |
| 8140 |
" this.bar(3, 5);" + |
| 8141 |
"};" + |
| 8142 |
"ns.Foo.prototype = {" + |
| 8143 |
" bar: function(x) {}" + |
| 8144 |
"};", |
| 8145 |
"Function ns.Foo.prototype.bar: called with 2 argument(s). " + |
| 8146 |
"Function requires at least 1 argument(s) and no more " + |
| 8147 |
"than 1 argument(s)."); |
| 8148 |
} |
| 8149 |
|
| 8150 |
public void testAnonymousPrototype2() throws Exception { |
| 8151 |
testTypes( |
| 8152 |
"/** @interface */ var Foo = function() {};" + |
| 8153 |
"Foo.prototype = {" + |
| 8154 |
" foo: function(x) {}" + |
| 8155 |
"};" + |
| 8156 |
"/**\n" + |
| 8157 |
" * @constructor\n" + |
| 8158 |
" * @implements {Foo}\n" + |
| 8159 |
" */ var Bar = function() {};", |
| 8160 |
"property foo on interface Foo is not implemented by type Bar"); |
| 8161 |
} |
| 8162 |
|
| 8163 |
public void testAnonymousType1() throws Exception { |
| 8164 |
testTypes("function f() { return {}; }" + |
| 8165 |
"/** @constructor */\n" + |
| 8166 |
"f().bar = function() {};"); |
| 8167 |
} |
| 8168 |
|
| 8169 |
public void testAnonymousType2() throws Exception { |
| 8170 |
testTypes("function f() { return {}; }" + |
| 8171 |
"/** @interface */\n" + |
| 8172 |
"f().bar = function() {};"); |
| 8173 |
} |
| 8174 |
|
| 8175 |
public void testAnonymousType3() throws Exception { |
| 8176 |
testTypes("function f() { return {}; }" + |
| 8177 |
"/** @enum */\n" + |
| 8178 |
"f().bar = {FOO: 1};"); |
| 8179 |
} |
| 8180 |
|
| 8181 |
public void testBang1() throws Exception { |
| 8182 |
testTypes("/** @param {Object} x\n@return {!Object} */\n" + |
| 8183 |
"function f(x) { return x; }", |
| 8184 |
"inconsistent return type\n" + |
| 8185 |
"found : (Object|null)\n" + |
| 8186 |
"required: Object"); |
| 8187 |
} |
| 8188 |
|
| 8189 |
public void testBang2() throws Exception { |
| 8190 |
testTypes("/** @param {Object} x\n@return {!Object} */\n" + |
| 8191 |
"function f(x) { return x ? x : new Object(); }"); |
| 8192 |
} |
| 8193 |
|
| 8194 |
public void testBang3() throws Exception { |
| 8195 |
testTypes("/** @param {Object} x\n@return {!Object} */\n" + |
| 8196 |
"function f(x) { return /** @type {!Object} */ (x); }"); |
| 8197 |
} |
| 8198 |
|
| 8199 |
public void testBang4() throws Exception { |
| 8200 |
testTypes("/**@param {Object} x\n@param {Object} y\n@return {boolean}*/\n" + |
| 8201 |
"function f(x, y) {\n" + |
| 8202 |
"if (typeof x != 'undefined') { return x == y; }\n" + |
| 8203 |
"else { return x != y; }\n}"); |
| 8204 |
} |
| 8205 |
|
| 8206 |
public void testBang5() throws Exception { |
| 8207 |
testTypes("/**@param {Object} x\n@param {Object} y\n@return {boolean}*/\n" + |
| 8208 |
"function f(x, y) { return !!x && x == y; }"); |
| 8209 |
} |
| 8210 |
|
| 8211 |
public void testBang6() throws Exception { |
| 8212 |
testTypes("/** @param {Object?} x\n@return {Object} */\n" + |
| 8213 |
"function f(x) { return x; }"); |
| 8214 |
} |
| 8215 |
|
| 8216 |
public void testBang7() throws Exception { |
| 8217 |
testTypes("/**@param {(Object,string,null)} x\n" + |
| 8218 |
"@return {(Object,string)}*/function f(x) { return x; }"); |
| 8219 |
} |
| 8220 |
|
| 8221 |
public void testDefinePropertyOnNullableObject1() throws Exception { |
| 8222 |
testTypes("/** @type {Object} */ var n = {};\n" + |
| 8223 |
"/** @type {number} */ n.x = 1;\n" + |
| 8224 |
"/** @return {boolean} */function f() { return n.x; }", |
| 8225 |
"inconsistent return type\n" + |
| 8226 |
"found : number\n" + |
| 8227 |
"required: boolean"); |
| 8228 |
} |
| 8229 |
|
| 8230 |
public void testDefinePropertyOnNullableObject2() throws Exception { |
| 8231 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 8232 |
"/** @param {T} t\n@return {boolean} */function f(t) {\n" + |
| 8233 |
"t.x = 1; return t.x; }", |
| 8234 |
"inconsistent return type\n" + |
| 8235 |
"found : number\n" + |
| 8236 |
"required: boolean"); |
| 8237 |
} |
| 8238 |
|
| 8239 |
public void testUnknownConstructorInstanceType1() throws Exception { |
| 8240 |
testTypes("/** @return {Array} */ function g(f) { return new f(); }"); |
| 8241 |
} |
| 8242 |
|
| 8243 |
public void testUnknownConstructorInstanceType2() throws Exception { |
| 8244 |
testTypes("function g(f) { return /** @type Array */(new f()); }"); |
| 8245 |
} |
| 8246 |
|
| 8247 |
public void testUnknownConstructorInstanceType3() throws Exception { |
| 8248 |
testTypes("function g(f) { var x = new f(); x.a = 1; return x; }"); |
| 8249 |
} |
| 8250 |
|
| 8251 |
public void testUnknownPrototypeChain() throws Exception { |
| 8252 |
testTypes("/**\n" + |
| 8253 |
"* @param {Object} co\n" + |
| 8254 |
" * @return {Object}\n" + |
| 8255 |
" */\n" + |
| 8256 |
"function inst(co) {\n" + |
| 8257 |
" /** @constructor */\n" + |
| 8258 |
" var c = function() {};\n" + |
| 8259 |
" c.prototype = co.prototype;\n" + |
| 8260 |
" return new c;\n" + |
| 8261 |
"}"); |
| 8262 |
} |
| 8263 |
|
| 8264 |
public void testNamespacedConstructor() throws Exception { |
| 8265 |
Node root = parseAndTypeCheck( |
| 8266 |
"var goog = {};" + |
| 8267 |
"/** @constructor */ goog.MyClass = function() {};" + |
| 8268 |
"/** @return {!goog.MyClass} */ " + |
| 8269 |
"function foo() { return new goog.MyClass(); }"); |
| 8270 |
|
| 8271 |
JSType typeOfFoo = root.getLastChild().getJSType(); |
| 8272 |
assert(typeOfFoo instanceof FunctionType); |
| 8273 |
|
| 8274 |
JSType retType = ((FunctionType) typeOfFoo).getReturnType(); |
| 8275 |
assert(retType instanceof ObjectType); |
| 8276 |
assertEquals("goog.MyClass", ((ObjectType) retType).getReferenceName()); |
| 8277 |
} |
| 8278 |
|
| 8279 |
public void testComplexNamespace() throws Exception { |
| 8280 |
String js = |
| 8281 |
"var goog = {};" + |
| 8282 |
"goog.foo = {};" + |
| 8283 |
"goog.foo.bar = 5;"; |
| 8284 |
|
| 8285 |
TypeCheckResult p = parseAndTypeCheckWithScope(js); |
| 8286 |
|
| 8287 |
|
| 8288 |
JSType googScopeType = p.scope.getVar("goog").getType(); |
| 8289 |
assertTrue(googScopeType instanceof ObjectType); |
| 8290 |
assertTrue("foo property not present on goog type", |
| 8291 |
((ObjectType) googScopeType).hasProperty("foo")); |
| 8292 |
assertFalse("bar property present on goog type", |
| 8293 |
((ObjectType) googScopeType).hasProperty("bar")); |
| 8294 |
|
| 8295 |
|
| 8296 |
Node varNode = p.root.getFirstChild(); |
| 8297 |
assertEquals(Token.VAR, varNode.getType()); |
| 8298 |
JSType googNodeType = varNode.getFirstChild().getJSType(); |
| 8299 |
assertTrue(googNodeType instanceof ObjectType); |
| 8300 |
|
| 8301 |
|
| 8302 |
assertTrue(googScopeType == googNodeType); |
| 8303 |
|
| 8304 |
|
| 8305 |
Node getpropFoo1 = varNode.getNext().getFirstChild().getFirstChild(); |
| 8306 |
assertEquals(Token.GETPROP, getpropFoo1.getType()); |
| 8307 |
assertEquals("goog", getpropFoo1.getFirstChild().getString()); |
| 8308 |
JSType googGetpropFoo1Type = getpropFoo1.getFirstChild().getJSType(); |
| 8309 |
assertTrue(googGetpropFoo1Type instanceof ObjectType); |
| 8310 |
|
| 8311 |
|
| 8312 |
assertTrue(googGetpropFoo1Type == googScopeType); |
| 8313 |
|
| 8314 |
|
| 8315 |
JSType googFooType = ((ObjectType) googScopeType).getPropertyType("foo"); |
| 8316 |
assertTrue(googFooType instanceof ObjectType); |
| 8317 |
|
| 8318 |
|
| 8319 |
|
| 8320 |
Node getpropFoo2 = varNode.getNext().getNext() |
| 8321 |
.getFirstChild().getFirstChild().getFirstChild(); |
| 8322 |
assertEquals(Token.GETPROP, getpropFoo2.getType()); |
| 8323 |
assertEquals("goog", getpropFoo2.getFirstChild().getString()); |
| 8324 |
JSType googGetpropFoo2Type = getpropFoo2.getFirstChild().getJSType(); |
| 8325 |
assertTrue(googGetpropFoo2Type instanceof ObjectType); |
| 8326 |
|
| 8327 |
|
| 8328 |
assertTrue(googGetpropFoo2Type == googScopeType); |
| 8329 |
|
| 8330 |
|
| 8331 |
|
| 8332 |
JSType googFooGetprop2Type = getpropFoo2.getJSType(); |
| 8333 |
assertTrue("goog.foo incorrectly annotated in goog.foo.bar selection", |
| 8334 |
googFooGetprop2Type instanceof ObjectType); |
| 8335 |
ObjectType googFooGetprop2ObjectType = (ObjectType) googFooGetprop2Type; |
| 8336 |
assertFalse("foo property present on goog.foo type", |
| 8337 |
googFooGetprop2ObjectType.hasProperty("foo")); |
| 8338 |
assertTrue("bar property not present on goog.foo type", |
| 8339 |
googFooGetprop2ObjectType.hasProperty("bar")); |
| 8340 |
assertTypeEquals("bar property on goog.foo type incorrectly inferred", |
| 8341 |
NUMBER_TYPE, googFooGetprop2ObjectType.getPropertyType("bar")); |
| 8342 |
} |
| 8343 |
|
| 8344 |
public void testAddingMethodsUsingPrototypeIdiomSimpleNamespace() |
| 8345 |
throws Exception { |
| 8346 |
Node js1Node = parseAndTypeCheck( |
| 8347 |
"/** @constructor */function A() {}" + |
| 8348 |
"A.prototype.m1 = 5"); |
| 8349 |
|
| 8350 |
ObjectType instanceType = getInstanceType(js1Node); |
| 8351 |
assertEquals(NATIVE_PROPERTIES_COUNT + 1, |
| 8352 |
instanceType.getPropertiesCount()); |
| 8353 |
checkObjectType(instanceType, "m1", NUMBER_TYPE); |
| 8354 |
} |
| 8355 |
|
| 8356 |
public void testAddingMethodsUsingPrototypeIdiomComplexNamespace1() |
| 8357 |
throws Exception { |
| 8358 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 8359 |
"var goog = {};" + |
| 8360 |
"goog.A = /** @constructor */function() {};" + |
| 8361 |
"/** @type number */goog.A.prototype.m1 = 5"); |
| 8362 |
|
| 8363 |
testAddingMethodsUsingPrototypeIdiomComplexNamespace(p); |
| 8364 |
} |
| 8365 |
|
| 8366 |
public void testAddingMethodsUsingPrototypeIdiomComplexNamespace2() |
| 8367 |
throws Exception { |
| 8368 |
TypeCheckResult p = parseAndTypeCheckWithScope( |
| 8369 |
"var goog = {};" + |
| 8370 |
"/** @constructor */goog.A = function() {};" + |
| 8371 |
"/** @type number */goog.A.prototype.m1 = 5"); |
| 8372 |
|
| 8373 |
testAddingMethodsUsingPrototypeIdiomComplexNamespace(p); |
| 8374 |
} |
| 8375 |
|
| 8376 |
private void testAddingMethodsUsingPrototypeIdiomComplexNamespace( |
| 8377 |
TypeCheckResult p) { |
| 8378 |
ObjectType goog = (ObjectType) p.scope.getVar("goog").getType(); |
| 8379 |
assertEquals(NATIVE_PROPERTIES_COUNT + 1, goog.getPropertiesCount()); |
| 8380 |
JSType googA = goog.getPropertyType("A"); |
| 8381 |
assertNotNull(googA); |
| 8382 |
assertTrue(googA instanceof FunctionType); |
| 8383 |
FunctionType googAFunction = (FunctionType) googA; |
| 8384 |
ObjectType classA = googAFunction.getInstanceType(); |
| 8385 |
assertEquals(NATIVE_PROPERTIES_COUNT + 1, classA.getPropertiesCount()); |
| 8386 |
checkObjectType(classA, "m1", NUMBER_TYPE); |
| 8387 |
} |
| 8388 |
|
| 8389 |
public void testAddingMethodsPrototypeIdiomAndObjectLiteralSimpleNamespace() |
| 8390 |
throws Exception { |
| 8391 |
Node js1Node = parseAndTypeCheck( |
| 8392 |
"/** @constructor */function A() {}" + |
| 8393 |
"A.prototype = {m1: 5, m2: true}"); |
| 8394 |
|
| 8395 |
ObjectType instanceType = getInstanceType(js1Node); |
| 8396 |
assertEquals(NATIVE_PROPERTIES_COUNT + 2, |
| 8397 |
instanceType.getPropertiesCount()); |
| 8398 |
checkObjectType(instanceType, "m1", NUMBER_TYPE); |
| 8399 |
checkObjectType(instanceType, "m2", BOOLEAN_TYPE); |
| 8400 |
} |
| 8401 |
|
| 8402 |
public void testDontAddMethodsIfNoConstructor() |
| 8403 |
throws Exception { |
| 8404 |
Node js1Node = parseAndTypeCheck( |
| 8405 |
"function A() {}" + |
| 8406 |
"A.prototype = {m1: 5, m2: true}"); |
| 8407 |
|
| 8408 |
JSType functionAType = js1Node.getFirstChild().getJSType(); |
| 8409 |
assertEquals("function (): undefined", functionAType.toString()); |
| 8410 |
assertTypeEquals(UNKNOWN_TYPE, |
| 8411 |
U2U_FUNCTION_TYPE.getPropertyType("m1")); |
| 8412 |
assertTypeEquals(UNKNOWN_TYPE, |
| 8413 |
U2U_FUNCTION_TYPE.getPropertyType("m2")); |
| 8414 |
} |
| 8415 |
|
| 8416 |
public void testFunctionAssignement() throws Exception { |
| 8417 |
testTypes("/**" + |
| 8418 |
"* @param {string} ph0" + |
| 8419 |
"* @param {string} ph1" + |
| 8420 |
"* @return {string}" + |
| 8421 |
"*/" + |
| 8422 |
"function MSG_CALENDAR_ACCESS_ERROR(ph0, ph1) {return ''}" + |
| 8423 |
"/** @type {Function} */" + |
| 8424 |
"var MSG_CALENDAR_ADD_ERROR = MSG_CALENDAR_ACCESS_ERROR;"); |
| 8425 |
} |
| 8426 |
|
| 8427 |
public void testAddMethodsPrototypeTwoWays() throws Exception { |
| 8428 |
Node js1Node = parseAndTypeCheck( |
| 8429 |
"/** @constructor */function A() {}" + |
| 8430 |
"A.prototype = {m1: 5, m2: true};" + |
| 8431 |
"A.prototype.m3 = 'third property!';"); |
| 8432 |
|
| 8433 |
ObjectType instanceType = getInstanceType(js1Node); |
| 8434 |
assertEquals("A", instanceType.toString()); |
| 8435 |
assertEquals(NATIVE_PROPERTIES_COUNT + 3, |
| 8436 |
instanceType.getPropertiesCount()); |
| 8437 |
checkObjectType(instanceType, "m1", NUMBER_TYPE); |
| 8438 |
checkObjectType(instanceType, "m2", BOOLEAN_TYPE); |
| 8439 |
checkObjectType(instanceType, "m3", STRING_TYPE); |
| 8440 |
} |
| 8441 |
|
| 8442 |
public void testPrototypePropertyTypes() throws Exception { |
| 8443 |
Node js1Node = parseAndTypeCheck( |
| 8444 |
"/** @constructor */function A() {\n" + |
| 8445 |
" /** @type string */ this.m1;\n" + |
| 8446 |
" /** @type Object? */ this.m2 = {};\n" + |
| 8447 |
" /** @type boolean */ this.m3;\n" + |
| 8448 |
"}\n" + |
| 8449 |
"/** @type string */ A.prototype.m4;\n" + |
| 8450 |
"/** @type number */ A.prototype.m5 = 0;\n" + |
| 8451 |
"/** @type boolean */ A.prototype.m6;\n"); |
| 8452 |
|
| 8453 |
ObjectType instanceType = getInstanceType(js1Node); |
| 8454 |
assertEquals(NATIVE_PROPERTIES_COUNT + 6, |
| 8455 |
instanceType.getPropertiesCount()); |
| 8456 |
checkObjectType(instanceType, "m1", STRING_TYPE); |
| 8457 |
checkObjectType(instanceType, "m2", |
| 8458 |
createUnionType(OBJECT_TYPE, NULL_TYPE)); |
| 8459 |
checkObjectType(instanceType, "m3", BOOLEAN_TYPE); |
| 8460 |
checkObjectType(instanceType, "m4", STRING_TYPE); |
| 8461 |
checkObjectType(instanceType, "m5", NUMBER_TYPE); |
| 8462 |
checkObjectType(instanceType, "m6", BOOLEAN_TYPE); |
| 8463 |
} |
| 8464 |
|
| 8465 |
public void testValueTypeBuiltInPrototypePropertyType() throws Exception { |
| 8466 |
Node node = parseAndTypeCheck("\"x\".charAt(0)"); |
| 8467 |
assertTypeEquals(STRING_TYPE, node.getFirstChild().getFirstChild().getJSType()); |
| 8468 |
} |
| 8469 |
|
| 8470 |
public void testDeclareBuiltInConstructor() throws Exception { |
| 8471 |
|
| 8472 |
|
| 8473 |
Node node = parseAndTypeCheck( |
| 8474 |
"/** @constructor */ var String = function(opt_str) {};\n" + |
| 8475 |
"(new String(\"x\")).charAt(0)"); |
| 8476 |
assertTypeEquals(STRING_TYPE, node.getLastChild().getFirstChild().getJSType()); |
| 8477 |
} |
| 8478 |
|
| 8479 |
public void testExtendBuiltInType1() throws Exception { |
| 8480 |
String externs = |
| 8481 |
"/** @constructor */ var String = function(opt_str) {};\n" + |
| 8482 |
"/**\n" + |
| 8483 |
"* @param {number} start\n" + |
| 8484 |
"* @param {number} opt_length\n" + |
| 8485 |
"* @return {string}\n" + |
| 8486 |
"*/\n" + |
| 8487 |
"String.prototype.substr = function(start, opt_length) {};\n"; |
| 8488 |
Node n1 = parseAndTypeCheck(externs + "(new String(\"x\")).substr(0,1);"); |
| 8489 |
assertTypeEquals(STRING_TYPE, n1.getLastChild().getFirstChild().getJSType()); |
| 8490 |
} |
| 8491 |
|
| 8492 |
public void testExtendBuiltInType2() throws Exception { |
| 8493 |
String externs = |
| 8494 |
"/** @constructor */ var String = function(opt_str) {};\n" + |
| 8495 |
"/**\n" + |
| 8496 |
"* @param {number} start\n" + |
| 8497 |
"* @param {number} opt_length\n" + |
| 8498 |
"* @return {string}\n" + |
| 8499 |
"*/\n" + |
| 8500 |
"String.prototype.substr = function(start, opt_length) {};\n"; |
| 8501 |
Node n2 = parseAndTypeCheck(externs + "\"x\".substr(0,1);"); |
| 8502 |
assertTypeEquals(STRING_TYPE, n2.getLastChild().getFirstChild().getJSType()); |
| 8503 |
} |
| 8504 |
|
| 8505 |
public void testExtendFunction1() throws Exception { |
| 8506 |
Node n = parseAndTypeCheck("/**@return {number}*/Function.prototype.f = " + |
| 8507 |
"function() { return 1; };\n" + |
| 8508 |
"(new Function()).f();"); |
| 8509 |
JSType type = n.getLastChild().getLastChild().getJSType(); |
| 8510 |
assertTypeEquals(NUMBER_TYPE, type); |
| 8511 |
} |
| 8512 |
|
| 8513 |
public void testExtendFunction2() throws Exception { |
| 8514 |
Node n = parseAndTypeCheck("/**@return {number}*/Function.prototype.f = " + |
| 8515 |
"function() { return 1; };\n" + |
| 8516 |
"(function() {}).f();"); |
| 8517 |
JSType type = n.getLastChild().getLastChild().getJSType(); |
| 8518 |
assertTypeEquals(NUMBER_TYPE, type); |
| 8519 |
} |
| 8520 |
|
| 8521 |
public void testInheritanceCheck1() throws Exception { |
| 8522 |
testTypes( |
| 8523 |
"/** @constructor */function Super() {};" + |
| 8524 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8525 |
"Sub.prototype.foo = function() {};"); |
| 8526 |
} |
| 8527 |
|
| 8528 |
public void testInheritanceCheck2() throws Exception { |
| 8529 |
testTypes( |
| 8530 |
"/** @constructor */function Super() {};" + |
| 8531 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8532 |
"/** @override */Sub.prototype.foo = function() {};", |
| 8533 |
"property foo not defined on any superclass of Sub"); |
| 8534 |
} |
| 8535 |
|
| 8536 |
public void testInheritanceCheck3() throws Exception { |
| 8537 |
testTypes( |
| 8538 |
"/** @constructor */function Super() {};" + |
| 8539 |
"Super.prototype.foo = function() {};" + |
| 8540 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8541 |
"Sub.prototype.foo = function() {};", |
| 8542 |
"property foo already defined on superclass Super; " + |
| 8543 |
"use @override to override it"); |
| 8544 |
} |
| 8545 |
|
| 8546 |
public void testInheritanceCheck4() throws Exception { |
| 8547 |
testTypes( |
| 8548 |
"/** @constructor */function Super() {};" + |
| 8549 |
"Super.prototype.foo = function() {};" + |
| 8550 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8551 |
"/** @override */Sub.prototype.foo = function() {};"); |
| 8552 |
} |
| 8553 |
|
| 8554 |
public void testInheritanceCheck5() throws Exception { |
| 8555 |
testTypes( |
| 8556 |
"/** @constructor */function Root() {};" + |
| 8557 |
"Root.prototype.foo = function() {};" + |
| 8558 |
"/** @constructor\n @extends {Root} */function Super() {};" + |
| 8559 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8560 |
"Sub.prototype.foo = function() {};", |
| 8561 |
"property foo already defined on superclass Root; " + |
| 8562 |
"use @override to override it"); |
| 8563 |
} |
| 8564 |
|
| 8565 |
public void testInheritanceCheck6() throws Exception { |
| 8566 |
testTypes( |
| 8567 |
"/** @constructor */function Root() {};" + |
| 8568 |
"Root.prototype.foo = function() {};" + |
| 8569 |
"/** @constructor\n @extends {Root} */function Super() {};" + |
| 8570 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8571 |
"/** @override */Sub.prototype.foo = function() {};"); |
| 8572 |
} |
| 8573 |
|
| 8574 |
public void testInheritanceCheck7() throws Exception { |
| 8575 |
testTypes( |
| 8576 |
"var goog = {};" + |
| 8577 |
"/** @constructor */goog.Super = function() {};" + |
| 8578 |
"goog.Super.prototype.foo = 3;" + |
| 8579 |
"/** @constructor\n @extends {goog.Super} */goog.Sub = function() {};" + |
| 8580 |
"goog.Sub.prototype.foo = 5;"); |
| 8581 |
} |
| 8582 |
|
| 8583 |
public void testInheritanceCheck8() throws Exception { |
| 8584 |
testTypes( |
| 8585 |
"var goog = {};" + |
| 8586 |
"/** @constructor */goog.Super = function() {};" + |
| 8587 |
"goog.Super.prototype.foo = 3;" + |
| 8588 |
"/** @constructor\n @extends {goog.Super} */goog.Sub = function() {};" + |
| 8589 |
"/** @override */goog.Sub.prototype.foo = 5;"); |
| 8590 |
} |
| 8591 |
|
| 8592 |
public void testInheritanceCheck9_1() throws Exception { |
| 8593 |
testTypes( |
| 8594 |
"/** @constructor */function Super() {};" + |
| 8595 |
"Super.prototype.foo = function() { return 3; };" + |
| 8596 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8597 |
"/** @override\n @return {number} */Sub.prototype.foo =\n" + |
| 8598 |
"function() { return 1; };"); |
| 8599 |
} |
| 8600 |
|
| 8601 |
public void testInheritanceCheck9_2() throws Exception { |
| 8602 |
testTypes( |
| 8603 |
"/** @constructor */function Super() {};" + |
| 8604 |
"/** @return {number} */" + |
| 8605 |
"Super.prototype.foo = function() { return 1; };" + |
| 8606 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8607 |
"/** @override */Sub.prototype.foo =\n" + |
| 8608 |
"function() {};"); |
| 8609 |
} |
| 8610 |
|
| 8611 |
public void testInheritanceCheck9_3() throws Exception { |
| 8612 |
testTypes( |
| 8613 |
"/** @constructor */function Super() {};" + |
| 8614 |
"/** @return {number} */" + |
| 8615 |
"Super.prototype.foo = function() { return 1; };" + |
| 8616 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8617 |
"/** @override\n @return {string} */Sub.prototype.foo =\n" + |
| 8618 |
"function() { return \"some string\" };", |
| 8619 |
"mismatch of the foo property type and the type of the property it " + |
| 8620 |
"overrides from superclass Super\n" + |
| 8621 |
"original: function (this:Super): number\n" + |
| 8622 |
"override: function (this:Sub): string"); |
| 8623 |
} |
| 8624 |
|
| 8625 |
public void testInheritanceCheck10_1() throws Exception { |
| 8626 |
testTypes( |
| 8627 |
"/** @constructor */function Root() {};" + |
| 8628 |
"Root.prototype.foo = function() { return 3; };" + |
| 8629 |
"/** @constructor\n @extends {Root} */function Super() {};" + |
| 8630 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8631 |
"/** @override\n @return {number} */Sub.prototype.foo =\n" + |
| 8632 |
"function() { return 1; };"); |
| 8633 |
} |
| 8634 |
|
| 8635 |
public void testInheritanceCheck10_2() throws Exception { |
| 8636 |
testTypes( |
| 8637 |
"/** @constructor */function Root() {};" + |
| 8638 |
"/** @return {number} */" + |
| 8639 |
"Root.prototype.foo = function() { return 1; };" + |
| 8640 |
"/** @constructor\n @extends {Root} */function Super() {};" + |
| 8641 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8642 |
"/** @override */Sub.prototype.foo =\n" + |
| 8643 |
"function() {};"); |
| 8644 |
} |
| 8645 |
|
| 8646 |
public void testInheritanceCheck10_3() throws Exception { |
| 8647 |
testTypes( |
| 8648 |
"/** @constructor */function Root() {};" + |
| 8649 |
"/** @return {number} */" + |
| 8650 |
"Root.prototype.foo = function() { return 1; };" + |
| 8651 |
"/** @constructor\n @extends {Root} */function Super() {};" + |
| 8652 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8653 |
"/** @override\n @return {string} */Sub.prototype.foo =\n" + |
| 8654 |
"function() { return \"some string\" };", |
| 8655 |
"mismatch of the foo property type and the type of the property it " + |
| 8656 |
"overrides from superclass Root\n" + |
| 8657 |
"original: function (this:Root): number\n" + |
| 8658 |
"override: function (this:Sub): string"); |
| 8659 |
} |
| 8660 |
|
| 8661 |
public void testInterfaceInheritanceCheck11() throws Exception { |
| 8662 |
testTypes( |
| 8663 |
"/** @constructor */function Super() {};" + |
| 8664 |
"/** @param {number} bar */Super.prototype.foo = function(bar) {};" + |
| 8665 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8666 |
"/** @override\n @param {string} bar */Sub.prototype.foo =\n" + |
| 8667 |
"function(bar) {};", |
| 8668 |
"mismatch of the foo property type and the type of the property it " + |
| 8669 |
"overrides from superclass Super\n" + |
| 8670 |
"original: function (this:Super, number): undefined\n" + |
| 8671 |
"override: function (this:Sub, string): undefined"); |
| 8672 |
} |
| 8673 |
|
| 8674 |
public void testInheritanceCheck12() throws Exception { |
| 8675 |
testTypes( |
| 8676 |
"var goog = {};" + |
| 8677 |
"/** @constructor */goog.Super = function() {};" + |
| 8678 |
"goog.Super.prototype.foo = 3;" + |
| 8679 |
"/** @constructor\n @extends {goog.Super} */goog.Sub = function() {};" + |
| 8680 |
"/** @override */goog.Sub.prototype.foo = \"some string\";"); |
| 8681 |
} |
| 8682 |
|
| 8683 |
public void testInheritanceCheck13() throws Exception { |
| 8684 |
testTypes( |
| 8685 |
"var goog = {};\n" + |
| 8686 |
"/** @constructor\n @extends {goog.Missing} */function Sub() {};" + |
| 8687 |
"/** @override */Sub.prototype.foo = function() {};", |
| 8688 |
"Bad type annotation. Unknown type goog.Missing"); |
| 8689 |
} |
| 8690 |
|
| 8691 |
public void testInheritanceCheck14() throws Exception { |
| 8692 |
testClosureTypes( |
| 8693 |
"var goog = {};\n" + |
| 8694 |
"/** @constructor\n @extends {goog.Missing} */\n" + |
| 8695 |
"goog.Super = function() {};\n" + |
| 8696 |
"/** @constructor\n @extends {goog.Super} */function Sub() {};" + |
| 8697 |
"/** @override */Sub.prototype.foo = function() {};", |
| 8698 |
"Bad type annotation. Unknown type goog.Missing"); |
| 8699 |
} |
| 8700 |
|
| 8701 |
public void testInheritanceCheck15() throws Exception { |
| 8702 |
testTypes( |
| 8703 |
"/** @constructor */function Super() {};" + |
| 8704 |
"/** @param {number} bar */Super.prototype.foo;" + |
| 8705 |
"/** @constructor\n @extends {Super} */function Sub() {};" + |
| 8706 |
"/** @override\n @param {number} bar */Sub.prototype.foo =\n" + |
| 8707 |
"function(bar) {};"); |
| 8708 |
} |
| 8709 |
|
| 8710 |
public void testInheritanceCheck16() throws Exception { |
| 8711 |
testTypes( |
| 8712 |
"var goog = {};" + |
| 8713 |
"/** @constructor */goog.Super = function() {};" + |
| 8714 |
"/** @type {number} */ goog.Super.prototype.foo = 3;" + |
| 8715 |
"/** @constructor\n @extends {goog.Super} */goog.Sub = function() {};" + |
| 8716 |
"/** @type {number} */ goog.Sub.prototype.foo = 5;", |
| 8717 |
"property foo already defined on superclass goog.Super; " + |
| 8718 |
"use @override to override it"); |
| 8719 |
} |
| 8720 |
|
| 8721 |
public void testInheritanceCheck17() throws Exception { |
| 8722 |
|
| 8723 |
|
| 8724 |
reportMissingOverrides = CheckLevel.OFF; |
| 8725 |
testTypes( |
| 8726 |
"var goog = {};" + |
| 8727 |
"/** @constructor */goog.Super = function() {};" + |
| 8728 |
"/** @param {number} x */ goog.Super.prototype.foo = function(x) {};" + |
| 8729 |
"/** @constructor\n @extends {goog.Super} */goog.Sub = function() {};" + |
| 8730 |
"/** @param {string} x */ goog.Sub.prototype.foo = function(x) {};", |
| 8731 |
"mismatch of the foo property type and the type of the property it " + |
| 8732 |
"overrides from superclass goog.Super\n" + |
| 8733 |
"original: function (this:goog.Super, number): undefined\n" + |
| 8734 |
"override: function (this:goog.Sub, string): undefined"); |
| 8735 |
} |
| 8736 |
|
| 8737 |
public void testInterfacePropertyOverride1() throws Exception { |
| 8738 |
testTypes( |
| 8739 |
"/** @interface */function Super() {};" + |
| 8740 |
"/** @desc description */Super.prototype.foo = function() {};" + |
| 8741 |
"/** @interface\n @extends {Super} */function Sub() {};" + |
| 8742 |
"/** @desc description */Sub.prototype.foo = function() {};"); |
| 8743 |
} |
| 8744 |
|
| 8745 |
public void testInterfacePropertyOverride2() throws Exception { |
| 8746 |
testTypes( |
| 8747 |
"/** @interface */function Root() {};" + |
| 8748 |
"/** @desc description */Root.prototype.foo = function() {};" + |
| 8749 |
"/** @interface\n @extends {Root} */function Super() {};" + |
| 8750 |
"/** @interface\n @extends {Super} */function Sub() {};" + |
| 8751 |
"/** @desc description */Sub.prototype.foo = function() {};"); |
| 8752 |
} |
| 8753 |
|
| 8754 |
public void testInterfaceInheritanceCheck1() throws Exception { |
| 8755 |
testTypes( |
| 8756 |
"/** @interface */function Super() {};" + |
| 8757 |
"/** @desc description */Super.prototype.foo = function() {};" + |
| 8758 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8759 |
"Sub.prototype.foo = function() {};", |
| 8760 |
"property foo already defined on interface Super; use @override to " + |
| 8761 |
"override it"); |
| 8762 |
} |
| 8763 |
|
| 8764 |
public void testInterfaceInheritanceCheck2() throws Exception { |
| 8765 |
testTypes( |
| 8766 |
"/** @interface */function Super() {};" + |
| 8767 |
"/** @desc description */Super.prototype.foo = function() {};" + |
| 8768 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8769 |
"/** @override */Sub.prototype.foo = function() {};"); |
| 8770 |
} |
| 8771 |
|
| 8772 |
public void testInterfaceInheritanceCheck3() throws Exception { |
| 8773 |
testTypes( |
| 8774 |
"/** @interface */function Root() {};" + |
| 8775 |
"/** @return {number} */Root.prototype.foo = function() {};" + |
| 8776 |
"/** @interface\n @extends {Root} */function Super() {};" + |
| 8777 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8778 |
"/** @return {number} */Sub.prototype.foo = function() { return 1;};", |
| 8779 |
"property foo already defined on interface Root; use @override to " + |
| 8780 |
"override it"); |
| 8781 |
} |
| 8782 |
|
| 8783 |
public void testInterfaceInheritanceCheck4() throws Exception { |
| 8784 |
testTypes( |
| 8785 |
"/** @interface */function Root() {};" + |
| 8786 |
"/** @return {number} */Root.prototype.foo = function() {};" + |
| 8787 |
"/** @interface\n @extends {Root} */function Super() {};" + |
| 8788 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8789 |
"/** @override\n * @return {number} */Sub.prototype.foo =\n" + |
| 8790 |
"function() { return 1;};"); |
| 8791 |
} |
| 8792 |
|
| 8793 |
public void testInterfaceInheritanceCheck5() throws Exception { |
| 8794 |
testTypes( |
| 8795 |
"/** @interface */function Super() {};" + |
| 8796 |
"/** @return {string} */Super.prototype.foo = function() {};" + |
| 8797 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8798 |
"/** @override\n @return {number} */Sub.prototype.foo =\n" + |
| 8799 |
"function() { return 1; };", |
| 8800 |
"mismatch of the foo property type and the type of the property it " + |
| 8801 |
"overrides from interface Super\n" + |
| 8802 |
"original: function (this:Super): string\n" + |
| 8803 |
"override: function (this:Sub): number"); |
| 8804 |
} |
| 8805 |
|
| 8806 |
public void testInterfaceInheritanceCheck6() throws Exception { |
| 8807 |
testTypes( |
| 8808 |
"/** @interface */function Root() {};" + |
| 8809 |
"/** @return {string} */Root.prototype.foo = function() {};" + |
| 8810 |
"/** @interface\n @extends {Root} */function Super() {};" + |
| 8811 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8812 |
"/** @override\n @return {number} */Sub.prototype.foo =\n" + |
| 8813 |
"function() { return 1; };", |
| 8814 |
"mismatch of the foo property type and the type of the property it " + |
| 8815 |
"overrides from interface Root\n" + |
| 8816 |
"original: function (this:Root): string\n" + |
| 8817 |
"override: function (this:Sub): number"); |
| 8818 |
} |
| 8819 |
|
| 8820 |
public void testInterfaceInheritanceCheck7() throws Exception { |
| 8821 |
testTypes( |
| 8822 |
"/** @interface */function Super() {};" + |
| 8823 |
"/** @param {number} bar */Super.prototype.foo = function(bar) {};" + |
| 8824 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8825 |
"/** @override\n @param {string} bar */Sub.prototype.foo =\n" + |
| 8826 |
"function(bar) {};", |
| 8827 |
"mismatch of the foo property type and the type of the property it " + |
| 8828 |
"overrides from interface Super\n" + |
| 8829 |
"original: function (this:Super, number): undefined\n" + |
| 8830 |
"override: function (this:Sub, string): undefined"); |
| 8831 |
} |
| 8832 |
|
| 8833 |
public void testInterfaceInheritanceCheck8() throws Exception { |
| 8834 |
testTypes( |
| 8835 |
"/** @constructor\n @implements {Super} */function Sub() {};" + |
| 8836 |
"/** @override */Sub.prototype.foo = function() {};", |
| 8837 |
new String[] { |
| 8838 |
"Bad type annotation. Unknown type Super", |
| 8839 |
"property foo not defined on any superclass of Sub" |
| 8840 |
}); |
| 8841 |
} |
| 8842 |
|
| 8843 |
public void testInterfaceInheritanceCheck9() throws Exception { |
| 8844 |
testTypes( |
| 8845 |
"/** @interface */ function I() {}" + |
| 8846 |
"/** @return {number} */ I.prototype.bar = function() {};" + |
| 8847 |
"/** @constructor */ function F() {}" + |
| 8848 |
"/** @return {number} */ F.prototype.bar = function() {return 3; };" + |
| 8849 |
"/** @return {number} */ F.prototype.foo = function() {return 3; };" + |
| 8850 |
"/** @constructor \n * @extends {F} \n * @implements {I} */ " + |
| 8851 |
"function G() {}" + |
| 8852 |
"/** @return {string} */ function f() { return new G().bar(); }", |
| 8853 |
"inconsistent return type\n" + |
| 8854 |
"found : number\n" + |
| 8855 |
"required: string"); |
| 8856 |
} |
| 8857 |
|
| 8858 |
public void testInterfaceInheritanceCheck10() throws Exception { |
| 8859 |
testTypes( |
| 8860 |
"/** @interface */ function I() {}" + |
| 8861 |
"/** @return {number} */ I.prototype.bar = function() {};" + |
| 8862 |
"/** @constructor */ function F() {}" + |
| 8863 |
"/** @return {number} */ F.prototype.foo = function() {return 3; };" + |
| 8864 |
"/** @constructor \n * @extends {F} \n * @implements {I} */ " + |
| 8865 |
"function G() {}" + |
| 8866 |
"/** @return {number} \n * @override */ " + |
| 8867 |
"G.prototype.bar = G.prototype.foo;" + |
| 8868 |
"/** @return {string} */ function f() { return new G().bar(); }", |
| 8869 |
"inconsistent return type\n" + |
| 8870 |
"found : number\n" + |
| 8871 |
"required: string"); |
| 8872 |
} |
| 8873 |
|
| 8874 |
public void testInterfaceInheritanceCheck12() throws Exception { |
| 8875 |
testTypes( |
| 8876 |
"/** @interface */ function I() {};\n" + |
| 8877 |
"/** @type {string} */ I.prototype.foobar;\n" + |
| 8878 |
"/** \n * @constructor \n * @implements {I} */\n" + |
| 8879 |
"function C() {\n" + |
| 8880 |
"/** \n * @type {number} */ this.foobar = 2;};\n" + |
| 8881 |
"/** @type {I} */ \n var test = new C(); alert(test.foobar);", |
| 8882 |
"mismatch of the foobar property type and the type of the property" + |
| 8883 |
" it overrides from interface I\n" + |
| 8884 |
"original: string\n" + |
| 8885 |
"override: number"); |
| 8886 |
} |
| 8887 |
|
| 8888 |
public void testInterfaceInheritanceCheck13() throws Exception { |
| 8889 |
testTypes( |
| 8890 |
"function abstractMethod() {};\n" + |
| 8891 |
"/** @interface */var base = function() {};\n" + |
| 8892 |
"/** @extends {base} \n @interface */ var Int = function() {}\n" + |
| 8893 |
"/** @type {{bar : !Function}} */ var x; \n" + |
| 8894 |
"/** @type {!Function} */ base.prototype.bar = abstractMethod; \n" + |
| 8895 |
"/** @type {Int} */ var foo;\n" + |
| 8896 |
"foo.bar();"); |
| 8897 |
} |
| 8898 |
|
| 8899 |
public void testInterfacePropertyNotImplemented() throws Exception { |
| 8900 |
testTypes( |
| 8901 |
"/** @interface */function Int() {};" + |
| 8902 |
"/** @desc description */Int.prototype.foo = function() {};" + |
| 8903 |
"/** @constructor\n @implements {Int} */function Foo() {};", |
| 8904 |
"property foo on interface Int is not implemented by type Foo"); |
| 8905 |
} |
| 8906 |
|
| 8907 |
public void testInterfacePropertyNotImplemented2() throws Exception { |
| 8908 |
testTypes( |
| 8909 |
"/** @interface */function Int() {};" + |
| 8910 |
"/** @desc description */Int.prototype.foo = function() {};" + |
| 8911 |
"/** @interface \n @extends {Int} */function Int2() {};" + |
| 8912 |
"/** @constructor\n @implements {Int2} */function Foo() {};", |
| 8913 |
"property foo on interface Int is not implemented by type Foo"); |
| 8914 |
} |
| 8915 |
|
| 8916 |
public void testStubConstructorImplementingInterface() throws Exception { |
| 8917 |
|
| 8918 |
|
| 8919 |
testTypes( |
| 8920 |
|
| 8921 |
"/** @interface */ function Int() {}\n" + |
| 8922 |
"/** @desc description */Int.prototype.foo = function() {};" + |
| 8923 |
"/** @constructor \n @implements {Int} */ var Foo;\n", |
| 8924 |
"", null, false); |
| 8925 |
} |
| 8926 |
|
| 8927 |
public void testObjectLiteral() throws Exception { |
| 8928 |
Node n = parseAndTypeCheck("var a = {m1: 7, m2: 'hello'}"); |
| 8929 |
|
| 8930 |
Node nameNode = n.getFirstChild().getFirstChild(); |
| 8931 |
Node objectNode = nameNode.getFirstChild(); |
| 8932 |
|
| 8933 |
|
| 8934 |
assertEquals(Token.NAME, nameNode.getType()); |
| 8935 |
assertEquals(Token.OBJECTLIT, objectNode.getType()); |
| 8936 |
|
| 8937 |
|
| 8938 |
ObjectType objectType = |
| 8939 |
(ObjectType) objectNode.getJSType(); |
| 8940 |
assertTypeEquals(NUMBER_TYPE, objectType.getPropertyType("m1")); |
| 8941 |
assertTypeEquals(STRING_TYPE, objectType.getPropertyType("m2")); |
| 8942 |
|
| 8943 |
|
| 8944 |
assertTypeEquals(objectType, nameNode.getJSType()); |
| 8945 |
} |
| 8946 |
|
| 8947 |
public void testObjectLiteralDeclaration1() throws Exception { |
| 8948 |
testTypes( |
| 8949 |
"var x = {" + |
| 8950 |
"/** @type {boolean} */ abc: true," + |
| 8951 |
"/** @type {number} */ 'def': 0," + |
| 8952 |
"/** @type {string} */ 3: 'fgh'" + |
| 8953 |
"};"); |
| 8954 |
} |
| 8955 |
|
| 8956 |
public void testObjectLiteralDeclaration2() throws Exception { |
| 8957 |
testTypes( |
| 8958 |
"var x = {" + |
| 8959 |
" /** @type {boolean} */ abc: true" + |
| 8960 |
"};" + |
| 8961 |
"x.abc = 0;", |
| 8962 |
"assignment to property abc of x\n" + |
| 8963 |
"found : number\n" + |
| 8964 |
"required: boolean"); |
| 8965 |
} |
| 8966 |
|
| 8967 |
public void testObjectLiteralDeclaration3() throws Exception { |
| 8968 |
testTypes( |
| 8969 |
"/** @param {{foo: !Function}} x */ function f(x) {}" + |
| 8970 |
"f({foo: function() {}});"); |
| 8971 |
} |
| 8972 |
|
| 8973 |
public void testObjectLiteralDeclaration4() throws Exception { |
| 8974 |
testClosureTypes( |
| 8975 |
"var x = {" + |
| 8976 |
" /** @param {boolean} x */ abc: function(x) {}" + |
| 8977 |
"};" + |
| 8978 |
"/**\n" + |
| 8979 |
" * @param {string} x\n" + |
| 8980 |
" * @suppress {duplicate}\n" + |
| 8981 |
" */ x.abc = function(x) {};", |
| 8982 |
"assignment to property abc of x\n" + |
| 8983 |
"found : function (string): undefined\n" + |
| 8984 |
"required: function (boolean): undefined"); |
| 8985 |
|
| 8986 |
|
| 8987 |
|
| 8988 |
} |
| 8989 |
|
| 8990 |
public void testObjectLiteralDeclaration5() throws Exception { |
| 8991 |
testTypes( |
| 8992 |
"var x = {" + |
| 8993 |
" /** @param {boolean} x */ abc: function(x) {}" + |
| 8994 |
"};" + |
| 8995 |
"/**\n" + |
| 8996 |
" * @param {boolean} x\n" + |
| 8997 |
" * @suppress {duplicate}\n" + |
| 8998 |
" */ x.abc = function(x) {};"); |
| 8999 |
} |
| 9000 |
|
| 9001 |
public void testObjectLiteralDeclaration6() throws Exception { |
| 9002 |
testTypes( |
| 9003 |
"var x = {};" + |
| 9004 |
"/**\n" + |
| 9005 |
" * @param {boolean} x\n" + |
| 9006 |
" * @suppress {duplicate}\n" + |
| 9007 |
" */ x.abc = function(x) {};" + |
| 9008 |
"x = {" + |
| 9009 |
" /**\n" + |
| 9010 |
" * @param {boolean} x\n" + |
| 9011 |
" * @suppress {duplicate}\n" + |
| 9012 |
" */" + |
| 9013 |
" abc: function(x) {}" + |
| 9014 |
"};"); |
| 9015 |
} |
| 9016 |
|
| 9017 |
public void testObjectLiteralDeclaration7() throws Exception { |
| 9018 |
testTypes( |
| 9019 |
"var x = {};" + |
| 9020 |
"/**\n" + |
| 9021 |
" * @type {function(boolean): undefined}\n" + |
| 9022 |
" */ x.abc = function(x) {};" + |
| 9023 |
"x = {" + |
| 9024 |
" /**\n" + |
| 9025 |
" * @param {boolean} x\n" + |
| 9026 |
" * @suppress {duplicate}\n" + |
| 9027 |
" */" + |
| 9028 |
" abc: function(x) {}" + |
| 9029 |
"};"); |
| 9030 |
} |
| 9031 |
|
| 9032 |
public void testCallDateConstructorAsFunction() throws Exception { |
| 9033 |
|
| 9034 |
|
| 9035 |
Node n = parseAndTypeCheck("Date()"); |
| 9036 |
assertTypeEquals(STRING_TYPE, n.getFirstChild().getFirstChild().getJSType()); |
| 9037 |
} |
| 9038 |
|
| 9039 |
|
| 9040 |
|
| 9041 |
|
| 9042 |
public void testCallErrorConstructorAsFunction() throws Exception { |
| 9043 |
Node n = parseAndTypeCheck("Error('x')"); |
| 9044 |
assertTypeEquals(ERROR_TYPE, |
| 9045 |
n.getFirstChild().getFirstChild().getJSType()); |
| 9046 |
} |
| 9047 |
|
| 9048 |
public void testCallArrayConstructorAsFunction() throws Exception { |
| 9049 |
Node n = parseAndTypeCheck("Array()"); |
| 9050 |
assertTypeEquals(ARRAY_TYPE, |
| 9051 |
n.getFirstChild().getFirstChild().getJSType()); |
| 9052 |
} |
| 9053 |
|
| 9054 |
public void testPropertyTypeOfUnionType() throws Exception { |
| 9055 |
testTypes("var a = {};" + |
| 9056 |
"/** @constructor */ a.N = function() {};\n" + |
| 9057 |
"a.N.prototype.p = 1;\n" + |
| 9058 |
"/** @constructor */ a.S = function() {};\n" + |
| 9059 |
"a.S.prototype.p = 'a';\n" + |
| 9060 |
"/** @param {!a.N|!a.S} x\n@return {string} */\n" + |
| 9061 |
"var f = function(x) { return x.p; };", |
| 9062 |
"inconsistent return type\n" + |
| 9063 |
"found : (number|string)\n" + |
| 9064 |
"required: string"); |
| 9065 |
} |
| 9066 |
|
| 9067 |
|
| 9068 |
|
| 9069 |
|
| 9070 |
|
| 9071 |
|
| 9072 |
|
| 9073 |
|
| 9074 |
|
| 9075 |
|
| 9076 |
|
| 9077 |
|
| 9078 |
|
| 9079 |
|
| 9080 |
|
| 9081 |
|
| 9082 |
|
| 9083 |
|
| 9084 |
|
| 9085 |
|
| 9086 |
|
| 9087 |
|
| 9088 |
|
| 9089 |
|
| 9090 |
|
| 9091 |
|
| 9092 |
|
| 9093 |
|
| 9094 |
|
| 9095 |
|
| 9096 |
|
| 9097 |
|
| 9098 |
public void testAnnotatedPropertyOnInterface1() throws Exception { |
| 9099 |
|
| 9100 |
|
| 9101 |
testTypes("/** @interface */ u.T = function() {};\n" + |
| 9102 |
"/** @return {number} */ u.T.prototype.f = function() {};"); |
| 9103 |
} |
| 9104 |
|
| 9105 |
public void testAnnotatedPropertyOnInterface2() throws Exception { |
| 9106 |
testTypes("/** @interface */ u.T = function() {};\n" + |
| 9107 |
"/** @return {number} */ u.T.prototype.f = function() { };"); |
| 9108 |
} |
| 9109 |
|
| 9110 |
public void testAnnotatedPropertyOnInterface3() throws Exception { |
| 9111 |
testTypes("/** @interface */ function T() {};\n" + |
| 9112 |
"/** @return {number} */ T.prototype.f = function() { };"); |
| 9113 |
} |
| 9114 |
|
| 9115 |
public void testAnnotatedPropertyOnInterface4() throws Exception { |
| 9116 |
testTypes( |
| 9117 |
CLOSURE_DEFS + |
| 9118 |
"/** @interface */ function T() {};\n" + |
| 9119 |
"/** @return {number} */ T.prototype.f = goog.abstractMethod;"); |
| 9120 |
} |
| 9121 |
|
| 9122 |
|
| 9123 |
|
| 9124 |
|
| 9125 |
|
| 9126 |
|
| 9127 |
|
| 9128 |
|
| 9129 |
|
| 9130 |
|
| 9131 |
|
| 9132 |
|
| 9133 |
|
| 9134 |
public void testWarnUnannotatedPropertyOnInterface5() throws Exception { |
| 9135 |
testTypes("/** @interface */ u.T = function () {};\n" + |
| 9136 |
"/** @desc x does something */u.T.prototype.x = function() {};"); |
| 9137 |
} |
| 9138 |
|
| 9139 |
public void testWarnUnannotatedPropertyOnInterface6() throws Exception { |
| 9140 |
testTypes("/** @interface */ function T() {};\n" + |
| 9141 |
"/** @desc x does something */T.prototype.x = function() {};"); |
| 9142 |
} |
| 9143 |
|
| 9144 |
|
| 9145 |
|
| 9146 |
|
| 9147 |
|
| 9148 |
|
| 9149 |
|
| 9150 |
|
| 9151 |
|
| 9152 |
public void testDataPropertyOnInterface1() throws Exception { |
| 9153 |
testTypes("/** @interface */ function T() {};\n" + |
| 9154 |
"/** @type {number} */T.prototype.x;"); |
| 9155 |
} |
| 9156 |
|
| 9157 |
public void testDataPropertyOnInterface2() throws Exception { |
| 9158 |
reportMissingOverrides = CheckLevel.OFF; |
| 9159 |
testTypes("/** @interface */ function T() {};\n" + |
| 9160 |
"/** @type {number} */T.prototype.x;\n" + |
| 9161 |
"/** @constructor \n" + |
| 9162 |
" * @implements {T} \n" + |
| 9163 |
" */\n" + |
| 9164 |
"function C() {}\n" + |
| 9165 |
"C.prototype.x = 'foo';", |
| 9166 |
"mismatch of the x property type and the type of the property it " + |
| 9167 |
"overrides from interface T\n" + |
| 9168 |
"original: number\n" + |
| 9169 |
"override: string"); |
| 9170 |
} |
| 9171 |
|
| 9172 |
public void testDataPropertyOnInterface3() throws Exception { |
| 9173 |
testTypes("/** @interface */ function T() {};\n" + |
| 9174 |
"/** @type {number} */T.prototype.x;\n" + |
| 9175 |
"/** @constructor \n" + |
| 9176 |
" * @implements {T} \n" + |
| 9177 |
" */\n" + |
| 9178 |
"function C() {}\n" + |
| 9179 |
"/** @override */\n" + |
| 9180 |
"C.prototype.x = 'foo';", |
| 9181 |
"mismatch of the x property type and the type of the property it " + |
| 9182 |
"overrides from interface T\n" + |
| 9183 |
"original: number\n" + |
| 9184 |
"override: string"); |
| 9185 |
} |
| 9186 |
|
| 9187 |
public void testDataPropertyOnInterface4() throws Exception { |
| 9188 |
testTypes("/** @interface */ function T() {};\n" + |
| 9189 |
"/** @type {number} */T.prototype.x;\n" + |
| 9190 |
"/** @constructor \n" + |
| 9191 |
" * @implements {T} \n" + |
| 9192 |
" */\n" + |
| 9193 |
"function C() { /** @type {string} */ \n this.x = 'foo'; }\n", |
| 9194 |
"mismatch of the x property type and the type of the property it " + |
| 9195 |
"overrides from interface T\n" + |
| 9196 |
"original: number\n" + |
| 9197 |
"override: string"); |
| 9198 |
} |
| 9199 |
|
| 9200 |
public void testWarnDataPropertyOnInterface3() throws Exception { |
| 9201 |
testTypes("/** @interface */ u.T = function () {};\n" + |
| 9202 |
"/** @type {number} */u.T.prototype.x = 1;", |
| 9203 |
"interface members can only be empty property declarations, " |
| 9204 |
+ "empty functions, or goog.abstractMethod"); |
| 9205 |
} |
| 9206 |
|
| 9207 |
public void testWarnDataPropertyOnInterface4() throws Exception { |
| 9208 |
testTypes("/** @interface */ function T() {};\n" + |
| 9209 |
"/** @type {number} */T.prototype.x = 1;", |
| 9210 |
"interface members can only be empty property declarations, " |
| 9211 |
+ "empty functions, or goog.abstractMethod"); |
| 9212 |
} |
| 9213 |
|
| 9214 |
|
| 9215 |
|
| 9216 |
|
| 9217 |
|
| 9218 |
|
| 9219 |
|
| 9220 |
|
| 9221 |
|
| 9222 |
|
| 9223 |
|
| 9224 |
|
| 9225 |
|
| 9226 |
|
| 9227 |
|
| 9228 |
|
| 9229 |
|
| 9230 |
|
| 9231 |
|
| 9232 |
|
| 9233 |
|
| 9234 |
|
| 9235 |
|
| 9236 |
|
| 9237 |
|
| 9238 |
|
| 9239 |
|
| 9240 |
|
| 9241 |
|
| 9242 |
|
| 9243 |
public void testErrorMismatchingPropertyOnInterface4() throws Exception { |
| 9244 |
testTypes("/** @interface */ u.T = function () {};\n" + |
| 9245 |
"/** @param {Number} foo */u.T.prototype.x =\n" + |
| 9246 |
"function() {};", |
| 9247 |
"parameter foo does not appear in u.T.prototype.x's parameter list"); |
| 9248 |
} |
| 9249 |
|
| 9250 |
public void testErrorMismatchingPropertyOnInterface5() throws Exception { |
| 9251 |
testTypes("/** @interface */ function T() {};\n" + |
| 9252 |
"/** @type {number} */T.prototype.x = function() { };", |
| 9253 |
"assignment to property x of T.prototype\n" + |
| 9254 |
"found : function (): undefined\n" + |
| 9255 |
"required: number"); |
| 9256 |
} |
| 9257 |
|
| 9258 |
public void testErrorMismatchingPropertyOnInterface6() throws Exception { |
| 9259 |
testClosureTypesMultipleWarnings( |
| 9260 |
"/** @interface */ function T() {};\n" + |
| 9261 |
"/** @return {number} */T.prototype.x = 1", |
| 9262 |
Lists.newArrayList( |
| 9263 |
"assignment to property x of T.prototype\n" + |
| 9264 |
"found : number\n" + |
| 9265 |
"required: function (this:T): number", |
| 9266 |
"interface members can only be empty property declarations, " + |
| 9267 |
"empty functions, or goog.abstractMethod")); |
| 9268 |
} |
| 9269 |
|
| 9270 |
public void testInterfaceNonEmptyFunction() throws Exception { |
| 9271 |
testTypes("/** @interface */ function T() {};\n" + |
| 9272 |
"T.prototype.x = function() { return 'foo'; }", |
| 9273 |
"interface member functions must have an empty body" |
| 9274 |
); |
| 9275 |
} |
| 9276 |
|
| 9277 |
public void testDoubleNestedInterface() throws Exception { |
| 9278 |
testTypes("/** @interface */ var I1 = function() {};\n" + |
| 9279 |
"/** @interface */ I1.I2 = function() {};\n" + |
| 9280 |
"/** @interface */ I1.I2.I3 = function() {};\n"); |
| 9281 |
} |
| 9282 |
|
| 9283 |
public void testStaticDataPropertyOnNestedInterface() throws Exception { |
| 9284 |
testTypes("/** @interface */ var I1 = function() {};\n" + |
| 9285 |
"/** @interface */ I1.I2 = function() {};\n" + |
| 9286 |
"/** @type {number} */ I1.I2.x = 1;\n"); |
| 9287 |
} |
| 9288 |
|
| 9289 |
public void testInterfaceInstantiation() throws Exception { |
| 9290 |
testTypes("/** @interface */var f = function(){}; new f", |
| 9291 |
"cannot instantiate non-constructor"); |
| 9292 |
} |
| 9293 |
|
| 9294 |
public void testPrototypeLoop() throws Exception { |
| 9295 |
testClosureTypesMultipleWarnings( |
| 9296 |
suppressMissingProperty("foo") + |
| 9297 |
"/** @constructor \n * @extends {T} */var T = function() {};" + |
| 9298 |
"alert((new T).foo);", |
| 9299 |
Lists.newArrayList( |
| 9300 |
"Parse error. Cycle detected in inheritance chain of type T", |
| 9301 |
"Could not resolve type in @extends tag of T")); |
| 9302 |
} |
| 9303 |
|
| 9304 |
public void testImplementsLoop() throws Exception { |
| 9305 |
testClosureTypesMultipleWarnings( |
| 9306 |
suppressMissingProperty("foo") + |
| 9307 |
"/** @constructor \n * @implements {T} */var T = function() {};" + |
| 9308 |
"alert((new T).foo);", |
| 9309 |
Lists.newArrayList( |
| 9310 |
"Parse error. Cycle detected in inheritance chain of type T")); |
| 9311 |
} |
| 9312 |
|
| 9313 |
public void testImplementsExtendsLoop() throws Exception { |
| 9314 |
testClosureTypesMultipleWarnings( |
| 9315 |
suppressMissingProperty("foo") + |
| 9316 |
"/** @constructor \n * @implements {F} */var G = function() {};" + |
| 9317 |
"/** @constructor \n * @extends {G} */var F = function() {};" + |
| 9318 |
"alert((new F).foo);", |
| 9319 |
Lists.newArrayList( |
| 9320 |
"Parse error. Cycle detected in inheritance chain of type F")); |
| 9321 |
} |
| 9322 |
|
| 9323 |
public void testInterfaceExtendsLoop() throws Exception { |
| 9324 |
|
| 9325 |
|
| 9326 |
testClosureTypesMultipleWarnings( |
| 9327 |
suppressMissingProperty("foo") + |
| 9328 |
"/** @interface \n * @extends {F} */var G = function() {};" + |
| 9329 |
"/** @interface \n * @extends {G} */var F = function() {};", |
| 9330 |
Lists.newArrayList( |
| 9331 |
"Could not resolve type in @extends tag of G")); |
| 9332 |
} |
| 9333 |
|
| 9334 |
public void testConversionFromInterfaceToRecursiveConstructor() |
| 9335 |
throws Exception { |
| 9336 |
testClosureTypesMultipleWarnings( |
| 9337 |
suppressMissingProperty("foo") + |
| 9338 |
"/** @interface */ var OtherType = function() {}\n" + |
| 9339 |
"/** @implements {MyType} \n * @constructor */\n" + |
| 9340 |
"var MyType = function() {}\n" + |
| 9341 |
"/** @type {MyType} */\n" + |
| 9342 |
"var x = /** @type {!OtherType} */ (new Object());", |
| 9343 |
Lists.newArrayList( |
| 9344 |
"Parse error. Cycle detected in inheritance chain of type MyType", |
| 9345 |
"initializing variable\n" + |
| 9346 |
"found : OtherType\n" + |
| 9347 |
"required: (MyType|null)")); |
| 9348 |
} |
| 9349 |
|
| 9350 |
public void testDirectPrototypeAssign() throws Exception { |
| 9351 |
|
| 9352 |
testTypes( |
| 9353 |
"/** @constructor */ function Foo() {}" + |
| 9354 |
"/** @constructor */ function Bar() {}" + |
| 9355 |
"/** @type {Array} */ Bar.prototype = new Foo()"); |
| 9356 |
} |
| 9357 |
|
| 9358 |
|
| 9359 |
|
| 9360 |
|
| 9361 |
public void testResolutionViaRegistry1() throws Exception { |
| 9362 |
testTypes("/** @constructor */ u.T = function() {};\n" + |
| 9363 |
"/** @type {(number|string)} */ u.T.prototype.a;\n" + |
| 9364 |
"/**\n" + |
| 9365 |
"* @param {u.T} t\n" + |
| 9366 |
"* @return {string}\n" + |
| 9367 |
"*/\n" + |
| 9368 |
"var f = function(t) { return t.a; };", |
| 9369 |
"inconsistent return type\n" + |
| 9370 |
"found : (number|string)\n" + |
| 9371 |
"required: string"); |
| 9372 |
} |
| 9373 |
|
| 9374 |
public void testResolutionViaRegistry2() throws Exception { |
| 9375 |
testTypes( |
| 9376 |
"/** @constructor */ u.T = function() {" + |
| 9377 |
" this.a = 0; };\n" + |
| 9378 |
"/**\n" + |
| 9379 |
"* @param {u.T} t\n" + |
| 9380 |
"* @return {string}\n" + |
| 9381 |
"*/\n" + |
| 9382 |
"var f = function(t) { return t.a; };", |
| 9383 |
"inconsistent return type\n" + |
| 9384 |
"found : number\n" + |
| 9385 |
"required: string"); |
| 9386 |
} |
| 9387 |
|
| 9388 |
public void testResolutionViaRegistry3() throws Exception { |
| 9389 |
testTypes("/** @constructor */ u.T = function() {};\n" + |
| 9390 |
"/** @type {(number|string)} */ u.T.prototype.a = 0;\n" + |
| 9391 |
"/**\n" + |
| 9392 |
"* @param {u.T} t\n" + |
| 9393 |
"* @return {string}\n" + |
| 9394 |
"*/\n" + |
| 9395 |
"var f = function(t) { return t.a; };", |
| 9396 |
"inconsistent return type\n" + |
| 9397 |
"found : (number|string)\n" + |
| 9398 |
"required: string"); |
| 9399 |
} |
| 9400 |
|
| 9401 |
public void testResolutionViaRegistry4() throws Exception { |
| 9402 |
testTypes("/** @constructor */ u.A = function() {};\n" + |
| 9403 |
"/**\n* @constructor\n* @extends {u.A}\n*/\nu.A.A = function() {}\n;" + |
| 9404 |
"/**\n* @constructor\n* @extends {u.A}\n*/\nu.A.B = function() {};\n" + |
| 9405 |
"var ab = new u.A.B();\n" + |
| 9406 |
"/** @type {!u.A} */ var a = ab;\n" + |
| 9407 |
"/** @type {!u.A.A} */ var aa = ab;\n", |
| 9408 |
"initializing variable\n" + |
| 9409 |
"found : u.A.B\n" + |
| 9410 |
"required: u.A.A"); |
| 9411 |
} |
| 9412 |
|
| 9413 |
public void testResolutionViaRegistry5() throws Exception { |
| 9414 |
Node n = parseAndTypeCheck("/** @constructor */ u.T = function() {}; u.T"); |
| 9415 |
JSType type = n.getLastChild().getLastChild().getJSType(); |
| 9416 |
assertFalse(type.isUnknownType()); |
| 9417 |
assertTrue(type instanceof FunctionType); |
| 9418 |
assertEquals("u.T", |
| 9419 |
((FunctionType) type).getInstanceType().getReferenceName()); |
| 9420 |
} |
| 9421 |
|
| 9422 |
public void testGatherProperyWithoutAnnotation1() throws Exception { |
| 9423 |
Node n = parseAndTypeCheck("/** @constructor */ var T = function() {};" + |
| 9424 |
"/** @type {!T} */var t; t.x; t;"); |
| 9425 |
JSType type = n.getLastChild().getLastChild().getJSType(); |
| 9426 |
assertFalse(type.isUnknownType()); |
| 9427 |
assertTrue(type instanceof ObjectType); |
| 9428 |
ObjectType objectType = (ObjectType) type; |
| 9429 |
assertFalse(objectType.hasProperty("x")); |
| 9430 |
Asserts.assertTypeCollectionEquals( |
| 9431 |
Lists.newArrayList(objectType), |
| 9432 |
registry.getTypesWithProperty("x")); |
| 9433 |
} |
| 9434 |
|
| 9435 |
public void testGatherProperyWithoutAnnotation2() throws Exception { |
| 9436 |
TypeCheckResult ns = |
| 9437 |
parseAndTypeCheckWithScope("/** @type {!Object} */var t; t.x; t;"); |
| 9438 |
Node n = ns.root; |
| 9439 |
JSType type = n.getLastChild().getLastChild().getJSType(); |
| 9440 |
assertFalse(type.isUnknownType()); |
| 9441 |
assertTypeEquals(type, OBJECT_TYPE); |
| 9442 |
assertTrue(type instanceof ObjectType); |
| 9443 |
ObjectType objectType = (ObjectType) type; |
| 9444 |
assertFalse(objectType.hasProperty("x")); |
| 9445 |
Asserts.assertTypeCollectionEquals( |
| 9446 |
Lists.newArrayList(OBJECT_TYPE), |
| 9447 |
registry.getTypesWithProperty("x")); |
| 9448 |
} |
| 9449 |
|
| 9450 |
public void testFunctionMasksVariableBug() throws Exception { |
| 9451 |
testTypes("var x = 4; var f = function x(b) { return b ? 1 : x(true); };", |
| 9452 |
"function x masks variable (IE bug)"); |
| 9453 |
} |
| 9454 |
|
| 9455 |
public void testDfa1() throws Exception { |
| 9456 |
testTypes("var x = null;\n x = 1;\n /** @type number */ var y = x;"); |
| 9457 |
} |
| 9458 |
|
| 9459 |
public void testDfa2() throws Exception { |
| 9460 |
testTypes("function u() {}\n" + |
| 9461 |
"/** @return {number} */ function f() {\nvar x = 'todo';\n" + |
| 9462 |
"if (u()) { x = 1; } else { x = 2; } return x;\n}"); |
| 9463 |
} |
| 9464 |
|
| 9465 |
public void testDfa3() throws Exception { |
| 9466 |
testTypes("function u() {}\n" + |
| 9467 |
"/** @return {number} */ function f() {\n" + |
| 9468 |
"/** @type {number|string} */ var x = 'todo';\n" + |
| 9469 |
"if (u()) { x = 1; } else { x = 2; } return x;\n}"); |
| 9470 |
} |
| 9471 |
|
| 9472 |
public void testDfa4() throws Exception { |
| 9473 |
testTypes("/** @param {Date?} d */ function f(d) {\n" + |
| 9474 |
"if (!d) { return; }\n" + |
| 9475 |
"/** @type {!Date} */ var e = d;\n}"); |
| 9476 |
} |
| 9477 |
|
| 9478 |
public void testDfa5() throws Exception { |
| 9479 |
testTypes("/** @return {string?} */ function u() {return 'a';}\n" + |
| 9480 |
"/** @param {string?} x\n@return {string} */ function f(x) {\n" + |
| 9481 |
"while (!x) { x = u(); }\nreturn x;\n}"); |
| 9482 |
} |
| 9483 |
|
| 9484 |
public void testDfa6() throws Exception { |
| 9485 |
testTypes("/** @return {Object?} */ function u() {return {};}\n" + |
| 9486 |
"/** @param {Object?} x */ function f(x) {\n" + |
| 9487 |
"while (x) { x = u(); if (!x) { x = u(); } }\n}"); |
| 9488 |
} |
| 9489 |
|
| 9490 |
public void testDfa7() throws Exception { |
| 9491 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 9492 |
"/** @type {Date?} */ T.prototype.x = null;\n" + |
| 9493 |
"/** @param {!T} t */ function f(t) {\n" + |
| 9494 |
"if (!t.x) { return; }\n" + |
| 9495 |
"/** @type {!Date} */ var e = t.x;\n}"); |
| 9496 |
} |
| 9497 |
|
| 9498 |
public void testDfa8() throws Exception { |
| 9499 |
testTypes("/** @constructor */ var T = function() {};\n" + |
| 9500 |
"/** @type {number|string} */ T.prototype.x = '';\n" + |
| 9501 |
"function u() {}\n" + |
| 9502 |
"/** @param {!T} t\n@return {number} */ function f(t) {\n" + |
| 9503 |
"if (u()) { t.x = 1; } else { t.x = 2; } return t.x;\n}"); |
| 9504 |
} |
| 9505 |
|
| 9506 |
public void testDfa9() throws Exception { |
| 9507 |
testTypes("function f() {\n/** @type {string?} */var x;\nx = null;\n" + |
| 9508 |
"if (x == null) { return 0; } else { return 1; } }", |
| 9509 |
"condition always evaluates to true\n" + |
| 9510 |
"left : null\n" + |
| 9511 |
"right: null"); |
| 9512 |
} |
| 9513 |
|
| 9514 |
public void testDfa10() throws Exception { |
| 9515 |
testTypes("/** @param {null} x */ function g(x) {}" + |
| 9516 |
"/** @param {string?} x */function f(x) {\n" + |
| 9517 |
"if (!x) { x = ''; }\n" + |
| 9518 |
"if (g(x)) { return 0; } else { return 1; } }", |
| 9519 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 9520 |
"found : string\n" + |
| 9521 |
"required: null"); |
| 9522 |
} |
| 9523 |
|
| 9524 |
public void testDfa11() throws Exception { |
| 9525 |
testTypes("/** @param {string} opt_x\n@return {string} */\n" + |
| 9526 |
"function f(opt_x) { if (!opt_x) { " + |
| 9527 |
"throw new Error('x cannot be empty'); } return opt_x; }"); |
| 9528 |
} |
| 9529 |
|
| 9530 |
public void testDfa12() throws Exception { |
| 9531 |
testTypes("/** @param {string} x \n * @constructor \n */" + |
| 9532 |
"var Bar = function(x) {};" + |
| 9533 |
"/** @param {string} x */ function g(x) { return true; }" + |
| 9534 |
"/** @param {string|number} opt_x */ " + |
| 9535 |
"function f(opt_x) { " + |
| 9536 |
" if (opt_x) { new Bar(g(opt_x) && 'x'); }" + |
| 9537 |
"}", |
| 9538 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 9539 |
"found : (number|string)\n" + |
| 9540 |
"required: string"); |
| 9541 |
} |
| 9542 |
|
| 9543 |
public void testDfa13() throws Exception { |
| 9544 |
testTypes( |
| 9545 |
"/**\n" + |
| 9546 |
" * @param {string} x \n" + |
| 9547 |
" * @param {number} y \n" + |
| 9548 |
" * @param {number} z \n" + |
| 9549 |
" */" + |
| 9550 |
"function g(x, y, z) {}" + |
| 9551 |
"function f() { " + |
| 9552 |
" var x = 'a'; g(x, x = 3, x);" + |
| 9553 |
"}"); |
| 9554 |
} |
| 9555 |
|
| 9556 |
public void testTypeInferenceWithCast1() throws Exception { |
| 9557 |
testTypes( |
| 9558 |
"/**@return {(number,null,undefined)}*/function u(x) {return null;}" + |
| 9559 |
"/**@param {number?} x\n@return {number?}*/function f(x) {return x;}" + |
| 9560 |
"/**@return {number?}*/function g(x) {" + |
| 9561 |
"var y = /**@type {number?}*/(u(x)); return f(y);}"); |
| 9562 |
} |
| 9563 |
|
| 9564 |
public void testTypeInferenceWithCast2() throws Exception { |
| 9565 |
testTypes( |
| 9566 |
"/**@return {(number,null,undefined)}*/function u(x) {return null;}" + |
| 9567 |
"/**@param {number?} x\n@return {number?}*/function f(x) {return x;}" + |
| 9568 |
"/**@return {number?}*/function g(x) {" + |
| 9569 |
"var y; y = /**@type {number?}*/(u(x)); return f(y);}"); |
| 9570 |
} |
| 9571 |
|
| 9572 |
public void testTypeInferenceWithCast3() throws Exception { |
| 9573 |
testTypes( |
| 9574 |
"/**@return {(number,null,undefined)}*/function u(x) {return 1;}" + |
| 9575 |
"/**@return {number}*/function g(x) {" + |
| 9576 |
"return /**@type {number}*/(u(x));}"); |
| 9577 |
} |
| 9578 |
|
| 9579 |
public void testTypeInferenceWithCast4() throws Exception { |
| 9580 |
testTypes( |
| 9581 |
"/**@return {(number,null,undefined)}*/function u(x) {return 1;}" + |
| 9582 |
"/**@return {number}*/function g(x) {" + |
| 9583 |
"return /**@type {number}*/(u(x)) && 1;}"); |
| 9584 |
} |
| 9585 |
|
| 9586 |
public void testTypeInferenceWithCast5() throws Exception { |
| 9587 |
testTypes( |
| 9588 |
"/** @param {number} x */ function foo(x) {}" + |
| 9589 |
"/** @param {{length:*}} y */ function bar(y) {" + |
| 9590 |
" /** @type {string} */ y.length;" + |
| 9591 |
" foo(y.length);" + |
| 9592 |
"}", |
| 9593 |
"actual parameter 1 of foo does not match formal parameter\n" + |
| 9594 |
"found : string\n" + |
| 9595 |
"required: number"); |
| 9596 |
} |
| 9597 |
|
| 9598 |
public void testTypeInferenceWithClosure1() throws Exception { |
| 9599 |
testTypes( |
| 9600 |
"/** @return {boolean} */" + |
| 9601 |
"function f() {" + |
| 9602 |
" /** @type {?string} */ var x = null;" + |
| 9603 |
" function g() { x = 'y'; } g(); " + |
| 9604 |
" return x == null;" + |
| 9605 |
"}"); |
| 9606 |
} |
| 9607 |
|
| 9608 |
public void testTypeInferenceWithClosure2() throws Exception { |
| 9609 |
testTypes( |
| 9610 |
"/** @return {boolean} */" + |
| 9611 |
"function f() {" + |
| 9612 |
" /** @type {?string} */ var x = null;" + |
| 9613 |
" function g() { x = 'y'; } g(); " + |
| 9614 |
" return x === 3;" + |
| 9615 |
"}", |
| 9616 |
"condition always evaluates to false\n" + |
| 9617 |
"left : (null|string)\n" + |
| 9618 |
"right: number"); |
| 9619 |
} |
| 9620 |
|
| 9621 |
public void testTypeInferenceWithNoEntry1() throws Exception { |
| 9622 |
testTypes( |
| 9623 |
"/** @param {number} x */ function f(x) {}" + |
| 9624 |
"/** @constructor */ function Foo() {}" + |
| 9625 |
"Foo.prototype.init = function() {" + |
| 9626 |
" /** @type {?{baz: number}} */ this.bar = {baz: 3};" + |
| 9627 |
"};" + |
| 9628 |
"/**\n" + |
| 9629 |
" * @extends {Foo}\n" + |
| 9630 |
" * @constructor\n" + |
| 9631 |
" */" + |
| 9632 |
"function SubFoo() {}" + |
| 9633 |
"/** Method */" + |
| 9634 |
"SubFoo.prototype.method = function() {" + |
| 9635 |
" for (var i = 0; i < 10; i++) {" + |
| 9636 |
" f(this.bar);" + |
| 9637 |
" f(this.bar.baz);" + |
| 9638 |
" }" + |
| 9639 |
"};", |
| 9640 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9641 |
"found : (null|{baz: number})\n" + |
| 9642 |
"required: number"); |
| 9643 |
} |
| 9644 |
|
| 9645 |
public void testTypeInferenceWithNoEntry2() throws Exception { |
| 9646 |
testClosureTypes( |
| 9647 |
CLOSURE_DEFS + |
| 9648 |
"/** @param {number} x */ function f(x) {}" + |
| 9649 |
"/** @param {!Object} x */ function g(x) {}" + |
| 9650 |
"/** @constructor */ function Foo() {}" + |
| 9651 |
"Foo.prototype.init = function() {" + |
| 9652 |
" /** @type {?{baz: number}} */ this.bar = {baz: 3};" + |
| 9653 |
"};" + |
| 9654 |
"/**\n" + |
| 9655 |
" * @extends {Foo}\n" + |
| 9656 |
" * @constructor\n" + |
| 9657 |
" */" + |
| 9658 |
"function SubFoo() {}" + |
| 9659 |
"/** Method */" + |
| 9660 |
"SubFoo.prototype.method = function() {" + |
| 9661 |
" for (var i = 0; i < 10; i++) {" + |
| 9662 |
" f(this.bar);" + |
| 9663 |
" goog.asserts.assert(this.bar);" + |
| 9664 |
" g(this.bar);" + |
| 9665 |
" }" + |
| 9666 |
"};", |
| 9667 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9668 |
"found : (null|{baz: number})\n" + |
| 9669 |
"required: number"); |
| 9670 |
} |
| 9671 |
|
| 9672 |
public void testForwardPropertyReference() throws Exception { |
| 9673 |
testTypes("/** @constructor */ var Foo = function() { this.init(); };" + |
| 9674 |
"/** @return {string} */" + |
| 9675 |
"Foo.prototype.getString = function() {" + |
| 9676 |
" return this.number_;" + |
| 9677 |
"};" + |
| 9678 |
"Foo.prototype.init = function() {" + |
| 9679 |
" /** @type {number} */" + |
| 9680 |
" this.number_ = 3;" + |
| 9681 |
"};", |
| 9682 |
"inconsistent return type\n" + |
| 9683 |
"found : number\n" + |
| 9684 |
"required: string"); |
| 9685 |
} |
| 9686 |
|
| 9687 |
public void testNoForwardTypeDeclaration() throws Exception { |
| 9688 |
testTypes( |
| 9689 |
"/** @param {MyType} x */ function f(x) {}", |
| 9690 |
"Bad type annotation. Unknown type MyType"); |
| 9691 |
} |
| 9692 |
|
| 9693 |
public void testNoForwardTypeDeclarationAndNoBraces() throws Exception { |
| 9694 |
testTypes("/** @return The result. */ function f() {}"); |
| 9695 |
} |
| 9696 |
|
| 9697 |
public void testForwardTypeDeclaration1() throws Exception { |
| 9698 |
testClosureTypes( |
| 9699 |
|
| 9700 |
"goog.addDependency();" + |
| 9701 |
"goog.addDependency('y', [goog]);" + |
| 9702 |
|
| 9703 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9704 |
"/** @param {MyType} x \n * @return {number} */" + |
| 9705 |
"function f(x) { return 3; }", null); |
| 9706 |
} |
| 9707 |
|
| 9708 |
public void testForwardTypeDeclaration2() throws Exception { |
| 9709 |
String f = "goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9710 |
"/** @param {MyType} x */ function f(x) { }"; |
| 9711 |
testClosureTypes(f, null); |
| 9712 |
testClosureTypes(f + "f(3);", |
| 9713 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9714 |
"found : number\n" + |
| 9715 |
"required: (MyType|null)"); |
| 9716 |
} |
| 9717 |
|
| 9718 |
public void testForwardTypeDeclaration3() throws Exception { |
| 9719 |
testClosureTypes( |
| 9720 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9721 |
"/** @param {MyType} x */ function f(x) { return x; }" + |
| 9722 |
"/** @constructor */ var MyType = function() {};" + |
| 9723 |
"f(3);", |
| 9724 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9725 |
"found : number\n" + |
| 9726 |
"required: (MyType|null)"); |
| 9727 |
} |
| 9728 |
|
| 9729 |
public void testForwardTypeDeclaration4() throws Exception { |
| 9730 |
testClosureTypes( |
| 9731 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9732 |
"/** @param {MyType} x */ function f(x) { return x; }" + |
| 9733 |
"/** @constructor */ var MyType = function() {};" + |
| 9734 |
"f(new MyType());", |
| 9735 |
null); |
| 9736 |
} |
| 9737 |
|
| 9738 |
public void testForwardTypeDeclaration5() throws Exception { |
| 9739 |
testClosureTypes( |
| 9740 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9741 |
"/**\n" + |
| 9742 |
" * @constructor\n" + |
| 9743 |
" * @extends {MyType}\n" + |
| 9744 |
" */ var YourType = function() {};" + |
| 9745 |
"/** @override */ YourType.prototype.method = function() {};", |
| 9746 |
"Could not resolve type in @extends tag of YourType"); |
| 9747 |
} |
| 9748 |
|
| 9749 |
public void testForwardTypeDeclaration6() throws Exception { |
| 9750 |
testClosureTypesMultipleWarnings( |
| 9751 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9752 |
"/**\n" + |
| 9753 |
" * @constructor\n" + |
| 9754 |
" * @implements {MyType}\n" + |
| 9755 |
" */ var YourType = function() {};" + |
| 9756 |
"/** @override */ YourType.prototype.method = function() {};", |
| 9757 |
Lists.newArrayList( |
| 9758 |
"Could not resolve type in @implements tag of YourType", |
| 9759 |
"property method not defined on any superclass of YourType")); |
| 9760 |
} |
| 9761 |
|
| 9762 |
public void testForwardTypeDeclaration7() throws Exception { |
| 9763 |
testClosureTypes( |
| 9764 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9765 |
"/** @param {MyType=} x */" + |
| 9766 |
"function f(x) { return x == undefined; }", null); |
| 9767 |
} |
| 9768 |
|
| 9769 |
public void testForwardTypeDeclaration8() throws Exception { |
| 9770 |
testClosureTypes( |
| 9771 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9772 |
"/** @param {MyType} x */" + |
| 9773 |
"function f(x) { return x.name == undefined; }", null); |
| 9774 |
} |
| 9775 |
|
| 9776 |
public void testForwardTypeDeclaration9() throws Exception { |
| 9777 |
testClosureTypes( |
| 9778 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9779 |
"/** @param {MyType} x */" + |
| 9780 |
"function f(x) { x.name = 'Bob'; }", null); |
| 9781 |
} |
| 9782 |
|
| 9783 |
public void testForwardTypeDeclaration10() throws Exception { |
| 9784 |
String f = "goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9785 |
"/** @param {MyType|number} x */ function f(x) { }"; |
| 9786 |
testClosureTypes(f, null); |
| 9787 |
testClosureTypes(f + "f(3);", null); |
| 9788 |
testClosureTypes(f + "f('3');", |
| 9789 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9790 |
"found : string\n" + |
| 9791 |
"required: (MyType|null|number)"); |
| 9792 |
} |
| 9793 |
|
| 9794 |
public void testForwardTypeDeclaration12() throws Exception { |
| 9795 |
|
| 9796 |
|
| 9797 |
testClosureTypes( |
| 9798 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9799 |
"/**\n" + |
| 9800 |
" * @param {!Function} ctor\n" + |
| 9801 |
" * @return {MyType}\n" + |
| 9802 |
" */\n" + |
| 9803 |
"function f(ctor) { return new ctor(); }", null); |
| 9804 |
} |
| 9805 |
|
| 9806 |
public void testForwardTypeDeclaration13() throws Exception { |
| 9807 |
|
| 9808 |
|
| 9809 |
|
| 9810 |
testClosureTypes( |
| 9811 |
"goog.addDependency('zzz.js', ['MyType'], []);" + |
| 9812 |
"/**\n" + |
| 9813 |
" * @param {!Function} ctor\n" + |
| 9814 |
" * @return {MyType}\n" + |
| 9815 |
" */\n" + |
| 9816 |
"function f(ctor) { return (new ctor()).impossibleProp; }", |
| 9817 |
"Property impossibleProp never defined on ?"); |
| 9818 |
} |
| 9819 |
|
| 9820 |
public void testDuplicateTypeDef() throws Exception { |
| 9821 |
testTypes( |
| 9822 |
"var goog = {};" + |
| 9823 |
"/** @constructor */ goog.Bar = function() {};" + |
| 9824 |
"/** @typedef {number} */ goog.Bar;", |
| 9825 |
"variable goog.Bar redefined with type None, " + |
| 9826 |
"original definition at [testcode]:1 " + |
| 9827 |
"with type function (new:goog.Bar): undefined"); |
| 9828 |
} |
| 9829 |
|
| 9830 |
public void testTypeDef1() throws Exception { |
| 9831 |
testTypes( |
| 9832 |
"var goog = {};" + |
| 9833 |
"/** @typedef {number} */ goog.Bar;" + |
| 9834 |
"/** @param {goog.Bar} x */ function f(x) {}" + |
| 9835 |
"f(3);"); |
| 9836 |
} |
| 9837 |
|
| 9838 |
public void testTypeDef2() throws Exception { |
| 9839 |
testTypes( |
| 9840 |
"var goog = {};" + |
| 9841 |
"/** @typedef {number} */ goog.Bar;" + |
| 9842 |
"/** @param {goog.Bar} x */ function f(x) {}" + |
| 9843 |
"f('3');", |
| 9844 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9845 |
"found : string\n" + |
| 9846 |
"required: number"); |
| 9847 |
} |
| 9848 |
|
| 9849 |
public void testTypeDef3() throws Exception { |
| 9850 |
testTypes( |
| 9851 |
"var goog = {};" + |
| 9852 |
"/** @typedef {number} */ var Bar;" + |
| 9853 |
"/** @param {Bar} x */ function f(x) {}" + |
| 9854 |
"f('3');", |
| 9855 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9856 |
"found : string\n" + |
| 9857 |
"required: number"); |
| 9858 |
} |
| 9859 |
|
| 9860 |
public void testTypeDef4() throws Exception { |
| 9861 |
testTypes( |
| 9862 |
"/** @constructor */ function A() {}" + |
| 9863 |
"/** @constructor */ function B() {}" + |
| 9864 |
"/** @typedef {(A|B)} */ var AB;" + |
| 9865 |
"/** @param {AB} x */ function f(x) {}" + |
| 9866 |
"f(new A()); f(new B()); f(1);", |
| 9867 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9868 |
"found : number\n" + |
| 9869 |
"required: (A|B|null)"); |
| 9870 |
} |
| 9871 |
|
| 9872 |
public void testTypeDef5() throws Exception { |
| 9873 |
|
| 9874 |
|
| 9875 |
|
| 9876 |
|
| 9877 |
testTypes( |
| 9878 |
"/** @param {AB} x */ function f(x) {}" + |
| 9879 |
"/** @constructor */ function A() {}" + |
| 9880 |
"/** @constructor */ function B() {}" + |
| 9881 |
"/** @typedef {(A|B)} */ var AB;" + |
| 9882 |
"f(new A()); f(new B()); f(1);", |
| 9883 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 9884 |
"found : number\n" + |
| 9885 |
"required: (A|B|null)"); |
| 9886 |
} |
| 9887 |
|
| 9888 |
public void testCircularTypeDef() throws Exception { |
| 9889 |
testTypes( |
| 9890 |
"var goog = {};" + |
| 9891 |
"/** @typedef {number|Array.<goog.Bar>} */ goog.Bar;" + |
| 9892 |
"/** @param {goog.Bar} x */ function f(x) {}" + |
| 9893 |
"f(3); f([3]); f([[3]]);"); |
| 9894 |
} |
| 9895 |
|
| 9896 |
public void testGetTypedPercent1() throws Exception { |
| 9897 |
String js = "var id = function(x) { return x; }\n" + |
| 9898 |
"var id2 = function(x) { return id(x); }"; |
| 9899 |
assertEquals(50.0, getTypedPercent(js), 0.1); |
| 9900 |
} |
| 9901 |
|
| 9902 |
public void testGetTypedPercent2() throws Exception { |
| 9903 |
String js = "var x = {}; x.y = 1;"; |
| 9904 |
assertEquals(100.0, getTypedPercent(js), 0.1); |
| 9905 |
} |
| 9906 |
|
| 9907 |
public void testGetTypedPercent3() throws Exception { |
| 9908 |
String js = "var f = function(x) { x.a = x.b; }"; |
| 9909 |
assertEquals(50.0, getTypedPercent(js), 0.1); |
| 9910 |
} |
| 9911 |
|
| 9912 |
public void testGetTypedPercent4() throws Exception { |
| 9913 |
String js = "var n = {};\n /** @constructor */ n.T = function() {};\n" + |
| 9914 |
"/** @type n.T */ var x = new n.T();"; |
| 9915 |
assertEquals(100.0, getTypedPercent(js), 0.1); |
| 9916 |
} |
| 9917 |
|
| 9918 |
public void testGetTypedPercent5() throws Exception { |
| 9919 |
String js = "/** @enum {number} */ keys = {A: 1,B: 2,C: 3};"; |
| 9920 |
assertEquals(100.0, getTypedPercent(js), 0.1); |
| 9921 |
} |
| 9922 |
|
| 9923 |
public void testGetTypedPercent6() throws Exception { |
| 9924 |
String js = "a = {TRUE: 1, FALSE: 0};"; |
| 9925 |
assertEquals(100.0, getTypedPercent(js), 0.1); |
| 9926 |
} |
| 9927 |
|
| 9928 |
private double getTypedPercent(String js) throws Exception { |
| 9929 |
Node n = compiler.parseTestCode(js); |
| 9930 |
|
| 9931 |
Node externs = new Node(Token.BLOCK); |
| 9932 |
Node externAndJsRoot = new Node(Token.BLOCK, externs, n); |
| 9933 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 9934 |
|
| 9935 |
TypeCheck t = makeTypeCheck(); |
| 9936 |
t.processForTesting(null, n); |
| 9937 |
return t.getTypedPercent(); |
| 9938 |
} |
| 9939 |
|
| 9940 |
private ObjectType getInstanceType(Node js1Node) { |
| 9941 |
JSType type = js1Node.getFirstChild().getJSType(); |
| 9942 |
assertNotNull(type); |
| 9943 |
assertTrue(type instanceof FunctionType); |
| 9944 |
FunctionType functionType = (FunctionType) type; |
| 9945 |
assertTrue(functionType.isConstructor()); |
| 9946 |
return functionType.getInstanceType(); |
| 9947 |
} |
| 9948 |
|
| 9949 |
public void testPrototypePropertyReference() throws Exception { |
| 9950 |
TypeCheckResult p = parseAndTypeCheckWithScope("" |
| 9951 |
+ "/** @constructor */\n" |
| 9952 |
+ "function Foo() {}\n" |
| 9953 |
+ "/** @param {number} a */\n" |
| 9954 |
+ "Foo.prototype.bar = function(a){};\n" |
| 9955 |
+ "/** @param {Foo} f */\n" |
| 9956 |
+ "function baz(f) {\n" |
| 9957 |
+ " Foo.prototype.bar.call(f, 3);\n" |
| 9958 |
+ "}"); |
| 9959 |
assertEquals(0, compiler.getErrorCount()); |
| 9960 |
assertEquals(0, compiler.getWarningCount()); |
| 9961 |
|
| 9962 |
assertTrue(p.scope.getVar("Foo").getType() instanceof FunctionType); |
| 9963 |
FunctionType fooType = (FunctionType) p.scope.getVar("Foo").getType(); |
| 9964 |
assertEquals("function (this:Foo, number): undefined", |
| 9965 |
fooType.getPrototype().getPropertyType("bar").toString()); |
| 9966 |
} |
| 9967 |
|
| 9968 |
public void testResolvingNamedTypes() throws Exception { |
| 9969 |
String js = "" |
| 9970 |
+ "/** @constructor */\n" |
| 9971 |
+ "var Foo = function() {}\n" |
| 9972 |
+ "/** @param {number} a */\n" |
| 9973 |
+ "Foo.prototype.foo = function(a) {\n" |
| 9974 |
+ " return this.baz().toString();\n" |
| 9975 |
+ "};\n" |
| 9976 |
+ "/** @return {Baz} */\n" |
| 9977 |
+ "Foo.prototype.baz = function() { return new Baz(); };\n" |
| 9978 |
+ "/** @constructor\n" |
| 9979 |
+ " * @extends Foo */\n" |
| 9980 |
+ "var Bar = function() {};" |
| 9981 |
+ "/** @constructor */\n" |
| 9982 |
+ "var Baz = function() {};"; |
| 9983 |
assertEquals(100.0, getTypedPercent(js), 0.1); |
| 9984 |
} |
| 9985 |
|
| 9986 |
public void testMissingProperty1() throws Exception { |
| 9987 |
testTypes( |
| 9988 |
"/** @constructor */ function Foo() {}" + |
| 9989 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 9990 |
"Foo.prototype.baz = function() { this.a = 3; };"); |
| 9991 |
} |
| 9992 |
|
| 9993 |
public void testMissingProperty2() throws Exception { |
| 9994 |
testTypes( |
| 9995 |
"/** @constructor */ function Foo() {}" + |
| 9996 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 9997 |
"Foo.prototype.baz = function() { this.b = 3; };", |
| 9998 |
"Property a never defined on Foo"); |
| 9999 |
} |
| 10000 |
|
| 10001 |
public void testMissingProperty3() throws Exception { |
| 10002 |
testTypes( |
| 10003 |
"/** @constructor */ function Foo() {}" + |
| 10004 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 10005 |
"(new Foo).a = 3;"); |
| 10006 |
} |
| 10007 |
|
| 10008 |
public void testMissingProperty4() throws Exception { |
| 10009 |
testTypes( |
| 10010 |
"/** @constructor */ function Foo() {}" + |
| 10011 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 10012 |
"(new Foo).b = 3;", |
| 10013 |
"Property a never defined on Foo"); |
| 10014 |
} |
| 10015 |
|
| 10016 |
public void testMissingProperty5() throws Exception { |
| 10017 |
testTypes( |
| 10018 |
"/** @constructor */ function Foo() {}" + |
| 10019 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 10020 |
"/** @constructor */ function Bar() { this.a = 3; };", |
| 10021 |
"Property a never defined on Foo"); |
| 10022 |
} |
| 10023 |
|
| 10024 |
public void testMissingProperty6() throws Exception { |
| 10025 |
testTypes( |
| 10026 |
"/** @constructor */ function Foo() {}" + |
| 10027 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 10028 |
"/** @constructor \n * @extends {Foo} */ " + |
| 10029 |
"function Bar() { this.a = 3; };"); |
| 10030 |
} |
| 10031 |
|
| 10032 |
public void testMissingProperty7() throws Exception { |
| 10033 |
testTypes( |
| 10034 |
"/** @param {Object} obj */" + |
| 10035 |
"function foo(obj) { return obj.impossible; }", |
| 10036 |
"Property impossible never defined on Object"); |
| 10037 |
} |
| 10038 |
|
| 10039 |
public void testMissingProperty8() throws Exception { |
| 10040 |
testTypes( |
| 10041 |
"/** @param {Object} obj */" + |
| 10042 |
"function foo(obj) { return typeof obj.impossible; }"); |
| 10043 |
} |
| 10044 |
|
| 10045 |
public void testMissingProperty9() throws Exception { |
| 10046 |
testTypes( |
| 10047 |
"/** @param {Object} obj */" + |
| 10048 |
"function foo(obj) { if (obj.impossible) { return true; } }"); |
| 10049 |
} |
| 10050 |
|
| 10051 |
public void testMissingProperty10() throws Exception { |
| 10052 |
testTypes( |
| 10053 |
"/** @param {Object} obj */" + |
| 10054 |
"function foo(obj) { while (obj.impossible) { return true; } }"); |
| 10055 |
} |
| 10056 |
|
| 10057 |
public void testMissingProperty11() throws Exception { |
| 10058 |
testTypes( |
| 10059 |
"/** @param {Object} obj */" + |
| 10060 |
"function foo(obj) { for (;obj.impossible;) { return true; } }"); |
| 10061 |
} |
| 10062 |
|
| 10063 |
public void testMissingProperty12() throws Exception { |
| 10064 |
testTypes( |
| 10065 |
"/** @param {Object} obj */" + |
| 10066 |
"function foo(obj) { do { } while (obj.impossible); }"); |
| 10067 |
} |
| 10068 |
|
| 10069 |
public void testMissingProperty13() throws Exception { |
| 10070 |
testTypes( |
| 10071 |
"var goog = {}; goog.isDef = function(x) { return false; };" + |
| 10072 |
"/** @param {Object} obj */" + |
| 10073 |
"function foo(obj) { return goog.isDef(obj.impossible); }"); |
| 10074 |
} |
| 10075 |
|
| 10076 |
public void testMissingProperty14() throws Exception { |
| 10077 |
testTypes( |
| 10078 |
"var goog = {}; goog.isDef = function(x) { return false; };" + |
| 10079 |
"/** @param {Object} obj */" + |
| 10080 |
"function foo(obj) { return goog.isNull(obj.impossible); }", |
| 10081 |
"Property isNull never defined on goog"); |
| 10082 |
} |
| 10083 |
|
| 10084 |
public void testMissingProperty15() throws Exception { |
| 10085 |
testTypes( |
| 10086 |
"/** @param {Object} x */" + |
| 10087 |
"function f(x) { if (x.foo) { x.foo(); } }"); |
| 10088 |
} |
| 10089 |
|
| 10090 |
public void testMissingProperty16() throws Exception { |
| 10091 |
testTypes( |
| 10092 |
"/** @param {Object} x */" + |
| 10093 |
"function f(x) { x.foo(); if (x.foo) {} }", |
| 10094 |
"Property foo never defined on Object"); |
| 10095 |
} |
| 10096 |
|
| 10097 |
public void testMissingProperty17() throws Exception { |
| 10098 |
testTypes( |
| 10099 |
"/** @param {Object} x */" + |
| 10100 |
"function f(x) { if (typeof x.foo == 'function') { x.foo(); } }"); |
| 10101 |
} |
| 10102 |
|
| 10103 |
public void testMissingProperty18() throws Exception { |
| 10104 |
testTypes( |
| 10105 |
"/** @param {Object} x */" + |
| 10106 |
"function f(x) { if (x.foo instanceof Function) { x.foo(); } }"); |
| 10107 |
} |
| 10108 |
|
| 10109 |
public void testMissingProperty19() throws Exception { |
| 10110 |
testTypes( |
| 10111 |
"/** @param {Object} x */" + |
| 10112 |
"function f(x) { if (x.bar) { if (x.foo) {} } else { x.foo(); } }", |
| 10113 |
"Property foo never defined on Object"); |
| 10114 |
} |
| 10115 |
|
| 10116 |
public void testMissingProperty20() throws Exception { |
| 10117 |
testTypes( |
| 10118 |
"/** @param {Object} x */" + |
| 10119 |
"function f(x) { if (x.foo) { } else { x.foo(); } }", |
| 10120 |
"Property foo never defined on Object"); |
| 10121 |
} |
| 10122 |
|
| 10123 |
public void testMissingProperty21() throws Exception { |
| 10124 |
testTypes( |
| 10125 |
"/** @param {Object} x */" + |
| 10126 |
"function f(x) { x.foo && x.foo(); }"); |
| 10127 |
} |
| 10128 |
|
| 10129 |
public void testMissingProperty22() throws Exception { |
| 10130 |
testTypes( |
| 10131 |
"/** @param {Object} x \n * @return {boolean} */" + |
| 10132 |
"function f(x) { return x.foo ? x.foo() : true; }"); |
| 10133 |
} |
| 10134 |
|
| 10135 |
public void testMissingProperty23() throws Exception { |
| 10136 |
testTypes( |
| 10137 |
"function f(x) { x.impossible(); }", |
| 10138 |
"Property impossible never defined on x"); |
| 10139 |
} |
| 10140 |
|
| 10141 |
public void testMissingProperty24() throws Exception { |
| 10142 |
testClosureTypes( |
| 10143 |
"goog.addDependency('zzz.js', ['MissingType'], []);" + |
| 10144 |
"/** @param {MissingType} x */" + |
| 10145 |
"function f(x) { x.impossible(); }", null); |
| 10146 |
} |
| 10147 |
|
| 10148 |
public void testMissingProperty25() throws Exception { |
| 10149 |
testTypes( |
| 10150 |
"/** @constructor */ var Foo = function() {};" + |
| 10151 |
"Foo.prototype.bar = function() {};" + |
| 10152 |
"/** @constructor */ var FooAlias = Foo;" + |
| 10153 |
"(new FooAlias()).bar();"); |
| 10154 |
} |
| 10155 |
|
| 10156 |
public void testMissingProperty26() throws Exception { |
| 10157 |
testTypes( |
| 10158 |
"/** @constructor */ var Foo = function() {};" + |
| 10159 |
"/** @constructor */ var FooAlias = Foo;" + |
| 10160 |
"FooAlias.prototype.bar = function() {};" + |
| 10161 |
"(new Foo()).bar();"); |
| 10162 |
} |
| 10163 |
|
| 10164 |
public void testMissingProperty27() throws Exception { |
| 10165 |
testClosureTypes( |
| 10166 |
"goog.addDependency('zzz.js', ['MissingType'], []);" + |
| 10167 |
"/** @param {?MissingType} x */" + |
| 10168 |
"function f(x) {" + |
| 10169 |
" for (var parent = x; parent; parent = parent.getParent()) {}" + |
| 10170 |
"}", null); |
| 10171 |
} |
| 10172 |
|
| 10173 |
public void testMissingProperty28() throws Exception { |
| 10174 |
testTypes( |
| 10175 |
"function f(obj) {" + |
| 10176 |
" /** @type {*} */ obj.foo;" + |
| 10177 |
" return obj.foo;" + |
| 10178 |
"}"); |
| 10179 |
testTypes( |
| 10180 |
"function f(obj) {" + |
| 10181 |
" /** @type {*} */ obj.foo;" + |
| 10182 |
" return obj.foox;" + |
| 10183 |
"}", |
| 10184 |
"Property foox never defined on obj"); |
| 10185 |
} |
| 10186 |
|
| 10187 |
public void testMissingProperty29() throws Exception { |
| 10188 |
|
| 10189 |
testTypes( |
| 10190 |
|
| 10191 |
"/** @constructor */ var Foo;" + |
| 10192 |
"Foo.prototype.opera;" + |
| 10193 |
"Foo.prototype.opera.postError;", |
| 10194 |
"", |
| 10195 |
null, |
| 10196 |
false); |
| 10197 |
} |
| 10198 |
|
| 10199 |
public void testMissingProperty30() throws Exception { |
| 10200 |
testTypes( |
| 10201 |
"/** @return {*} */" + |
| 10202 |
"function f() {" + |
| 10203 |
" return {};" + |
| 10204 |
"}" + |
| 10205 |
"f().a = 3;" + |
| 10206 |
"/** @param {Object} y */ function g(y) { return y.a; }"); |
| 10207 |
} |
| 10208 |
|
| 10209 |
public void testMissingProperty31() throws Exception { |
| 10210 |
testTypes( |
| 10211 |
"/** @return {Array|number} */" + |
| 10212 |
"function f() {" + |
| 10213 |
" return [];" + |
| 10214 |
"}" + |
| 10215 |
"f().a = 3;" + |
| 10216 |
"/** @param {Array} y */ function g(y) { return y.a; }"); |
| 10217 |
} |
| 10218 |
|
| 10219 |
public void testMissingProperty32() throws Exception { |
| 10220 |
testTypes( |
| 10221 |
"/** @return {Array|number} */" + |
| 10222 |
"function f() {" + |
| 10223 |
" return [];" + |
| 10224 |
"}" + |
| 10225 |
"f().a = 3;" + |
| 10226 |
"/** @param {Date} y */ function g(y) { return y.a; }", |
| 10227 |
"Property a never defined on Date"); |
| 10228 |
} |
| 10229 |
|
| 10230 |
public void testMissingProperty33() throws Exception { |
| 10231 |
testTypes( |
| 10232 |
"/** @param {Object} x */" + |
| 10233 |
"function f(x) { !x.foo || x.foo(); }"); |
| 10234 |
} |
| 10235 |
|
| 10236 |
public void testMissingProperty34() throws Exception { |
| 10237 |
testTypes( |
| 10238 |
"/** @fileoverview \n * @suppress {missingProperties} */" + |
| 10239 |
"/** @constructor */ function Foo() {}" + |
| 10240 |
"Foo.prototype.bar = function() { return this.a; };" + |
| 10241 |
"Foo.prototype.baz = function() { this.b = 3; };"); |
| 10242 |
} |
| 10243 |
|
| 10244 |
public void testMissingProperty35() throws Exception { |
| 10245 |
|
| 10246 |
testTypes( |
| 10247 |
"/** @constructor */ function Foo() {}" + |
| 10248 |
"/** @constructor */ function Bar() {}" + |
| 10249 |
"/** @constructor */ function Baz() {}" + |
| 10250 |
"/** @param {Foo|Bar} x */ function f(x) { x.specialProp = 1; }" + |
| 10251 |
"/** @param {Bar|Baz} x */ function g(x) { return x.specialProp; }"); |
| 10252 |
} |
| 10253 |
|
| 10254 |
public void testMissingProperty36() throws Exception { |
| 10255 |
|
| 10256 |
|
| 10257 |
testTypes( |
| 10258 |
"/** @constructor */ function Foo() {}" + |
| 10259 |
"Foo.prototype.baz = 0;" + |
| 10260 |
"/** @constructor \n * @extends {Foo} */ function SubFoo() {}" + |
| 10261 |
"SubFoo.prototype.bar = 0;" + |
| 10262 |
"/** @param {{bar: number}} x */ function f(x) { return x.baz; }"); |
| 10263 |
} |
| 10264 |
|
| 10265 |
public void testMissingProperty37() throws Exception { |
| 10266 |
|
| 10267 |
|
| 10268 |
testTypes( |
| 10269 |
"/** @param {{isVisible: boolean}} x */ function f(x){" + |
| 10270 |
" x.isVisible = false;" + |
| 10271 |
"}" + |
| 10272 |
"/** @constructor */ function Foo() {}" + |
| 10273 |
"/**\n" + |
| 10274 |
" * @constructor \n" + |
| 10275 |
" * @extends {Foo}\n" + |
| 10276 |
" */ function SubFoo() {}" + |
| 10277 |
"/** @type {boolean} */ SubFoo.prototype.isVisible = true;" + |
| 10278 |
"/**\n" + |
| 10279 |
" * @param {Foo} x\n" + |
| 10280 |
" * @return {boolean}\n" + |
| 10281 |
" */\n" + |
| 10282 |
"function g(x) { return x.isVisible; }"); |
| 10283 |
} |
| 10284 |
|
| 10285 |
public void testMissingProperty38() throws Exception { |
| 10286 |
testTypes( |
| 10287 |
"/** @constructor */ function Foo() {}" + |
| 10288 |
"/** @constructor */ function Bar() {}" + |
| 10289 |
"/** @return {Foo|Bar} */ function f() { return new Foo(); }" + |
| 10290 |
"f().missing;", |
| 10291 |
"Property missing never defined on (Bar|Foo|null)"); |
| 10292 |
} |
| 10293 |
|
| 10294 |
public void testMissingProperty39() throws Exception { |
| 10295 |
testTypes( |
| 10296 |
"/** @return {string|number} */ function f() { return 3; }" + |
| 10297 |
"f().length;"); |
| 10298 |
} |
| 10299 |
|
| 10300 |
public void testMissingProperty40() throws Exception { |
| 10301 |
testClosureTypes( |
| 10302 |
"goog.addDependency('zzz.js', ['MissingType'], []);" + |
| 10303 |
"/** @param {(Array|MissingType)} x */" + |
| 10304 |
"function f(x) { x.impossible(); }", null); |
| 10305 |
} |
| 10306 |
|
| 10307 |
public void testMissingProperty41() throws Exception { |
| 10308 |
testTypes( |
| 10309 |
"/** @param {(Array|Date)} x */" + |
| 10310 |
"function f(x) { if (x.impossible) x.impossible(); }"); |
| 10311 |
} |
| 10312 |
|
| 10313 |
|
| 10314 |
public void testMissingProperty42() throws Exception { |
| 10315 |
testTypes( |
| 10316 |
"/** @param {Object} x */" + |
| 10317 |
"function f(x) { " + |
| 10318 |
" if (typeof x.impossible == 'undefined') throw Error();" + |
| 10319 |
" return x.impossible;" + |
| 10320 |
"}"); |
| 10321 |
} |
| 10322 |
|
| 10323 |
public void testMissingProperty43() throws Exception { |
| 10324 |
testTypes( |
| 10325 |
"function f(x) { " + |
| 10326 |
" return /** @type {number} */ (x.impossible) && 1;" + |
| 10327 |
"}"); |
| 10328 |
} |
| 10329 |
|
| 10330 |
public void testReflectObject1() throws Exception { |
| 10331 |
testClosureTypes( |
| 10332 |
"var goog = {}; goog.reflect = {}; " + |
| 10333 |
"goog.reflect.object = function(x, y){};" + |
| 10334 |
"/** @constructor */ function A() {}" + |
| 10335 |
"goog.reflect.object(A, {x: 3});", |
| 10336 |
null); |
| 10337 |
} |
| 10338 |
|
| 10339 |
public void testReflectObject2() throws Exception { |
| 10340 |
testClosureTypes( |
| 10341 |
"var goog = {}; goog.reflect = {}; " + |
| 10342 |
"goog.reflect.object = function(x, y){};" + |
| 10343 |
"/** @param {string} x */ function f(x) {}" + |
| 10344 |
"/** @constructor */ function A() {}" + |
| 10345 |
"goog.reflect.object(A, {x: f(1 + 1)});", |
| 10346 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 10347 |
"found : number\n" + |
| 10348 |
"required: string"); |
| 10349 |
} |
| 10350 |
|
| 10351 |
public void testLends1() throws Exception { |
| 10352 |
testTypes( |
| 10353 |
"function extend(x, y) {}" + |
| 10354 |
"/** @constructor */ function Foo() {}" + |
| 10355 |
"extend(Foo, /** @lends */ ({bar: 1}));", |
| 10356 |
"Bad type annotation. missing object name in @lends tag"); |
| 10357 |
} |
| 10358 |
|
| 10359 |
public void testLends2() throws Exception { |
| 10360 |
testTypes( |
| 10361 |
"function extend(x, y) {}" + |
| 10362 |
"/** @constructor */ function Foo() {}" + |
| 10363 |
"extend(Foo, /** @lends {Foob} */ ({bar: 1}));", |
| 10364 |
"Variable Foob not declared before @lends annotation."); |
| 10365 |
} |
| 10366 |
|
| 10367 |
public void testLends3() throws Exception { |
| 10368 |
testTypes( |
| 10369 |
"function extend(x, y) {}" + |
| 10370 |
"/** @constructor */ function Foo() {}" + |
| 10371 |
"extend(Foo, {bar: 1});" + |
| 10372 |
"alert(Foo.bar);", |
| 10373 |
"Property bar never defined on Foo"); |
| 10374 |
} |
| 10375 |
|
| 10376 |
public void testLends4() throws Exception { |
| 10377 |
testTypes( |
| 10378 |
"function extend(x, y) {}" + |
| 10379 |
"/** @constructor */ function Foo() {}" + |
| 10380 |
"extend(Foo, /** @lends {Foo} */ ({bar: 1}));" + |
| 10381 |
"alert(Foo.bar);"); |
| 10382 |
} |
| 10383 |
|
| 10384 |
public void testLends5() throws Exception { |
| 10385 |
testTypes( |
| 10386 |
"function extend(x, y) {}" + |
| 10387 |
"/** @constructor */ function Foo() {}" + |
| 10388 |
"extend(Foo, {bar: 1});" + |
| 10389 |
"alert((new Foo()).bar);", |
| 10390 |
"Property bar never defined on Foo"); |
| 10391 |
} |
| 10392 |
|
| 10393 |
public void testLends6() throws Exception { |
| 10394 |
testTypes( |
| 10395 |
"function extend(x, y) {}" + |
| 10396 |
"/** @constructor */ function Foo() {}" + |
| 10397 |
"extend(Foo, /** @lends {Foo.prototype} */ ({bar: 1}));" + |
| 10398 |
"alert((new Foo()).bar);"); |
| 10399 |
} |
| 10400 |
|
| 10401 |
public void testLends7() throws Exception { |
| 10402 |
testTypes( |
| 10403 |
"function extend(x, y) {}" + |
| 10404 |
"/** @constructor */ function Foo() {}" + |
| 10405 |
"extend(Foo, /** @lends {Foo.prototype|Foo} */ ({bar: 1}));", |
| 10406 |
"Bad type annotation. expected closing }"); |
| 10407 |
} |
| 10408 |
|
| 10409 |
public void testLends8() throws Exception { |
| 10410 |
testTypes( |
| 10411 |
"function extend(x, y) {}" + |
| 10412 |
"/** @type {number} */ var Foo = 3;" + |
| 10413 |
"extend(Foo, /** @lends {Foo} */ ({bar: 1}));", |
| 10414 |
"May only lend properties to object types. Foo has type number."); |
| 10415 |
} |
| 10416 |
|
| 10417 |
public void testLends9() throws Exception { |
| 10418 |
testClosureTypesMultipleWarnings( |
| 10419 |
"function extend(x, y) {}" + |
| 10420 |
"/** @constructor */ function Foo() {}" + |
| 10421 |
"extend(Foo, /** @lends {!Foo} */ ({bar: 1}));", |
| 10422 |
Lists.newArrayList( |
| 10423 |
"Bad type annotation. expected closing }", |
| 10424 |
"Bad type annotation. missing object name in @lends tag")); |
| 10425 |
} |
| 10426 |
|
| 10427 |
public void testLends10() throws Exception { |
| 10428 |
testTypes( |
| 10429 |
"function defineClass(x) { return function() {}; } " + |
| 10430 |
"/** @constructor */" + |
| 10431 |
"var Foo = defineClass(" + |
| 10432 |
" /** @lends {Foo.prototype} */ ({/** @type {number} */ bar: 1}));" + |
| 10433 |
"/** @return {string} */ function f() { return (new Foo()).bar; }", |
| 10434 |
"inconsistent return type\n" + |
| 10435 |
"found : number\n" + |
| 10436 |
"required: string"); |
| 10437 |
} |
| 10438 |
|
| 10439 |
public void testLends11() throws Exception { |
| 10440 |
testTypes( |
| 10441 |
"function defineClass(x, y) { return function() {}; } " + |
| 10442 |
"/** @constructor */" + |
| 10443 |
"var Foo = function() {};" + |
| 10444 |
"/** @return {*} */ Foo.prototype.bar = function() { return 3; };" + |
| 10445 |
"/**\n" + |
| 10446 |
" * @constructor\n" + |
| 10447 |
" * @extends {Foo}\n" + |
| 10448 |
" */\n" + |
| 10449 |
"var SubFoo = defineClass(Foo, " + |
| 10450 |
" /** @lends {SubFoo.prototype} */ ({\n" + |
| 10451 |
" /** @return {number} */ bar: function() { return 3; }}));" + |
| 10452 |
"/** @return {string} */ function f() { return (new SubFoo()).bar(); }", |
| 10453 |
"inconsistent return type\n" + |
| 10454 |
"found : number\n" + |
| 10455 |
"required: string"); |
| 10456 |
} |
| 10457 |
|
| 10458 |
public void testDeclaredNativeTypeEquality() throws Exception { |
| 10459 |
Node n = parseAndTypeCheck("/** @constructor */ function Object() {};"); |
| 10460 |
assertTypeEquals(registry.getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE), |
| 10461 |
n.getFirstChild().getJSType()); |
| 10462 |
} |
| 10463 |
|
| 10464 |
public void testUndefinedVar() throws Exception { |
| 10465 |
Node n = parseAndTypeCheck("var undefined;"); |
| 10466 |
assertTypeEquals(registry.getNativeType(JSTypeNative.VOID_TYPE), |
| 10467 |
n.getFirstChild().getFirstChild().getJSType()); |
| 10468 |
} |
| 10469 |
|
| 10470 |
public void testFlowScopeBug1() throws Exception { |
| 10471 |
Node n = parseAndTypeCheck("/** @param {number} a \n" |
| 10472 |
+ "* @param {number} b */\n" |
| 10473 |
+ "function f(a, b) {\n" |
| 10474 |
+ "/** @type number */" |
| 10475 |
+ "var i = 0;" |
| 10476 |
+ "for (; (i + a) < b; ++i) {}}"); |
| 10477 |
|
| 10478 |
|
| 10479 |
assertTypeEquals(registry.getNativeType(JSTypeNative.NUMBER_TYPE), |
| 10480 |
n.getFirstChild().getLastChild().getLastChild().getFirstChild() |
| 10481 |
.getNext().getFirstChild().getJSType()); |
| 10482 |
} |
| 10483 |
|
| 10484 |
public void testFlowScopeBug2() throws Exception { |
| 10485 |
Node n = parseAndTypeCheck("/** @constructor */ function Foo() {};\n" |
| 10486 |
+ "Foo.prototype.hi = false;" |
| 10487 |
+ "function foo(a, b) {\n" |
| 10488 |
+ " /** @type Array */" |
| 10489 |
+ " var arr;" |
| 10490 |
+ " /** @type number */" |
| 10491 |
+ " var iter;" |
| 10492 |
+ " for (iter = 0; iter < arr.length; ++ iter) {" |
| 10493 |
+ " /** @type Foo */" |
| 10494 |
+ " var afoo = arr[iter];" |
| 10495 |
+ " afoo;" |
| 10496 |
+ " }" |
| 10497 |
+ "}"); |
| 10498 |
|
| 10499 |
|
| 10500 |
assertTypeEquals(registry.createNullableType(registry.getType("Foo")), |
| 10501 |
n.getLastChild().getLastChild().getLastChild().getLastChild() |
| 10502 |
.getLastChild().getLastChild().getJSType()); |
| 10503 |
} |
| 10504 |
|
| 10505 |
public void testAddSingletonGetter() { |
| 10506 |
Node n = parseAndTypeCheck( |
| 10507 |
"/** @constructor */ function Foo() {};\n" + |
| 10508 |
"goog.addSingletonGetter(Foo);"); |
| 10509 |
ObjectType o = (ObjectType) n.getFirstChild().getJSType(); |
| 10510 |
assertEquals("function (): Foo", |
| 10511 |
o.getPropertyType("getInstance").toString()); |
| 10512 |
assertEquals("Foo", o.getPropertyType("instance_").toString()); |
| 10513 |
} |
| 10514 |
|
| 10515 |
public void testTypeCheckStandaloneAST() throws Exception { |
| 10516 |
Node n = compiler.parseTestCode("function Foo() { }"); |
| 10517 |
typeCheck(n); |
| 10518 |
MemoizedScopeCreator scopeCreator = new MemoizedScopeCreator( |
| 10519 |
new TypedScopeCreator(compiler)); |
| 10520 |
Scope topScope = scopeCreator.createScope(n, null); |
| 10521 |
|
| 10522 |
Node second = compiler.parseTestCode("new Foo"); |
| 10523 |
|
| 10524 |
Node externs = new Node(Token.BLOCK); |
| 10525 |
Node externAndJsRoot = new Node(Token.BLOCK, externs, second); |
| 10526 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 10527 |
|
| 10528 |
new TypeCheck( |
| 10529 |
compiler, |
| 10530 |
new SemanticReverseAbstractInterpreter( |
| 10531 |
compiler.getCodingConvention(), registry), |
| 10532 |
registry, topScope, scopeCreator, CheckLevel.WARNING, CheckLevel.OFF) |
| 10533 |
.process(null, second); |
| 10534 |
|
| 10535 |
assertEquals(1, compiler.getWarningCount()); |
| 10536 |
assertEquals("cannot instantiate non-constructor", |
| 10537 |
compiler.getWarnings()[0].description); |
| 10538 |
} |
| 10539 |
|
| 10540 |
public void testUpdateParameterTypeOnClosure() throws Exception { |
| 10541 |
testTypes( |
| 10542 |
"/**\n" + |
| 10543 |
"* @constructor\n" + |
| 10544 |
"* @param {*=} opt_value\n" + |
| 10545 |
"* @return {?}\n" + |
| 10546 |
"*/\n" + |
| 10547 |
"function Object(opt_value) {}\n" + |
| 10548 |
"/**\n" + |
| 10549 |
"* @constructor\n" + |
| 10550 |
"* @param {...*} var_args\n" + |
| 10551 |
"*/\n" + |
| 10552 |
"function Function(var_args) {}\n" + |
| 10553 |
"/**\n" + |
| 10554 |
"* @type {Function}\n" + |
| 10555 |
"*/\n" + |
| 10556 |
|
| 10557 |
|
| 10558 |
"Object.prototype.constructor = function() {};\n", |
| 10559 |
"/**\n" + |
| 10560 |
"* @param {function(): boolean} fn\n" + |
| 10561 |
"*/\n" + |
| 10562 |
"function f(fn) {}\n" + |
| 10563 |
"f(function(g) { });\n", |
| 10564 |
null, |
| 10565 |
false); |
| 10566 |
} |
| 10567 |
|
| 10568 |
public void testTemplatedThisType1() throws Exception { |
| 10569 |
testTypes( |
| 10570 |
"/** @constructor */\n" + |
| 10571 |
"function Foo() {}\n" + |
| 10572 |
"/**\n" + |
| 10573 |
" * @this {T}\n" + |
| 10574 |
" * @return {T}\n" + |
| 10575 |
" * @template T\n" + |
| 10576 |
" */\n" + |
| 10577 |
"Foo.prototype.method = function() {};\n" + |
| 10578 |
"/**\n" + |
| 10579 |
" * @constructor\n" + |
| 10580 |
" * @extends {Foo}\n" + |
| 10581 |
" */\n" + |
| 10582 |
"function Bar() {}\n" + |
| 10583 |
"var g = new Bar().method();\n" + |
| 10584 |
"/**\n" + |
| 10585 |
" * @param {number} a\n" + |
| 10586 |
" */\n" + |
| 10587 |
"function compute(a) {};\n" + |
| 10588 |
"compute(g);\n", |
| 10589 |
|
| 10590 |
"actual parameter 1 of compute does not match formal parameter\n" + |
| 10591 |
"found : Bar\n" + |
| 10592 |
"required: number"); |
| 10593 |
} |
| 10594 |
|
| 10595 |
public void testTemplatedThisType2() throws Exception { |
| 10596 |
testTypes( |
| 10597 |
"/**\n" + |
| 10598 |
" * @this {Array.<T>|{length:number}}\n" + |
| 10599 |
" * @return {T}\n" + |
| 10600 |
" * @template T\n" + |
| 10601 |
" */\n" + |
| 10602 |
"Array.prototype.method = function() {};\n" + |
| 10603 |
"(function(){\n" + |
| 10604 |
" Array.prototype.method.call(arguments);" + |
| 10605 |
"})();"); |
| 10606 |
} |
| 10607 |
|
| 10608 |
public void testTemplateType1() throws Exception { |
| 10609 |
testTypes( |
| 10610 |
"/**\n" + |
| 10611 |
"* @param {T} x\n" + |
| 10612 |
"* @param {T} y\n" + |
| 10613 |
"* @param {function(this:T, ...)} z\n" + |
| 10614 |
"* @template T\n" + |
| 10615 |
"*/\n" + |
| 10616 |
"function f(x, y, z) {}\n" + |
| 10617 |
"f(this, this, function() { this });"); |
| 10618 |
} |
| 10619 |
|
| 10620 |
public void testTemplateType2() throws Exception { |
| 10621 |
|
| 10622 |
|
| 10623 |
testTypes( |
| 10624 |
"/**\n" + |
| 10625 |
"* @param {T} x\n" + |
| 10626 |
"* @param {function(this:T, ...)} y\n" + |
| 10627 |
"* @template T\n" + |
| 10628 |
"*/\n" + |
| 10629 |
"function f(x, y) {}\n" + |
| 10630 |
"f(0, function() {});"); |
| 10631 |
} |
| 10632 |
|
| 10633 |
public void testTemplateType3() throws Exception { |
| 10634 |
testTypes( |
| 10635 |
"/**" + |
| 10636 |
" * @param {T} v\n" + |
| 10637 |
" * @param {function(T)} f\n" + |
| 10638 |
" * @template T\n" + |
| 10639 |
" */\n" + |
| 10640 |
"function call(v, f) { f.call(null, v); }" + |
| 10641 |
"/** @type {string} */ var s;" + |
| 10642 |
"call(3, function(x) {" + |
| 10643 |
" x = true;" + |
| 10644 |
" s = x;" + |
| 10645 |
"});", |
| 10646 |
"assignment\n" + |
| 10647 |
"found : boolean\n" + |
| 10648 |
"required: string"); |
| 10649 |
} |
| 10650 |
|
| 10651 |
public void testTemplateType4() throws Exception { |
| 10652 |
testTypes( |
| 10653 |
"/**" + |
| 10654 |
" * @param {...T} p\n" + |
| 10655 |
" * @return {T} \n" + |
| 10656 |
" * @template T\n" + |
| 10657 |
" */\n" + |
| 10658 |
"function fn(p) { return p; }\n" + |
| 10659 |
"/** @type {!Object} */ var x;" + |
| 10660 |
"x = fn(3, null);", |
| 10661 |
"assignment\n" + |
| 10662 |
"found : (null|number)\n" + |
| 10663 |
"required: Object"); |
| 10664 |
} |
| 10665 |
|
| 10666 |
public void testTemplateType5() throws Exception { |
| 10667 |
compiler.getOptions().setCodingConvention(new GoogleCodingConvention()); |
| 10668 |
testTypes( |
| 10669 |
"var CGI_PARAM_RETRY_COUNT = 'rc';" + |
| 10670 |
"" + |
| 10671 |
"/**" + |
| 10672 |
" * @param {...T} p\n" + |
| 10673 |
" * @return {T} \n" + |
| 10674 |
" * @template T\n" + |
| 10675 |
" */\n" + |
| 10676 |
"function fn(p) { return p; }\n" + |
| 10677 |
"/** @type {!Object} */ var x;" + |
| 10678 |
"" + |
| 10679 |
"/** @return {void} */\n" + |
| 10680 |
"function aScope() {\n" + |
| 10681 |
" x = fn(CGI_PARAM_RETRY_COUNT, 1);\n" + |
| 10682 |
"}", |
| 10683 |
"assignment\n" + |
| 10684 |
"found : (number|string)\n" + |
| 10685 |
"required: Object"); |
| 10686 |
} |
| 10687 |
|
| 10688 |
public void testTemplateType6() throws Exception { |
| 10689 |
testTypes( |
| 10690 |
"/**" + |
| 10691 |
" * @param {Array.<T>} arr \n" + |
| 10692 |
" * @param {?function(T)} f \n" + |
| 10693 |
" * @return {T} \n" + |
| 10694 |
" * @template T\n" + |
| 10695 |
" */\n" + |
| 10696 |
"function fn(arr, f) { return arr[0]; }\n" + |
| 10697 |
"/** @param {Array.<number>} arr */ function g(arr) {" + |
| 10698 |
" /** @type {!Object} */ var x = fn.call(null, arr, null);" + |
| 10699 |
"}", |
| 10700 |
"initializing variable\n" + |
| 10701 |
"found : number\n" + |
| 10702 |
"required: Object"); |
| 10703 |
} |
| 10704 |
|
| 10705 |
public void testTemplateType7() throws Exception { |
| 10706 |
|
| 10707 |
|
| 10708 |
|
| 10709 |
testTypes( |
| 10710 |
"/** @type {!Array.<string>} */\n" + |
| 10711 |
"var query = [];\n" + |
| 10712 |
"query.push(1);\n"); |
| 10713 |
} |
| 10714 |
|
| 10715 |
public void testTemplateType8() throws Exception { |
| 10716 |
testTypes( |
| 10717 |
"/** @constructor \n" + |
| 10718 |
" * @classTemplate S,T\n" + |
| 10719 |
" */\n" + |
| 10720 |
"function Bar() {}\n" + |
| 10721 |
"/**" + |
| 10722 |
" * @param {Bar.<T>} bar \n" + |
| 10723 |
" * @return {T} \n" + |
| 10724 |
" * @template T\n" + |
| 10725 |
" */\n" + |
| 10726 |
"function fn(bar) {}\n" + |
| 10727 |
"/** @param {Bar.<number>} bar */ function g(bar) {" + |
| 10728 |
" /** @type {!Object} */ var x = fn(bar);" + |
| 10729 |
"}", |
| 10730 |
"initializing variable\n" + |
| 10731 |
"found : number\n" + |
| 10732 |
"required: Object"); |
| 10733 |
} |
| 10734 |
|
| 10735 |
public void disable_testBadTemplateType4() throws Exception { |
| 10736 |
|
| 10737 |
|
| 10738 |
|
| 10739 |
testTypes( |
| 10740 |
"/**\n" + |
| 10741 |
"* @template T\n" + |
| 10742 |
"*/\n" + |
| 10743 |
"function f() {}\n" + |
| 10744 |
"f();", |
| 10745 |
FunctionTypeBuilder.TEMPLATE_TYPE_EXPECTED.format()); |
| 10746 |
} |
| 10747 |
|
| 10748 |
public void disable_testBadTemplateType5() throws Exception { |
| 10749 |
|
| 10750 |
|
| 10751 |
|
| 10752 |
testTypes( |
| 10753 |
"/**\n" + |
| 10754 |
"* @template T\n" + |
| 10755 |
"* @return {T}\n" + |
| 10756 |
"*/\n" + |
| 10757 |
"function f() {}\n" + |
| 10758 |
"f();", |
| 10759 |
FunctionTypeBuilder.TEMPLATE_TYPE_EXPECTED.format()); |
| 10760 |
} |
| 10761 |
|
| 10762 |
public void disable_testFunctionLiteralUndefinedThisArgument() |
| 10763 |
throws Exception { |
| 10764 |
|
| 10765 |
|
| 10766 |
|
| 10767 |
testTypes("" |
| 10768 |
+ "/**\n" |
| 10769 |
+ " * @param {function(this:T, ...)?} fn\n" |
| 10770 |
+ " * @param {?T} opt_obj\n" |
| 10771 |
+ " * @template T\n" |
| 10772 |
+ " */\n" |
| 10773 |
+ "function baz(fn, opt_obj) {}\n" |
| 10774 |
+ "baz(function() { this; });", |
| 10775 |
"Function literal argument refers to undefined this argument"); |
| 10776 |
} |
| 10777 |
|
| 10778 |
public void testFunctionLiteralDefinedThisArgument() throws Exception { |
| 10779 |
testTypes("" |
| 10780 |
+ "/**\n" |
| 10781 |
+ " * @param {function(this:T, ...)?} fn\n" |
| 10782 |
+ " * @param {?T} opt_obj\n" |
| 10783 |
+ " * @template T\n" |
| 10784 |
+ " */\n" |
| 10785 |
+ "function baz(fn, opt_obj) {}\n" |
| 10786 |
+ "baz(function() { this; }, {});"); |
| 10787 |
} |
| 10788 |
|
| 10789 |
public void testFunctionLiteralDefinedThisArgument2() throws Exception { |
| 10790 |
testTypes("" |
| 10791 |
+ "/** @param {string} x */ function f(x) {}" |
| 10792 |
+ "/**\n" |
| 10793 |
+ " * @param {?function(this:T, ...)} fn\n" |
| 10794 |
+ " * @param {T=} opt_obj\n" |
| 10795 |
+ " * @template T\n" |
| 10796 |
+ " */\n" |
| 10797 |
+ "function baz(fn, opt_obj) {}\n" |
| 10798 |
+ "function g() { baz(function() { f(this.length); }, []); }", |
| 10799 |
"actual parameter 1 of f does not match formal parameter\n" |
| 10800 |
+ "found : number\n" |
| 10801 |
+ "required: string"); |
| 10802 |
} |
| 10803 |
|
| 10804 |
public void testFunctionLiteralUnreadNullThisArgument() throws Exception { |
| 10805 |
testTypes("" |
| 10806 |
+ "/**\n" |
| 10807 |
+ " * @param {function(this:T, ...)?} fn\n" |
| 10808 |
+ " * @param {?T} opt_obj\n" |
| 10809 |
+ " * @template T\n" |
| 10810 |
+ " */\n" |
| 10811 |
+ "function baz(fn, opt_obj) {}\n" |
| 10812 |
+ "baz(function() {}, null);"); |
| 10813 |
} |
| 10814 |
|
| 10815 |
public void testUnionTemplateThisType() throws Exception { |
| 10816 |
testTypes( |
| 10817 |
"/** @constructor */ function F() {}" + |
| 10818 |
"/** @return {F|Array} */ function g() { return []; }" + |
| 10819 |
"/** @param {F} x */ function h(x) { }" + |
| 10820 |
"/**\n" + |
| 10821 |
"* @param {T} x\n" + |
| 10822 |
"* @param {function(this:T, ...)} y\n" + |
| 10823 |
"* @template T\n" + |
| 10824 |
"*/\n" + |
| 10825 |
"function f(x, y) {}\n" + |
| 10826 |
"f(g(), function() { h(this); });", |
| 10827 |
"actual parameter 1 of h does not match formal parameter\n" + |
| 10828 |
"found : (Array|F|null)\n" + |
| 10829 |
"required: (F|null)"); |
| 10830 |
} |
| 10831 |
|
| 10832 |
public void testActiveXObject() throws Exception { |
| 10833 |
testTypes( |
| 10834 |
"/** @type {Object} */ var x = new ActiveXObject();" + |
| 10835 |
"/** @type { {impossibleProperty} } */ var y = new ActiveXObject();"); |
| 10836 |
} |
| 10837 |
|
| 10838 |
public void testRecordType1() throws Exception { |
| 10839 |
testTypes( |
| 10840 |
"/** @param {{prop: number}} x */" + |
| 10841 |
"function f(x) {}" + |
| 10842 |
"f({});", |
| 10843 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 10844 |
"found : {prop: (number|undefined)}\n" + |
| 10845 |
"required: {prop: number}"); |
| 10846 |
} |
| 10847 |
|
| 10848 |
public void testRecordType2() throws Exception { |
| 10849 |
testTypes( |
| 10850 |
"/** @param {{prop: (number|undefined)}} x */" + |
| 10851 |
"function f(x) {}" + |
| 10852 |
"f({});"); |
| 10853 |
} |
| 10854 |
|
| 10855 |
public void testRecordType3() throws Exception { |
| 10856 |
testTypes( |
| 10857 |
"/** @param {{prop: number}} x */" + |
| 10858 |
"function f(x) {}" + |
| 10859 |
"f({prop: 'x'});", |
| 10860 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 10861 |
"found : {prop: (number|string)}\n" + |
| 10862 |
"required: {prop: number}"); |
| 10863 |
} |
| 10864 |
|
| 10865 |
public void testRecordType4() throws Exception { |
| 10866 |
|
| 10867 |
|
| 10868 |
|
| 10869 |
testClosureTypesMultipleWarnings( |
| 10870 |
"/** @param {{prop: (number|undefined)}} x */" + |
| 10871 |
"function f(x) {}" + |
| 10872 |
"/** @param {{prop: (string|undefined)}} x */" + |
| 10873 |
"function g(x) {}" + |
| 10874 |
"var x = {}; f(x); g(x);", |
| 10875 |
Lists.newArrayList( |
| 10876 |
"actual parameter 1 of f does not match formal parameter\n" + |
| 10877 |
"found : {prop: (number|string|undefined)}\n" + |
| 10878 |
"required: {prop: (number|undefined)}", |
| 10879 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 10880 |
"found : {prop: (number|string|undefined)}\n" + |
| 10881 |
"required: {prop: (string|undefined)}")); |
| 10882 |
} |
| 10883 |
|
| 10884 |
public void testRecordType5() throws Exception { |
| 10885 |
testTypes( |
| 10886 |
"/** @param {{prop: (number|undefined)}} x */" + |
| 10887 |
"function f(x) {}" + |
| 10888 |
"/** @param {{otherProp: (string|undefined)}} x */" + |
| 10889 |
"function g(x) {}" + |
| 10890 |
"var x = {}; f(x); g(x);"); |
| 10891 |
} |
| 10892 |
|
| 10893 |
public void testRecordType6() throws Exception { |
| 10894 |
testTypes( |
| 10895 |
"/** @return {{prop: (number|undefined)}} x */" + |
| 10896 |
"function f() { return {}; }"); |
| 10897 |
} |
| 10898 |
|
| 10899 |
public void testRecordType7() throws Exception { |
| 10900 |
testTypes( |
| 10901 |
"/** @return {{prop: (number|undefined)}} x */" + |
| 10902 |
"function f() { var x = {}; g(x); return x; }" + |
| 10903 |
"/** @param {number} x */" + |
| 10904 |
"function g(x) {}", |
| 10905 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 10906 |
"found : {prop: (number|undefined)}\n" + |
| 10907 |
"required: number"); |
| 10908 |
} |
| 10909 |
|
| 10910 |
public void testRecordType8() throws Exception { |
| 10911 |
testTypes( |
| 10912 |
"/** @return {{prop: (number|string)}} x */" + |
| 10913 |
"function f() { var x = {prop: 3}; g(x.prop); return x; }" + |
| 10914 |
"/** @param {string} x */" + |
| 10915 |
"function g(x) {}", |
| 10916 |
"actual parameter 1 of g does not match formal parameter\n" + |
| 10917 |
"found : number\n" + |
| 10918 |
"required: string"); |
| 10919 |
} |
| 10920 |
|
| 10921 |
public void testDuplicateRecordFields1() throws Exception { |
| 10922 |
testTypes("/**" |
| 10923 |
+ "* @param {{x:string, x:number}} a" |
| 10924 |
+ "*/" |
| 10925 |
+ "function f(a) {};", |
| 10926 |
"Parse error. Duplicate record field x"); |
| 10927 |
} |
| 10928 |
|
| 10929 |
public void testDuplicateRecordFields2() throws Exception { |
| 10930 |
testTypes("/**" |
| 10931 |
+ "* @param {{name:string,number:x,number:y}} a" |
| 10932 |
+ " */" |
| 10933 |
+ "function f(a) {};", |
| 10934 |
new String[] {"Bad type annotation. Unknown type x", |
| 10935 |
"Parse error. Duplicate record field number", |
| 10936 |
"Bad type annotation. Unknown type y"}); |
| 10937 |
} |
| 10938 |
|
| 10939 |
public void testMultipleExtendsInterface1() throws Exception { |
| 10940 |
testTypes("/** @interface */ function base1() {}\n" |
| 10941 |
+ "/** @interface */ function base2() {}\n" |
| 10942 |
+ "/** @interface\n" |
| 10943 |
+ "* @extends {base1}\n" |
| 10944 |
+ "* @extends {base2}\n" |
| 10945 |
+ "*/\n" |
| 10946 |
+ "function derived() {}"); |
| 10947 |
} |
| 10948 |
|
| 10949 |
public void testMultipleExtendsInterface2() throws Exception { |
| 10950 |
testTypes( |
| 10951 |
"/** @interface */function Int0() {};" + |
| 10952 |
"/** @interface */function Int1() {};" + |
| 10953 |
"/** @desc description */Int0.prototype.foo = function() {};" + |
| 10954 |
"/** @interface \n @extends {Int0} \n @extends {Int1} */" + |
| 10955 |
"function Int2() {};" + |
| 10956 |
"/** @constructor\n @implements {Int2} */function Foo() {};", |
| 10957 |
"property foo on interface Int0 is not implemented by type Foo"); |
| 10958 |
} |
| 10959 |
|
| 10960 |
public void testMultipleExtendsInterface3() throws Exception { |
| 10961 |
testTypes( |
| 10962 |
"/** @interface */function Int0() {};" + |
| 10963 |
"/** @interface */function Int1() {};" + |
| 10964 |
"/** @desc description */Int1.prototype.foo = function() {};" + |
| 10965 |
"/** @interface \n @extends {Int0} \n @extends {Int1} */" + |
| 10966 |
"function Int2() {};" + |
| 10967 |
"/** @constructor\n @implements {Int2} */function Foo() {};", |
| 10968 |
"property foo on interface Int1 is not implemented by type Foo"); |
| 10969 |
} |
| 10970 |
|
| 10971 |
public void testMultipleExtendsInterface4() throws Exception { |
| 10972 |
testTypes( |
| 10973 |
"/** @interface */function Int0() {};" + |
| 10974 |
"/** @interface */function Int1() {};" + |
| 10975 |
"/** @interface \n @extends {Int0} \n @extends {Int1} \n" + |
| 10976 |
" @extends {number} */" + |
| 10977 |
"function Int2() {};" + |
| 10978 |
"/** @constructor\n @implements {Int2} */function Foo() {};", |
| 10979 |
"Int2 @extends non-object type number"); |
| 10980 |
} |
| 10981 |
|
| 10982 |
public void testMultipleExtendsInterface5() throws Exception { |
| 10983 |
testTypes( |
| 10984 |
"/** @interface */function Int0() {};" + |
| 10985 |
"/** @constructor */function Int1() {};" + |
| 10986 |
"/** @desc description @ return {string} x */" + |
| 10987 |
"/** @interface \n @extends {Int0} \n @extends {Int1} */" + |
| 10988 |
"function Int2() {};", |
| 10989 |
"Int2 cannot extend this type; interfaces can only extend interfaces"); |
| 10990 |
} |
| 10991 |
|
| 10992 |
public void testMultipleExtendsInterface6() throws Exception { |
| 10993 |
testTypes( |
| 10994 |
"/** @interface */function Super1() {};" + |
| 10995 |
"/** @interface */function Super2() {};" + |
| 10996 |
"/** @param {number} bar */Super2.prototype.foo = function(bar) {};" + |
| 10997 |
"/** @interface\n @extends {Super1}\n " + |
| 10998 |
"@extends {Super2} */function Sub() {};" + |
| 10999 |
"/** @override\n @param {string} bar */Sub.prototype.foo =\n" + |
| 11000 |
"function(bar) {};", |
| 11001 |
"mismatch of the foo property type and the type of the property it " + |
| 11002 |
"overrides from superclass Super2\n" + |
| 11003 |
"original: function (this:Super2, number): undefined\n" + |
| 11004 |
"override: function (this:Sub, string): undefined"); |
| 11005 |
} |
| 11006 |
|
| 11007 |
public void testMultipleExtendsInterfaceAssignment() throws Exception { |
| 11008 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 11009 |
"/** @interface */ var I2 = function() {}\n" + |
| 11010 |
"/** @interface\n@extends {I1}\n@extends {I2}*/" + |
| 11011 |
"var I3 = function() {};\n" + |
| 11012 |
"/** @constructor\n@implements {I3}*/var T = function() {};\n" + |
| 11013 |
"var t = new T();\n" + |
| 11014 |
"/** @type {I1} */var i1 = t;\n" + |
| 11015 |
"/** @type {I2} */var i2 = t;\n" + |
| 11016 |
"/** @type {I3} */var i3 = t;\n" + |
| 11017 |
"i1 = i3;\n" + |
| 11018 |
"i2 = i3;\n"); |
| 11019 |
} |
| 11020 |
|
| 11021 |
public void testMultipleExtendsInterfaceParamPass() throws Exception { |
| 11022 |
testTypes("/** @interface */var I1 = function() {};\n" + |
| 11023 |
"/** @interface */ var I2 = function() {}\n" + |
| 11024 |
"/** @interface\n@extends {I1}\n@extends {I2}*/" + |
| 11025 |
"var I3 = function() {};\n" + |
| 11026 |
"/** @constructor\n@implements {I3}*/var T = function() {};\n" + |
| 11027 |
"var t = new T();\n" + |
| 11028 |
"/** @param x I1 \n@param y I2\n@param z I3*/function foo(x,y,z){};\n" + |
| 11029 |
"foo(t,t,t)\n"); |
| 11030 |
} |
| 11031 |
|
| 11032 |
public void testBadMultipleExtendsClass() throws Exception { |
| 11033 |
testTypes("/** @constructor */ function base1() {}\n" |
| 11034 |
+ "/** @constructor */ function base2() {}\n" |
| 11035 |
+ "/** @constructor\n" |
| 11036 |
+ "* @extends {base1}\n" |
| 11037 |
+ "* @extends {base2}\n" |
| 11038 |
+ "*/\n" |
| 11039 |
+ "function derived() {}", |
| 11040 |
"Bad type annotation. type annotation incompatible " |
| 11041 |
+ "with other annotations"); |
| 11042 |
} |
| 11043 |
|
| 11044 |
public void testInterfaceExtendsResolution() throws Exception { |
| 11045 |
testTypes("/** @interface \n @extends {A} */ function B() {};\n" + |
| 11046 |
"/** @constructor \n @implements {B} */ function C() {};\n" + |
| 11047 |
"/** @interface */ function A() {};"); |
| 11048 |
} |
| 11049 |
|
| 11050 |
public void testPropertyCanBeDefinedInObject() throws Exception { |
| 11051 |
testTypes("/** @interface */ function I() {};" + |
| 11052 |
"I.prototype.bar = function() {};" + |
| 11053 |
"/** @type {Object} */ var foo;" + |
| 11054 |
"foo.bar();"); |
| 11055 |
} |
| 11056 |
|
| 11057 |
private void checkObjectType(ObjectType objectType, String propertyName, |
| 11058 |
JSType expectedType) { |
| 11059 |
assertTrue("Expected " + objectType.getReferenceName() + |
| 11060 |
" to have property " + |
| 11061 |
propertyName, objectType.hasProperty(propertyName)); |
| 11062 |
assertTypeEquals("Expected " + objectType.getReferenceName() + |
| 11063 |
"'s property " + |
| 11064 |
propertyName + " to have type " + expectedType, |
| 11065 |
expectedType, objectType.getPropertyType(propertyName)); |
| 11066 |
} |
| 11067 |
|
| 11068 |
public void testExtendedInterfacePropertiesCompatibility1() throws Exception { |
| 11069 |
testTypes( |
| 11070 |
"/** @interface */function Int0() {};" + |
| 11071 |
"/** @interface */function Int1() {};" + |
| 11072 |
"/** @type {number} */" + |
| 11073 |
"Int0.prototype.foo;" + |
| 11074 |
"/** @type {string} */" + |
| 11075 |
"Int1.prototype.foo;" + |
| 11076 |
"/** @interface \n @extends {Int0} \n @extends {Int1} */" + |
| 11077 |
"function Int2() {};", |
| 11078 |
"Interface Int2 has a property foo with incompatible types in its " + |
| 11079 |
"super interfaces Int0 and Int1"); |
| 11080 |
} |
| 11081 |
|
| 11082 |
public void testExtendedInterfacePropertiesCompatibility2() throws Exception { |
| 11083 |
testTypes( |
| 11084 |
"/** @interface */function Int0() {};" + |
| 11085 |
"/** @interface */function Int1() {};" + |
| 11086 |
"/** @interface */function Int2() {};" + |
| 11087 |
"/** @type {number} */" + |
| 11088 |
"Int0.prototype.foo;" + |
| 11089 |
"/** @type {string} */" + |
| 11090 |
"Int1.prototype.foo;" + |
| 11091 |
"/** @type {Object} */" + |
| 11092 |
"Int2.prototype.foo;" + |
| 11093 |
"/** @interface \n @extends {Int0} \n @extends {Int1} \n" + |
| 11094 |
"@extends {Int2}*/" + |
| 11095 |
"function Int3() {};", |
| 11096 |
new String[] { |
| 11097 |
"Interface Int3 has a property foo with incompatible types in " + |
| 11098 |
"its super interfaces Int0 and Int1", |
| 11099 |
"Interface Int3 has a property foo with incompatible types in " + |
| 11100 |
"its super interfaces Int1 and Int2" |
| 11101 |
}); |
| 11102 |
} |
| 11103 |
|
| 11104 |
public void testExtendedInterfacePropertiesCompatibility3() throws Exception { |
| 11105 |
testTypes( |
| 11106 |
"/** @interface */function Int0() {};" + |
| 11107 |
"/** @interface */function Int1() {};" + |
| 11108 |
"/** @type {number} */" + |
| 11109 |
"Int0.prototype.foo;" + |
| 11110 |
"/** @type {string} */" + |
| 11111 |
"Int1.prototype.foo;" + |
| 11112 |
"/** @interface \n @extends {Int1} */ function Int2() {};" + |
| 11113 |
"/** @interface \n @extends {Int0} \n @extends {Int2} */" + |
| 11114 |
"function Int3() {};", |
| 11115 |
"Interface Int3 has a property foo with incompatible types in its " + |
| 11116 |
"super interfaces Int0 and Int1"); |
| 11117 |
} |
| 11118 |
|
| 11119 |
public void testExtendedInterfacePropertiesCompatibility4() throws Exception { |
| 11120 |
testTypes( |
| 11121 |
"/** @interface */function Int0() {};" + |
| 11122 |
"/** @interface \n @extends {Int0} */ function Int1() {};" + |
| 11123 |
"/** @type {number} */" + |
| 11124 |
"Int0.prototype.foo;" + |
| 11125 |
"/** @interface */function Int2() {};" + |
| 11126 |
"/** @interface \n @extends {Int2} */ function Int3() {};" + |
| 11127 |
"/** @type {string} */" + |
| 11128 |
"Int2.prototype.foo;" + |
| 11129 |
"/** @interface \n @extends {Int1} \n @extends {Int3} */" + |
| 11130 |
"function Int4() {};", |
| 11131 |
"Interface Int4 has a property foo with incompatible types in its " + |
| 11132 |
"super interfaces Int0 and Int2"); |
| 11133 |
} |
| 11134 |
|
| 11135 |
public void testExtendedInterfacePropertiesCompatibility5() throws Exception { |
| 11136 |
testTypes( |
| 11137 |
"/** @interface */function Int0() {};" + |
| 11138 |
"/** @interface */function Int1() {};" + |
| 11139 |
"/** @type {number} */" + |
| 11140 |
"Int0.prototype.foo;" + |
| 11141 |
"/** @type {string} */" + |
| 11142 |
"Int1.prototype.foo;" + |
| 11143 |
"/** @interface \n @extends {Int1} */ function Int2() {};" + |
| 11144 |
"/** @interface \n @extends {Int0} \n @extends {Int2} */" + |
| 11145 |
"function Int3() {};" + |
| 11146 |
"/** @interface */function Int4() {};" + |
| 11147 |
"/** @type {number} */" + |
| 11148 |
"Int4.prototype.foo;" + |
| 11149 |
"/** @interface \n @extends {Int3} \n @extends {Int4} */" + |
| 11150 |
"function Int5() {};", |
| 11151 |
new String[] { |
| 11152 |
"Interface Int3 has a property foo with incompatible types in its" + |
| 11153 |
" super interfaces Int0 and Int1", |
| 11154 |
"Interface Int5 has a property foo with incompatible types in its" + |
| 11155 |
" super interfaces Int1 and Int4"}); |
| 11156 |
} |
| 11157 |
|
| 11158 |
public void testExtendedInterfacePropertiesCompatibility6() throws Exception { |
| 11159 |
testTypes( |
| 11160 |
"/** @interface */function Int0() {};" + |
| 11161 |
"/** @interface */function Int1() {};" + |
| 11162 |
"/** @type {number} */" + |
| 11163 |
"Int0.prototype.foo;" + |
| 11164 |
"/** @type {string} */" + |
| 11165 |
"Int1.prototype.foo;" + |
| 11166 |
"/** @interface \n @extends {Int1} */ function Int2() {};" + |
| 11167 |
"/** @interface \n @extends {Int0} \n @extends {Int2} */" + |
| 11168 |
"function Int3() {};" + |
| 11169 |
"/** @interface */function Int4() {};" + |
| 11170 |
"/** @type {string} */" + |
| 11171 |
"Int4.prototype.foo;" + |
| 11172 |
"/** @interface \n @extends {Int3} \n @extends {Int4} */" + |
| 11173 |
"function Int5() {};", |
| 11174 |
"Interface Int3 has a property foo with incompatible types in its" + |
| 11175 |
" super interfaces Int0 and Int1"); |
| 11176 |
} |
| 11177 |
|
| 11178 |
public void testExtendedInterfacePropertiesCompatibility7() throws Exception { |
| 11179 |
testTypes( |
| 11180 |
"/** @interface */function Int0() {};" + |
| 11181 |
"/** @interface */function Int1() {};" + |
| 11182 |
"/** @type {number} */" + |
| 11183 |
"Int0.prototype.foo;" + |
| 11184 |
"/** @type {string} */" + |
| 11185 |
"Int1.prototype.foo;" + |
| 11186 |
"/** @interface \n @extends {Int1} */ function Int2() {};" + |
| 11187 |
"/** @interface \n @extends {Int0} \n @extends {Int2} */" + |
| 11188 |
"function Int3() {};" + |
| 11189 |
"/** @interface */function Int4() {};" + |
| 11190 |
"/** @type {Object} */" + |
| 11191 |
"Int4.prototype.foo;" + |
| 11192 |
"/** @interface \n @extends {Int3} \n @extends {Int4} */" + |
| 11193 |
"function Int5() {};", |
| 11194 |
new String[] { |
| 11195 |
"Interface Int3 has a property foo with incompatible types in its" + |
| 11196 |
" super interfaces Int0 and Int1", |
| 11197 |
"Interface Int5 has a property foo with incompatible types in its" + |
| 11198 |
" super interfaces Int1 and Int4"}); |
| 11199 |
} |
| 11200 |
|
| 11201 |
public void testExtendedInterfacePropertiesCompatibility8() throws Exception { |
| 11202 |
testTypes( |
| 11203 |
"/** @interface */function Int0() {};" + |
| 11204 |
"/** @interface */function Int1() {};" + |
| 11205 |
"/** @type {number} */" + |
| 11206 |
"Int0.prototype.foo;" + |
| 11207 |
"/** @type {string} */" + |
| 11208 |
"Int1.prototype.bar;" + |
| 11209 |
"/** @interface \n @extends {Int1} */ function Int2() {};" + |
| 11210 |
"/** @interface \n @extends {Int0} \n @extends {Int2} */" + |
| 11211 |
"function Int3() {};" + |
| 11212 |
"/** @interface */function Int4() {};" + |
| 11213 |
"/** @type {Object} */" + |
| 11214 |
"Int4.prototype.foo;" + |
| 11215 |
"/** @type {Null} */" + |
| 11216 |
"Int4.prototype.bar;" + |
| 11217 |
"/** @interface \n @extends {Int3} \n @extends {Int4} */" + |
| 11218 |
"function Int5() {};", |
| 11219 |
new String[] { |
| 11220 |
"Interface Int5 has a property bar with incompatible types in its" + |
| 11221 |
" super interfaces Int1 and Int4", |
| 11222 |
"Interface Int5 has a property foo with incompatible types in its" + |
| 11223 |
" super interfaces Int0 and Int4"}); |
| 11224 |
} |
| 11225 |
|
| 11226 |
public void testGenerics1() throws Exception { |
| 11227 |
String fnDecl = "/** \n" + |
| 11228 |
" * @param {T} x \n" + |
| 11229 |
" * @param {function(T):T} y \n" + |
| 11230 |
" * @template T\n" + |
| 11231 |
" */ \n" + |
| 11232 |
"function f(x,y) { return y(x); }\n"; |
| 11233 |
|
| 11234 |
testTypes( |
| 11235 |
fnDecl + |
| 11236 |
"/** @type {string} */" + |
| 11237 |
"var out;" + |
| 11238 |
"/** @type {string} */" + |
| 11239 |
"var result = f('hi', function(x){ out = x; return x; });"); |
| 11240 |
|
| 11241 |
testTypes( |
| 11242 |
fnDecl + |
| 11243 |
"/** @type {string} */" + |
| 11244 |
"var out;" + |
| 11245 |
"var result = f(0, function(x){ out = x; return x; });", |
| 11246 |
"assignment\n" + |
| 11247 |
"found : number\n" + |
| 11248 |
"required: string"); |
| 11249 |
|
| 11250 |
testTypes( |
| 11251 |
fnDecl + |
| 11252 |
"var out;" + |
| 11253 |
"/** @type {string} */" + |
| 11254 |
"var result = f(0, function(x){ out = x; return x; });", |
| 11255 |
"assignment\n" + |
| 11256 |
"found : number\n" + |
| 11257 |
"required: string"); |
| 11258 |
} |
| 11259 |
|
| 11260 |
public void testFilter0() |
| 11261 |
throws Exception { |
| 11262 |
testTypes( |
| 11263 |
"/**\n" + |
| 11264 |
" * @param {T} arr\n" + |
| 11265 |
" * @return {T}\n" + |
| 11266 |
" * @template T\n" + |
| 11267 |
" */\n" + |
| 11268 |
"var filter = function(arr){};\n" + |
| 11269 |
|
| 11270 |
"/** @type {!Array.<string>} */" + |
| 11271 |
"var arr;\n" + |
| 11272 |
"/** @type {!Array.<string>} */" + |
| 11273 |
"var result = filter(arr);"); |
| 11274 |
} |
| 11275 |
|
| 11276 |
public void testFilter1() |
| 11277 |
throws Exception { |
| 11278 |
testTypes( |
| 11279 |
"/**\n" + |
| 11280 |
" * @param {!Array.<T>} arr\n" + |
| 11281 |
" * @return {!Array.<T>}\n" + |
| 11282 |
" * @template T\n" + |
| 11283 |
" */\n" + |
| 11284 |
"var filter = function(arr){};\n" + |
| 11285 |
|
| 11286 |
"/** @type {!Array.<string>} */" + |
| 11287 |
"var arr;\n" + |
| 11288 |
"/** @type {!Array.<string>} */" + |
| 11289 |
"var result = filter(arr);"); |
| 11290 |
} |
| 11291 |
|
| 11292 |
public void testFilter2() |
| 11293 |
throws Exception { |
| 11294 |
testTypes( |
| 11295 |
"/**\n" + |
| 11296 |
" * @param {!Array.<T>} arr\n" + |
| 11297 |
" * @return {!Array.<T>}\n" + |
| 11298 |
" * @template T\n" + |
| 11299 |
" */\n" + |
| 11300 |
"var filter = function(arr){};\n" + |
| 11301 |
|
| 11302 |
"/** @type {!Array.<string>} */" + |
| 11303 |
"var arr;\n" + |
| 11304 |
"/** @type {!Array.<number>} */" + |
| 11305 |
"var result = filter(arr);", |
| 11306 |
"initializing variable\n" + |
| 11307 |
"found : Array.<string>\n" + |
| 11308 |
"required: Array.<number>"); |
| 11309 |
} |
| 11310 |
|
| 11311 |
public void testFilter3() |
| 11312 |
throws Exception { |
| 11313 |
testTypes( |
| 11314 |
"/**\n" + |
| 11315 |
" * @param {Array.<T>} arr\n" + |
| 11316 |
" * @return {Array.<T>}\n" + |
| 11317 |
" * @template T\n" + |
| 11318 |
" */\n" + |
| 11319 |
"var filter = function(arr){};\n" + |
| 11320 |
|
| 11321 |
"/** @type {Array.<string>} */" + |
| 11322 |
"var arr;\n" + |
| 11323 |
"/** @type {Array.<number>} */" + |
| 11324 |
"var result = filter(arr);", |
| 11325 |
"initializing variable\n" + |
| 11326 |
"found : (Array.<string>|null)\n" + |
| 11327 |
"required: (Array.<number>|null)"); |
| 11328 |
} |
| 11329 |
|
| 11330 |
public void testBackwardsInferenceGoogArrayFilter1() |
| 11331 |
throws Exception { |
| 11332 |
testClosureTypes( |
| 11333 |
CLOSURE_DEFS + |
| 11334 |
"/** @type {Array.<string>} */" + |
| 11335 |
"var arr;\n" + |
| 11336 |
"/** @type {!Array.<number>} */" + |
| 11337 |
"var result = goog.array.filter(" + |
| 11338 |
" arr," + |
| 11339 |
" function(item,index,src) {return false;});", |
| 11340 |
"initializing variable\n" + |
| 11341 |
"found : Array.<string>\n" + |
| 11342 |
"required: Array.<number>"); |
| 11343 |
} |
| 11344 |
|
| 11345 |
public void testBackwardsInferenceGoogArrayFilter2() throws Exception { |
| 11346 |
testClosureTypes( |
| 11347 |
CLOSURE_DEFS + |
| 11348 |
"/** @type {number} */" + |
| 11349 |
"var out;" + |
| 11350 |
"/** @type {Array.<string>} */" + |
| 11351 |
"var arr;\n" + |
| 11352 |
"var out4 = goog.array.filter(" + |
| 11353 |
" arr," + |
| 11354 |
" function(item,index,src) {out = item;});", |
| 11355 |
"assignment\n" + |
| 11356 |
"found : string\n" + |
| 11357 |
"required: number"); |
| 11358 |
} |
| 11359 |
|
| 11360 |
public void testBackwardsInferenceGoogArrayFilter3() throws Exception { |
| 11361 |
testClosureTypes( |
| 11362 |
CLOSURE_DEFS + |
| 11363 |
"/** @type {string} */" + |
| 11364 |
"var out;" + |
| 11365 |
"/** @type {Array.<string>} */ var arr;\n" + |
| 11366 |
"var result = goog.array.filter(" + |
| 11367 |
" arr," + |
| 11368 |
" function(item,index,src) {out = index;});", |
| 11369 |
"assignment\n" + |
| 11370 |
"found : number\n" + |
| 11371 |
"required: string"); |
| 11372 |
} |
| 11373 |
|
| 11374 |
public void testBackwardsInferenceGoogArrayFilter4() throws Exception { |
| 11375 |
testClosureTypes( |
| 11376 |
CLOSURE_DEFS + |
| 11377 |
"/** @type {string} */" + |
| 11378 |
"var out;" + |
| 11379 |
"/** @type {Array.<string>} */ var arr;\n" + |
| 11380 |
"var out4 = goog.array.filter(" + |
| 11381 |
" arr," + |
| 11382 |
" function(item,index,srcArr) {out = srcArr;});", |
| 11383 |
"assignment\n" + |
| 11384 |
"found : (null|{length: number})\n" + |
| 11385 |
"required: string"); |
| 11386 |
} |
| 11387 |
|
| 11388 |
public void testCatchExpression1() throws Exception { |
| 11389 |
testTypes( |
| 11390 |
"function fn() {" + |
| 11391 |
" /** @type {number} */" + |
| 11392 |
" var out = 0;" + |
| 11393 |
" try {\n" + |
| 11394 |
" foo();\n" + |
| 11395 |
" } catch (/** @type {string} */ e) {\n" + |
| 11396 |
" out = e;" + |
| 11397 |
" }" + |
| 11398 |
"}\n", |
| 11399 |
"assignment\n" + |
| 11400 |
"found : string\n" + |
| 11401 |
"required: number"); |
| 11402 |
} |
| 11403 |
|
| 11404 |
public void testCatchExpression2() throws Exception { |
| 11405 |
testTypes( |
| 11406 |
"function fn() {" + |
| 11407 |
" /** @type {number} */" + |
| 11408 |
" var out = 0;" + |
| 11409 |
" /** @type {string} */" + |
| 11410 |
" var e;" + |
| 11411 |
" try {\n" + |
| 11412 |
" foo();\n" + |
| 11413 |
" } catch (e) {\n" + |
| 11414 |
" out = e;" + |
| 11415 |
" }" + |
| 11416 |
"}\n"); |
| 11417 |
} |
| 11418 |
|
| 11419 |
public void testTemplatized1() throws Exception { |
| 11420 |
testTypes( |
| 11421 |
"/** @type {!Array.<string>} */" + |
| 11422 |
"var arr1 = [];\n" + |
| 11423 |
"/** @type {!Array.<number>} */" + |
| 11424 |
"var arr2 = [];\n" + |
| 11425 |
"arr1 = arr2;", |
| 11426 |
"assignment\n" + |
| 11427 |
"found : Array.<number>\n" + |
| 11428 |
"required: Array.<string>"); |
| 11429 |
} |
| 11430 |
|
| 11431 |
public void testTemplatized2() throws Exception { |
| 11432 |
testTypes( |
| 11433 |
"/** @type {!Array.<string>} */" + |
| 11434 |
"var arr1 = /** @type {!Array.<number>} */([]);\n", |
| 11435 |
"initializing variable\n" + |
| 11436 |
"found : Array.<number>\n" + |
| 11437 |
"required: Array.<string>"); |
| 11438 |
} |
| 11439 |
|
| 11440 |
public void testTemplatized3() throws Exception { |
| 11441 |
testTypes( |
| 11442 |
"/** @type {Array.<string>} */" + |
| 11443 |
"var arr1 = /** @type {!Array.<number>} */([]);\n", |
| 11444 |
"initializing variable\n" + |
| 11445 |
"found : Array.<number>\n" + |
| 11446 |
"required: (Array.<string>|null)"); |
| 11447 |
} |
| 11448 |
|
| 11449 |
public void testTemplatized4() throws Exception { |
| 11450 |
testTypes( |
| 11451 |
"/** @type {Array.<string>} */" + |
| 11452 |
"var arr1 = [];\n" + |
| 11453 |
"/** @type {Array.<number>} */" + |
| 11454 |
"var arr2 = arr1;\n", |
| 11455 |
"initializing variable\n" + |
| 11456 |
"found : (Array.<string>|null)\n" + |
| 11457 |
"required: (Array.<number>|null)"); |
| 11458 |
} |
| 11459 |
|
| 11460 |
public void testTemplatized5() throws Exception { |
| 11461 |
testTypes( |
| 11462 |
"/**\n" + |
| 11463 |
" * @param {Object.<T>} obj\n" + |
| 11464 |
" * @return {boolean|undefined}\n" + |
| 11465 |
" * @template T\n" + |
| 11466 |
" */\n" + |
| 11467 |
"var some = function(obj) {" + |
| 11468 |
" for (var key in obj) if (obj[key]) return true;" + |
| 11469 |
"};" + |
| 11470 |
"/** @return {!Array} */ function f() { return []; }" + |
| 11471 |
"/** @return {!Array.<string>} */ function g() { return []; }" + |
| 11472 |
"some(f());\n" + |
| 11473 |
"some(g());\n"); |
| 11474 |
} |
| 11475 |
|
| 11476 |
public void testTemplatizedTypeSubtypes2() throws Exception { |
| 11477 |
JSType arrayOfNumber = createTemplatizedType( |
| 11478 |
ARRAY_TYPE, NUMBER_TYPE); |
| 11479 |
JSType arrayOfString = createTemplatizedType( |
| 11480 |
ARRAY_TYPE, STRING_TYPE); |
| 11481 |
assertFalse(arrayOfString.isSubtype(createUnionType(arrayOfNumber, NULL_VOID))); |
| 11482 |
|
| 11483 |
} |
| 11484 |
|
| 11485 |
private void testTypes(String js) throws Exception { |
| 11486 |
testTypes(js, (String) null); |
| 11487 |
} |
| 11488 |
|
| 11489 |
private void testTypes(String js, String description) throws Exception { |
| 11490 |
testTypes(js, description, false); |
| 11491 |
} |
| 11492 |
|
| 11493 |
private void testTypes(String js, DiagnosticType type) throws Exception { |
| 11494 |
testTypes(js, type.format(), false); |
| 11495 |
} |
| 11496 |
|
| 11497 |
private void testClosureTypes(String js, String description) |
| 11498 |
throws Exception { |
| 11499 |
testClosureTypesMultipleWarnings(js, |
| 11500 |
description == null ? null : Lists.newArrayList(description)); |
| 11501 |
} |
| 11502 |
|
| 11503 |
private void testClosureTypesMultipleWarnings( |
| 11504 |
String js, List<String> descriptions) throws Exception { |
| 11505 |
Node n = compiler.parseTestCode(js); |
| 11506 |
Node externs = new Node(Token.BLOCK); |
| 11507 |
Node externAndJsRoot = new Node(Token.BLOCK, externs, n); |
| 11508 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 11509 |
|
| 11510 |
assertEquals("parsing error: " + |
| 11511 |
Joiner.on(", ").join(compiler.getErrors()), |
| 11512 |
0, compiler.getErrorCount()); |
| 11513 |
|
| 11514 |
|
| 11515 |
new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR) |
| 11516 |
.process(null, n); |
| 11517 |
|
| 11518 |
CodingConvention convention = compiler.getCodingConvention(); |
| 11519 |
new TypeCheck(compiler, |
| 11520 |
new ClosureReverseAbstractInterpreter( |
| 11521 |
convention, registry).append( |
| 11522 |
new SemanticReverseAbstractInterpreter( |
| 11523 |
convention, registry)) |
| 11524 |
.getFirst(), |
| 11525 |
registry) |
| 11526 |
.processForTesting(null, n); |
| 11527 |
|
| 11528 |
assertEquals( |
| 11529 |
"unexpected error(s) : " + |
| 11530 |
Joiner.on(", ").join(compiler.getErrors()), |
| 11531 |
0, compiler.getErrorCount()); |
| 11532 |
|
| 11533 |
if (descriptions == null) { |
| 11534 |
assertEquals( |
| 11535 |
"unexpected warning(s) : " + |
| 11536 |
Joiner.on(", ").join(compiler.getWarnings()), |
| 11537 |
0, compiler.getWarningCount()); |
| 11538 |
} else { |
| 11539 |
assertEquals( |
| 11540 |
"unexpected warning(s) : " + |
| 11541 |
Joiner.on(", ").join(compiler.getWarnings()), |
| 11542 |
descriptions.size(), compiler.getWarningCount()); |
| 11543 |
Set<String> actualWarningDescriptions = Sets.newHashSet(); |
| 11544 |
for (int i = 0; i < descriptions.size(); i++) { |
| 11545 |
actualWarningDescriptions.add(compiler.getWarnings()[i].description); |
| 11546 |
} |
| 11547 |
assertEquals( |
| 11548 |
Sets.newHashSet(descriptions), actualWarningDescriptions); |
| 11549 |
} |
| 11550 |
} |
| 11551 |
|
| 11552 |
void testTypes(String js, String description, boolean isError) |
| 11553 |
throws Exception { |
| 11554 |
testTypes(DEFAULT_EXTERNS, js, description, isError); |
| 11555 |
} |
| 11556 |
|
| 11557 |
void testTypes(String externs, String js, String description, boolean isError) |
| 11558 |
throws Exception { |
| 11559 |
parseAndTypeCheck(externs, js); |
| 11560 |
|
| 11561 |
JSError[] errors = compiler.getErrors(); |
| 11562 |
if (description != null && isError) { |
| 11563 |
assertTrue("expected an error", errors.length > 0); |
| 11564 |
assertEquals(description, errors[0].description); |
| 11565 |
errors = Arrays.asList(errors).subList(1, errors.length).toArray( |
| 11566 |
new JSError[errors.length - 1]); |
| 11567 |
} |
| 11568 |
if (errors.length > 0) { |
| 11569 |
fail("unexpected error(s):\n" + Joiner.on("\n").join(errors)); |
| 11570 |
} |
| 11571 |
|
| 11572 |
JSError[] warnings = compiler.getWarnings(); |
| 11573 |
if (description != null && !isError) { |
| 11574 |
assertTrue("expected a warning", warnings.length > 0); |
| 11575 |
assertEquals(description, warnings[0].description); |
| 11576 |
warnings = Arrays.asList(warnings).subList(1, warnings.length).toArray( |
| 11577 |
new JSError[warnings.length - 1]); |
| 11578 |
} |
| 11579 |
if (warnings.length > 0) { |
| 11580 |
fail("unexpected warnings(s):\n" + Joiner.on("\n").join(warnings)); |
| 11581 |
} |
| 11582 |
} |
| 11583 |
|
| 11584 |
|
| 11585 |
|
| 11586 |
|
| 11587 |
private Node parseAndTypeCheck(String js) { |
| 11588 |
return parseAndTypeCheck(DEFAULT_EXTERNS, js); |
| 11589 |
} |
| 11590 |
|
| 11591 |
private Node parseAndTypeCheck(String externs, String js) { |
| 11592 |
return parseAndTypeCheckWithScope(externs, js).root; |
| 11593 |
} |
| 11594 |
|
| 11595 |
|
| 11596 |
|
| 11597 |
|
| 11598 |
|
| 11599 |
private TypeCheckResult parseAndTypeCheckWithScope(String js) { |
| 11600 |
return parseAndTypeCheckWithScope(DEFAULT_EXTERNS, js); |
| 11601 |
} |
| 11602 |
|
| 11603 |
private TypeCheckResult parseAndTypeCheckWithScope( |
| 11604 |
String externs, String js) { |
| 11605 |
compiler.init( |
| 11606 |
Lists.newArrayList(SourceFile.fromCode("[externs]", externs)), |
| 11607 |
Lists.newArrayList(SourceFile.fromCode("[testcode]", js)), |
| 11608 |
compiler.getOptions()); |
| 11609 |
|
| 11610 |
Node n = compiler.getInput(new InputId("[testcode]")).getAstRoot(compiler); |
| 11611 |
Node externsNode = compiler.getInput(new InputId("[externs]")) |
| 11612 |
.getAstRoot(compiler); |
| 11613 |
Node externAndJsRoot = new Node(Token.BLOCK, externsNode, n); |
| 11614 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 11615 |
|
| 11616 |
assertEquals("parsing error: " + |
| 11617 |
Joiner.on(", ").join(compiler.getErrors()), |
| 11618 |
0, compiler.getErrorCount()); |
| 11619 |
|
| 11620 |
Scope s = makeTypeCheck().processForTesting(externsNode, n); |
| 11621 |
return new TypeCheckResult(n, s); |
| 11622 |
} |
| 11623 |
|
| 11624 |
private Node typeCheck(Node n) { |
| 11625 |
Node externsNode = new Node(Token.BLOCK); |
| 11626 |
Node externAndJsRoot = new Node(Token.BLOCK, externsNode, n); |
| 11627 |
externAndJsRoot.setIsSyntheticBlock(true); |
| 11628 |
|
| 11629 |
makeTypeCheck().processForTesting(null, n); |
| 11630 |
return n; |
| 11631 |
} |
| 11632 |
|
| 11633 |
private TypeCheck makeTypeCheck() { |
| 11634 |
return new TypeCheck( |
| 11635 |
compiler, |
| 11636 |
new SemanticReverseAbstractInterpreter( |
| 11637 |
compiler.getCodingConvention(), registry), |
| 11638 |
registry, |
| 11639 |
reportMissingOverrides, |
| 11640 |
CheckLevel.OFF); |
| 11641 |
} |
| 11642 |
|
| 11643 |
void testTypes(String js, String[] warnings) throws Exception { |
| 11644 |
Node n = compiler.parseTestCode(js); |
| 11645 |
assertEquals(0, compiler.getErrorCount()); |
| 11646 |
Node externsNode = new Node(Token.BLOCK); |
| 11647 |
|
| 11648 |
new Node(Token.BLOCK, externsNode, n); |
| 11649 |
|
| 11650 |
makeTypeCheck().processForTesting(null, n); |
| 11651 |
assertEquals(0, compiler.getErrorCount()); |
| 11652 |
if (warnings != null) { |
| 11653 |
assertEquals(warnings.length, compiler.getWarningCount()); |
| 11654 |
JSError[] messages = compiler.getWarnings(); |
| 11655 |
for (int i = 0; i < warnings.length && i < compiler.getWarningCount(); |
| 11656 |
i++) { |
| 11657 |
assertEquals(warnings[i], messages[i].description); |
| 11658 |
} |
| 11659 |
} else { |
| 11660 |
assertEquals(0, compiler.getWarningCount()); |
| 11661 |
} |
| 11662 |
} |
| 11663 |
|
| 11664 |
String suppressMissingProperty(String ... props) { |
| 11665 |
String result = "function dummy(x) { "; |
| 11666 |
for (String prop : props) { |
| 11667 |
result += "x." + prop + " = 3;"; |
| 11668 |
} |
| 11669 |
return result + "}"; |
| 11670 |
} |
| 11671 |
|
| 11672 |
private static class TypeCheckResult { |
| 11673 |
private final Node root; |
| 11674 |
private final Scope scope; |
| 11675 |
|
| 11676 |
private TypeCheckResult(Node root, Scope scope) { |
| 11677 |
this.root = root; |
| 11678 |
this.scope = scope; |
| 11679 |
} |
| 11680 |
} |
| 11681 |
} |