Debian Codegraph @

test/com/google/javascript/jscomp/parsing/IRFactoryTest.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.parsing;
18
19 import com.google.common.collect.Sets;
20 import com.google.javascript.jscomp.SourceFile;
21 import com.google.javascript.jscomp.parsing.Config.LanguageMode;
22 import com.google.javascript.jscomp.testing.TestErrorReporter;
23 import com.google.javascript.rhino.JSDocInfo;
24 import com.google.javascript.rhino.Node;
25 import com.google.javascript.rhino.head.CompilerEnvirons;
26 import com.google.javascript.rhino.head.Parser;
27 import com.google.javascript.rhino.head.ast.AstRoot;
28 import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
29
30 /**
31 * Tests {@link IRFactory}.
32 */
33 public class IRFactoryTest extends BaseJSTypeTestCase {
34
35 private LanguageMode mode = LanguageMode.ECMASCRIPT3;
36
37 @Override
38 protected void setUp() throws Exception {
39 super.setUp();
40 mode = LanguageMode.ECMASCRIPT3;
41 }
42
43 public void testStrictScript() throws Exception {
44 assertNull(newParse("").getDirectives());
45 assertEquals(
46 Sets.newHashSet("use strict"),
47 newParse("'use strict'").getDirectives());
48 }
49
50 public void testArrayLiteral2() throws Exception {
51 testNewParser("[a, , b]",
52 "SCRIPT 1 [source_file: FileName.js] [length: 8]\n" +
53 " EXPR_RESULT 1 [source_file: FileName.js] [length: 8]\n" +
54 " ARRAYLIT 1 [source_file: FileName.js] [length: 8]\n" +
55 " NAME a 1 [source_file: FileName.js] [length: 1]\n" +
56 " EMPTY 1 [source_file: FileName.js] [length: 1]\n" +
57 " NAME b 1 [source_file: FileName.js] [length: 1]\n");
58 }
59
60 public void testArrayLiteral4() throws Exception {
61 testNewParser("[,,,a,,b]",
62 "SCRIPT 1 [source_file: FileName.js] [length: 9]\n" +
63 " EXPR_RESULT 1 [source_file: FileName.js] [length: 9]\n" +
64 " ARRAYLIT 1 [source_file: FileName.js] [length: 9]\n" +
65 " EMPTY 1 [source_file: FileName.js] [length: 1]\n" +
66 " EMPTY 1 [source_file: FileName.js] [length: 1]\n" +
67 " EMPTY 1 [source_file: FileName.js] [length: 1]\n" +
68 " NAME a 1 [source_file: FileName.js] [length: 1]\n" +
69 " EMPTY 1 [source_file: FileName.js] [length: 1]\n" +
70 " NAME b 1 [source_file: FileName.js] [length: 1]\n");
71 }
72
73 public void testObjectLiteral() {
74 newParse("var o = {}");
75 }
76
77 public void testObjectLiteral2() {
78 newParse("var o = {a: 1}");
79 }
80
81 public void testObjectLiteral3() {
82 newParse("var o = {a: 1, b: 2}");
83 }
84
85 public void testObjectLiteral4() {
86 newParse("var o = {1: 'a'}");
87 }
88
89 public void testObjectLiteral5() {
90 newParse("var o = {'a': 'a'}");
91 }
92
93 public void testObjectLiteral6() {
94 testNewParser("({1: true})",
95 "SCRIPT 1 [source_file: FileName.js] [length: 11]\n" +
96 " EXPR_RESULT 1 [source_file: FileName.js] [length: 10]\n" +
97 " OBJECTLIT 1 [source_file: FileName.js] [length: 9]\n" +
98 " STRING_KEY 1 1 [quoted: 1] [source_file: FileName.js] [length: 1]\n" +
99 " TRUE 1 [source_file: FileName.js] [length: 4]\n");
100 }
101
102 public void testObjectLiteral7() {
103 mode = LanguageMode.ECMASCRIPT5;
104
105 testNewParser("({get 1() {}})",
106 "SCRIPT 1 [source_file: FileName.js] [length: 14]\n" +
107 " EXPR_RESULT 1 [source_file: FileName.js] [length: 13]\n" +
108 " OBJECTLIT 1 [source_file: FileName.js] [length: 12]\n" +
109 " GETTER_DEF 1 1 [quoted: 1] [source_file: FileName.js] [length: 1]\n" +
110 " FUNCTION 1 [source_file: FileName.js] [length: 6]\n" +
111 " NAME 1 [source_file: FileName.js]\n" +
112 " PARAM_LIST 1 [source_file: FileName.js]\n" +
113 " BLOCK 1 [source_file: FileName.js] [length: 2]\n");
114 }
115
116 public void testObjectLiteral8() {
117 mode = LanguageMode.ECMASCRIPT5;
118
119 testNewParser("({set 1(a) {}})",
120 "SCRIPT 1 [source_file: FileName.js] [length: 15]\n" +
121 " EXPR_RESULT 1 [source_file: FileName.js] [length: 14]\n" +
122 " OBJECTLIT 1 [source_file: FileName.js] [length: 13]\n" +
123 " SETTER_DEF 1 1 [quoted: 1] [source_file: FileName.js] [length: 1]\n" +
124 " FUNCTION 1 [source_file: FileName.js] [length: 7]\n" +
125 " NAME 1 [source_file: FileName.js]\n" +
126 " PARAM_LIST 1 [source_file: FileName.js]\n" +
127 " NAME a 1 [source_file: FileName.js] [length: 1]\n" +
128 " BLOCK 1 [source_file: FileName.js] [length: 2]\n");
129 }
130
131 // The old and new parser produce different results now with labels, and
132 // named breaks and continues, so disable these tests.
133 public void testLabel() {
134 testNewParser("foo: bar",
135 "SCRIPT 1 [source_file: FileName.js] [length: 8]\n" +
136 " LABEL 1 [source_file: FileName.js] [length: 4]\n" +
137 " LABEL_NAME foo 1 [source_file: FileName.js] [length: 4]\n" +
138 " EXPR_RESULT 1 [source_file: FileName.js] [length: 3]\n" +
139 " NAME bar 1 [source_file: FileName.js] [length: 3]\n");
140 }
141
142 public void testLabel2() {
143 testNewParser("l: while (f()) { if (g()) { continue l; } }",
144 "SCRIPT 1 [source_file: FileName.js] [length: 43]\n" +
145 " LABEL 1 [source_file: FileName.js] [length: 2]\n" +
146 " LABEL_NAME l 1 [source_file: FileName.js] [length: 2]\n" +
147 " WHILE 1 [source_file: FileName.js] [length: 40]\n" +
148 " CALL 1 [source_file: FileName.js] [length: 3]\n" +
149 " NAME f 1 [source_file: FileName.js] [length: 1]\n" +
150 " BLOCK 1 [source_file: FileName.js] [length: 28]\n" +
151 " IF 1 [source_file: FileName.js] [length: 24]\n" +
152 " CALL 1 [source_file: FileName.js] [length: 3]\n" +
153 " NAME g 1 [source_file: FileName.js] [length: 1]\n" +
154 " BLOCK 1 [source_file: FileName.js] [length: 15]\n" +
155 " CONTINUE 1 [source_file: FileName.js] [length: 11]\n" +
156 " LABEL_NAME l 1 [source_file: FileName.js] [length: 1]\n");
157 }
158
159 public void testLabel3() {
160 testNewParser("Foo:Bar:X:{ break Bar; }",
161 "SCRIPT 1 [source_file: FileName.js] [length: 24]\n" +
162 " LABEL 1 [source_file: FileName.js] [length: 4]\n" +
163 " LABEL_NAME Foo 1 [source_file: FileName.js] [length: 4]\n" +
164 " LABEL 1 [source_file: FileName.js] [length: 4]\n" +
165 " LABEL_NAME Bar 1 [source_file: FileName.js] [length: 4]\n" +
166 " LABEL 1 [source_file: FileName.js] [length: 2]\n" +
167 " LABEL_NAME X 1 [source_file: FileName.js] [length: 2]\n" +
168 " BLOCK 1 [source_file: FileName.js] [length: 14]\n" +
169 " BREAK 1 [source_file: FileName.js] [length: 10]\n" +
170 " LABEL_NAME Bar 1 [source_file: FileName.js] [length: 3]\n");
171 }
172
173 public void testNegation1() {
174 testNewParser("-a",
175 "SCRIPT 1 [source_file: FileName.js] [length: 2]\n" +
176 " EXPR_RESULT 1 [source_file: FileName.js] [length: 2]\n" +
177 " NEG 1 [source_file: FileName.js] [length: 2]\n" +
178 " NAME a 1 [source_file: FileName.js] [length: 1]\n");
179 }
180
181 public void testNegation2() {
182 testNewParser("-2",
183 "SCRIPT 1 [source_file: FileName.js] [length: 2]\n" +
184 " EXPR_RESULT 1 [source_file: FileName.js] [length: 2]\n" +
185 " NUMBER -2.0 1 [source_file: FileName.js] [length: 1]\n");
186 }
187
188 public void testNegation3() {
189 testNewParser("1 - -2",
190 "SCRIPT 1 [source_file: FileName.js] [length: 6]\n" +
191 " EXPR_RESULT 1 [source_file: FileName.js] [length: 6]\n" +
192 " SUB 1 [source_file: FileName.js] [length: 6]\n" +
193 " NUMBER 1.0 1 [source_file: FileName.js] [length: 1]\n" +
194 " NUMBER -2.0 1 [source_file: FileName.js] [length: 1]\n");
195 }
196
197 public void testGetter() {
198 mode = LanguageMode.ECMASCRIPT5;
199 testNewParser("({get a() {}})",
200 "SCRIPT 1 [source_file: FileName.js] [length: 14]\n" +
201 " EXPR_RESULT 1 [source_file: FileName.js] [length: 13]\n" +
202 " OBJECTLIT 1 [source_file: FileName.js] [length: 12]\n" +
203 " GETTER_DEF a 1 [source_file: FileName.js] [length: 1]\n" +
204 " FUNCTION 1 [source_file: FileName.js] [length: 6]\n" +
205 " NAME 1 [source_file: FileName.js]\n" +
206 " PARAM_LIST 1 [source_file: FileName.js]\n" +
207 " BLOCK 1 [source_file: FileName.js] [length: 2]\n");
208 }
209
210 public void testSetter() {
211 mode = LanguageMode.ECMASCRIPT5;
212 testNewParser("({set a(x) {}})",
213 "SCRIPT 1 [source_file: FileName.js] [length: 15]\n" +
214 " EXPR_RESULT 1 [source_file: FileName.js] [length: 14]\n" +
215 " OBJECTLIT 1 [source_file: FileName.js] [length: 13]\n" +
216 " SETTER_DEF a 1 [source_file: FileName.js] [length: 1]\n" +
217 " FUNCTION 1 [source_file: FileName.js] [length: 7]\n" +
218 " NAME 1 [source_file: FileName.js]\n" +
219 " PARAM_LIST 1 [source_file: FileName.js]\n" +
220 " NAME x 1 [source_file: FileName.js] [length: 1]\n" +
221 " BLOCK 1 [source_file: FileName.js] [length: 2]\n");
222 }
223
224 public void testDelete1() {
225 testNoParseError("delete a.b;");
226 }
227
228 public void testDelete2() {
229 testNoParseError("delete a['b'];");
230 }
231
232 public void testDelete3() {
233 // This is allowed in ES3 and ES5, but not in ES5/strict. There
234 // is a strict mode check for this.
235 testNoParseError("delete a;");
236 }
237
238 public void testDelete4() {
239 testParseError("delete 'x';",
240 "Invalid delete operand. Only properties can be deleted.");
241 }
242
243 public void testCommentPositions1() {
244 Node root = newParse("/** @param {string} x */function a(x) {};" +
245 "/** @param {string} x */function b(x) {}");
246 Node a = root.getFirstChild();
247 Node b = root.getLastChild();
248 assertMarkerPosition(a, 1, 4);
249 assertMarkerPosition(b, 1, 45);
250 }
251
252 public void testCommentPositions2() {
253 Node root = newParse(
254 "/* foo \n" +
255 " bar \n" +
256 "*/\n" +
257 "/** @param {string} x */\n" +
258 "function a(x) {};\n" +
259 "\n" +
260 "/* bar \n" +
261 " foo \n" +
262 " foo */\n" +
263 "\n" +
264 "/** @param {string} x */\n" +
265 "function b(x) {};");
266 assertMarkerPosition(root.getFirstChild(), 4, 4);
267 assertMarkerPosition(root.getFirstChild().getNext().getNext(), 11, 6);
268 }
269
270 public void testLiteralLocation() {
271 Node root = newParse(
272 "var d =\n" +
273 " \"foo\";\n" +
274 "var e =\n" +
275 " 1;\n" +
276 "var f = \n" +
277 " 1.2;\n" +
278 "var g = \n" +
279 " 2e5;\n" +
280 "var h = \n" +
281 " 'bar';\n");
282
283 Node firstStmt = root.getFirstChild();
284 Node firstLiteral = firstStmt.getFirstChild().getFirstChild();
285 Node secondStmt = firstStmt.getNext();
286 Node secondLiteral = secondStmt.getFirstChild().getFirstChild();
287 Node thirdStmt = secondStmt.getNext();
288 Node thirdLiteral = thirdStmt.getFirstChild().getFirstChild();
289 Node fourthStmt = thirdStmt.getNext();
290 Node fourthLiteral = fourthStmt.getFirstChild().getFirstChild();
291 Node fifthStmt = fourthStmt.getNext();
292 Node fifthLiteral = fifthStmt.getFirstChild().getFirstChild();
293
294 assertNodePosition(2, 4, firstLiteral);
295 assertNodePosition(4, 4, secondLiteral);
296 assertNodePosition(6, 4, thirdLiteral);
297 assertNodePosition(8, 4, fourthLiteral);
298 assertNodePosition(10, 4, fifthLiteral);
299 }
300
301 public void testSwitchLocation() {
302 Node root = newParse(
303 "switch (a) {\n" +
304 " //{\n" +
305 " case 1:\n" +
306 " b++;\n" +
307 " case 2:\n" +
308 " default:\n" +
309 " b--;\n" +
310 " }\n");
311
312 Node switchStmt = root.getFirstChild();
313 Node switchVar = switchStmt.getFirstChild();
314 Node firstCase = switchVar.getNext();
315 Node caseArg = firstCase.getFirstChild();
316 Node caseBody = caseArg.getNext();
317 Node caseExprStmt = caseBody.getFirstChild();
318 Node incrExpr = caseExprStmt.getFirstChild();
319 Node incrVar = incrExpr.getFirstChild();
320 Node secondCase = firstCase.getNext();
321 Node defaultCase = secondCase.getNext();
322
323 assertNodePosition(1, 0, switchStmt);
324 assertNodePosition(1, 8, switchVar);
325 assertNodePosition(3, 3, firstCase);
326 assertNodePosition(3, 8, caseArg);
327 assertNodePosition(3, 3, caseBody);
328 assertNodePosition(4, 5, caseExprStmt);
329 assertNodePosition(4, 5, incrExpr);
330 assertNodePosition(4, 5, incrVar);
331 assertNodePosition(5, 3, secondCase);
332 assertNodePosition(6, 3, defaultCase);
333 }
334
335 public void testFunctionParamLocation() {
336 Node root = newParse(
337 "function\n" +
338 " foo(a,\n" +
339 " b,\n" +
340 " c)\n" +
341 "{}\n");
342
343 Node function = root.getFirstChild();
344 Node functionName = function.getFirstChild();
345 Node params = functionName.getNext();
346 Node param1 = params.getFirstChild();
347 Node param2 = param1.getNext();
348 Node param3 = param2.getNext();
349 Node body = params.getNext();
350
351 assertNodePosition(1, 0, function);
352 assertNodePosition(2, 5, functionName);
353 // params corresponds to the LP token.
354 // Can't be on a separate line because of inferred
355 // semicolons.
356 assertNodePosition(2, 8, params);
357 assertNodePosition(2, 9, param1);
358 assertNodePosition(3, 5, param2);
359 assertNodePosition(4, 5, param3);
360 assertNodePosition(5, 0, body);
361 }
362
363 public void testVarDeclLocation() {
364 Node root = newParse(
365 "var\n" +
366 " a =\n" +
367 " 3\n");
368 Node varDecl = root.getFirstChild();
369 Node varName = varDecl.getFirstChild();
370 Node varExpr = varName.getFirstChild();
371
372 assertNodePosition(1, 0, varDecl);
373 assertNodePosition(2, 4, 1, varName);
374 assertNodePosition(3, 4, 1, varExpr);
375 }
376
377 public void testReturnLocation() {
378 Node root = newParse(
379 "function\n" +
380 " foo(\n" +
381 " a,\n" +
382 " b,\n" +
383 " c) {\n" +
384 " return\n" +
385 " 4;\n" +
386 "}\n");
387
388 Node function = root.getFirstChild();
389 Node functionName = function.getFirstChild();
390 Node params = functionName.getNext();
391 Node body = params.getNext();
392 Node returnStmt = body.getFirstChild();
393 Node exprStmt = returnStmt.getNext();
394 Node returnVal = exprStmt.getFirstChild();
395
396 assertNodePosition(6, 4, returnStmt);
397 assertNodePosition(7, 4, exprStmt);
398 assertNodePosition(7, 4, returnVal);
399 }
400
401 public void testLinenoFor() {
402 Node root = newParse(
403 "for(\n" +
404 ";\n" +
405 ";\n" +
406 ") {\n" +
407 "}\n");
408
409 Node forNode = root.getFirstChild();
410 Node initClause= forNode.getFirstChild();
411 Node condClause = initClause.getNext();
412 Node incrClause = condClause.getNext();
413
414 assertNodePosition(1, 0, forNode);
415 assertNodePosition(2, 0, initClause);
416 assertNodePosition(3, 0, condClause);
417 // TODO(bowdidge) Incorrectly gets charno position when EmptyExpression
418 // has its absolute position on the carriage return. For now, the
419 // line number gets reported correctly (on the next line) but the
420 // character position is -1, so the overall line/char pair in our tree
421 // is -1.
422 assertNodePosition(-1, -1, incrClause); // should be 4
423 }
424
425 public void testBinaryExprLocation() {
426 Node root = newParse(
427 "var d = a\n" +
428 " + \n" +
429 " b;\n" +
430 "var\n" +
431 " e =\n" +
432 " a +\n" +
433 " c;\n" +
434 "var f = b\n" +
435 " / c;\n");
436
437 Node firstVarDecl = root.getFirstChild();
438 Node firstVar = firstVarDecl.getFirstChild();
439 Node firstVarAdd = firstVar.getFirstChild();
440
441 Node secondVarDecl = firstVarDecl.getNext();
442 Node secondVar = secondVarDecl.getFirstChild();
443 Node secondVarAdd = secondVar.getFirstChild();
444
445 Node thirdVarDecl = secondVarDecl.getNext();
446 Node thirdVar = thirdVarDecl.getFirstChild();
447 Node thirdVarAdd = thirdVar.getFirstChild();
448
449 assertNodePosition(1, 0, firstVarDecl);
450 assertNodePosition(1, 4, firstVar);
451 assertNodePosition(1, 8, firstVarAdd);
452 assertNodePosition(1, 8, firstVarAdd.getFirstChild());
453 assertNodePosition(3, 4, firstVarAdd.getLastChild());
454
455 assertNodePosition(4, 0, secondVarDecl);
456 assertNodePosition(5, 4, secondVar);
457 assertNodePosition(6, 4, secondVarAdd);
458 assertNodePosition(6, 4, secondVarAdd.getFirstChild());
459 assertNodePosition(7, 4, secondVarAdd.getLastChild());
460
461 assertNodePosition(8, 0, thirdVarDecl);
462 assertNodePosition(8, 4, thirdVar);
463 assertNodePosition(8, 8, thirdVarAdd);
464 assertNodePosition(8, 8, thirdVarAdd.getFirstChild());
465 assertNodePosition(9, 6, thirdVarAdd.getLastChild());
466 }
467
468 public void testPrefixLocation() {
469 Node root = newParse(
470 "a++;\n" +
471 "--\n" +
472 "b;\n");
473
474 Node firstStmt = root.getFirstChild();
475 Node secondStmt = firstStmt.getNext();
476 Node firstOp = firstStmt.getFirstChild();
477 Node secondOp = secondStmt.getFirstChild();
478
479 assertNodePosition(1, 0, firstOp);
480 assertNodePosition(2, 0, secondOp);
481 }
482
483 public void testIfLocation() {
484 Node root = newParse(
485 "if\n" +
486 " (a == 3)\n" +
487 "{\n" +
488 " b = 0;\n" +
489 "}\n" +
490 " else\n" +
491 "{\n" +
492 " c = 1;\n" +
493 "}\n");
494
495 Node ifStmt = root.getFirstChild();
496 Node eqClause = ifStmt.getFirstChild();
497 Node thenClause = eqClause.getNext();
498 Node elseClause = thenClause.getNext();
499
500 assertNodePosition(1, 0, ifStmt);
501 assertNodePosition(2, 3, eqClause);
502 assertNodePosition(3, 0, thenClause);
503 assertNodePosition(7, 0, elseClause);
504 }
505
506 public void testTryLocation() {
507 Node root = newParse(
508 "try {\n" +
509 " var x = 1;\n" +
510 "} catch\n" +
511 " (err)\n" +
512 "{\n" +
513 "} finally {\n" +
514 " var y = 2;\n" +
515 "}\n");
516
517 Node tryStmt = root.getFirstChild();
518 Node tryBlock = tryStmt.getFirstChild();
519 Node catchBlock = tryBlock.getNext();
520 Node catchVarBlock = catchBlock.getFirstChild();
521 Node catchVar = catchVarBlock.getFirstChild();
522 Node finallyBlock = catchBlock.getNext();
523 Node finallyStmt = finallyBlock.getFirstChild();
524
525 assertNodePosition(1, 0, tryStmt);
526 assertNodePosition(1, 4, tryBlock);
527 assertNodePosition(3, 2, catchVarBlock);
528 assertNodePosition(4, 4, catchVar);
529 assertNodePosition(3, 0, catchBlock);
530 assertNodePosition(6, 10, finallyBlock);
531 assertNodePosition(7, 2, finallyStmt);
532 }
533
534 public void testHookLocation() {
535 Node root = newParse(
536 "a\n" +
537 "?\n" +
538 "b\n" +
539 ":\n" +
540 "c\n" +
541 ";\n");
542
543 Node hookExpr = root.getFirstChild().getFirstChild();
544 Node condExpr = hookExpr.getFirstChild();
545 Node thenExpr = condExpr.getNext();
546 Node elseExpr = thenExpr.getNext();
547
548 assertNodePosition(2, 0, hookExpr);
549 assertNodePosition(1, 0, condExpr);
550 assertNodePosition(3, 0, thenExpr);
551 assertNodePosition(5, 0, elseExpr);
552 }
553
554 public void testLabelLocation() {
555 Node root = newParse(
556 "foo:\n" +
557 "a = 1;\n" +
558 "bar:\n" +
559 "b = 2;\n");
560
561 Node firstStmt = root.getFirstChild();
562 Node secondStmt = firstStmt.getNext();
563
564 assertNodePosition(1, 0, firstStmt);
565 assertNodePosition(3, 0, secondStmt);
566 }
567
568 public void testCompareLocation() {
569 Node root = newParse(
570 "a\n" +
571 "<\n" +
572 "b\n");
573
574 Node condClause = root.getFirstChild().getFirstChild();
575 Node lhs = condClause.getFirstChild();
576 Node rhs = lhs.getNext();
577
578 assertNodePosition(1, 0, condClause);
579 assertNodePosition(1, 0, lhs);
580 assertNodePosition(3, 0, rhs);
581 }
582
583 public void testEqualityLocation() {
584 Node root = newParse(
585 "a\n" +
586 "==\n" +
587 "b\n");
588
589 Node condClause = root.getFirstChild().getFirstChild();
590 Node lhs = condClause.getFirstChild();
591 Node rhs = lhs.getNext();
592
593 assertNodePosition(1, 0, condClause);
594 assertNodePosition(1, 0, lhs);
595 assertNodePosition(3, 0, rhs);
596 }
597
598 public void testPlusEqLocation() {
599 Node root = newParse(
600 "a\n" +
601 "+=\n" +
602 "b\n");
603
604 Node condClause = root.getFirstChild().getFirstChild();
605 Node lhs = condClause.getFirstChild();
606 Node rhs = lhs.getNext();
607
608 assertNodePosition(1, 0, condClause);
609 assertNodePosition(1, 0, lhs);
610 assertNodePosition(3, 0, rhs);
611 }
612
613 public void testCommaLocation() {
614 Node root = newParse(
615 "a,\n" +
616 "b,\n" +
617 "c;\n");
618
619 Node statement = root.getFirstChild();
620 Node comma1 = statement.getFirstChild();
621 Node comma2 = comma1.getFirstChild();
622 Node cRef = comma2.getNext();
623 Node aRef = comma2.getFirstChild();
624 Node bRef = aRef.getNext();
625
626 assertNodePosition(1, 0, comma2);
627 assertNodePosition(1, 0, aRef);
628 assertNodePosition(2, 0, bRef);
629 assertNodePosition(3, 0, cRef);
630 }
631
632 public void testRegexpLocation() {
633 Node root = newParse(
634 "var path =\n" +
635 "replace(\n" +
636 "/a/g," +
637 "'/');\n");
638
639 Node firstVarDecl = root.getFirstChild();
640 Node firstVar = firstVarDecl.getFirstChild();
641 Node callNode = firstVar.getFirstChild();
642 Node fnName = callNode.getFirstChild();
643 Node regexObject = fnName.getNext();
644 Node aString = regexObject.getFirstChild();
645 Node endRegexString = regexObject.getNext();
646
647 assertNodePosition(1, 0, firstVarDecl);
648 assertNodePosition(1, 4, 4, firstVar);
649 assertNodePosition(2, 0, 18, callNode);
650 assertNodePosition(2, 0, 7, fnName);
651 assertNodePosition(3, 0, regexObject);
652 assertNodePosition(3, 0, aString);
653 assertNodePosition(3, 5, endRegexString);
654 }
655
656 public void testNestedOr() {
657 Node root = newParse(
658 "if (a && \n" +
659 " b() || \n" +
660 " /* comment */\n" +
661 " c) {\n" +
662 "}\n"
663 );
664
665 Node ifStmt = root.getFirstChild();
666 Node orClause = ifStmt.getFirstChild();
667 Node andClause = orClause.getFirstChild();
668 Node cName = andClause.getNext();
669
670 assertNodePosition(1, 0, ifStmt);
671 assertNodePosition(1, 4, orClause);
672 assertNodePosition(1, 4, andClause);
673 assertNodePosition(4, 4, cName);
674
675 }
676
677 public void testBitwiseOps() {
678 Node root = newParse(
679 "if (a & \n" +
680 " b() | \n" +
681 " /* comment */\n" +
682 " c) {\n" +
683 "}\n"
684 );
685
686 Node ifStmt = root.getFirstChild();
687 Node bitOr = ifStmt.getFirstChild();
688 Node bitAnd = bitOr.getFirstChild();
689 Node cName = bitAnd.getNext();
690
691 assertNodePosition(1, 0, ifStmt);
692 assertNodePosition(1, 4, bitOr);
693 assertNodePosition(1, 4, bitAnd);
694 assertNodePosition(4, 4, cName);
695
696 }
697
698 public void testObjectLitLocation() {
699 Node root = newParse(
700 "var foo =\n" +
701 "{ \n" +
702 "'A' : 'A', \n" +
703 "'B' : 'B', \n" +
704 "'C' :\n" +
705 " 'C' \n" +
706 "};\n");
707
708 Node firstVarDecl = root.getFirstChild();
709 Node firstVar = firstVarDecl.getFirstChild();
710 Node firstObjectLit = firstVar.getFirstChild();
711 Node firstKey = firstObjectLit.getFirstChild();
712 Node firstValue = firstKey.getFirstChild();
713
714 Node secondKey = firstKey.getNext();
715 Node secondValue = secondKey.getFirstChild();
716
717 Node thirdKey = secondKey.getNext();
718 Node thirdValue = thirdKey.getFirstChild();
719
720 assertNodePosition(1, 4, firstVar);
721 assertNodePosition(2, 0, firstObjectLit);
722
723 assertNodePosition(3, 0, firstKey);
724 assertNodePosition(3, 6, firstValue);
725
726 assertNodePosition(4, 0, secondKey);
727 assertNodePosition(4, 6, secondValue);
728
729 assertNodePosition(5, 0, thirdKey);
730 assertNodePosition(6, 4, thirdValue);
731 }
732
733 public void testTryWithoutCatchLocation() {
734 Node root = newParse(
735 "try {\n" +
736 " var x = 1;\n" +
737 "} finally {\n" +
738 " var y = 2;\n" +
739 "}\n");
740
741 Node tryStmt = root.getFirstChild();
742 Node tryBlock = tryStmt.getFirstChild();
743 Node catchBlock = tryBlock.getNext();
744 Node finallyBlock = catchBlock.getNext();
745 Node finallyStmt = finallyBlock.getFirstChild();
746
747 assertNodePosition(1, 0, tryStmt);
748 assertNodePosition(1, 4, tryBlock);
749 assertNodePosition(3, 0, catchBlock);
750 assertNodePosition(3, 10, finallyBlock);
751 assertNodePosition(4, 2, finallyStmt);
752 }
753
754 public void testTryWithoutFinallyLocation() {
755 Node root = newParse(
756 "try {\n" +
757 " var x = 1;\n" +
758 "} catch (ex) {\n" +
759 " var y = 2;\n" +
760 "}\n");
761
762 Node tryStmt = root.getFirstChild();
763 Node tryBlock = tryStmt.getFirstChild();
764 Node catchBlock = tryBlock.getNext();
765 Node catchStmt = catchBlock.getFirstChild();
766 Node exceptionVar = catchStmt.getFirstChild();
767 Node exceptionBlock = exceptionVar.getNext();
768 Node varDecl = exceptionBlock.getFirstChild();
769
770 assertNodePosition(1, 0, tryStmt);
771 assertNodePosition(1, 4, tryBlock);
772 assertNodePosition(3, 0, catchBlock);
773 assertNodePosition(3, 2, catchStmt);
774 assertNodePosition(3, 9, exceptionVar);
775 assertNodePosition(3, 13, exceptionBlock);
776 assertNodePosition(4, 2, varDecl);
777 }
778
779 public void testMultilineEqLocation() {
780 Node root = newParse(
781 "if\n" +
782 " (((a == \n" +
783 " 3) && \n" +
784 " (b == 2)) || \n" +
785 " (c == 1)) {\n" +
786 "}\n");
787 Node ifStmt = root.getFirstChild();
788 Node orTest = ifStmt.getFirstChild();
789 Node andTest = orTest.getFirstChild();
790 Node cTest = andTest.getNext();
791 Node aTest = andTest.getFirstChild();
792 Node bTest = aTest.getNext();
793
794 assertNodePosition(1, 0, ifStmt);
795 assertNodePosition(2, 7, orTest);
796 assertNodePosition(2, 7, andTest);
797 assertNodePosition(2, 7, aTest);
798 assertNodePosition(4, 3, bTest);
799 assertNodePosition(5, 2, cTest);
800 }
801
802 public void testMultilineBitTestLocation() {
803 Node root = newParse(
804 "if (\n" +
805 " ((a \n" +
806 " | 3 \n" +
807 " ) == \n" +
808 " (b \n" +
809 " & 2)) && \n" +
810 " ((a \n" +
811 " ^ 0xffff) \n" +
812 " != \n" +
813 " (c \n" +
814 " << 1))) {\n" +
815 "}\n");
816
817 Node ifStmt = root.getFirstChild();
818 Node andTest = ifStmt.getFirstChild();
819 Node eqTest = andTest.getFirstChild();
820 Node notEqTest = eqTest.getNext();
821
822 Node bitOrTest = eqTest.getFirstChild();
823 Node bitAndTest = bitOrTest.getNext();
824
825 Node bitXorTest = notEqTest.getFirstChild();
826 Node bitShiftTest = bitXorTest.getNext();
827
828 assertNodePosition(1, 0, ifStmt);
829
830 assertNodePosition(2, 8, eqTest);
831 assertNodePosition(7, 8, notEqTest);
832
833 assertNodePosition(2, 8, bitOrTest);
834 assertNodePosition(5, 8, bitAndTest);
835 assertNodePosition(7, 8, bitXorTest);
836 assertNodePosition(10, 8, bitShiftTest);
837 }
838
839 public void testCallLocation() {
840 Node root = newParse(
841 "a.\n" +
842 "b.\n" +
843 "cccc(1);\n");
844
845 Node exprStmt = root.getFirstChild();
846 Node functionCall = exprStmt.getFirstChild();
847 Node functionProp = functionCall.getFirstChild();
848 Node firstNameComponent = functionProp.getFirstChild();
849 Node lastNameComponent = firstNameComponent.getNext();
850 Node aNameComponent = firstNameComponent.getFirstChild();
851 Node bNameComponent = aNameComponent.getNext();
852
853 assertNodePosition(1, 0, 13, functionCall);
854 assertNodePosition(1, 0, 10, functionProp);
855 // TODO(bowdidge) New Rhino doesn't keep the position of the dot handy.
856 // New Rhino treats the location of the qualified name as the beginning of
857 // the whole name.
858 assertNodePosition(1, 0, 4, firstNameComponent);
859 assertNodePosition(3, 0, 4, lastNameComponent);
860
861 assertNodePosition(1, 0, 1, aNameComponent);
862 assertNodePosition(2, 0, 1, bNameComponent);
863 }
864
865 public void testNewLocation() {
866 Node root = newParse(
867 "new c();\n");
868
869 Node exprStmt = root.getFirstChild();
870 Node newExpr = exprStmt.getFirstChild();
871 assertNodePosition(1, 0, 7, newExpr);
872 }
873
874 public void testNewLocationMultiLine() {
875 Node root = newParse(
876 "new \n" +
877 "c();\n");
878
879 Node exprStmt = root.getFirstChild();
880 Node newExpr = exprStmt.getFirstChild();
881 assertNodePosition(1, 0, 10, newExpr);
882 }
883
884 public void testLinenoDeclaration() {
885 Node root = newParse(
886 "a.\n" +
887 "b=\n" +
888 "function() {};\n");
889
890 Node exprStmt = root.getFirstChild();
891 Node fnAssignment = exprStmt.getFirstChild();
892 Node aDotbName = fnAssignment.getFirstChild();
893 Node aName = aDotbName.getFirstChild();
894 Node bName = aName.getNext();
895 Node fnNode = aDotbName.getNext();
896 Node fnName = fnNode.getFirstChild();
897
898 assertNodePosition(1, 0, fnAssignment);
899 // TODO(bowdidge) New Rhino doesn't keep track of the position of the dot.
900 //assertNodePosition(1, 1, aDotbName);
901 assertNodePosition(1, 0, aName);
902 assertNodePosition(2, 0, bName);
903 assertNodePosition(3, 0, fnNode);
904 assertNodePosition(3, 8, fnName);
905 }
906
907 final String INVALID_ASSIGNMENT_TARGET = "invalid assignment target";
908 final String INVALID_INCREMENT_TARGET = "invalid increment target";
909 final String INVALID_DECREMENT_TARGET = "invalid decrement target";
910
911 final String INVALID_INC_OPERAND = "Invalid increment operand";
912 final String INVALID_DEC_OPERAND = "Invalid decrement operand";
913
914 public void testAssignmentValidation() {
915 testNoParseError("x=1");
916 testNoParseError("x.y=1");
917 testNoParseError("f().y=1");
918 testParseError("(x||y)=1", INVALID_ASSIGNMENT_TARGET);
919 testParseError("(x?y:z)=1", INVALID_ASSIGNMENT_TARGET);
920 testParseError("f()=1", INVALID_ASSIGNMENT_TARGET);
921
922 testNoParseError("x+=1");
923 testNoParseError("x.y+=1");
924 testNoParseError("f().y+=1");
925 testParseError("(x||y)+=1", INVALID_ASSIGNMENT_TARGET);
926 testParseError("(x?y:z)+=1", INVALID_ASSIGNMENT_TARGET);
927 testParseError("f()+=1", INVALID_ASSIGNMENT_TARGET);
928
929 testParseError("f()++", INVALID_INCREMENT_TARGET);
930 testParseError("f()--", INVALID_DECREMENT_TARGET);
931 testParseError("++f()", INVALID_INCREMENT_TARGET);
932 testParseError("--f()", INVALID_DECREMENT_TARGET);
933 }
934
935 private void testNoParseError(String string) {
936 testParseError(string, (String)null);
937 }
938
939 private void testParseError(String string, String error) {
940 testParseError(string, error == null ? null : new String[] { error });
941 }
942
943 private void testParseError(String string, String[] errors) {
944 newParse(string, new TestErrorReporter(errors, null));
945 assertTrue("unexpected warnings reported",
946 errorReporter.hasEncounteredAllWarnings());
947 assertTrue("expected error were not reported",
948 errorReporter.hasEncounteredAllErrors());
949 }
950
951 private void assertMarkerPosition(Node n, int lineno, int charno) {
952 int count = 0;
953 for (JSDocInfo.Marker marker : n.getJSDocInfo().getMarkers()) {
954 assertEquals(lineno, marker.getAnnotation().getStartLine());
955 assertEquals(charno, marker.getAnnotation().getPositionOnStartLine());
956 count++;
957 }
958 assertEquals(1, count);
959 }
960
961 private void assertNodePosition(int lineno, int charno, Node n) {
962 assertEquals("Line number", lineno, n.getLineno());
963 assertEquals("Column position", charno, n.getCharno());
964 }
965
966 private void assertNodePosition(int lineno, int charno, int length, Node n) {
967 assertEquals("Line number", lineno, n.getLineno());
968 assertEquals("Column position", charno, n.getCharno());
969 assertEquals("Length", length, n.getLength());
970 }
971
972
973 private void testNewParser(String code, String expected) {
974 String actual = newParse(code).toStringTree();
975 assertEquals(expected, actual);
976 }
977
978 private Node newParse(String string) {
979 return newParse(string, new TestErrorReporter(null, null));
980 }
981
982 private Node newParse(String string, TestErrorReporter errorReporter) {
983 CompilerEnvirons environment = new CompilerEnvirons();
984
985 environment.setRecordingComments(true);
986 environment.setRecordingLocalJsDocComments(true);
987
988 Parser p = new Parser(environment);
989 AstRoot script = p.parse(string, null, 1);
990
991 Config config = ParserRunner.createConfig(true, mode, false);
992 Node root = IRFactory.transformTree(
993 script, SourceFile.fromCode("FileName.js", string), string, config, errorReporter);
994
995 return root;
996 }
997 }