Skip to content

Commit 68bdbe0

Browse files
committed
Merge pull request microsoft#7715 from Microsoft/6974AddLibImplementation
6974 add lib implementation
2 parents 9705c89 + 1490547 commit 68bdbe0

346 files changed

Lines changed: 8786 additions & 2988 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Jakefile.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,53 @@ var harnessSources = harnessCoreSources.concat([
165165
return path.join(serverDirectory, f);
166166
}));
167167

168-
var librarySourceMap = [
169-
{ target: "lib.core.d.ts", sources: ["header.d.ts", "core.d.ts"] },
170-
{ target: "lib.dom.d.ts", sources: ["importcore.d.ts", "intl.d.ts", "dom.generated.d.ts"], },
171-
{ target: "lib.webworker.d.ts", sources: ["importcore.d.ts", "intl.d.ts", "webworker.generated.d.ts"], },
172-
{ target: "lib.scriptHost.d.ts", sources: ["importcore.d.ts", "scriptHost.d.ts"], },
173-
{ target: "lib.d.ts", sources: ["header.d.ts", "core.d.ts", "intl.d.ts", "dom.generated.d.ts", "webworker.importscripts.d.ts", "scriptHost.d.ts"], },
174-
{ target: "lib.core.es6.d.ts", sources: ["header.d.ts", "core.d.ts", "es6.d.ts"]},
175-
{ target: "lib.es6.d.ts", sources: ["header.d.ts", "es6.d.ts", "core.d.ts", "intl.d.ts", "dom.generated.d.ts", "dom.es6.d.ts", "webworker.importscripts.d.ts", "scriptHost.d.ts"] },
176-
{ target: "lib.core.es7.d.ts", sources: ["header.d.ts", "core.d.ts", "es6.d.ts", "es7.d.ts"]},
177-
{ target: "lib.es7.d.ts", sources: ["header.d.ts", "es6.d.ts", "es7.d.ts", "core.d.ts", "intl.d.ts", "dom.generated.d.ts", "dom.es6.d.ts", "webworker.importscripts.d.ts", "scriptHost.d.ts"] }
168+
var es2015LibrarySources = [
169+
"es2015.array.d.ts",
170+
"es2015.collection.d.ts",
171+
"es2015.function.d.ts",
172+
"es2015.generator.d.ts",
173+
"es2015.iterable.d.ts",
174+
"es2015.math.d.ts",
175+
"es2015.number.d.ts",
176+
"es2015.object.d.ts",
177+
"es2015.promise.d.ts",
178+
"es2015.proxy.d.ts",
179+
"es2015.reflect.d.ts",
180+
"es2015.regexp.d.ts",
181+
"es2015.string.d.ts",
182+
"es2015.symbol.d.ts",
183+
"es2015.symbol.wellknown.d.ts",
178184
];
179185

186+
var es2015LibrarySourceMap = es2015LibrarySources.map(function(source) {
187+
return { target: "lib." + source, sources: ["header.d.ts", source] };
188+
});
189+
190+
var es2016LibrarySource = [ "es2016.array.include.d.ts" ];
191+
192+
var es2016LibrarySourceMap = es2016LibrarySource.map(function(source) {
193+
return { target: "lib." + source, sources: ["header.d.ts", source] };
194+
})
195+
196+
var hostsLibrarySources = ["dom.generated.d.ts", "webworker.importscripts.d.ts", "scripthost.d.ts"]
197+
198+
var librarySourceMap = [
199+
// Host library
200+
{ target: "lib.dom.d.ts", sources: ["header.d.ts", "dom.generated.d.ts"], },
201+
{ target: "lib.dom.iterable.d.ts", sources: ["header.d.ts", "dom.iterable.d.ts"], },
202+
{ target: "lib.webworker.d.ts", sources: ["header.d.ts", "webworker.generated.d.ts"], },
203+
{ target: "lib.scripthost.d.ts", sources: ["header.d.ts", "scripthost.d.ts"], },
204+
205+
// JavaScript library
206+
{ target: "lib.es5.d.ts", sources: ["header.d.ts", "es5.d.ts"] },
207+
{ target: "lib.es2015.d.ts", sources: ["header.d.ts", "es2015.d.ts"] },
208+
{ target: "lib.es2016.d.ts", sources: ["header.d.ts", "es2016.d.ts"] },
209+
210+
// JavaScript + all host library
211+
{ target: "lib.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(hostsLibrarySources), },
212+
{ target: "lib.full.es2015.d.ts", sources: ["header.d.ts", "es5.d.ts"].concat(es2015LibrarySources, hostsLibrarySources), },
213+
].concat(es2015LibrarySourceMap, es2016LibrarySourceMap);
214+
180215
var libraryTargets = librarySourceMap.map(function (f) {
181216
return path.join(builtLocalDirectory, f.target);
182217
});

src/compiler/checker.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace ts {
138138

139139
const globals: SymbolTable = {};
140140

141-
let globalESSymbolConstructorSymbol: Symbol;
141+
let getGlobalESSymbolConstructorSymbol: () => Symbol;
142142

143143
let getGlobalPromiseConstructorSymbol: () => Symbol;
144144

@@ -150,14 +150,19 @@ namespace ts {
150150
let globalNumberType: ObjectType;
151151
let globalBooleanType: ObjectType;
152152
let globalRegExpType: ObjectType;
153-
let globalTemplateStringsArrayType: ObjectType;
154-
let globalESSymbolType: ObjectType;
155-
let globalIterableType: GenericType;
156-
let globalIteratorType: GenericType;
157-
let globalIterableIteratorType: GenericType;
158-
159153
let anyArrayType: Type;
160154
let anyReadonlyArrayType: Type;
155+
156+
// The library files are only loaded when the feature is used.
157+
// This allows users to just specify library files they want to used through --lib
158+
// and they will not get an error from not having unrelated library files
159+
let getGlobalTemplateStringsArrayType: () => ObjectType;
160+
161+
let getGlobalESSymbolType: () => ObjectType;
162+
let getGlobalIterableType: () => GenericType;
163+
let getGlobalIteratorType: () => GenericType;
164+
let getGlobalIterableIteratorType: () => GenericType;
165+
161166
let getGlobalClassDecoratorType: () => ObjectType;
162167
let getGlobalParameterDecoratorType: () => ObjectType;
163168
let getGlobalPropertyDecoratorType: () => ObjectType;
@@ -3973,7 +3978,7 @@ namespace ts {
39733978
type = globalBooleanType;
39743979
}
39753980
else if (type.flags & TypeFlags.ESSymbol) {
3976-
type = globalESSymbolType;
3981+
type = getGlobalESSymbolType();
39773982
}
39783983
return type;
39793984
}
@@ -4744,10 +4749,6 @@ namespace ts {
47444749
return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol);
47454750
}
47464751

4747-
function getGlobalESSymbolConstructorSymbol() {
4748-
return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"));
4749-
}
4750-
47514752
/**
47524753
* Creates a TypeReference for a generic `TypedPropertyDescriptor<T>`.
47534754
*/
@@ -4766,11 +4767,11 @@ namespace ts {
47664767
}
47674768

47684769
function createIterableType(elementType: Type): Type {
4769-
return createTypeFromGenericGlobalType(globalIterableType, [elementType]);
4770+
return createTypeFromGenericGlobalType(getGlobalIterableType(), [elementType]);
47704771
}
47714772

47724773
function createIterableIteratorType(elementType: Type): Type {
4773-
return createTypeFromGenericGlobalType(globalIterableIteratorType, [elementType]);
4774+
return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(), [elementType]);
47744775
}
47754776

47764777
function createArrayType(elementType: Type): Type {
@@ -10434,7 +10435,7 @@ namespace ts {
1043410435
return getEffectiveDecoratorArgumentType(<Decorator>node, argIndex);
1043510436
}
1043610437
else if (argIndex === 0 && node.kind === SyntaxKind.TaggedTemplateExpression) {
10437-
return globalTemplateStringsArrayType;
10438+
return getGlobalTemplateStringsArrayType();
1043810439
}
1043910440

1044010441
// This is not a synthetic argument, so we return 'undefined'
@@ -14198,7 +14199,7 @@ namespace ts {
1419814199
if (!typeAsIterable.iterableElementType) {
1419914200
// As an optimization, if the type is instantiated directly using the globalIterableType (Iterable<number>),
1420014201
// then just grab its type argument.
14201-
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIterableType) {
14202+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === getGlobalIterableType()) {
1420214203
typeAsIterable.iterableElementType = (<GenericType>type).typeArguments[0];
1420314204
}
1420414205
else {
@@ -14244,7 +14245,7 @@ namespace ts {
1424414245
if (!typeAsIterator.iteratorElementType) {
1424514246
// As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator<number>),
1424614247
// then just grab its type argument.
14247-
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIteratorType) {
14248+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === getGlobalIteratorType()) {
1424814249
typeAsIterator.iteratorElementType = (<GenericType>type).typeArguments[0];
1424914250
}
1425014251
else {
@@ -14288,7 +14289,7 @@ namespace ts {
1428814289

1428914290
// As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator<number>),
1429014291
// then just grab its type argument.
14291-
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === globalIterableIteratorType) {
14292+
if ((type.flags & TypeFlags.Reference) && (<GenericType>type).target === getGlobalIterableIteratorType()) {
1429214293
return (<GenericType>type).typeArguments[0];
1429314294
}
1429414295

@@ -16898,12 +16899,14 @@ namespace ts {
1689816899
globalNumberType = getGlobalType("Number");
1689916900
globalBooleanType = getGlobalType("Boolean");
1690016901
globalRegExpType = getGlobalType("RegExp");
16902+
1690116903
jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
1690216904
getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator"));
1690316905
getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator"));
1690416906
getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator"));
1690516907
getGlobalParameterDecoratorType = memoize(() => getGlobalType("ParameterDecorator"));
1690616908
getGlobalTypedPropertyDescriptorType = memoize(() => getGlobalType("TypedPropertyDescriptor", /*arity*/ 1));
16909+
getGlobalESSymbolConstructorSymbol = memoize(() => getGlobalValueSymbol("Symbol"));
1690716910
getGlobalPromiseType = memoize(() => getGlobalType("Promise", /*arity*/ 1));
1690816911
tryGetGlobalPromiseType = memoize(() => getGlobalSymbol("Promise", SymbolFlags.Type, /*diagnostic*/ undefined) && getGlobalPromiseType());
1690916912
getGlobalPromiseLikeType = memoize(() => getGlobalType("PromiseLike", /*arity*/ 1));
@@ -16912,27 +16915,19 @@ namespace ts {
1691216915
getGlobalPromiseConstructorLikeType = memoize(() => getGlobalType("PromiseConstructorLike"));
1691316916
getGlobalThenableType = memoize(createThenableType);
1691416917

16915-
// If we're in ES6 mode, load the TemplateStringsArray.
16916-
// Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios.
16918+
getGlobalTemplateStringsArrayType = memoize(() => getGlobalType("TemplateStringsArray"));
16919+
1691716920
if (languageVersion >= ScriptTarget.ES6) {
16918-
globalTemplateStringsArrayType = getGlobalType("TemplateStringsArray");
16919-
globalESSymbolType = getGlobalType("Symbol");
16920-
globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol");
16921-
globalIterableType = <GenericType>getGlobalType("Iterable", /*arity*/ 1);
16922-
globalIteratorType = <GenericType>getGlobalType("Iterator", /*arity*/ 1);
16923-
globalIterableIteratorType = <GenericType>getGlobalType("IterableIterator", /*arity*/ 1);
16921+
getGlobalESSymbolType = memoize(() => getGlobalType("Symbol"));
16922+
getGlobalIterableType = memoize(() => <GenericType>getGlobalType("Iterable", /*arity*/ 1));
16923+
getGlobalIteratorType = memoize(() => <GenericType>getGlobalType("Iterator", /*arity*/ 1));
16924+
getGlobalIterableIteratorType = memoize(() => <GenericType>getGlobalType("IterableIterator", /*arity*/ 1));
1692416925
}
1692516926
else {
16926-
globalTemplateStringsArrayType = unknownType;
16927-
16928-
// Consider putting Symbol interface in lib.d.ts. On the plus side, putting it in lib.d.ts would make it
16929-
// extensible for Polyfilling Symbols. But putting it into lib.d.ts could also break users that have
16930-
// a global Symbol already, particularly if it is a class.
16931-
globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
16932-
globalESSymbolConstructorSymbol = undefined;
16933-
globalIterableType = emptyGenericType;
16934-
globalIteratorType = emptyGenericType;
16935-
globalIterableIteratorType = emptyGenericType;
16927+
getGlobalESSymbolType = memoize(() => emptyObjectType);
16928+
getGlobalIterableType = memoize(() => emptyGenericType);
16929+
getGlobalIteratorType = memoize(() => emptyGenericType);
16930+
getGlobalIterableIteratorType = memoize(() => emptyGenericType);
1693616931
}
1693716932

1693816933
anyArrayType = createArrayType(anyType);

src/compiler/commandLineParser.ts

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ namespace ts {
162162
},
163163
{
164164
name: "pretty",
165-
paramType: Diagnostics.KIND,
166165
description: Diagnostics.Stylize_errors_and_messages_using_color_and_context_experimental,
167166
type: "boolean"
168167
},
@@ -332,6 +331,43 @@ namespace ts {
332331
type: "boolean",
333332
description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output
334333
},
334+
{
335+
name: "lib",
336+
type: "list",
337+
element: {
338+
name: "lib",
339+
type: {
340+
// JavaScript only
341+
"es5": "lib.es5.d.ts",
342+
"es6": "lib.es2015.d.ts",
343+
"es2015": "lib.es2015.d.ts",
344+
"es7": "lib.es2016.d.ts",
345+
"es2016": "lib.es2016.d.ts",
346+
// Host only
347+
"dom": "lib.dom.d.ts",
348+
"webworker": "lib.webworker.d.ts",
349+
"scripthost": "lib.scripthost.d.ts",
350+
// ES2015 Or ESNext By-feature options
351+
"es2015.array": "lib.es2015.array.d.ts",
352+
"es2015.collection": "lib.es2015.collection.d.ts",
353+
"es2015.generator": "lib.es2015.generator.d.ts",
354+
"es2015.function": "lib.es2015.function.d.ts",
355+
"es2015.iterable": "lib.es2015.iterable.d.ts",
356+
"es2015.math": "lib.es2015.math.d.ts",
357+
"es2015.number": "lib.es2015.number.d.ts",
358+
"es2015.object": "lib.es2015.object.d.ts",
359+
"es2015.promise": "lib.es2015.promise.d.ts",
360+
"es2015.proxy": "lib.es2015.proxy.d.ts",
361+
"es2015.reflect": "lib.es2015.reflect.d.ts",
362+
"es2015.regexp": "lib.es2015.regexp.d.ts",
363+
"es2015.string": "lib.es2015.string.d.ts",
364+
"es2015.symbol": "lib.es2015.symbol.d.ts",
365+
"es2015.symbol.wellknown": "lib.es2015.symbol.wellknown.d.ts",
366+
"es2016.array.include": "lib.es2016.array.include.d.ts"
367+
},
368+
},
369+
description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon
370+
},
335371
{
336372
name: "strictNullChecks",
337373
type: "boolean",
@@ -370,6 +406,7 @@ namespace ts {
370406
}
371407

372408
let optionNameMapCache: OptionNameMap;
409+
373410
/* @internal */
374411
export function getOptionNameMap(): OptionNameMap {
375412
if (optionNameMapCache) {
@@ -399,6 +436,32 @@ namespace ts {
399436
return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType);
400437
}
401438

439+
/* @internal */
440+
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
441+
const key = (value || "").trim().toLowerCase();
442+
const map = opt.type;
443+
if (hasProperty(map, key)) {
444+
return map[key];
445+
}
446+
else {
447+
errors.push(createCompilerDiagnosticForInvalidCustomType(opt));
448+
}
449+
}
450+
451+
/* @internal */
452+
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (string | number)[] {
453+
const values = (value || "").trim().split(",");
454+
switch (opt.element.type) {
455+
case "number":
456+
return ts.map(values, parseInt);
457+
case "string":
458+
return ts.map(values, v => v || "");
459+
default:
460+
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
461+
}
462+
}
463+
464+
/* @internal */
402465
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine {
403466
const options: CompilerOptions = {};
404467
const fileNames: string[] = [];
@@ -453,12 +516,12 @@ namespace ts {
453516
i++;
454517
break;
455518
case "list":
456-
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i]);
519+
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
457520
i++;
458521
break;
459522
// If not a primitive, the possible types are specified in what is effectively a map of options.
460523
default:
461-
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i]);
524+
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i], errors);
462525
i++;
463526
break;
464527
}
@@ -471,29 +534,6 @@ namespace ts {
471534
else {
472535
fileNames.push(s);
473536
}
474-
475-
function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string) {
476-
const key = (value || "").trim().toLowerCase();
477-
const map = opt.type;
478-
if (hasProperty(map, key)) {
479-
return map[key];
480-
}
481-
else {
482-
errors.push(createCompilerDiagnosticForInvalidCustomType(opt));
483-
}
484-
}
485-
486-
function parseListTypeOption(opt: CommandLineOptionOfListType, value: string): (string | number)[] {
487-
const values = (value || "").trim().split(",");
488-
switch (opt.element.type) {
489-
case "number":
490-
return ts.map(values, parseInt);
491-
case "string":
492-
return ts.map(values, v => v || "");
493-
default:
494-
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v)), v => !!v);
495-
}
496-
}
497537
}
498538
}
499539

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,10 @@
24922492
"category": "Message",
24932493
"code": 6078
24942494
},
2495+
"Specify library files to be included in the compilation: ": {
2496+
"category": "Message",
2497+
"code": 6079
2498+
},
24952499
"Specify JSX code generation: 'preserve' or 'react'": {
24962500
"category": "Message",
24972501
"code": 6080

0 commit comments

Comments
 (0)