| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.parsing; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableList; |
| 20 |
import com.google.javascript.jscomp.parsing.Config.LanguageMode; |
| 21 |
import com.google.javascript.jscomp.testing.TestErrorReporter; |
| 22 |
import com.google.javascript.rhino.JSDocInfo; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
import com.google.javascript.rhino.head.ScriptRuntime; |
| 26 |
import com.google.javascript.rhino.jstype.SimpleSourceFile; |
| 27 |
import com.google.javascript.rhino.jstype.StaticSourceFile; |
| 28 |
import com.google.javascript.rhino.testing.BaseJSTypeTestCase; |
| 29 |
|
| 30 |
import java.io.IOException; |
| 31 |
import java.util.List; |
| 32 |
import java.util.logging.Logger; |
| 33 |
|
| 34 |
public class ParserTest extends BaseJSTypeTestCase { |
| 35 |
private static final String SUSPICIOUS_COMMENT_WARNING = |
| 36 |
IRFactory.SUSPICIOUS_COMMENT_WARNING; |
| 37 |
|
| 38 |
private static final String TRAILING_COMMA_MESSAGE = |
| 39 |
ScriptRuntime.getMessage0("msg.extra.trailing.comma"); |
| 40 |
|
| 41 |
private static final String BAD_PROPERTY_MESSAGE = |
| 42 |
ScriptRuntime.getMessage0("msg.bad.prop"); |
| 43 |
|
| 44 |
private static final String MISSING_GT_MESSAGE = |
| 45 |
"Bad type annotation. " + |
| 46 |
com.google.javascript.rhino.ScriptRuntime.getMessage0( |
| 47 |
"msg.jsdoc.missing.gt"); |
| 48 |
|
| 49 |
private static final String MISPLACED_TYPE_ANNOTATION = |
| 50 |
IRFactory.MISPLACED_TYPE_ANNOTATION; |
| 51 |
|
| 52 |
private Config.LanguageMode mode; |
| 53 |
private boolean isIdeMode = false; |
| 54 |
|
| 55 |
@Override |
| 56 |
protected void setUp() throws Exception { |
| 57 |
super.setUp(); |
| 58 |
mode = LanguageMode.ECMASCRIPT3; |
| 59 |
isIdeMode = false; |
| 60 |
} |
| 61 |
|
| 62 |
public void testLinenoCharnoAssign1() throws Exception { |
| 63 |
Node assign = parse("a = b").getFirstChild().getFirstChild(); |
| 64 |
|
| 65 |
assertEquals(Token.ASSIGN, assign.getType()); |
| 66 |
assertEquals(1, assign.getLineno()); |
| 67 |
assertEquals(0, assign.getCharno()); |
| 68 |
} |
| 69 |
|
| 70 |
public void testLinenoCharnoAssign2() throws Exception { |
| 71 |
Node assign = parse("\n a.g.h.k = 45").getFirstChild().getFirstChild(); |
| 72 |
|
| 73 |
assertEquals(Token.ASSIGN, assign.getType()); |
| 74 |
assertEquals(2, assign.getLineno()); |
| 75 |
assertEquals(1, assign.getCharno()); |
| 76 |
} |
| 77 |
|
| 78 |
public void testLinenoCharnoCall() throws Exception { |
| 79 |
Node call = parse("\n foo(123);").getFirstChild().getFirstChild(); |
| 80 |
|
| 81 |
assertEquals(Token.CALL, call.getType()); |
| 82 |
assertEquals(2, call.getLineno()); |
| 83 |
assertEquals(1, call.getCharno()); |
| 84 |
} |
| 85 |
|
| 86 |
public void testLinenoCharnoGetProp1() throws Exception { |
| 87 |
Node getprop = parse("\n foo.bar").getFirstChild().getFirstChild(); |
| 88 |
|
| 89 |
assertEquals(Token.GETPROP, getprop.getType()); |
| 90 |
assertEquals(2, getprop.getLineno()); |
| 91 |
assertEquals(1, getprop.getCharno()); |
| 92 |
|
| 93 |
Node name = getprop.getFirstChild().getNext(); |
| 94 |
assertEquals(Token.STRING, name.getType()); |
| 95 |
assertEquals(2, name.getLineno()); |
| 96 |
assertEquals(5, name.getCharno()); |
| 97 |
} |
| 98 |
|
| 99 |
public void testLinenoCharnoGetProp2() throws Exception { |
| 100 |
Node getprop = parse("\n foo.\nbar").getFirstChild().getFirstChild(); |
| 101 |
|
| 102 |
assertEquals(Token.GETPROP, getprop.getType()); |
| 103 |
assertEquals(2, getprop.getLineno()); |
| 104 |
assertEquals(1, getprop.getCharno()); |
| 105 |
|
| 106 |
Node name = getprop.getFirstChild().getNext(); |
| 107 |
assertEquals(Token.STRING, name.getType()); |
| 108 |
assertEquals(3, name.getLineno()); |
| 109 |
assertEquals(0, name.getCharno()); |
| 110 |
} |
| 111 |
|
| 112 |
public void testLinenoCharnoGetelem1() throws Exception { |
| 113 |
Node call = parse("\n foo[123]").getFirstChild().getFirstChild(); |
| 114 |
|
| 115 |
assertEquals(Token.GETELEM, call.getType()); |
| 116 |
assertEquals(2, call.getLineno()); |
| 117 |
assertEquals(1, call.getCharno()); |
| 118 |
} |
| 119 |
|
| 120 |
public void testLinenoCharnoGetelem2() throws Exception { |
| 121 |
Node call = parse("\n \n foo()[123]").getFirstChild().getFirstChild(); |
| 122 |
|
| 123 |
assertEquals(Token.GETELEM, call.getType()); |
| 124 |
assertEquals(3, call.getLineno()); |
| 125 |
assertEquals(1, call.getCharno()); |
| 126 |
} |
| 127 |
|
| 128 |
public void testLinenoCharnoGetelem3() throws Exception { |
| 129 |
Node call = parse("\n \n (8 + kl)[123]").getFirstChild().getFirstChild(); |
| 130 |
|
| 131 |
assertEquals(Token.GETELEM, call.getType()); |
| 132 |
assertEquals(3, call.getLineno()); |
| 133 |
assertEquals(2, call.getCharno()); |
| 134 |
} |
| 135 |
|
| 136 |
public void testLinenoCharnoForComparison() throws Exception { |
| 137 |
Node lt = |
| 138 |
parse("for (; i < j;){}").getFirstChild().getFirstChild().getNext(); |
| 139 |
|
| 140 |
assertEquals(Token.LT, lt.getType()); |
| 141 |
assertEquals(1, lt.getLineno()); |
| 142 |
assertEquals(7, lt.getCharno()); |
| 143 |
} |
| 144 |
|
| 145 |
public void testLinenoCharnoHook() throws Exception { |
| 146 |
Node n = parse("\n a ? 9 : 0").getFirstChild().getFirstChild(); |
| 147 |
|
| 148 |
assertEquals(Token.HOOK, n.getType()); |
| 149 |
assertEquals(2, n.getLineno()); |
| 150 |
assertEquals(1, n.getCharno()); |
| 151 |
} |
| 152 |
|
| 153 |
public void testLinenoCharnoArrayLiteral() throws Exception { |
| 154 |
Node n = parse("\n [8, 9]").getFirstChild().getFirstChild(); |
| 155 |
|
| 156 |
assertEquals(Token.ARRAYLIT, n.getType()); |
| 157 |
assertEquals(2, n.getLineno()); |
| 158 |
assertEquals(2, n.getCharno()); |
| 159 |
|
| 160 |
n = n.getFirstChild(); |
| 161 |
|
| 162 |
assertEquals(Token.NUMBER, n.getType()); |
| 163 |
assertEquals(2, n.getLineno()); |
| 164 |
assertEquals(3, n.getCharno()); |
| 165 |
|
| 166 |
n = n.getNext(); |
| 167 |
|
| 168 |
assertEquals(Token.NUMBER, n.getType()); |
| 169 |
assertEquals(2, n.getLineno()); |
| 170 |
assertEquals(6, n.getCharno()); |
| 171 |
} |
| 172 |
|
| 173 |
public void testLinenoCharnoObjectLiteral() throws Exception { |
| 174 |
Node n = parse("\n\n var a = {a:0\n,b :1};") |
| 175 |
.getFirstChild().getFirstChild().getFirstChild(); |
| 176 |
|
| 177 |
assertEquals(Token.OBJECTLIT, n.getType()); |
| 178 |
assertEquals(3, n.getLineno()); |
| 179 |
assertEquals(9, n.getCharno()); |
| 180 |
|
| 181 |
Node key = n.getFirstChild(); |
| 182 |
|
| 183 |
assertEquals(Token.STRING_KEY, key.getType()); |
| 184 |
assertEquals(3, key.getLineno()); |
| 185 |
assertEquals(10, key.getCharno()); |
| 186 |
|
| 187 |
Node value = key.getFirstChild(); |
| 188 |
|
| 189 |
assertEquals(Token.NUMBER, value.getType()); |
| 190 |
assertEquals(3, value.getLineno()); |
| 191 |
assertEquals(12, value.getCharno()); |
| 192 |
|
| 193 |
key = key.getNext(); |
| 194 |
|
| 195 |
assertEquals(Token.STRING_KEY, key.getType()); |
| 196 |
assertEquals(4, key.getLineno()); |
| 197 |
assertEquals(1, key.getCharno()); |
| 198 |
|
| 199 |
value = key.getFirstChild(); |
| 200 |
|
| 201 |
assertEquals(Token.NUMBER, value.getType()); |
| 202 |
assertEquals(4, value.getLineno()); |
| 203 |
assertEquals(4, value.getCharno()); |
| 204 |
} |
| 205 |
|
| 206 |
public void testLinenoCharnoAdd() throws Exception { |
| 207 |
testLinenoCharnoBinop("+"); |
| 208 |
} |
| 209 |
|
| 210 |
public void testLinenoCharnoSub() throws Exception { |
| 211 |
testLinenoCharnoBinop("-"); |
| 212 |
} |
| 213 |
|
| 214 |
public void testLinenoCharnoMul() throws Exception { |
| 215 |
testLinenoCharnoBinop("*"); |
| 216 |
} |
| 217 |
|
| 218 |
public void testLinenoCharnoDiv() throws Exception { |
| 219 |
testLinenoCharnoBinop("/"); |
| 220 |
} |
| 221 |
|
| 222 |
public void testLinenoCharnoMod() throws Exception { |
| 223 |
testLinenoCharnoBinop("%"); |
| 224 |
} |
| 225 |
|
| 226 |
public void testLinenoCharnoShift() throws Exception { |
| 227 |
testLinenoCharnoBinop("<<"); |
| 228 |
} |
| 229 |
|
| 230 |
public void testLinenoCharnoBinaryAnd() throws Exception { |
| 231 |
testLinenoCharnoBinop("&"); |
| 232 |
} |
| 233 |
|
| 234 |
public void testLinenoCharnoAnd() throws Exception { |
| 235 |
testLinenoCharnoBinop("&&"); |
| 236 |
} |
| 237 |
|
| 238 |
public void testLinenoCharnoBinaryOr() throws Exception { |
| 239 |
testLinenoCharnoBinop("|"); |
| 240 |
} |
| 241 |
|
| 242 |
public void testLinenoCharnoOr() throws Exception { |
| 243 |
testLinenoCharnoBinop("||"); |
| 244 |
} |
| 245 |
|
| 246 |
public void testLinenoCharnoLt() throws Exception { |
| 247 |
testLinenoCharnoBinop("<"); |
| 248 |
} |
| 249 |
|
| 250 |
public void testLinenoCharnoLe() throws Exception { |
| 251 |
testLinenoCharnoBinop("<="); |
| 252 |
} |
| 253 |
|
| 254 |
public void testLinenoCharnoGt() throws Exception { |
| 255 |
testLinenoCharnoBinop(">"); |
| 256 |
} |
| 257 |
|
| 258 |
public void testLinenoCharnoGe() throws Exception { |
| 259 |
testLinenoCharnoBinop(">="); |
| 260 |
} |
| 261 |
|
| 262 |
private void testLinenoCharnoBinop(String binop) { |
| 263 |
Node op = parse("var a = 89 " + binop + " 76").getFirstChild(). |
| 264 |
getFirstChild().getFirstChild(); |
| 265 |
|
| 266 |
assertEquals(1, op.getLineno()); |
| 267 |
assertEquals(8, op.getCharno()); |
| 268 |
} |
| 269 |
|
| 270 |
public void testJSDocAttachment1() { |
| 271 |
Node varNode = parse("/** @type number */var a;").getFirstChild(); |
| 272 |
|
| 273 |
|
| 274 |
assertEquals(Token.VAR, varNode.getType()); |
| 275 |
JSDocInfo info = varNode.getJSDocInfo(); |
| 276 |
assertNotNull(info); |
| 277 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 278 |
|
| 279 |
|
| 280 |
Node nameNode = varNode.getFirstChild(); |
| 281 |
assertEquals(Token.NAME, nameNode.getType()); |
| 282 |
assertNull(nameNode.getJSDocInfo()); |
| 283 |
} |
| 284 |
|
| 285 |
public void testJSDocAttachment2() { |
| 286 |
Node varNode = parse("/** @type number */var a,b;").getFirstChild(); |
| 287 |
|
| 288 |
|
| 289 |
assertEquals(Token.VAR, varNode.getType()); |
| 290 |
JSDocInfo info = varNode.getJSDocInfo(); |
| 291 |
assertNotNull(info); |
| 292 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 293 |
|
| 294 |
|
| 295 |
Node nameNode1 = varNode.getFirstChild(); |
| 296 |
assertEquals(Token.NAME, nameNode1.getType()); |
| 297 |
assertNull(nameNode1.getJSDocInfo()); |
| 298 |
|
| 299 |
|
| 300 |
Node nameNode2 = nameNode1.getNext(); |
| 301 |
assertEquals(Token.NAME, nameNode2.getType()); |
| 302 |
assertNull(nameNode2.getJSDocInfo()); |
| 303 |
} |
| 304 |
|
| 305 |
public void testJSDocAttachment3() { |
| 306 |
Node assignNode = parse( |
| 307 |
"/** @type number */goog.FOO = 5;").getFirstChild().getFirstChild(); |
| 308 |
|
| 309 |
|
| 310 |
assertEquals(Token.ASSIGN, assignNode.getType()); |
| 311 |
JSDocInfo info = assignNode.getJSDocInfo(); |
| 312 |
assertNotNull(info); |
| 313 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 314 |
} |
| 315 |
|
| 316 |
public void testJSDocAttachment4() { |
| 317 |
Node varNode = parse( |
| 318 |
"var a, /** @define {number} */b = 5;").getFirstChild(); |
| 319 |
|
| 320 |
|
| 321 |
assertEquals(Token.VAR, varNode.getType()); |
| 322 |
assertNull(varNode.getJSDocInfo()); |
| 323 |
|
| 324 |
|
| 325 |
Node a = varNode.getFirstChild(); |
| 326 |
assertNull(a.getJSDocInfo()); |
| 327 |
|
| 328 |
|
| 329 |
Node b = a.getNext(); |
| 330 |
JSDocInfo info = b.getJSDocInfo(); |
| 331 |
assertNotNull(info); |
| 332 |
assertTrue(info.isDefine()); |
| 333 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 334 |
} |
| 335 |
|
| 336 |
public void testJSDocAttachment5() { |
| 337 |
Node varNode = parse( |
| 338 |
"var /** @type number */a, /** @define {number} */b = 5;") |
| 339 |
.getFirstChild(); |
| 340 |
|
| 341 |
|
| 342 |
assertEquals(Token.VAR, varNode.getType()); |
| 343 |
assertNull(varNode.getJSDocInfo()); |
| 344 |
|
| 345 |
|
| 346 |
Node a = varNode.getFirstChild(); |
| 347 |
assertNotNull(a.getJSDocInfo()); |
| 348 |
JSDocInfo info = a.getJSDocInfo(); |
| 349 |
assertNotNull(info); |
| 350 |
assertFalse(info.isDefine()); |
| 351 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 352 |
|
| 353 |
|
| 354 |
Node b = a.getNext(); |
| 355 |
info = b.getJSDocInfo(); |
| 356 |
assertNotNull(info); |
| 357 |
assertTrue(info.isDefine()); |
| 358 |
assertTypeEquals(NUMBER_TYPE, info.getType()); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public void testJSDocAttachment6() throws Exception { |
| 366 |
Node functionNode = parse( |
| 367 |
"var a = /** @param {number} index */5;" + |
| 368 |
"/** @return boolean */function f(index){}") |
| 369 |
.getFirstChild().getNext(); |
| 370 |
|
| 371 |
assertEquals(Token.FUNCTION, functionNode.getType()); |
| 372 |
JSDocInfo info = functionNode.getJSDocInfo(); |
| 373 |
assertNotNull(info); |
| 374 |
assertFalse(info.hasParameter("index")); |
| 375 |
assertTrue(info.hasReturnType()); |
| 376 |
assertTypeEquals(UNKNOWN_TYPE, info.getReturnType()); |
| 377 |
} |
| 378 |
|
| 379 |
public void testJSDocAttachment7() { |
| 380 |
Node varNode = parse("/** */var a;").getFirstChild(); |
| 381 |
|
| 382 |
|
| 383 |
assertEquals(Token.VAR, varNode.getType()); |
| 384 |
|
| 385 |
|
| 386 |
Node nameNode = varNode.getFirstChild(); |
| 387 |
assertEquals(Token.NAME, nameNode.getType()); |
| 388 |
assertNull(nameNode.getJSDocInfo()); |
| 389 |
} |
| 390 |
|
| 391 |
public void testJSDocAttachment8() { |
| 392 |
Node varNode = parse("/** x */var a;").getFirstChild(); |
| 393 |
|
| 394 |
|
| 395 |
assertEquals(Token.VAR, varNode.getType()); |
| 396 |
|
| 397 |
|
| 398 |
Node nameNode = varNode.getFirstChild(); |
| 399 |
assertEquals(Token.NAME, nameNode.getType()); |
| 400 |
assertNull(nameNode.getJSDocInfo()); |
| 401 |
} |
| 402 |
|
| 403 |
public void testJSDocAttachment9() { |
| 404 |
Node varNode = parse("/** \n x */var a;").getFirstChild(); |
| 405 |
|
| 406 |
|
| 407 |
assertEquals(Token.VAR, varNode.getType()); |
| 408 |
|
| 409 |
|
| 410 |
Node nameNode = varNode.getFirstChild(); |
| 411 |
assertEquals(Token.NAME, nameNode.getType()); |
| 412 |
assertNull(nameNode.getJSDocInfo()); |
| 413 |
} |
| 414 |
|
| 415 |
public void testJSDocAttachment10() { |
| 416 |
Node varNode = parse("/** x\n */var a;").getFirstChild(); |
| 417 |
|
| 418 |
|
| 419 |
assertEquals(Token.VAR, varNode.getType()); |
| 420 |
|
| 421 |
|
| 422 |
Node nameNode = varNode.getFirstChild(); |
| 423 |
assertEquals(Token.NAME, nameNode.getType()); |
| 424 |
assertNull(nameNode.getJSDocInfo()); |
| 425 |
} |
| 426 |
|
| 427 |
public void testJSDocAttachment11() { |
| 428 |
Node varNode = |
| 429 |
parse("/** @type {{x : number, 'y' : string, z}} */var a;") |
| 430 |
.getFirstChild(); |
| 431 |
|
| 432 |
|
| 433 |
assertEquals(Token.VAR, varNode.getType()); |
| 434 |
JSDocInfo info = varNode.getJSDocInfo(); |
| 435 |
assertNotNull(info); |
| 436 |
|
| 437 |
assertTypeEquals(createRecordTypeBuilder(). |
| 438 |
addProperty("x", NUMBER_TYPE, null). |
| 439 |
addProperty("y", STRING_TYPE, null). |
| 440 |
addProperty("z", UNKNOWN_TYPE, null). |
| 441 |
build(), |
| 442 |
info.getType()); |
| 443 |
|
| 444 |
|
| 445 |
Node nameNode = varNode.getFirstChild(); |
| 446 |
assertEquals(Token.NAME, nameNode.getType()); |
| 447 |
assertNull(nameNode.getJSDocInfo()); |
| 448 |
} |
| 449 |
|
| 450 |
public void testJSDocAttachment12() { |
| 451 |
Node varNode = |
| 452 |
parse("var a = {/** @type {Object} */ b: c};") |
| 453 |
.getFirstChild(); |
| 454 |
Node objectLitNode = varNode.getFirstChild().getFirstChild(); |
| 455 |
assertEquals(Token.OBJECTLIT, objectLitNode.getType()); |
| 456 |
assertNotNull(objectLitNode.getFirstChild().getJSDocInfo()); |
| 457 |
} |
| 458 |
|
| 459 |
public void testJSDocAttachment13() { |
| 460 |
Node varNode = parse("/** foo */ var a;").getFirstChild(); |
| 461 |
assertNotNull(varNode.getJSDocInfo()); |
| 462 |
} |
| 463 |
|
| 464 |
public void testJSDocAttachment14() { |
| 465 |
Node varNode = parse("/** */ var a;").getFirstChild(); |
| 466 |
assertNull(varNode.getJSDocInfo()); |
| 467 |
} |
| 468 |
|
| 469 |
public void testJSDocAttachment15() { |
| 470 |
Node varNode = parse("/** \n * \n */ var a;").getFirstChild(); |
| 471 |
assertNull(varNode.getJSDocInfo()); |
| 472 |
} |
| 473 |
|
| 474 |
public void testJSDocAttachment16() { |
| 475 |
Node exprCall = |
| 476 |
parse("/** @private */ x(); function f() {};").getFirstChild(); |
| 477 |
assertEquals(Token.EXPR_RESULT, exprCall.getType()); |
| 478 |
assertNull(exprCall.getNext().getJSDocInfo()); |
| 479 |
assertNotNull(exprCall.getFirstChild().getJSDocInfo()); |
| 480 |
} |
| 481 |
|
| 482 |
public void testInlineJSDocAttachment1() { |
| 483 |
Node fn = parse("function f(/** string */ x) {}").getFirstChild(); |
| 484 |
assertTrue(fn.isFunction()); |
| 485 |
|
| 486 |
JSDocInfo info = |
| 487 |
fn.getFirstChild().getNext().getFirstChild().getJSDocInfo(); |
| 488 |
assertNotNull(info); |
| 489 |
assertTypeEquals(STRING_TYPE, info.getType()); |
| 490 |
} |
| 491 |
|
| 492 |
public void testInlineJSDocAttachment2() { |
| 493 |
Node fn = parse( |
| 494 |
"function f(/**\n" + |
| 495 |
" * {string}\n" + |
| 496 |
" */ x) {}").getFirstChild(); |
| 497 |
assertTrue(fn.isFunction()); |
| 498 |
|
| 499 |
JSDocInfo info = |
| 500 |
fn.getFirstChild().getNext().getFirstChild().getJSDocInfo(); |
| 501 |
assertNotNull(info); |
| 502 |
assertTypeEquals(STRING_TYPE, info.getType()); |
| 503 |
} |
| 504 |
|
| 505 |
public void testInlineJSDocAttachment3() { |
| 506 |
parse( |
| 507 |
"function f(/** @type {string} */ x) {}", |
| 508 |
"Bad type annotation. type not recognized due to syntax error"); |
| 509 |
} |
| 510 |
|
| 511 |
public void testInlineJSDocAttachment4() { |
| 512 |
parse( |
| 513 |
"function f(/**\n" + |
| 514 |
" * @type {string}\n" + |
| 515 |
" */ x) {}", |
| 516 |
"Bad type annotation. type not recognized due to syntax error"); |
| 517 |
} |
| 518 |
|
| 519 |
public void testIncorrectJSDocDoesNotAlterJSParsing1() throws Exception { |
| 520 |
assertNodeEquality( |
| 521 |
parse("var a = [1,2]"), |
| 522 |
parse("/** @type Array.<number*/var a = [1,2]", |
| 523 |
MISSING_GT_MESSAGE)); |
| 524 |
} |
| 525 |
|
| 526 |
public void testIncorrectJSDocDoesNotAlterJSParsing2() throws Exception { |
| 527 |
assertNodeEquality( |
| 528 |
parse("var a = [1,2]"), |
| 529 |
parse("/** @type {Array.<number}*/var a = [1,2]", |
| 530 |
MISSING_GT_MESSAGE)); |
| 531 |
} |
| 532 |
|
| 533 |
public void testIncorrectJSDocDoesNotAlterJSParsing3() throws Exception { |
| 534 |
assertNodeEquality( |
| 535 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 536 |
parse("/** @param {Array.<number} nums */" + |
| 537 |
"C.prototype.say=function(nums) {alert(nums.join(','));};", |
| 538 |
MISSING_GT_MESSAGE)); |
| 539 |
} |
| 540 |
|
| 541 |
public void testIncorrectJSDocDoesNotAlterJSParsing4() throws Exception { |
| 542 |
assertNodeEquality( |
| 543 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 544 |
parse("/** @return boolean */" + |
| 545 |
"C.prototype.say=function(nums) {alert(nums.join(','));};")); |
| 546 |
} |
| 547 |
|
| 548 |
public void testIncorrectJSDocDoesNotAlterJSParsing5() throws Exception { |
| 549 |
assertNodeEquality( |
| 550 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 551 |
parse("/** @param boolean this is some string*/" + |
| 552 |
"C.prototype.say=function(nums) {alert(nums.join(','));};")); |
| 553 |
} |
| 554 |
|
| 555 |
public void testIncorrectJSDocDoesNotAlterJSParsing6() throws Exception { |
| 556 |
assertNodeEquality( |
| 557 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 558 |
parse("/** @param {bool!*%E$} */" + |
| 559 |
"C.prototype.say=function(nums) {alert(nums.join(','));};", |
| 560 |
"Bad type annotation. expected closing }", |
| 561 |
"Bad type annotation. expecting a variable name in a @param tag")); |
| 562 |
} |
| 563 |
|
| 564 |
public void testIncorrectJSDocDoesNotAlterJSParsing7() throws Exception { |
| 565 |
assertNodeEquality( |
| 566 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 567 |
parse("/** @see */" + |
| 568 |
"C.prototype.say=function(nums) {alert(nums.join(','));};", |
| 569 |
"@see tag missing description")); |
| 570 |
} |
| 571 |
|
| 572 |
public void testIncorrectJSDocDoesNotAlterJSParsing8() throws Exception { |
| 573 |
assertNodeEquality( |
| 574 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 575 |
parse("/** @author */" + |
| 576 |
"C.prototype.say=function(nums) {alert(nums.join(','));};", |
| 577 |
"@author tag missing author")); |
| 578 |
} |
| 579 |
|
| 580 |
public void testIncorrectJSDocDoesNotAlterJSParsing9() throws Exception { |
| 581 |
assertNodeEquality( |
| 582 |
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"), |
| 583 |
parse("/** @someillegaltag */" + |
| 584 |
"C.prototype.say=function(nums) {alert(nums.join(','));};", |
| 585 |
"illegal use of unknown JSDoc tag \"someillegaltag\";" |
| 586 |
+ " ignoring it")); |
| 587 |
} |
| 588 |
|
| 589 |
public void testUnescapedSlashInRegexpCharClass() throws Exception { |
| 590 |
|
| 591 |
parse("var foo = /[/]/;"); |
| 592 |
parse("var foo = /[hi there/]/;"); |
| 593 |
parse("var foo = /[/yo dude]/;"); |
| 594 |
parse("var foo = /\\/[@#$/watashi/wa/suteevu/desu]/;"); |
| 595 |
} |
| 596 |
|
| 597 |
private void assertNodeEquality(Node expected, Node found) { |
| 598 |
String message = expected.checkTreeEquals(found); |
| 599 |
if (message != null) { |
| 600 |
fail(message); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
@SuppressWarnings("unchecked") |
| 605 |
public void testParse() { |
| 606 |
Node a = Node.newString(Token.NAME, "a"); |
| 607 |
a.addChildToFront(Node.newString(Token.NAME, "b")); |
| 608 |
List<ParserResult> testCases = ImmutableList.of( |
| 609 |
new ParserResult( |
| 610 |
"3;", |
| 611 |
createScript(new Node(Token.EXPR_RESULT, Node.newNumber(3.0)))), |
| 612 |
new ParserResult( |
| 613 |
"var a = b;", |
| 614 |
createScript(new Node(Token.VAR, a))), |
| 615 |
new ParserResult( |
| 616 |
"\"hell\\\no\\ world\\\n\\\n!\"", |
| 617 |
createScript(new Node(Token.EXPR_RESULT, |
| 618 |
Node.newString(Token.STRING, "hello world!"))))); |
| 619 |
|
| 620 |
for (ParserResult testCase : testCases) { |
| 621 |
assertNodeEquality(testCase.node, parse(testCase.code)); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
private Node createScript(Node n) { |
| 626 |
Node script = new Node(Token.SCRIPT); |
| 627 |
script.addChildToBack(n); |
| 628 |
return script; |
| 629 |
} |
| 630 |
|
| 631 |
public void testTrailingCommaWarning1() { |
| 632 |
parse("var a = ['foo', 'bar'];"); |
| 633 |
} |
| 634 |
|
| 635 |
public void testTrailingCommaWarning2() { |
| 636 |
parse("var a = ['foo',,'bar'];"); |
| 637 |
} |
| 638 |
|
| 639 |
public void testTrailingCommaWarning3() { |
| 640 |
parse("var a = ['foo', 'bar',];", TRAILING_COMMA_MESSAGE); |
| 641 |
mode = LanguageMode.ECMASCRIPT5; |
| 642 |
parse("var a = ['foo', 'bar',];"); |
| 643 |
} |
| 644 |
|
| 645 |
public void testTrailingCommaWarning4() { |
| 646 |
parse("var a = [,];", TRAILING_COMMA_MESSAGE); |
| 647 |
mode = LanguageMode.ECMASCRIPT5; |
| 648 |
parse("var a = [,];"); |
| 649 |
} |
| 650 |
|
| 651 |
public void testTrailingCommaWarning5() { |
| 652 |
parse("var a = {'foo': 'bar'};"); |
| 653 |
} |
| 654 |
|
| 655 |
public void testTrailingCommaWarning6() { |
| 656 |
parse("var a = {'foo': 'bar',};", TRAILING_COMMA_MESSAGE); |
| 657 |
mode = LanguageMode.ECMASCRIPT5; |
| 658 |
parse("var a = {'foo': 'bar',};"); |
| 659 |
} |
| 660 |
|
| 661 |
public void testTrailingCommaWarning7() { |
| 662 |
parseError("var a = {,};", BAD_PROPERTY_MESSAGE); |
| 663 |
} |
| 664 |
|
| 665 |
public void testSuspiciousBlockCommentWarning1() { |
| 666 |
parse("/* @type {number} */ var x = 3;", SUSPICIOUS_COMMENT_WARNING); |
| 667 |
} |
| 668 |
|
| 669 |
public void testSuspiciousBlockCommentWarning2() { |
| 670 |
parse("/* \n * @type {number} */ var x = 3;", SUSPICIOUS_COMMENT_WARNING); |
| 671 |
} |
| 672 |
|
| 673 |
public void testCatchClauseForbidden() { |
| 674 |
parseError("try { } catch (e if true) {}", |
| 675 |
"Catch clauses are not supported"); |
| 676 |
} |
| 677 |
|
| 678 |
public void testConstForbidden() { |
| 679 |
parseError("const x = 3;", "Unsupported syntax: CONST"); |
| 680 |
} |
| 681 |
|
| 682 |
public void testDestructuringAssignForbidden() { |
| 683 |
parseError("var [x, y] = foo();", "destructuring assignment forbidden"); |
| 684 |
} |
| 685 |
|
| 686 |
public void testDestructuringAssignForbidden2() { |
| 687 |
parseError("var {x, y} = foo();", "missing : after property id"); |
| 688 |
} |
| 689 |
|
| 690 |
public void testDestructuringAssignForbidden3() { |
| 691 |
parseError("var {x: x, y: y} = foo();", |
| 692 |
"destructuring assignment forbidden"); |
| 693 |
} |
| 694 |
|
| 695 |
public void testDestructuringAssignForbidden4() { |
| 696 |
parseError("[x, y] = foo();", |
| 697 |
"destructuring assignment forbidden", |
| 698 |
"invalid assignment target"); |
| 699 |
} |
| 700 |
|
| 701 |
public void testLetForbidden() { |
| 702 |
parseError("function f() { let (x = 3) { alert(x); }; }", |
| 703 |
"missing ; before statement", "syntax error"); |
| 704 |
} |
| 705 |
|
| 706 |
public void testYieldForbidden() { |
| 707 |
parseError("function f() { yield 3; }", "missing ; before statement"); |
| 708 |
} |
| 709 |
|
| 710 |
public void testBracelessFunctionForbidden() { |
| 711 |
parseError("var sq = function(x) x * x;", |
| 712 |
"missing { before function body"); |
| 713 |
} |
| 714 |
|
| 715 |
public void testGeneratorsForbidden() { |
| 716 |
parseError("var i = (x for (x in obj));", |
| 717 |
"Unsupported syntax: GENEXPR"); |
| 718 |
} |
| 719 |
|
| 720 |
public void testGettersForbidden1() { |
| 721 |
parseError("var x = {get foo() { return 3; }};", |
| 722 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 723 |
} |
| 724 |
|
| 725 |
public void testGettersForbidden2() { |
| 726 |
parseError("var x = {get foo bar() { return 3; }};", |
| 727 |
"invalid property id"); |
| 728 |
} |
| 729 |
|
| 730 |
public void testGettersForbidden3() { |
| 731 |
parseError("var x = {a getter:function b() { return 3; }};", |
| 732 |
"missing : after property id", "syntax error"); |
| 733 |
} |
| 734 |
|
| 735 |
public void testGettersForbidden4() { |
| 736 |
parseError("var x = {\"a\" getter:function b() { return 3; }};", |
| 737 |
"missing : after property id", "syntax error"); |
| 738 |
} |
| 739 |
|
| 740 |
public void testGettersForbidden5() { |
| 741 |
parseError("var x = {a: 2, get foo() { return 3; }};", |
| 742 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 743 |
} |
| 744 |
|
| 745 |
public void testSettersForbidden() { |
| 746 |
parseError("var x = {set foo() { return 3; }};", |
| 747 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 748 |
} |
| 749 |
|
| 750 |
public void testSettersForbidden2() { |
| 751 |
parseError("var x = {a setter:function b() { return 3; }};", |
| 752 |
"missing : after property id", "syntax error"); |
| 753 |
} |
| 754 |
|
| 755 |
public void testFileOverviewJSDoc1() { |
| 756 |
Node n = parse("/** @fileoverview Hi mom! */ function Foo() {}"); |
| 757 |
assertEquals(Token.FUNCTION, n.getFirstChild().getType()); |
| 758 |
assertTrue(n.getJSDocInfo() != null); |
| 759 |
assertNull(n.getFirstChild().getJSDocInfo()); |
| 760 |
assertEquals("Hi mom!", |
| 761 |
n.getJSDocInfo().getFileOverview()); |
| 762 |
} |
| 763 |
|
| 764 |
public void testFileOverviewJSDocDoesNotHoseParsing() { |
| 765 |
assertEquals( |
| 766 |
Token.FUNCTION, |
| 767 |
parse("/** @fileoverview Hi mom! \n */ function Foo() {}") |
| 768 |
.getFirstChild().getType()); |
| 769 |
assertEquals( |
| 770 |
Token.FUNCTION, |
| 771 |
parse("/** @fileoverview Hi mom! \n * * * */ function Foo() {}") |
| 772 |
.getFirstChild().getType()); |
| 773 |
assertEquals( |
| 774 |
Token.FUNCTION, |
| 775 |
parse("/** @fileoverview \n * x */ function Foo() {}") |
| 776 |
.getFirstChild().getType()); |
| 777 |
assertEquals( |
| 778 |
Token.FUNCTION, |
| 779 |
parse("/** @fileoverview \n * x \n */ function Foo() {}") |
| 780 |
.getFirstChild().getType()); |
| 781 |
} |
| 782 |
|
| 783 |
public void testFileOverviewJSDoc2() { |
| 784 |
Node n = parse("/** @fileoverview Hi mom! */ " + |
| 785 |
"/** @constructor */ function Foo() {}"); |
| 786 |
assertTrue(n.getJSDocInfo() != null); |
| 787 |
assertEquals("Hi mom!", n.getJSDocInfo().getFileOverview()); |
| 788 |
assertTrue(n.getFirstChild().getJSDocInfo() != null); |
| 789 |
assertFalse(n.getFirstChild().getJSDocInfo().hasFileOverview()); |
| 790 |
assertTrue(n.getFirstChild().getJSDocInfo().isConstructor()); |
| 791 |
} |
| 792 |
|
| 793 |
public void testObjectLiteralDoc1() { |
| 794 |
Node n = parse("var x = {/** @type {number} */ 1: 2};"); |
| 795 |
|
| 796 |
Node objectLit = n.getFirstChild().getFirstChild().getFirstChild(); |
| 797 |
assertEquals(Token.OBJECTLIT, objectLit.getType()); |
| 798 |
|
| 799 |
Node number = objectLit.getFirstChild(); |
| 800 |
assertEquals(Token.STRING_KEY, number.getType()); |
| 801 |
assertNotNull(number.getJSDocInfo()); |
| 802 |
} |
| 803 |
|
| 804 |
public void testDuplicatedParam() { |
| 805 |
parse("function foo(x, x) {}", "Duplicate parameter name \"x\"."); |
| 806 |
} |
| 807 |
|
| 808 |
public void testGetter() { |
| 809 |
mode = LanguageMode.ECMASCRIPT3; |
| 810 |
parseError("var x = {get 1(){}};", |
| 811 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 812 |
parseError("var x = {get 'a'(){}};", |
| 813 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 814 |
parseError("var x = {get a(){}};", |
| 815 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 816 |
mode = LanguageMode.ECMASCRIPT5; |
| 817 |
parse("var x = {get 1(){}};"); |
| 818 |
parse("var x = {get 'a'(){}};"); |
| 819 |
parse("var x = {get a(){}};"); |
| 820 |
parseError("var x = {get a(b){}};", "getters may not have parameters"); |
| 821 |
} |
| 822 |
|
| 823 |
public void testSetter() { |
| 824 |
mode = LanguageMode.ECMASCRIPT3; |
| 825 |
parseError("var x = {set 1(x){}};", |
| 826 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 827 |
parseError("var x = {set 'a'(x){}};", |
| 828 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 829 |
parseError("var x = {set a(x){}};", |
| 830 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 831 |
mode = LanguageMode.ECMASCRIPT5; |
| 832 |
parse("var x = {set 1(x){}};"); |
| 833 |
parse("var x = {set 'a'(x){}};"); |
| 834 |
parse("var x = {set a(x){}};"); |
| 835 |
parseError("var x = {set a(){}};", |
| 836 |
"setters must have exactly one parameter"); |
| 837 |
} |
| 838 |
|
| 839 |
public void testLamestWarningEver() { |
| 840 |
|
| 841 |
parse("var x = /** @type {undefined} */ (y);"); |
| 842 |
parse("var x = /** @type {void} */ (y);"); |
| 843 |
} |
| 844 |
|
| 845 |
public void testUnfinishedComment() { |
| 846 |
parseError("/** this is a comment ", "unterminated comment"); |
| 847 |
} |
| 848 |
|
| 849 |
public void testParseBlockDescription() { |
| 850 |
Node n = parse("/** This is a variable. */ var x;"); |
| 851 |
Node var = n.getFirstChild(); |
| 852 |
assertNotNull(var.getJSDocInfo()); |
| 853 |
assertEquals("This is a variable.", |
| 854 |
var.getJSDocInfo().getBlockDescription()); |
| 855 |
} |
| 856 |
|
| 857 |
public void testUnnamedFunctionStatement() { |
| 858 |
|
| 859 |
parseError("function() {};", "unnamed function statement"); |
| 860 |
parseError("if (true) { function() {}; }", "unnamed function statement"); |
| 861 |
parse("function f() {};"); |
| 862 |
|
| 863 |
parse("(function f() {});"); |
| 864 |
parse("(function () {});"); |
| 865 |
} |
| 866 |
|
| 867 |
public void testReservedKeywords() { |
| 868 |
mode = LanguageMode.ECMASCRIPT3; |
| 869 |
|
| 870 |
parseError("var boolean;", "missing variable name"); |
| 871 |
parseError("function boolean() {};", |
| 872 |
"missing ( before function parameters."); |
| 873 |
parseError("boolean = 1;", "identifier is a reserved word"); |
| 874 |
parseError("class = 1;", "identifier is a reserved word"); |
| 875 |
parseError("public = 2;", "identifier is a reserved word"); |
| 876 |
|
| 877 |
mode = LanguageMode.ECMASCRIPT5; |
| 878 |
|
| 879 |
parse("var boolean;"); |
| 880 |
parse("function boolean() {};"); |
| 881 |
parse("boolean = 1;"); |
| 882 |
parseError("class = 1;", "identifier is a reserved word"); |
| 883 |
parse("public = 2;"); |
| 884 |
|
| 885 |
mode = LanguageMode.ECMASCRIPT5_STRICT; |
| 886 |
|
| 887 |
parse("var boolean;"); |
| 888 |
parse("function boolean() {};"); |
| 889 |
parse("boolean = 1;"); |
| 890 |
parseError("class = 1;", "identifier is a reserved word"); |
| 891 |
parseError("public = 2;", "identifier is a reserved word"); |
| 892 |
} |
| 893 |
|
| 894 |
public void testKeywordsAsProperties() { |
| 895 |
mode = LanguageMode.ECMASCRIPT3; |
| 896 |
|
| 897 |
parseError("var x = {function: 1};", "invalid property id"); |
| 898 |
parseError("x.function;", "missing name after . operator"); |
| 899 |
parseError("var x = {get x(){} };", |
| 900 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 901 |
parseError("var x = {get function(){} };", "invalid property id"); |
| 902 |
parseError("var x = {get 'function'(){} };", |
| 903 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 904 |
parseError("var x = {get 1(){} };", |
| 905 |
IRFactory.GETTER_ERROR_MESSAGE); |
| 906 |
parseError("var x = {set function(a){} };", "invalid property id"); |
| 907 |
parseError("var x = {set 'function'(a){} };", |
| 908 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 909 |
parseError("var x = {set 1(a){} };", |
| 910 |
IRFactory.SETTER_ERROR_MESSAGE); |
| 911 |
parseError("var x = {class: 1};", "invalid property id"); |
| 912 |
parseError("x.class;", "missing name after . operator"); |
| 913 |
parse("var x = {let: 1};"); |
| 914 |
parse("x.let;"); |
| 915 |
parse("var x = {yield: 1};"); |
| 916 |
parse("x.yield;"); |
| 917 |
|
| 918 |
mode = LanguageMode.ECMASCRIPT5; |
| 919 |
|
| 920 |
parse("var x = {function: 1};"); |
| 921 |
parse("x.function;"); |
| 922 |
parse("var x = {get function(){} };"); |
| 923 |
parse("var x = {get 'function'(){} };"); |
| 924 |
parse("var x = {get 1(){} };"); |
| 925 |
parse("var x = {set function(a){} };"); |
| 926 |
parse("var x = {set 'function'(a){} };"); |
| 927 |
parse("var x = {set 1(a){} };"); |
| 928 |
parse("var x = {class: 1};"); |
| 929 |
parse("x.class;"); |
| 930 |
parse("var x = {let: 1};"); |
| 931 |
parse("x.let;"); |
| 932 |
parse("var x = {yield: 1};"); |
| 933 |
parse("x.yield;"); |
| 934 |
|
| 935 |
mode = LanguageMode.ECMASCRIPT5_STRICT; |
| 936 |
|
| 937 |
parse("var x = {function: 1};"); |
| 938 |
parse("x.function;"); |
| 939 |
parse("var x = {get function(){} };"); |
| 940 |
parse("var x = {get 'function'(){} };"); |
| 941 |
parse("var x = {get 1(){} };"); |
| 942 |
parse("var x = {set function(a){} };"); |
| 943 |
parse("var x = {set 'function'(a){} };"); |
| 944 |
parse("var x = {set 1(a){} };"); |
| 945 |
parse("var x = {class: 1};"); |
| 946 |
parse("x.class;"); |
| 947 |
parse("var x = {let: 1};"); |
| 948 |
parse("x.let;"); |
| 949 |
parse("var x = {yield: 1};"); |
| 950 |
parse("x.yield;"); |
| 951 |
} |
| 952 |
|
| 953 |
public void testGetPropFunctionName() { |
| 954 |
parseError("function a.b() {}", |
| 955 |
"missing ( before function parameters."); |
| 956 |
parseError("var x = function a.b() {}", |
| 957 |
"missing ( before function parameters."); |
| 958 |
} |
| 959 |
|
| 960 |
public void testGetPropFunctionNameIdeMode() { |
| 961 |
|
| 962 |
|
| 963 |
isIdeMode = true; |
| 964 |
parseError("function a.b() {}", |
| 965 |
"missing ( before function parameters.", |
| 966 |
"missing formal parameter", |
| 967 |
"missing ) after formal parameters", |
| 968 |
"missing { before function body", |
| 969 |
"syntax error", |
| 970 |
"missing ; before statement", |
| 971 |
"missing ; before statement", |
| 972 |
"missing } after function body", |
| 973 |
"Unsupported syntax: ERROR", |
| 974 |
"Unsupported syntax: ERROR"); |
| 975 |
parseError("var x = function a.b() {}", |
| 976 |
"missing ( before function parameters.", |
| 977 |
"missing formal parameter", |
| 978 |
"missing ) after formal parameters", |
| 979 |
"missing { before function body", |
| 980 |
"syntax error", |
| 981 |
"missing ; before statement", |
| 982 |
"missing ; before statement", |
| 983 |
"missing } after function body", |
| 984 |
"Unsupported syntax: ERROR", |
| 985 |
"Unsupported syntax: ERROR"); |
| 986 |
} |
| 987 |
|
| 988 |
public void testIdeModePartialTree() { |
| 989 |
Node partialTree = parseError("function Foo() {} f.", |
| 990 |
"missing name after . operator"); |
| 991 |
assertNull(partialTree); |
| 992 |
|
| 993 |
isIdeMode = true; |
| 994 |
partialTree = parseError("function Foo() {} f.", |
| 995 |
"missing name after . operator"); |
| 996 |
assertNotNull(partialTree); |
| 997 |
} |
| 998 |
|
| 999 |
public void testForEach() { |
| 1000 |
parseError( |
| 1001 |
"function f(stamp, status) {\n" + |
| 1002 |
" for each ( var curTiming in this.timeLog.timings ) {\n" + |
| 1003 |
" if ( curTiming.callId == stamp ) {\n" + |
| 1004 |
" curTiming.flag = status;\n" + |
| 1005 |
" break;\n" + |
| 1006 |
" }\n" + |
| 1007 |
" }\n" + |
| 1008 |
"};", |
| 1009 |
"unsupported language extension: for each"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
public void testMisplacedTypeAnnotation1() { |
| 1013 |
|
| 1014 |
parse( |
| 1015 |
"var o = {};" + |
| 1016 |
"/** @type {string} */ o.prop1 = 1, o.prop2 = 2;", |
| 1017 |
MISPLACED_TYPE_ANNOTATION); |
| 1018 |
} |
| 1019 |
|
| 1020 |
public void testMisplacedTypeAnnotation2() { |
| 1021 |
|
| 1022 |
parse( |
| 1023 |
"var o = /** @type {string} */ getValue();", |
| 1024 |
MISPLACED_TYPE_ANNOTATION); |
| 1025 |
} |
| 1026 |
|
| 1027 |
public void testMisplacedTypeAnnotation3() { |
| 1028 |
|
| 1029 |
parse( |
| 1030 |
"var o = 1 + /** @type {string} */ value;", |
| 1031 |
MISPLACED_TYPE_ANNOTATION); |
| 1032 |
} |
| 1033 |
|
| 1034 |
public void testMisplacedTypeAnnotation4() { |
| 1035 |
|
| 1036 |
parse( |
| 1037 |
"var o = /** @type {!Array.<string>} */ ['hello', 'you'];", |
| 1038 |
MISPLACED_TYPE_ANNOTATION); |
| 1039 |
} |
| 1040 |
|
| 1041 |
public void testMisplacedTypeAnnotation5() { |
| 1042 |
|
| 1043 |
parse( |
| 1044 |
"var o = (/** @type {!Foo} */ {});", |
| 1045 |
MISPLACED_TYPE_ANNOTATION); |
| 1046 |
} |
| 1047 |
|
| 1048 |
public void testMisplacedTypeAnnotation6() { |
| 1049 |
parse("var o = /** @type {function():string} */ function() {return 'str';}", |
| 1050 |
MISPLACED_TYPE_ANNOTATION); |
| 1051 |
} |
| 1052 |
|
| 1053 |
public void testValidTypeAnnotation1() { |
| 1054 |
parse("/** @type {string} */ var o = 'str';"); |
| 1055 |
parse("var /** @type {string} */ o = 'str', /** @type {number} */ p = 0;"); |
| 1056 |
parse("/** @type {function():string} */ function o() { return 'str'; }"); |
| 1057 |
parse("var o = {}; /** @type {string} */ o.prop = 'str';"); |
| 1058 |
parse("var o = {}; /** @type {string} */ o['prop'] = 'str';"); |
| 1059 |
parse("var o = { /** @type {string} */ prop : 'str' };"); |
| 1060 |
parse("var o = { /** @type {string} */ 'prop' : 'str' };"); |
| 1061 |
parse("var o = { /** @type {string} */ 1 : 'str' };"); |
| 1062 |
} |
| 1063 |
|
| 1064 |
public void testValidTypeAnnotation2() { |
| 1065 |
mode = LanguageMode.ECMASCRIPT5; |
| 1066 |
parse("var o = { /** @type {string} */ get prop() { return 'str' }};"); |
| 1067 |
parse("var o = { /** @type {string} */ set prop(s) {}};"); |
| 1068 |
} |
| 1069 |
|
| 1070 |
public void testValidTypeAnnotation3() { |
| 1071 |
|
| 1072 |
|
| 1073 |
parse("try {} catch (/** @type {Error} */ e) {}"); |
| 1074 |
} |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
private Node parseError(String string, String... errors) { |
| 1081 |
TestErrorReporter testErrorReporter = new TestErrorReporter(errors, null); |
| 1082 |
Node script = null; |
| 1083 |
try { |
| 1084 |
StaticSourceFile file = new SimpleSourceFile("input", false); |
| 1085 |
script = ParserRunner.parse( |
| 1086 |
file, string, ParserRunner.createConfig(isIdeMode, mode, false), |
| 1087 |
testErrorReporter, Logger.getAnonymousLogger()).ast; |
| 1088 |
} catch (IOException e) { |
| 1089 |
throw new RuntimeException(e); |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
assertTrue(testErrorReporter.hasEncounteredAllErrors()); |
| 1094 |
assertTrue(testErrorReporter.hasEncounteredAllWarnings()); |
| 1095 |
|
| 1096 |
return script; |
| 1097 |
} |
| 1098 |
|
| 1099 |
private Node parse(String string, String... warnings) { |
| 1100 |
TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings); |
| 1101 |
Node script = null; |
| 1102 |
try { |
| 1103 |
StaticSourceFile file = new SimpleSourceFile("input", false); |
| 1104 |
script = ParserRunner.parse( |
| 1105 |
file, string, ParserRunner.createConfig(true, mode, false), |
| 1106 |
testErrorReporter, Logger.getAnonymousLogger()).ast; |
| 1107 |
} catch (IOException e) { |
| 1108 |
throw new RuntimeException(e); |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
assertTrue(testErrorReporter.hasEncounteredAllErrors()); |
| 1113 |
assertTrue(testErrorReporter.hasEncounteredAllWarnings()); |
| 1114 |
|
| 1115 |
return script; |
| 1116 |
} |
| 1117 |
|
| 1118 |
private static class ParserResult { |
| 1119 |
private final String code; |
| 1120 |
private final Node node; |
| 1121 |
|
| 1122 |
private ParserResult(String code, Node node) { |
| 1123 |
this.code = code; |
| 1124 |
this.node = node; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |