-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BaseJSTypeTestCase.java
612 lines (578 loc) · 25.2 KB
/
BaseJSTypeTestCase.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
/*
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1997-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Bob Jervis
* Google Inc.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which
* case the provisions of the GPL are applicable instead of those above. If
* you wish to allow use of your version of this file only under the terms of
* the GPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replacing
* them with the notice and other provisions required by the GPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the GPL.
*
* ***** END LICENSE BLOCK ***** */
package com.google.javascript.rhino.testing;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.jstype.FunctionBuilder;
import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.RecordTypeBuilder;
import com.google.javascript.rhino.jstype.TemplatizedType;
import junit.framework.TestCase;
public abstract class BaseJSTypeTestCase extends TestCase {
protected static final Joiner LINE_JOINER = Joiner.on('\n');
protected JSTypeRegistry registry;
protected TestErrorReporter errorReporter;
protected JSType ALL_TYPE;
protected ObjectType NO_OBJECT_TYPE;
protected ObjectType NO_TYPE;
protected ObjectType NO_RESOLVED_TYPE;
protected FunctionType ARRAY_FUNCTION_TYPE;
protected ObjectType ARRAY_TYPE;
protected JSType BOOLEAN_OBJECT_FUNCTION_TYPE;
protected ObjectType BOOLEAN_OBJECT_TYPE;
protected JSType BOOLEAN_TYPE;
protected ObjectType CHECKED_UNKNOWN_TYPE;
protected JSType DATE_FUNCTION_TYPE;
protected ObjectType DATE_TYPE;
protected JSType ERROR_FUNCTION_TYPE;
protected ObjectType ERROR_TYPE;
protected JSType EVAL_ERROR_FUNCTION_TYPE;
protected ObjectType EVAL_ERROR_TYPE;
protected FunctionType FUNCTION_FUNCTION_TYPE;
protected FunctionType FUNCTION_INSTANCE_TYPE;
protected ObjectType FUNCTION_PROTOTYPE;
protected JSType GREATEST_FUNCTION_TYPE;
protected JSType LEAST_FUNCTION_TYPE;
protected JSType MATH_TYPE;
protected JSType NULL_TYPE;
protected JSType NUMBER_OBJECT_FUNCTION_TYPE;
protected ObjectType NUMBER_OBJECT_TYPE;
protected JSType NUMBER_STRING_BOOLEAN;
protected JSType NUMBER_TYPE;
protected FunctionType OBJECT_FUNCTION_TYPE;
protected JSType NULL_VOID;
protected JSType OBJECT_NUMBER_STRING;
protected JSType OBJECT_NUMBER_STRING_BOOLEAN;
protected JSType OBJECT_PROTOTYPE;
protected ObjectType OBJECT_TYPE;
protected JSType RANGE_ERROR_FUNCTION_TYPE;
protected ObjectType RANGE_ERROR_TYPE;
protected JSType REFERENCE_ERROR_FUNCTION_TYPE;
protected ObjectType REFERENCE_ERROR_TYPE;
protected JSType REGEXP_FUNCTION_TYPE;
protected ObjectType REGEXP_TYPE;
protected JSType STRING_OBJECT_FUNCTION_TYPE;
protected ObjectType STRING_OBJECT_TYPE;
protected JSType STRING_TYPE;
protected JSType SYNTAX_ERROR_FUNCTION_TYPE;
protected ObjectType SYNTAX_ERROR_TYPE;
protected JSType TYPE_ERROR_FUNCTION_TYPE;
protected ObjectType TYPE_ERROR_TYPE;
protected FunctionType U2U_CONSTRUCTOR_TYPE;
protected FunctionType U2U_FUNCTION_TYPE;
protected ObjectType UNKNOWN_TYPE;
protected JSType URI_ERROR_FUNCTION_TYPE;
protected ObjectType URI_ERROR_TYPE;
protected JSType VOID_TYPE;
protected int NATIVE_PROPERTIES_COUNT;
@Override
protected void setUp() throws Exception {
super.setUp();
errorReporter = new TestErrorReporter(null, null);
registry = new JSTypeRegistry(errorReporter, ImmutableSet.of("forwardDeclared"));
initTypes();
}
protected void initTypes() {
ALL_TYPE =
registry.getNativeType(JSTypeNative.ALL_TYPE);
NO_OBJECT_TYPE =
registry.getNativeObjectType(JSTypeNative.NO_OBJECT_TYPE);
NO_TYPE =
registry.getNativeObjectType(JSTypeNative.NO_TYPE);
NO_RESOLVED_TYPE =
registry.getNativeObjectType(JSTypeNative.NO_RESOLVED_TYPE);
ARRAY_FUNCTION_TYPE =
registry.getNativeFunctionType(JSTypeNative.ARRAY_FUNCTION_TYPE);
ARRAY_TYPE =
registry.getNativeObjectType(JSTypeNative.ARRAY_TYPE);
BOOLEAN_OBJECT_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.BOOLEAN_OBJECT_FUNCTION_TYPE);
BOOLEAN_OBJECT_TYPE =
registry.getNativeObjectType(JSTypeNative.BOOLEAN_OBJECT_TYPE);
BOOLEAN_TYPE =
registry.getNativeType(JSTypeNative.BOOLEAN_TYPE);
CHECKED_UNKNOWN_TYPE =
registry.getNativeObjectType(JSTypeNative.CHECKED_UNKNOWN_TYPE);
DATE_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.DATE_FUNCTION_TYPE);
DATE_TYPE =
registry.getNativeObjectType(JSTypeNative.DATE_TYPE);
ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.ERROR_FUNCTION_TYPE);
ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.ERROR_TYPE);
EVAL_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.EVAL_ERROR_FUNCTION_TYPE);
EVAL_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.EVAL_ERROR_TYPE);
FUNCTION_FUNCTION_TYPE =
registry.getNativeFunctionType(JSTypeNative.FUNCTION_FUNCTION_TYPE);
FUNCTION_INSTANCE_TYPE =
registry.getNativeFunctionType(JSTypeNative.FUNCTION_INSTANCE_TYPE);
FUNCTION_PROTOTYPE =
registry.getNativeObjectType(JSTypeNative.FUNCTION_PROTOTYPE);
GREATEST_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.GREATEST_FUNCTION_TYPE);
LEAST_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.LEAST_FUNCTION_TYPE);
NULL_TYPE =
registry.getNativeType(JSTypeNative.NULL_TYPE);
NUMBER_OBJECT_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.NUMBER_OBJECT_FUNCTION_TYPE);
NUMBER_OBJECT_TYPE =
registry.getNativeObjectType(JSTypeNative.NUMBER_OBJECT_TYPE);
NUMBER_STRING_BOOLEAN =
registry.getNativeType(JSTypeNative.NUMBER_STRING_BOOLEAN);
NUMBER_TYPE =
registry.getNativeType(JSTypeNative.NUMBER_TYPE);
OBJECT_FUNCTION_TYPE =
registry.getNativeFunctionType(JSTypeNative.OBJECT_FUNCTION_TYPE);
NULL_VOID =
registry.getNativeType(JSTypeNative.NULL_VOID);
OBJECT_NUMBER_STRING =
registry.getNativeType(JSTypeNative.OBJECT_NUMBER_STRING);
OBJECT_NUMBER_STRING_BOOLEAN =
registry.getNativeType(JSTypeNative.OBJECT_NUMBER_STRING_BOOLEAN);
OBJECT_PROTOTYPE =
registry.getNativeType(JSTypeNative.OBJECT_PROTOTYPE);
OBJECT_TYPE =
registry.getNativeObjectType(JSTypeNative.OBJECT_TYPE);
RANGE_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.RANGE_ERROR_FUNCTION_TYPE);
RANGE_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.RANGE_ERROR_TYPE);
REFERENCE_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.REFERENCE_ERROR_FUNCTION_TYPE);
REFERENCE_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.REFERENCE_ERROR_TYPE);
REGEXP_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.REGEXP_FUNCTION_TYPE);
REGEXP_TYPE =
registry.getNativeObjectType(JSTypeNative.REGEXP_TYPE);
STRING_OBJECT_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.STRING_OBJECT_FUNCTION_TYPE);
STRING_OBJECT_TYPE =
registry.getNativeObjectType(JSTypeNative.STRING_OBJECT_TYPE);
STRING_TYPE =
registry.getNativeType(JSTypeNative.STRING_TYPE);
SYNTAX_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.SYNTAX_ERROR_FUNCTION_TYPE);
SYNTAX_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.SYNTAX_ERROR_TYPE);
TYPE_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.TYPE_ERROR_FUNCTION_TYPE);
TYPE_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.TYPE_ERROR_TYPE);
U2U_CONSTRUCTOR_TYPE =
registry.getNativeFunctionType(JSTypeNative.U2U_CONSTRUCTOR_TYPE);
U2U_FUNCTION_TYPE =
registry.getNativeFunctionType(JSTypeNative.U2U_FUNCTION_TYPE);
UNKNOWN_TYPE =
registry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
URI_ERROR_FUNCTION_TYPE =
registry.getNativeType(JSTypeNative.URI_ERROR_FUNCTION_TYPE);
URI_ERROR_TYPE =
registry.getNativeObjectType(JSTypeNative.URI_ERROR_TYPE);
VOID_TYPE =
registry.getNativeType(JSTypeNative.VOID_TYPE);
addNativeProperties(registry);
NATIVE_PROPERTIES_COUNT = OBJECT_TYPE.getPropertiesCount();
}
/** Adds a basic set of properties to the native types. */
public static void addNativeProperties(JSTypeRegistry registry) {
JSType booleanType = registry.getNativeType(JSTypeNative.BOOLEAN_TYPE);
JSType numberType = registry.getNativeType(JSTypeNative.NUMBER_TYPE);
JSType stringType = registry.getNativeType(JSTypeNative.STRING_TYPE);
JSType unknownType = registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
ObjectType objectType =
registry.getNativeObjectType(JSTypeNative.OBJECT_TYPE);
ObjectType arrayType =
registry.getNativeObjectType(JSTypeNative.ARRAY_TYPE);
ObjectType dateType =
registry.getNativeObjectType(JSTypeNative.DATE_TYPE);
ObjectType regexpType =
registry.getNativeObjectType(JSTypeNative.REGEXP_TYPE);
ObjectType booleanObjectType =
registry.getNativeObjectType(JSTypeNative.BOOLEAN_OBJECT_TYPE);
ObjectType numberObjectType =
registry.getNativeObjectType(JSTypeNative.NUMBER_OBJECT_TYPE);
ObjectType stringObjectType =
registry.getNativeObjectType(JSTypeNative.STRING_OBJECT_TYPE);
ObjectType objectPrototype = registry
.getNativeFunctionType(JSTypeNative.OBJECT_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, objectPrototype, "constructor", objectType);
addMethod(registry, objectPrototype, "toString", stringType);
addMethod(registry, objectPrototype, "toLocaleString", stringType);
addMethod(registry, objectPrototype, "valueOf", unknownType);
addMethod(registry, objectPrototype, "hasOwnProperty", booleanType);
addMethod(registry, objectPrototype, "isPrototypeOf", booleanType);
addMethod(registry, objectPrototype, "propertyIsEnumerable", booleanType);
ObjectType arrayPrototype = registry
.getNativeFunctionType(JSTypeNative.ARRAY_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, arrayPrototype, "constructor", arrayType);
addMethod(registry, arrayPrototype, "toString", stringType);
addMethod(registry, arrayPrototype, "toLocaleString", stringType);
addMethod(registry, arrayPrototype, "concat", arrayType);
addMethod(registry, arrayPrototype, "join", stringType);
addMethod(registry, arrayPrototype, "pop", unknownType);
addMethod(registry, arrayPrototype, "push", numberType);
addMethod(registry, arrayPrototype, "reverse", arrayType);
addMethod(registry, arrayPrototype, "shift", unknownType);
addMethod(registry, arrayPrototype, "slice", arrayType);
addMethod(registry, arrayPrototype, "sort", arrayType);
addMethod(registry, arrayPrototype, "splice", arrayType);
addMethod(registry, arrayPrototype, "unshift", numberType);
arrayType.defineDeclaredProperty("length", numberType, null);
ObjectType booleanPrototype = registry
.getNativeFunctionType(JSTypeNative.BOOLEAN_OBJECT_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, booleanPrototype, "constructor", booleanObjectType);
addMethod(registry, booleanPrototype, "toString", stringType);
addMethod(registry, booleanPrototype, "valueOf", booleanType);
ObjectType datePrototype = registry
.getNativeFunctionType(JSTypeNative.DATE_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, datePrototype, "constructor", dateType);
addMethod(registry, datePrototype, "toString", stringType);
addMethod(registry, datePrototype, "toDateString", stringType);
addMethod(registry, datePrototype, "toTimeString", stringType);
addMethod(registry, datePrototype, "toLocaleString", stringType);
addMethod(registry, datePrototype, "toLocaleDateString", stringType);
addMethod(registry, datePrototype, "toLocaleTimeString", stringType);
addMethod(registry, datePrototype, "valueOf", numberType);
addMethod(registry, datePrototype, "getTime", numberType);
addMethod(registry, datePrototype, "getFullYear", numberType);
addMethod(registry, datePrototype, "getUTCFullYear", numberType);
addMethod(registry, datePrototype, "getMonth", numberType);
addMethod(registry, datePrototype, "getUTCMonth", numberType);
addMethod(registry, datePrototype, "getDate", numberType);
addMethod(registry, datePrototype, "getUTCDate", numberType);
addMethod(registry, datePrototype, "getDay", numberType);
addMethod(registry, datePrototype, "getUTCDay", numberType);
addMethod(registry, datePrototype, "getHours", numberType);
addMethod(registry, datePrototype, "getUTCHours", numberType);
addMethod(registry, datePrototype, "getMinutes", numberType);
addMethod(registry, datePrototype, "getUTCMinutes", numberType);
addMethod(registry, datePrototype, "getSeconds", numberType);
addMethod(registry, datePrototype, "getUTCSeconds", numberType);
addMethod(registry, datePrototype, "getMilliseconds", numberType);
addMethod(registry, datePrototype, "getUTCMilliseconds", numberType);
addMethod(registry, datePrototype, "getTimezoneOffset", numberType);
addMethod(registry, datePrototype, "setTime", numberType);
addMethod(registry, datePrototype, "setMilliseconds", numberType);
addMethod(registry, datePrototype, "setUTCMilliseconds", numberType);
addMethod(registry, datePrototype, "setSeconds", numberType);
addMethod(registry, datePrototype, "setUTCSeconds", numberType);
addMethod(registry, datePrototype, "setMinutes", numberType);
addMethod(registry, datePrototype, "setUTCMinutes", numberType);
addMethod(registry, datePrototype, "setHours", numberType);
addMethod(registry, datePrototype, "setUTCHours", numberType);
addMethod(registry, datePrototype, "setDate", numberType);
addMethod(registry, datePrototype, "setUTCDate", numberType);
addMethod(registry, datePrototype, "setMonth", numberType);
addMethod(registry, datePrototype, "setUTCMonth", numberType);
addMethod(registry, datePrototype, "setFullYear", numberType);
addMethod(registry, datePrototype, "setUTCFullYear", numberType);
addMethod(registry, datePrototype, "toUTCString", stringType);
addMethod(registry, datePrototype, "toGMTString", stringType);
ObjectType numberPrototype = registry
.getNativeFunctionType(JSTypeNative.NUMBER_OBJECT_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, numberPrototype, "constructor", numberObjectType);
addMethod(registry, numberPrototype, "toString", stringType);
addMethod(registry, numberPrototype, "toLocaleString", stringType);
addMethod(registry, numberPrototype, "valueOf", numberType);
addMethod(registry, numberPrototype, "toFixed", stringType);
addMethod(registry, numberPrototype, "toExponential", stringType);
addMethod(registry, numberPrototype, "toPrecision", stringType);
ObjectType regexpPrototype = registry
.getNativeFunctionType(JSTypeNative.REGEXP_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, regexpPrototype, "constructor", regexpType);
addMethod(registry, regexpPrototype, "exec",
registry.createNullableType(arrayType));
addMethod(registry, regexpPrototype, "test", booleanType);
addMethod(registry, regexpPrototype, "toString", stringType);
regexpType.defineDeclaredProperty("source", stringType, null);
regexpType.defineDeclaredProperty("global", booleanType, null);
regexpType.defineDeclaredProperty("ignoreCase", booleanType, null);
regexpType.defineDeclaredProperty("multiline", booleanType, null);
regexpType.defineDeclaredProperty("lastIndex", numberType, null);
ObjectType stringPrototype = registry
.getNativeFunctionType(JSTypeNative.STRING_OBJECT_FUNCTION_TYPE)
.getPrototype();
addMethod(registry, stringPrototype, "constructor", stringObjectType);
addMethod(registry, stringPrototype, "toString", stringType);
addMethod(registry, stringPrototype, "valueOf", stringType);
addMethod(registry, stringPrototype, "charAt", stringType);
addMethod(registry, stringPrototype, "charCodeAt", numberType);
addMethod(registry, stringPrototype, "concat", stringType);
addMethod(registry, stringPrototype, "indexOf", numberType);
addMethod(registry, stringPrototype, "lastIndexOf", numberType);
addMethod(registry, stringPrototype, "localeCompare", numberType);
addMethod(registry, stringPrototype, "match",
registry.createNullableType(arrayType));
addMethod(registry, stringPrototype, "replace", stringType);
addMethod(registry, stringPrototype, "search", numberType);
addMethod(registry, stringPrototype, "slice", stringType);
addMethod(registry, stringPrototype, "split", arrayType);
addMethod(registry, stringPrototype, "substring", stringType);
addMethod(registry, stringPrototype, "toLowerCase", stringType);
addMethod(registry, stringPrototype, "toLocaleLowerCase", stringType);
addMethod(registry, stringPrototype, "toUpperCase", stringType);
addMethod(registry, stringPrototype, "toLocaleUpperCase", stringType);
stringObjectType.defineDeclaredProperty("length", numberType, null);
}
private static void addMethod(
JSTypeRegistry registry, ObjectType receivingType, String methodName,
JSType returnType) {
receivingType.defineDeclaredProperty(methodName,
new FunctionBuilder(registry).withReturnType(returnType).build(),
null);
}
protected JSType createUnionType(JSType... variants) {
return registry.createUnionType(variants);
}
protected RecordTypeBuilder createRecordTypeBuilder() {
return new RecordTypeBuilder(registry);
}
protected JSType createNullableType(JSType type) {
return registry.createNullableType(type);
}
protected JSType createOptionalType(JSType type) {
return registry.createOptionalType(type);
}
protected TemplatizedType createTemplatizedType(
ObjectType baseType, ImmutableList<JSType> templatizedTypes) {
return registry.createTemplatizedType(baseType, templatizedTypes);
}
protected TemplatizedType createTemplatizedType(
ObjectType baseType, JSType... templatizedType) {
return createTemplatizedType(
baseType, ImmutableList.copyOf(templatizedType));
}
/**
* Asserts that a Node representing a type expression resolves to the
* correct {@code JSType}.
*/
protected void assertTypeEquals(JSType expected, Node actual) {
assertTypeEquals(expected, new JSTypeExpression(actual, ""));
}
/**
* Asserts that a a type expression resolves to the correct {@code JSType}.
*/
protected void assertTypeEquals(JSType expected, JSTypeExpression actual) {
assertEquals(expected, resolve(actual));
}
/**
* Resolves a type expression, expecting the given warnings.
*/
protected JSType resolve(JSTypeExpression n, String... warnings) {
errorReporter.setWarnings(warnings);
return n.evaluate(null, registry);
}
/**
* A definition of all extern types. This should be kept in sync with
* javascript/externs/es3.js. This is used to check that the built-in types
* declared in {@link JSTypeRegistry} have the same type as that in the
* externs. It can also be used for any tests that want to use built-in types
* in their externs.
*/
public static final String ALL_NATIVE_EXTERN_TYPES =
"/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_value\n"
+ " * @return {!Object}\n"
+ " */\n"
+ "function Object(opt_value) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {...*} var_args\n"
+ " */\n"
+ "\n"
+ "function Function(var_args) {}\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {...*} var_args\n"
+ " * @return {!Array.<?>}\n"
+ " * @template T\n"
+ " */\n"
+ "function Array(var_args) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_value\n"
+ " * @return {boolean}\n"
+ " */\n"
+ "function Boolean(opt_value) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_value\n"
+ " * @return {number}\n"
+ " */\n"
+ "function Number(opt_value) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {?=} opt_yr_num\n"
+ " * @param {?=} opt_mo_num\n"
+ " * @param {?=} opt_day_num\n"
+ " * @param {?=} opt_hr_num\n"
+ " * @param {?=} opt_min_num\n"
+ " * @param {?=} opt_sec_num\n"
+ " * @param {?=} opt_ms_num\n"
+ " * @return {string}\n"
+ " */\n"
+ "function Date(opt_yr_num, opt_mo_num, opt_day_num, opt_hr_num,"
+ " opt_min_num, opt_sec_num, opt_ms_num) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_str\n"
+ " * @return {string}\n"
+ " */\n"
+ "function String(opt_str) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_pattern\n"
+ " * @param {*=} opt_flags\n"
+ " * @return {!RegExp}\n"
+ " */\n"
+ "function RegExp(opt_pattern, opt_flags) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!Error}\n"
+ " */\n"
+ "function Error(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!EvalError}\n"
+ " */\n"
+ "function EvalError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!RangeError}\n"
+ " */\n"
+ "function RangeError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!ReferenceError}\n"
+ " */\n"
+ "function ReferenceError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!SyntaxError}\n"
+ " */\n"
+ "function SyntaxError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!TypeError}\n"
+ " */\n"
+ "function TypeError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @constructor\n"
+ " * @extends {Error}\n"
+ " * @param {*=} opt_message\n"
+ " * @param {*=} opt_file\n"
+ " * @param {*=} opt_line\n"
+ " * @return {!URIError}\n"
+ " */\n"
+ "function URIError(opt_message, opt_file, opt_line) {}\n"
+ "\n"
+ "/**\n"
+ " * @param {string} progId\n"
+ " * @param {string=} opt_location\n"
+ " * @constructor\n"
+ " */\n"
+ "function ActiveXObject(progId, opt_location) {}\n";
protected final void assertTypeEquals(JSType a, JSType b) {
Asserts.assertTypeEquals(a, b);
}
protected final void assertTypeEquals(String msg, JSType a, JSType b) {
Asserts.assertTypeEquals(msg, a, b);
}
protected final void assertTypeNotEquals(JSType a, JSType b) {
Asserts.assertTypeNotEquals(a, b);
}
protected final void assertTypeNotEquals(String msg, JSType a, JSType b) {
Asserts.assertTypeNotEquals(msg, a, b);
}
}