-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JsDocInfoParserTest.java
4546 lines (3743 loc) · 158 KB
/
JsDocInfoParserTest.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 2007 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 com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.SourceFile;
import com.google.javascript.jscomp.parsing.Config.JsDocParsing;
import com.google.javascript.jscomp.parsing.Config.LanguageMode;
import com.google.javascript.jscomp.parsing.Config.RunMode;
import com.google.javascript.jscomp.parsing.Config.SourceLocationInformation;
import com.google.javascript.jscomp.parsing.ParserRunner.ParseResult;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfo.Marker;
import com.google.javascript.rhino.JSDocInfo.Visibility;
import com.google.javascript.rhino.JSDocInfoBuilder;
import com.google.javascript.rhino.JSTypeExpression;
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.jstype.JSType;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.TemplateType;
import com.google.javascript.rhino.testing.BaseJSTypeTestCase;
import com.google.javascript.rhino.testing.TestErrorReporter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class JsDocInfoParserTest extends BaseJSTypeTestCase {
private Set<String> extraAnnotations;
private Set<String> extraSuppressions;
private JSDocInfoBuilder fileLevelJsDocBuilder = null;
private static final String MISSING_TYPE_DECL_WARNING_TEXT =
"Missing type declaration.";
@Override
public void setUp() throws Exception {
super.setUp();
fileLevelJsDocBuilder = null;
extraAnnotations = new HashSet<>(ParserRunner.createConfig(
LanguageMode.ECMASCRIPT3, null)
.annotationNames.keySet());
extraSuppressions = new HashSet<>(ParserRunner.createConfig(
LanguageMode.ECMASCRIPT3, null).suppressionNames);
extraSuppressions.add("x");
extraSuppressions.add("y");
extraSuppressions.add("z");
}
public void testParseTypeViaStatic1() throws Exception {
Node typeNode = parseType("null");
assertTypeEquals(NULL_TYPE, typeNode);
}
public void testParseTypeViaStatic2() throws Exception {
Node typeNode = parseType("string");
assertTypeEquals(STRING_TYPE, typeNode);
}
public void testParseTypeViaStatic3() throws Exception {
Node typeNode = parseType("!Date");
assertTypeEquals(DATE_TYPE, typeNode);
}
public void testParseTypeViaStatic4() throws Exception {
Node typeNode = parseType("boolean|string");
assertTypeEquals(createUnionType(BOOLEAN_TYPE, STRING_TYPE), typeNode);
}
public void testParseInvalidTypeViaStatic() throws Exception {
Node typeNode = parseType("sometype.<anothertype");
assertThat(typeNode).isNull();
}
public void testParseInvalidTypeViaStatic2() throws Exception {
Node typeNode = parseType("");
assertThat(typeNode).isNull();
}
public void testParseNamedType1() throws Exception {
assertThat(parse("@type {null}", "Unexpected end of file")).isNull();
}
public void testParseNamedType2() throws Exception {
JSDocInfo info =
parse("@type null*/", "Bad type annotation. Type annotations should have curly braces.");
assertTypeEquals(NULL_TYPE, info.getType());
}
public void testParseNamedType3() throws Exception {
JSDocInfo info = parse("@type {string}*/");
assertTypeEquals(STRING_TYPE, info.getType());
}
public void testParseNamedType4() throws Exception {
// Multi-line @type.
JSDocInfo info = parse("@type \n {string}*/");
assertTypeEquals(STRING_TYPE, info.getType());
}
public void testParseNamedType5() throws Exception {
JSDocInfo info = parse("@type {!goog.\nBar}*/");
assertTypeEquals(
registry.createNamedType("goog.Bar", null, -1, -1),
info.getType());
}
public void testParseNamedType6() throws Exception {
JSDocInfo info = parse("@type {!goog.\n * Bar.\n * Baz}*/");
assertTypeEquals(
registry.createNamedType("goog.Bar.Baz", null, -1, -1),
info.getType());
}
public void testParseNamedTypeError1() throws Exception {
// To avoid parsing ambiguities, type names must end in a '.' to
// get the continuation behavior.
parse("@type {!goog\n * .Bar} */",
"Bad type annotation. expected closing }");
}
public void testParseNamedTypeError2() throws Exception {
parse("@type {!goog.\n * Bar\n * .Baz} */",
"Bad type annotation. expected closing }");
}
public void testParseNamespaceType1() throws Exception {
JSDocInfo info = parse("@type {goog.}*/");
assertTypeEquals(
registry.createNamedType("goog.", null, -1, -1),
info.getType());
}
public void testTypedefType1() throws Exception {
JSDocInfo info = parse("@typedef {string} */");
assertThat(info.hasTypedefType()).isTrue();
assertTypeEquals(STRING_TYPE, info.getTypedefType());
}
public void testTypedefType2() throws Exception {
JSDocInfo info = parse("@typedef \n {string}*/");
assertThat(info.hasTypedefType()).isTrue();
assertTypeEquals(STRING_TYPE, info.getTypedefType());
}
public void testTypedefType3() throws Exception {
JSDocInfo info = parse("@typedef \n {(string|number)}*/");
assertThat(info.hasTypedefType()).isTrue();
assertTypeEquals(createUnionType(NUMBER_TYPE, STRING_TYPE), info.getTypedefType());
}
public void testParseStringType1() throws Exception {
assertTypeEquals(STRING_TYPE, parse("@type {string}*/").getType());
}
public void testParseStringType2() throws Exception {
assertTypeEquals(STRING_OBJECT_TYPE, parse("@type {!String}*/").getType());
}
public void testParseBooleanType1() throws Exception {
assertTypeEquals(BOOLEAN_TYPE, parse("@type {boolean}*/").getType());
}
public void testParseBooleanType2() throws Exception {
assertTypeEquals(
BOOLEAN_OBJECT_TYPE, parse("@type {!Boolean}*/").getType());
}
public void testParseNumberType1() throws Exception {
assertTypeEquals(NUMBER_TYPE, parse("@type {number}*/").getType());
}
public void testParseNumberType2() throws Exception {
assertTypeEquals(NUMBER_OBJECT_TYPE, parse("@type {!Number}*/").getType());
}
public void testParseNullType1() throws Exception {
assertTypeEquals(NULL_TYPE, parse("@type {null}*/").getType());
}
public void testParseNullType2() throws Exception {
assertTypeEquals(NULL_TYPE, parse("@type {Null}*/").getType());
}
public void testParseAllType1() throws Exception {
testParseType("*");
}
public void testParseAllType2() throws Exception {
testParseType("*?", "*");
}
public void testParseObjectType() throws Exception {
assertTypeEquals(OBJECT_TYPE, parse("@type {!Object}*/").getType());
}
public void testParseDateType() throws Exception {
assertTypeEquals(DATE_TYPE, parse("@type {!Date}*/").getType());
}
public void testParseFunctionType() throws Exception {
assertTypeEquals(
createNullableType(U2U_CONSTRUCTOR_TYPE),
parse("@type {Function}*/").getType());
}
public void testParseRegExpType() throws Exception {
assertTypeEquals(REGEXP_TYPE, parse("@type {!RegExp}*/").getType());
}
public void testParseErrorTypes() throws Exception {
assertTypeEquals(ERROR_TYPE, parse("@type {!Error}*/").getType());
assertTypeEquals(URI_ERROR_TYPE, parse("@type {!URIError}*/").getType());
assertTypeEquals(EVAL_ERROR_TYPE, parse("@type {!EvalError}*/").getType());
assertTypeEquals(REFERENCE_ERROR_TYPE,
parse("@type {!ReferenceError}*/").getType());
assertTypeEquals(TYPE_ERROR_TYPE, parse("@type {!TypeError}*/").getType());
assertTypeEquals(
RANGE_ERROR_TYPE, parse("@type {!RangeError}*/").getType());
assertTypeEquals(
SYNTAX_ERROR_TYPE, parse("@type {!SyntaxError}*/").getType());
}
public void testParseUndefinedType1() throws Exception {
assertTypeEquals(VOID_TYPE, parse("@type {undefined}*/").getType());
}
public void testParseUndefinedType2() throws Exception {
assertTypeEquals(VOID_TYPE, parse("@type {Undefined}*/").getType());
}
public void testParseUndefinedType3() throws Exception {
assertTypeEquals(VOID_TYPE, parse("@type {void}*/").getType());
}
public void testParseTemplatizedTypeAlternateSyntax() throws Exception {
JSDocInfo info = parse("@type {!Array<number>} */");
assertTypeEquals(createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
}
public void testParseTemplatizedType1() throws Exception {
JSDocInfo info =
parse(
"@type !Array.<number> */",
"Bad type annotation. Type annotations should have curly braces.");
assertTypeEquals(createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
}
public void testParseTemplatizedType2() throws Exception {
JSDocInfo info = parse("@type {!Array.<number>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
}
public void testParseTemplatizedType4() throws Exception {
JSDocInfo info = parse("@type {!Array.<(number|null)>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
createUnionType(NUMBER_TYPE, NULL_TYPE)),
info.getType());
}
public void testParseTemplatizedType5() throws Exception {
JSDocInfo info = parse("@type {!Array.<Array.<(number|null)>>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
createUnionType(NULL_TYPE,
createTemplatizedType(ARRAY_TYPE,
createUnionType(NUMBER_TYPE, NULL_TYPE)))),
info.getType());
}
public void testParseTemplatizedType6() throws Exception {
JSDocInfo info = parse("@type {!Array.<!Array.<(number|null)>>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
createTemplatizedType(ARRAY_TYPE,
createUnionType(NUMBER_TYPE, NULL_TYPE))),
info.getType());
}
public void testParseTemplatizedType7() throws Exception {
JSDocInfo info = parse("@type {!Array.<function():Date>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
registry.createFunctionType(
createUnionType(DATE_TYPE, NULL_TYPE))),
info.getType());
}
public void testParseTemplatizedType8() throws Exception {
JSDocInfo info = parse("@type {!Array.<function():!Date>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
registry.createFunctionType(DATE_TYPE)),
info.getType());
}
public void testParseTemplatizedType9() throws Exception {
JSDocInfo info = parse("@type {!Array.<Date|number>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
createUnionType(DATE_TYPE, NUMBER_TYPE, NULL_TYPE)),
info.getType());
}
public void testParseTemplatizedType10() throws Exception {
JSDocInfo info = parse("@type {!Array.<Date|number|boolean>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE,
createUnionType(DATE_TYPE, NUMBER_TYPE, BOOLEAN_TYPE, NULL_TYPE)),
info.getType());
}
public void testParseTemplatizedType11() throws Exception {
JSDocInfo info = parse("@type {!Object.<number>}*/");
assertTypeEquals(
createTemplatizedType(
OBJECT_TYPE, ImmutableList.of(UNKNOWN_TYPE, NUMBER_TYPE)),
info.getType());
assertTemplatizedTypeEquals(
registry.getObjectElementKey(), NUMBER_TYPE, info.getType());
}
public void testParseTemplatizedType12() throws Exception {
JSDocInfo info = parse("@type {!Object.<string,number>}*/");
assertTypeEquals(
createTemplatizedType(
OBJECT_TYPE, ImmutableList.of(STRING_TYPE, NUMBER_TYPE)),
info.getType());
assertTemplatizedTypeEquals(
registry.getObjectElementKey(), NUMBER_TYPE, info.getType());
assertTemplatizedTypeEquals(
registry.getObjectIndexKey(), STRING_TYPE, info.getType());
}
public void testParseTemplatizedType13() throws Exception {
JSDocInfo info = parse("@type {!Array.<?>} */");
assertTypeEquals(createTemplatizedType(ARRAY_TYPE, UNKNOWN_TYPE), info.getType());
}
public void testParseUnionType2() throws Exception {
JSDocInfo info = parse("@type {boolean|null}*/");
assertTypeEquals(createUnionType(BOOLEAN_TYPE, NULL_TYPE), info.getType());
}
public void testParseUnionType6() throws Exception {
JSDocInfo info = parse("@type {Array.<boolean>|null}*/");
assertTypeEquals(createUnionType(
createTemplatizedType(
ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
}
public void testParseUnionType7() throws Exception {
JSDocInfo info = parse("@type {null|Array.<boolean>}*/");
assertTypeEquals(createUnionType(
createTemplatizedType(
ARRAY_TYPE, BOOLEAN_TYPE), NULL_TYPE), info.getType());
}
public void testParseUnionType10() throws Exception {
parse("@type {string|}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseUnionType12() throws Exception {
parse("@type {()}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseUnionType13() throws Exception {
testParseType("(function(this:Date)|function(this:String):number)", "Function");
}
public void testParseUnionType14() throws Exception {
testParseType(
"(function(...(function(number):boolean)):number)|function(this:String, string):number",
"Function");
}
public void testParseUnionType15() throws Exception {
testParseType("*|number", "*");
}
public void testParseUnionType16() throws Exception {
testParseType("number|*", "*");
}
public void testParseUnionType17() throws Exception {
testParseType("string|number|*", "*");
}
public void testParseUnionType19() throws Exception {
JSDocInfo info = parse("@type {(?)} */");
assertTypeEquals(UNKNOWN_TYPE, info.getType());
}
public void testParseUnionTypeError2() throws Exception {
parse("@type {string||number} */",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseUnknownType1() throws Exception {
testParseType("?");
}
public void testParseUnknownType2() throws Exception {
testParseType("(?|number)", "?");
}
public void testParseUnknownType3() throws Exception {
testParseType("(number|?)", "?");
}
public void testParseFunctionalType1() throws Exception {
testParseType("function (): number");
}
public void testParseFunctionalType2() throws Exception {
testParseType("function (number, string): boolean");
}
public void testParseFunctionalType3() throws Exception {
testParseType(
"function(this:Array)", "function (this:Array): ?");
}
public void testParseFunctionalType4() throws Exception {
testParseType("function (...number): boolean");
}
public void testParseFunctionalType5() throws Exception {
testParseType("function (number, ...string): boolean");
}
public void testParseFunctionalType6() throws Exception {
testParseType(
"function (this:Date, number): (boolean|number|string)");
}
public void testParseFunctionalType7() throws Exception {
testParseType("function()", "function (): ?");
}
public void testParseFunctionalType9() throws Exception {
testParseType(
"function(this:Array,!Date,...(boolean?))",
"function (this:Array, Date, ...(boolean|null)): ?");
}
public void testParseFunctionalType10() throws Exception {
testParseType(
"function(...(Object?)):boolean?",
"function (...(Object|null)): (boolean|null)");
}
public void testParseFunctionalType12() throws Exception {
testParseType(
"function(...)",
"function (...?): ?");
}
public void testParseFunctionalType13() throws Exception {
testParseType(
"function(...): void",
"function (...?): undefined");
}
public void testParseFunctionalType14() throws Exception {
testParseType("function (*, string, number): boolean");
}
public void testParseFunctionalType15() throws Exception {
testParseType("function (?, string): boolean");
}
public void testParseFunctionalType16() throws Exception {
testParseType("function (string, ?): ?");
}
public void testParseFunctionalType17() throws Exception {
testParseType("(function (?): ?|number)");
}
public void testParseFunctionalType18() throws Exception {
testParseType("function (?): (?|number)", "function (?): ?");
}
public void testParseFunctionalType19() throws Exception {
testParseType(
"function(...?): void",
"function (...?): undefined");
}
public void testStructuralConstructor() throws Exception {
JSType type = testParseType(
"function (new:Object)", "function (new:Object): ?");
assertThat(type.isConstructor()).isTrue();
assertThat(type.isNominalConstructor()).isFalse();
}
public void testStructuralConstructor2() throws Exception {
JSType type = testParseType(
"function (new:?)",
// toString skips unknowns, but isConstructor reveals the truth.
"function (): ?");
assertThat(type.isConstructor()).isTrue();
assertThat(type.isNominalConstructor()).isFalse();
}
public void testStructuralConstructor3() throws Exception {
resolve(parse("@type {function (new:*)} */").getType(),
"constructed type must be an object type");
}
public void testNominalConstructor() throws Exception {
ObjectType type = testParseType("Array", "(Array|null)").dereference();
assertThat(type.getConstructor().isNominalConstructor()).isTrue();
}
public void testBug1419535() throws Exception {
parse("@type {function(Object, string, *)?} */");
parse("@type {function(Object, string, *)|null} */");
}
public void testIssue477() throws Exception {
parse("@type {function} */", "Bad type annotation. missing opening (");
}
public void testMalformedThisAnnotation() throws Exception {
parse(
"@this */",
"Bad type annotation. Type annotations should have curly braces.",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseFunctionalTypeError1() throws Exception {
parse("@type {function number):string}*/",
"Bad type annotation. missing opening (");
}
public void testParseFunctionalTypeError2() throws Exception {
parse("@type {function( number}*/",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError3() throws Exception {
parse("@type {function(...[number], string)}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseFunctionalTypeError4() throws Exception {
parse("@type {function(string, ...number, boolean):string}*/",
"Bad type annotation. variable length argument must be last");
}
public void testParseFunctionalTypeError5() throws Exception {
parse("@type {function (thi:Array)}*/",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError6() throws Exception {
resolve(parse("@type {function (this:number)}*/").getType(),
"this type must be an object type");
}
public void testParseFunctionalTypeError7() throws Exception {
parse("@type {function(...[number)}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseFunctionalTypeError8() throws Exception {
parse("@type {function(...number])}*/",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError9() throws Exception {
parse("@type {function (new:Array, this:Object)} */",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError10() throws Exception {
parse("@type {function (this:Array, new:Object)} */",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError11() throws Exception {
parse("@type {function (Array, new:Object)} */",
"Bad type annotation. missing closing )");
}
public void testParseFunctionalTypeError12() throws Exception {
resolve(parse("@type {function (new:number)}*/").getType(),
"constructed type must be an object type");
}
public void testParseFunctionalTypeError13() throws Exception {
parse("@type {function (...[number]): boolean} */",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseFunctionalTypeError14() throws Exception {
parse("@type {function (number, ...[string]): boolean} */",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseFunctionalType8() throws Exception {
parse("@type {function(this:Array,...[boolean])} */",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseArrayTypeError1() throws Exception {
parse("@type {[number}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseArrayTypeError2() throws Exception {
parse("@type {number]}*/",
"Bad type annotation. expected closing }");
}
public void testParseArrayTypeError3() throws Exception {
parse("@type {[(number,boolean,Object?])]}*/",
"Bad type annotation. type not recognized due to syntax error");
}
public void testParseArrayTypeError5() throws Exception {
parse("@type {[Object]}*/",
"Bad type annotation. type not recognized due to syntax error");
}
private JSType testParseType(String type) throws Exception {
return testParseType(type, type);
}
private JSType testParseType(
String type, String typeExpected) throws Exception {
JSDocInfo info = parse("@type {" + type + "}*/");
assertThat(info).isNotNull();
assertThat(info.hasType()).isTrue();
JSType actual = resolve(info.getType());
assertThat(actual.toString()).isEqualTo(typeExpected);
return actual;
}
public void testParseNullableModifiers1() throws Exception {
JSDocInfo info = parse("@type {string?}*/");
assertTypeEquals(createNullableType(STRING_TYPE), info.getType());
}
public void testParseNullableModifiers2() throws Exception {
JSDocInfo info = parse("@type {!Array.<string?>}*/");
assertTypeEquals(
createTemplatizedType(
ARRAY_TYPE, createUnionType(STRING_TYPE, NULL_TYPE)),
info.getType());
}
public void testParseNullableModifiers3() throws Exception {
JSDocInfo info = parse("@type {Array.<boolean>?}*/");
assertTypeEquals(
createNullableType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE)),
info.getType());
}
public void testParseNullableModifiers7() throws Exception {
JSDocInfo info = parse("@type {string?|boolean}*/");
assertTypeEquals(
createUnionType(createNullableType(STRING_TYPE), BOOLEAN_TYPE),
info.getType());
}
public void testParseNullableModifiers8() throws Exception {
JSDocInfo info = parse("@type {string|boolean?}*/");
assertTypeEquals(
createUnionType(STRING_TYPE, createNullableType(BOOLEAN_TYPE)),
info.getType());
}
public void testParseNullableModifiers9() throws Exception {
JSDocInfo info = parse("@type {foo.Hello.World?}*/");
assertTypeEquals(
createNullableType(
registry.createNamedType(
"foo.Hello.World", null, -1, -1)),
info.getType());
}
public void testParseOptionalModifier() throws Exception {
JSDocInfo info = parse("@type {function(number=)}*/");
assertTypeEquals(
registry.createFunctionType(
UNKNOWN_TYPE, registry.createOptionalParameters(NUMBER_TYPE)),
info.getType());
}
public void testParseNewline1() throws Exception {
JSDocInfo info = parse("@type {string\n* }\n*/");
assertTypeEquals(STRING_TYPE, info.getType());
}
public void testParseNewline2() throws Exception {
JSDocInfo info = parse("@type {!Array.<\n* number\n* >} */");
assertTypeEquals(createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE), info.getType());
}
public void testParseNewline4() throws Exception {
JSDocInfo info = parse("@type {!Array.<(number|\n* null)>}*/");
assertTypeEquals(
createTemplatizedType(ARRAY_TYPE, createUnionType(NUMBER_TYPE, NULL_TYPE)), info.getType());
}
public void testParseNewline5() throws Exception {
JSDocInfo info = parse("@type {!Array.<function(\n* )\n* :\n* Date>}*/");
assertTypeEquals(
createTemplatizedType(
ARRAY_TYPE, registry.createFunctionType(createUnionType(DATE_TYPE, NULL_TYPE))),
info.getType());
}
public void testParseReturnType1() throws Exception {
JSDocInfo info =
parse("@return {null|string|Array.<boolean>}*/");
assertTypeEquals(
createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
NULL_TYPE, STRING_TYPE),
info.getReturnType());
}
public void testParseReturnType2() throws Exception {
JSDocInfo info = parse("@returns {null|(string|Array.<boolean>)}*/");
assertTypeEquals(
createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
NULL_TYPE, STRING_TYPE),
info.getReturnType());
}
public void testParseReturnType3() throws Exception {
JSDocInfo info = parse("@return {((null|Array.<boolean>|string)|boolean)}*/");
assertTypeEquals(
createUnionType(createTemplatizedType(ARRAY_TYPE, BOOLEAN_TYPE),
NULL_TYPE, STRING_TYPE, BOOLEAN_TYPE),
info.getReturnType());
}
public void testParseThisType1() throws Exception {
JSDocInfo info =
parse("@this {goog.foo.Bar}*/");
assertTypeEquals(
registry.createNamedType("goog.foo.Bar", null, -1, -1),
info.getThisType());
}
public void testParseThisType2() throws Exception {
JSDocInfo info =
parse(
"@this goog.foo.Bar*/",
"Bad type annotation. Type annotations should have curly braces.");
assertTypeEquals(registry.createNamedType("goog.foo.Bar", null, -1, -1), info.getThisType());
}
public void testParseThisType3() throws Exception {
parse(
"@type {number}\n@this {goog.foo.Bar}*/",
"Bad type annotation. type annotation incompatible " + "with other annotations");
}
public void testParseThisType4() throws Exception {
resolve(parse("@this {number}*/").getThisType(), "@this must specify an object type");
}
public void testParseThisType5() throws Exception {
parse("@this {Date|Error}*/");
}
public void testParseThisType6() throws Exception {
resolve(parse("@this {Date|number}*/").getThisType(),
"@this must specify an object type");
}
public void testParseParam1() throws Exception {
JSDocInfo info = parse("@param {number} index*/");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
}
public void testParseParam2() throws Exception {
JSDocInfo info = parse("@param index*/",
MISSING_TYPE_DECL_WARNING_TEXT);
assertThat(info.getParameterCount()).isEqualTo(1);
assertThat(info.getParameterType("index")).isNull();
}
public void testParseParam3() throws Exception {
JSDocInfo info = parse("@param {number} index useful comments*/");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
}
public void testParseParam4() throws Exception {
JSDocInfo info = parse("@param index useful comments*/",
MISSING_TYPE_DECL_WARNING_TEXT);
assertThat(info.getParameterCount()).isEqualTo(1);
assertThat(info.getParameterType("index")).isNull();
}
public void testParseParam5() throws Exception {
// Test for multi-line @param.
JSDocInfo info = parse("@param {number} \n index */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
}
public void testParseParam6() throws Exception {
// Test for multi-line @param.
JSDocInfo info = parse("@param {number} \n * index */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(NUMBER_TYPE, info.getParameterType("index"));
}
public void testParseParam7() throws Exception {
// Optional @param
JSDocInfo info = parse("@param {number=} index */");
assertTypeEquals(
registry.createOptionalType(NUMBER_TYPE),
info.getParameterType("index"));
}
public void testParseParam8() throws Exception {
// Var args @param
JSDocInfo info = parse("@param {...number} index */");
assertTypeEquals(
registry.createOptionalType(NUMBER_TYPE),
info.getParameterType("index"));
}
public void testParseParam9() throws Exception {
parse("@param {...number=} index */",
"Bad type annotation. expected closing }",
"Bad type annotation. expecting a variable name in a @param tag");
}
public void testParseParam10() throws Exception {
parse("@param {...number index */",
"Bad type annotation. expected closing }");
}
public void testParseParam11() throws Exception {
parse("@param {number= index */",
"Bad type annotation. expected closing }");
}
public void testParseParam12() throws Exception {
JSDocInfo info = parse("@param {...number|string} index */");
assertTypeEquals(
registry.createOptionalType(
registry.createUnionType(STRING_TYPE, NUMBER_TYPE)),
info.getParameterType("index"));
}
public void testParseParam13() throws Exception {
JSDocInfo info = parse("@param {...(number|string)} index */");
assertTypeEquals(
registry.createOptionalType(
registry.createUnionType(STRING_TYPE, NUMBER_TYPE)),
info.getParameterType("index"));
}
public void testParseParam14() throws Exception {
JSDocInfo info = parse("@param {string} [index] */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(registry.createOptionalType(STRING_TYPE), info.getParameterType("index"));
}
public void testParseParam15() throws Exception {
JSDocInfo info = parse("@param {string} [index */",
"Bad type annotation. missing closing ]");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(STRING_TYPE, info.getParameterType("index"));
}
public void testParseParam16() throws Exception {
JSDocInfo info = parse("@param {string} index] */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(STRING_TYPE, info.getParameterType("index"));
}
public void testParseParam17() throws Exception {
JSDocInfo info = parse("@param {string=} [index] */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(registry.createOptionalType(STRING_TYPE), info.getParameterType("index"));
}
public void testParseParam18() throws Exception {
JSDocInfo info = parse("@param {...string} [index] */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(registry.createOptionalType(STRING_TYPE), info.getParameterType("index"));
}
public void testParseParam19() throws Exception {
JSDocInfo info = parse("@param {...} [index] */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(registry.createOptionalType(UNKNOWN_TYPE), info.getParameterType("index"));
assertThat(info.getParameterType("index").isVarArgs()).isTrue();
}
public void testParseParam20() throws Exception {
JSDocInfo info = parse("@param {?=} index */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(UNKNOWN_TYPE, info.getParameterType("index"));
}
public void testParseParam21() throws Exception {
JSDocInfo info = parse("@param {...?} index */");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(UNKNOWN_TYPE, info.getParameterType("index"));
assertThat(info.getParameterType("index").isVarArgs()).isTrue();
}
public void testParseParam22() throws Exception {
JSDocInfo info = parse("@param {string} .index */", "invalid param name \".index\"");
assertThat(info).isNull();
}
public void testParseParam23() throws Exception {
JSDocInfo info = parse("@param {string} index. */", "invalid param name \"index.\"");
assertThat(info).isNull();
}
public void testParseParam24() throws Exception {
JSDocInfo info = parse("@param {string} foo.bar */", "invalid param name \"foo.bar\"");
assertThat(info).isNull();
}
public void testParseParam25() throws Exception {
JSDocInfo info = parse(
"@param {string} foo.bar\n * @param {string} baz */", "invalid param name \"foo.bar\"");
assertThat(info.getParameterCount()).isEqualTo(1);
assertTypeEquals(STRING_TYPE, info.getParameterType("baz"));
}
public void testParseParam26() throws Exception {
JSDocInfo info = parse(
"@param {{a: number, b: number}} {a, b}\n*/",
"Bad type annotation. expecting a variable name in a @param tag");
assertThat(info).isNull();
}
public void testParseParam27() throws Exception {
JSDocInfo info = parse(
"@param {{a: number, b: number}} '{a, b}'\n*/", "invalid param name \"'\"");
assertThat(info).isNull();
}
public void testParseThrows1() throws Exception {
JSDocInfo info = parse("@throws {number} Some number */");
assertThat(info.getThrownTypes()).hasSize(1);
assertTypeEquals(NUMBER_TYPE, info.getThrownTypes().get(0));
}
public void testParseThrows2() throws Exception {
JSDocInfo info = parse("@throws {number} Some number\n "
+ "*@throws {String} A string */");
assertThat(info.getThrownTypes()).hasSize(2);
assertTypeEquals(NUMBER_TYPE, info.getThrownTypes().get(0));
}
public void testParseRecordType1() throws Exception {
parseFull("/** @param {{x}} n\n*/");
}
public void testParseRecordType2() throws Exception {
parseFull("/** @param {{z, y}} n\n*/");