-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ParserTest.java
3932 lines (3273 loc) · 128 KB
/
ParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.parsing;
import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.parsing.Config.StrictMode.SLOPPY;
import static com.google.javascript.jscomp.parsing.Config.StrictMode.STRICT;
import static com.google.javascript.jscomp.parsing.JsDocInfoParser.BAD_TYPE_WIKI_LINK;
import static com.google.javascript.jscomp.parsing.parser.testing.FeatureSetSubject.assertFS;
import static com.google.javascript.jscomp.testing.NodeSubject.assertNode;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.parsing.Config.LanguageMode;
import com.google.javascript.jscomp.parsing.ParserRunner.ParseResult;
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleSourceFile;
import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import com.google.javascript.rhino.testing.TestErrorReporter;
import java.util.List;
public final class ParserTest extends BaseJSTypeTestCase {
private static final String SUSPICIOUS_COMMENT_WARNING =
IRFactory.SUSPICIOUS_COMMENT_WARNING;
private static final String TRAILING_COMMA_MESSAGE =
"Trailing comma is not legal in an ECMA-262 object initializer";
private static final String MISSING_GT_MESSAGE =
"Bad type annotation. missing closing >" + BAD_TYPE_WIKI_LINK;
private static final String UNLABELED_BREAK = "unlabelled break must be inside loop or switch";
private static final String UNEXPECTED_CONTINUE =
"continue must be inside loop";
private static final String UNEXPECTED_RETURN =
"return must be inside function";
private static final String UNEXPECTED_LABELLED_CONTINUE =
"continue can only use labeles of iteration statements";
private static final String UNDEFINED_LABEL = "undefined label";
private static final String HTML_COMMENT_WARNING = "In some cases, '<!--' " +
"and '-->' are treated as a '//' " +
"for legacy reasons. Removing this from your code is " +
"safe for all browsers currently in use.";
private static final String INVALID_ASSIGNMENT_TARGET = "invalid assignment target";
private static final String SEMICOLON_EXPECTED = "Semi-colon expected";
private Config.LanguageMode mode;
private Config.StrictMode strictMode;
private boolean isIdeMode = false;
private FeatureSet expectedFeatures;
@Override
protected void setUp() throws Exception {
super.setUp();
mode = LanguageMode.ECMASCRIPT3;
strictMode = SLOPPY;
isIdeMode = false;
expectedFeatures = FeatureSet.BARE_MINIMUM;
}
public void testExponentOperator() {
mode = LanguageMode.ECMASCRIPT7;
strictMode = STRICT;
parseError("-x**y", "Unary operator '-' requires parentheses before '**'");
expectFeatures(Feature.EXPONENT_OP);
parse("x**y");
// Parentheses are required for disambiguation when a unary expression is desired as
// the left operand.
parse("-(x**y)");
parse("(-x)**y");
// Parens are not required for unary operator on the right operand
parse("x**-y");
parse("x/y**z");
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
parseWarning(
"x**y", requiresLanguageModeMessage(LanguageMode.ECMASCRIPT7, Feature.EXPONENT_OP));
}
public void testExponentAssignmentOperator() {
mode = LanguageMode.ECMASCRIPT7;
strictMode = STRICT;
expectFeatures(Feature.EXPONENT_OP);
parse("x**=y;");
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
parseWarning(
"x**=y;", requiresLanguageModeMessage(LanguageMode.ECMASCRIPT7, Feature.EXPONENT_OP));
}
public void testFunction() {
parse("var f = function(x,y,z) { return 0; }");
parse("function f(x,y,z) { return 0; }");
}
public void testFunctionTrailingComma() {
mode = LanguageMode.ECMASCRIPT8;
expectFeatures(Feature.TRAILING_COMMA_IN_PARAM_LIST);
parse("var f = function(x,y,z,) {}");
parse("function f(x,y,z,) {}");
}
public void testFunctionTrailingCommaPreES8() {
mode = LanguageMode.ECMASCRIPT7;
parseError(
"var f = function(x,y,z,) {}",
"Invalid trailing comma in formal parameter list");
parseError(
"function f(x,y,z,) {}",
"Invalid trailing comma in formal parameter list");
}
public void testFunctionExtraTrailingComma() {
parseError("var f = function(x,y,z,,) {}", "')' expected");
parseError("function f(x,y,z,,) {}", "')' expected");
}
public void testCallTrailingComma() {
mode = LanguageMode.ECMASCRIPT8;
expectFeatures(Feature.TRAILING_COMMA_IN_PARAM_LIST);
parse("f(x,y,z,);");
}
public void testCallTrailingCommaPreES8() {
mode = LanguageMode.ECMASCRIPT7;
parseError(
"f(x,y,z,);",
"Invalid trailing comma in arguments list");
}
public void testCallExtraTrailingComma() {
parseError("f(x,y,z,,);", "')' expected");
}
public void testWhile() {
parse("while(1) { break; }");
}
public void testNestedWhile() {
parse("while(1) { while(1) { break; } }");
}
public void testBreak() {
parseError("break;", UNLABELED_BREAK);
}
public void testContinue() {
parseError("continue;", UNEXPECTED_CONTINUE);
}
public void testBreakCrossFunction() {
parseError("while(1) { var f = function() { break; } }", UNLABELED_BREAK);
}
public void testBreakCrossFunctionInFor() {
parseError("while(1) {for(var f = function () { break; };;) {}}", UNLABELED_BREAK);
}
public void testBreakInForOf() {
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
expectFeatures(Feature.FOR_OF);
parse(""
+ "for (var x of [1, 2, 3]) {\n"
+ " if (x == 2) break;\n"
+ "}");
}
public void testContinueToSwitch() {
parseError("switch(1) {case(1): continue; }", UNEXPECTED_CONTINUE);
}
public void testContinueToSwitchWithNoCases() {
parse("switch(1){}");
}
public void testContinueToSwitchWithTwoCases() {
parseError("switch(1){case(1):break;case(2):continue;}", UNEXPECTED_CONTINUE);
}
public void testContinueToSwitchWithDefault() {
parseError("switch(1){case(1):break;case(2):default:continue;}", UNEXPECTED_CONTINUE);
}
public void testContinueToLabelSwitch() {
parseError(
"while(1) {a: switch(1) {case(1): continue a; }}",
UNEXPECTED_LABELLED_CONTINUE);
}
public void testContinueOutsideSwitch() {
parse("b: while(1) { a: switch(1) { case(1): continue b; } }");
}
public void testContinueNotCrossFunction1() {
parse("a:switch(1){case(1):var f = function(){a:while(1){continue a;}}}");
}
public void testContinueNotCrossFunction2() {
parseError(
"a:switch(1){case(1):var f = function(){while(1){continue a;}}}",
UNDEFINED_LABEL + " \"a\"");
}
public void testContinueInForOf() {
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
expectFeatures(Feature.FOR_OF);
parse(""
+ "for (var x of [1, 2, 3]) {\n"
+ " if (x == 2) continue;\n"
+ "}");
}
/** @bug 19100575 */
public void testVarSourceLocations() {
isIdeMode = true;
Node n = parse("var x, y = 1;");
Node var = n.getFirstChild();
assertNode(var).hasType(Token.VAR);
Node x = var.getFirstChild();
assertNode(x).hasType(Token.NAME);
assertNode(x).hasCharno("var ".length());
Node y = x.getNext();
assertNode(y).hasType(Token.NAME);
assertNode(y).hasCharno("var x, ".length());
assertNode(y).hasLength("y = 1".length());
}
public void testSourceLocationsNonAscii() {
Node n = parse("'안녕세계!'");
Node exprResult = n.getFirstChild();
Node string = exprResult.getFirstChild();
assertNode(string).hasType(Token.STRING);
assertNode(string).hasLength(7); // 2 quotes, plus 5 characters
}
public void testReturn() {
parse("function foo() { return 1; }");
parseError("return;", UNEXPECTED_RETURN);
parseError("return 1;", UNEXPECTED_RETURN);
}
public void testThrow() {
parse("throw Error();");
parse("throw new Error();");
parse("throw '';");
parseError("throw;", "semicolon/newline not allowed after 'throw'");
parseError("throw\nError();", "semicolon/newline not allowed after 'throw'");
}
public void testLabel1() {
parse("foo:bar");
}
public void testLabel2() {
parse("{foo:bar}");
}
public void testLabel3() {
parse("foo:bar:baz");
}
public void testDuplicateLabelWithoutBraces() {
parseError("foo:foo:bar", "Duplicate label \"foo\"");
}
public void testDuplicateLabelWithBraces() {
parseError("foo:{bar;foo:baz}", "Duplicate label \"foo\"");
}
public void testDuplicateLabelWithFor() {
parseError("foo:for(;;){foo:bar}", "Duplicate label \"foo\"");
}
public void testNonDuplicateLabelSiblings() {
parse("foo:1;foo:2");
}
public void testNonDuplicateLabelCrossFunction() {
parse("foo:(function(){foo:2})");
}
public void testLabeledFunctionDeclaration() {
parseError(
"foo:function f() {}", "Functions can only be declared at top level or inside a block.");
}
public void testLabeledClassDeclaration() {
mode = LanguageMode.ECMASCRIPT6;
parseError(
"foo:class Foo {}", "Classes can only be declared at top level or inside a block.");
}
public void testLinenoCharnoAssign1() throws Exception {
Node assign = parse("a = b").getFirstFirstChild();
assertNode(assign).hasType(Token.ASSIGN);
assertNode(assign).hasLineno(1);
assertNode(assign).hasCharno(0);
}
public void testLinenoCharnoAssign2() throws Exception {
Node assign = parse("\n a.g.h.k = 45").getFirstFirstChild();
assertNode(assign).hasType(Token.ASSIGN);
assertNode(assign).hasLineno(2);
assertNode(assign).hasCharno(1);
}
public void testLinenoCharnoCall() throws Exception {
Node call = parse("\n foo(123);").getFirstFirstChild();
assertNode(call).hasType(Token.CALL);
assertNode(call).hasLineno(2);
assertNode(call).hasCharno(1);
}
public void testLinenoCharnoGetProp1() throws Exception {
Node getprop = parse("\n foo.bar").getFirstFirstChild();
assertNode(getprop).hasType(Token.GETPROP);
assertNode(getprop).hasLineno(2);
assertNode(getprop).hasCharno(1);
Node name = getprop.getSecondChild();
assertNode(name).hasType(Token.STRING);
assertNode(name).hasLineno(2);
assertNode(name).hasCharno(5);
}
public void testLinenoCharnoGetProp2() throws Exception {
Node getprop = parse("\n foo.\nbar").getFirstFirstChild();
assertNode(getprop).hasType(Token.GETPROP);
assertNode(getprop).hasLineno(2);
assertNode(getprop).hasCharno(1);
Node name = getprop.getSecondChild();
assertNode(name).hasType(Token.STRING);
assertNode(name).hasLineno(3);
assertNode(name).hasCharno(0);
}
public void testLinenoCharnoGetelem1() throws Exception {
Node call = parse("\n foo[123]").getFirstFirstChild();
assertNode(call).hasType(Token.GETELEM);
assertNode(call).hasLineno(2);
assertNode(call).hasCharno(1);
}
public void testLinenoCharnoGetelem2() throws Exception {
Node call = parse("\n \n foo()[123]").getFirstFirstChild();
assertNode(call).hasType(Token.GETELEM);
assertNode(call).hasLineno(3);
assertNode(call).hasCharno(1);
}
public void testLinenoCharnoGetelem3() throws Exception {
Node call = parse("\n \n (8 + kl)[123]").getFirstFirstChild();
assertNode(call).hasType(Token.GETELEM);
assertNode(call).hasLineno(3);
assertNode(call).hasCharno(1);
}
public void testLinenoCharnoForComparison() throws Exception {
Node lt =
parse("for (; i < j;){}").getFirstChild().getSecondChild();
assertNode(lt).hasType(Token.LT);
assertNode(lt).hasLineno(1);
assertNode(lt).hasCharno(7);
}
public void testLinenoCharnoHook() throws Exception {
Node n = parse("\n a ? 9 : 0").getFirstFirstChild();
assertNode(n).hasType(Token.HOOK);
assertNode(n).hasLineno(2);
assertNode(n).hasCharno(1);
}
public void testLinenoCharnoArrayLiteral() throws Exception {
Node n = parse("\n [8, 9]").getFirstFirstChild();
assertNode(n).hasType(Token.ARRAYLIT);
assertNode(n).hasLineno(2);
assertNode(n).hasCharno(2);
n = n.getFirstChild();
assertNode(n).hasType(Token.NUMBER);
assertNode(n).hasLineno(2);
assertNode(n).hasCharno(3);
n = n.getNext();
assertNode(n).hasType(Token.NUMBER);
assertNode(n).hasLineno(2);
assertNode(n).hasCharno(6);
}
public void testLinenoCharnoObjectLiteral() throws Exception {
Node n = parse("\n\n var a = {a:0\n,b :1};")
.getFirstFirstChild().getFirstChild();
assertNode(n).hasType(Token.OBJECTLIT);
assertNode(n).hasLineno(3);
assertNode(n).hasCharno(9);
Node key = n.getFirstChild();
assertNode(key).hasType(Token.STRING_KEY);
assertNode(key).hasLineno(3);
assertNode(key).hasCharno(10);
Node value = key.getFirstChild();
assertNode(value).hasType(Token.NUMBER);
assertNode(value).hasLineno(3);
assertNode(value).hasCharno(12);
key = key.getNext();
assertNode(key).hasType(Token.STRING_KEY);
assertNode(key).hasLineno(4);
assertNode(key).hasCharno(1);
value = key.getFirstChild();
assertNode(value).hasType(Token.NUMBER);
assertNode(value).hasLineno(4);
assertNode(value).hasCharno(4);
}
public void testLinenoCharnoAdd() throws Exception {
testLinenoCharnoBinop("+");
}
public void testLinenoCharnoSub() throws Exception {
testLinenoCharnoBinop("-");
}
public void testLinenoCharnoMul() throws Exception {
testLinenoCharnoBinop("*");
}
public void testLinenoCharnoDiv() throws Exception {
testLinenoCharnoBinop("/");
}
public void testLinenoCharnoMod() throws Exception {
testLinenoCharnoBinop("%");
}
public void testLinenoCharnoShift() throws Exception {
testLinenoCharnoBinop("<<");
}
public void testLinenoCharnoBinaryAnd() throws Exception {
testLinenoCharnoBinop("&");
}
public void testLinenoCharnoAnd() throws Exception {
testLinenoCharnoBinop("&&");
}
public void testLinenoCharnoBinaryOr() throws Exception {
testLinenoCharnoBinop("|");
}
public void testLinenoCharnoOr() throws Exception {
testLinenoCharnoBinop("||");
}
public void testLinenoCharnoLt() throws Exception {
testLinenoCharnoBinop("<");
}
public void testLinenoCharnoLe() throws Exception {
testLinenoCharnoBinop("<=");
}
public void testLinenoCharnoGt() throws Exception {
testLinenoCharnoBinop(">");
}
public void testLinenoCharnoGe() throws Exception {
testLinenoCharnoBinop(">=");
}
private void testLinenoCharnoBinop(String binop) {
Node op = parse("var a = 89 " + binop + " 76;").getFirstChild().
getFirstFirstChild();
assertNode(op).hasLineno(1);
assertNode(op).hasCharno(8);
}
public void testJSDocAttachment1() {
Node varNode = parse("/** @type {number} */var a;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
JSDocInfo varInfo = varNode.getJSDocInfo();
assertThat(varInfo).isNotNull();
assertTypeEquals(NUMBER_TYPE, varInfo.getType());
// VAR NAME
Node varNameNode = varNode.getFirstChild();
assertNode(varNameNode).hasType(Token.NAME);
assertThat(varNameNode.getJSDocInfo()).isNull();
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
expectFeatures(Feature.LET_DECLARATIONS);
Node letNode = parse("/** @type {number} */let a;").getFirstChild();
// LET
assertNode(letNode).hasType(Token.LET);
JSDocInfo letInfo = letNode.getJSDocInfo();
assertThat(letInfo).isNotNull();
assertTypeEquals(NUMBER_TYPE, letInfo.getType());
// LET NAME
Node letNameNode = letNode.getFirstChild();
assertNode(letNameNode).hasType(Token.NAME);
assertThat(letNameNode.getJSDocInfo()).isNull();
expectFeatures(Feature.CONST_DECLARATIONS);
Node constNode = parse("/** @type {number} */const a = 0;").getFirstChild();
// CONST
assertNode(constNode).hasType(Token.CONST);
JSDocInfo constInfo = constNode.getJSDocInfo();
assertThat(constInfo).isNotNull();
assertTypeEquals(NUMBER_TYPE, constInfo.getType());
// CONST NAME
Node constNameNode = constNode.getFirstChild();
assertNode(constNameNode).hasType(Token.NAME);
assertThat(constNameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment2() {
Node varNode = parse("/** @type {number} */var a,b;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
JSDocInfo info = varNode.getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(NUMBER_TYPE, info.getType());
// First NAME
Node nameNode1 = varNode.getFirstChild();
assertNode(nameNode1).hasType(Token.NAME);
assertThat(nameNode1.getJSDocInfo()).isNull();
// Second NAME
Node nameNode2 = nameNode1.getNext();
assertNode(nameNode2).hasType(Token.NAME);
assertThat(nameNode2.getJSDocInfo()).isNull();
}
public void testJSDocAttachment3() {
Node assignNode = parse("/** @type {number} */goog.FOO = 5;").getFirstFirstChild();
assertNode(assignNode).hasType(Token.ASSIGN);
JSDocInfo info = assignNode.getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(NUMBER_TYPE, info.getType());
}
public void testJSDocAttachment4() {
Node varNode = parse(
"var a, /** @define {number} */ b = 5;").getFirstChild();
// ASSIGN
assertNode(varNode).hasType(Token.VAR);
assertThat(varNode.getJSDocInfo()).isNull();
// a
Node a = varNode.getFirstChild();
assertThat(a.getJSDocInfo()).isNull();
// b
Node b = a.getNext();
JSDocInfo info = b.getJSDocInfo();
assertThat(info).isNotNull();
assertThat(info.isDefine()).isTrue();
assertTypeEquals(NUMBER_TYPE, info.getType());
}
public void testJSDocAttachment5() {
Node varNode =
parse("var /** @type {number} */a, /** @define {number} */b = 5;").getFirstChild();
// ASSIGN
assertNode(varNode).hasType(Token.VAR);
assertThat(varNode.getJSDocInfo()).isNull();
// a
Node a = varNode.getFirstChild();
assertThat(a.getJSDocInfo()).isNotNull();
JSDocInfo info = a.getJSDocInfo();
assertThat(info).isNotNull();
assertThat(info.isDefine()).isFalse();
assertTypeEquals(NUMBER_TYPE, info.getType());
// b
Node b = a.getNext();
info = b.getJSDocInfo();
assertThat(info).isNotNull();
assertThat(info.isDefine()).isTrue();
assertTypeEquals(NUMBER_TYPE, info.getType());
}
/**
* Tests that a JSDoc comment in an unexpected place of the code does not
* propagate to following code due to {@link JSDocInfo} aggregation.
*/
public void testJSDocAttachment6() throws Exception {
Node functionNode = parse(
"var a = /** @param {number} index */5;"
+ "/** @return {boolean} */function f(index){}")
.getSecondChild();
assertNode(functionNode).hasType(Token.FUNCTION);
JSDocInfo info = functionNode.getJSDocInfo();
assertThat(info).isNotNull();
assertThat(info.hasParameter("index")).isFalse();
assertThat(info.hasReturnType()).isTrue();
assertTypeEquals(BOOLEAN_TYPE, info.getReturnType());
}
public void testJSDocAttachment7() {
Node varNode = parse("/** */var a;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
// NAME
Node nameNode = varNode.getFirstChild();
assertNode(nameNode).hasType(Token.NAME);
assertThat(nameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment8() {
Node varNode = parse("/** x */var a;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
// NAME
Node nameNode = varNode.getFirstChild();
assertNode(nameNode).hasType(Token.NAME);
assertThat(nameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment9() {
Node varNode = parse("/** \n x */var a;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
// NAME
Node nameNode = varNode.getFirstChild();
assertNode(nameNode).hasType(Token.NAME);
assertThat(nameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment10() {
Node varNode = parse("/** x\n */var a;").getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
// NAME
Node nameNode = varNode.getFirstChild();
assertNode(nameNode).hasType(Token.NAME);
assertThat(nameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment11() {
Node varNode =
parse("/** @type {{x : number, 'y' : string, z}} */var a;")
.getFirstChild();
// VAR
assertNode(varNode).hasType(Token.VAR);
JSDocInfo info = varNode.getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(
createRecordTypeBuilder()
.addProperty("x", NUMBER_TYPE, null)
.addProperty("y", STRING_TYPE, null)
.addProperty("z", UNKNOWN_TYPE, null)
.build(),
info.getType());
// NAME
Node nameNode = varNode.getFirstChild();
assertNode(nameNode).hasType(Token.NAME);
assertThat(nameNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment12() {
Node varNode =
parse("var a = {/** @type {Object} */ b: c};")
.getFirstChild();
Node objectLitNode = varNode.getFirstFirstChild();
assertNode(objectLitNode).hasType(Token.OBJECTLIT);
assertThat(objectLitNode.getFirstChild().getJSDocInfo()).isNotNull();
}
public void testJSDocAttachment13() {
Node varNode = parse("/** foo */ var a;").getFirstChild();
assertThat(varNode.getJSDocInfo()).isNotNull();
}
public void testJSDocAttachment14() {
Node varNode = parse("/** */ var a;").getFirstChild();
assertThat(varNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment15() {
Node varNode = parse("/** \n * \n */ var a;").getFirstChild();
assertThat(varNode.getJSDocInfo()).isNull();
}
public void testJSDocAttachment16() {
Node exprCall =
parse("/** @private */ x(); function f() {};").getFirstChild();
assertNode(exprCall).hasType(Token.EXPR_RESULT);
assertThat(exprCall.getNext().getJSDocInfo()).isNull();
assertThat(exprCall.getFirstChild().getJSDocInfo()).isNotNull();
}
public void testJSDocAttachment17() {
Node fn =
parse(
"function f() { " +
" return /** @type {string} */ (g(1 /** @desc x */));" +
"};").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
Node cast = fn.getLastChild().getFirstFirstChild();
assertNode(cast).hasType(Token.CAST);
}
public void testJSDocAttachment18() {
Node fn =
parse(
"function f() { " +
" var x = /** @type {string} */ (y);" +
"};").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
Node cast = fn.getLastChild().getFirstFirstChild().getFirstChild();
assertNode(cast).hasType(Token.CAST);
}
public void testJSDocAttachment19() {
Node fn =
parse(
"function f() { " +
" /** @type {string} */" +
" return;" +
"};").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
Node ret = fn.getLastChild().getFirstChild();
assertNode(ret).hasType(Token.RETURN);
assertThat(ret.getJSDocInfo()).isNotNull();
}
public void testJSDocAttachment20() {
Node fn =
parse(
"function f() { " +
" /** @type {string} */" +
" if (true) return;" +
"};").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
Node ret = fn.getLastChild().getFirstChild();
assertNode(ret).hasType(Token.IF);
assertThat(ret.getJSDocInfo()).isNotNull();
}
public void testJSDocAttachment21() {
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
expectFeatures(Feature.CONST_DECLARATIONS);
parse("/** @param {string} x */ const f = function() {};");
expectFeatures(Feature.LET_DECLARATIONS);
parse("/** @param {string} x */ let f = function() {};");
}
// Tests that JSDoc gets attached to the children of export nodes, and there are no warnings.
// See https://github.com/google/closure-compiler/issues/781
public void testJSDocAttachment22() {
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;
expectFeatures(Feature.MODULES);
Node n = parse("/** @param {string} x */ export function f(x) {};");
Node export = n.getFirstFirstChild();
assertNode(export).hasType(Token.EXPORT);
assertThat(export.getJSDocInfo()).isNull();
assertThat(export.getFirstChild().getJSDocInfo()).isNotNull();
assertThat(export.getFirstChild().getJSDocInfo().hasParameter("x")).isTrue();
}
public void testInlineJSDocAttachment1() {
Node fn = parse("function f(/** string */ x) {}").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
JSDocInfo info = fn.getSecondChild().getFirstChild().getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(STRING_TYPE, info.getType());
}
public void testInlineJSDocAttachment2() {
Node fn = parse(
"function f(/** ? */ x) {}").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
JSDocInfo info = fn.getSecondChild().getFirstChild().getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(UNKNOWN_TYPE, info.getType());
}
public void testInlineJSDocAttachment3() {
parse("function f(/** @type {string} */ x) {}");
}
public void testInlineJSDocAttachment4() {
parse(
"function f(/**\n" +
" * @type {string}\n" +
" */ x) {}");
}
public void testInlineJSDocAttachment5() {
Node vardecl = parse("var /** string */ x = 'asdf';").getFirstChild();
JSDocInfo info = vardecl.getFirstChild().getJSDocInfo();
assertThat(info).isNotNull();
assertThat(info.hasType()).isTrue();
assertTypeEquals(STRING_TYPE, info.getType());
}
public void testInlineJSDocAttachment6() {
Node fn = parse("function f(/** {attr: number} */ x) {}").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
JSDocInfo info = fn.getSecondChild().getFirstChild().getJSDocInfo();
assertThat(info).isNotNull();
assertTypeEquals(
createRecordTypeBuilder().addProperty("attr", NUMBER_TYPE, null).build(), info.getType());
}
public void testInlineJSDocWithOptionalType() {
Node fn = parse("function f(/** string= */ x) {}").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
JSDocInfo info = fn.getSecondChild().getFirstChild().getJSDocInfo();
assertThat(info.getType().isOptionalArg()).isTrue();
}
public void testInlineJSDocWithVarArgs() {
Node fn = parse("function f(/** ...string */ x) {}").getFirstChild();
assertNode(fn).hasType(Token.FUNCTION);
JSDocInfo info = fn.getSecondChild().getFirstChild().getJSDocInfo();
assertThat(info.getType().isVarArgs()).isTrue();
}
public void testIncorrectJSDocDoesNotAlterJSParsing1() throws Exception {
assertNodeEquality(
parse("var a = [1,2]"),
parseWarning("/** @type {Array<number} */var a = [1,2]",
MISSING_GT_MESSAGE));
}
public void testIncorrectJSDocDoesNotAlterJSParsing2() throws Exception {
assertNodeEquality(
parse("var a = [1,2]"),
parseWarning("/** @type {Array.<number}*/var a = [1,2]",
MISSING_GT_MESSAGE));
}
public void testIncorrectJSDocDoesNotAlterJSParsing3() throws Exception {
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parseWarning("/** @param {Array.<number} nums */" +
"C.prototype.say=function(nums) {alert(nums.join(','));};",
MISSING_GT_MESSAGE));
}
public void testIncorrectJSDocDoesNotAlterJSParsing4() throws Exception {
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parse("/** @return {boolean} */" +
"C.prototype.say=function(nums) {alert(nums.join(','));};"));
}
public void testIncorrectJSDocDoesNotAlterJSParsing5() throws Exception {
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parse("/** @param {boolean} this is some string*/" +
"C.prototype.say=function(nums) {alert(nums.join(','));};"));
}
public void testIncorrectJSDocDoesNotAlterJSParsing6() throws Exception {
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parseWarning(
"/** @param {bool!*%E$} */"
+ "C.prototype.say=function(nums) {alert(nums.join(','));};",
"Bad type annotation. expected closing }" + BAD_TYPE_WIKI_LINK,
"Bad type annotation. expecting a variable name in a @param tag."
+ BAD_TYPE_WIKI_LINK));
}
public void testIncorrectJSDocDoesNotAlterJSParsing7() throws Exception {
isIdeMode = true;
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parseWarning("/** @see */" +
"C.prototype.say=function(nums) {alert(nums.join(','));};",
"@see tag missing description"));
}
public void testIncorrectJSDocDoesNotAlterJSParsing8() throws Exception {
isIdeMode = true;
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parseWarning("/** @author */" +
"C.prototype.say=function(nums) {alert(nums.join(','));};",
"@author tag missing author"));
}
public void testIncorrectJSDocDoesNotAlterJSParsing9() throws Exception {
assertNodeEquality(
parse("C.prototype.say=function(nums) {alert(nums.join(','));};"),
parseWarning("/** @someillegaltag */" +
"C.prototype.say=function(nums) {alert(nums.join(','));};",
"illegal use of unknown JSDoc tag \"someillegaltag\";"
+ " ignoring it"));
}
public void testMisplacedDescAnnotation_noWarning() {
parse("/** @desc Foo. */ var MSG_BAR = goog.getMsg('hello');");
parse("/** @desc Foo. */ x.y.z.MSG_BAR = goog.getMsg('hello');");