Don't create new types for toplevel every time

1. Add a global type. This is only accessible via toplevel `this` in
javascript scripts, but could be the type of other values later, and
also given a typename later.
2. Use the type of the containing file in the export case.
This commit is contained in:
Nathan Shively-Sanders 2018-03-28 14:13:51 -07:00
parent 59e2617ebc
commit 2050027a83
2 changed files with 13 additions and 3 deletions

View file

@ -2366,7 +2366,7 @@ namespace ts {
// this.foo assignment in a source file
// Bind this property in the global namespace or in the exports if in commonjs
if ((thisContainer as SourceFile).commonJsModuleIndicator) {
declareSymbol(file.symbol.exports, file.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
declareSymbol(thisContainer.symbol.exports, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
}
else {
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);

View file

@ -419,6 +419,7 @@ namespace ts {
let deferredGlobalAsyncIteratorType: GenericType;
let deferredGlobalAsyncIterableIteratorType: GenericType;
let deferredGlobalTemplateStringsArrayType: ObjectType;
let deferredGlobalGlobalType: ObjectType;
let deferredNodes: Node[];
let deferredUnusedIdentifierNodes: Node[];
@ -7612,6 +7613,10 @@ namespace ts {
return deferredGlobalIterableIteratorType || (deferredGlobalIterableIteratorType = getGlobalType("IterableIterator" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
}
function getGlobalGlobalType() {
return deferredGlobalGlobalType || (deferredGlobalGlobalType = createAnonymousType(undefined, globals, emptyArray, emptyArray, createIndexInfo(anyType, /*isReadonly*/ false), undefined)) || emptyObjectType;
}
function getGlobalTypeOrUndefined(name: __String, arity = 0): ObjectType {
const symbol = getGlobalSymbol(name, SymbolFlags.Type, /*diagnostic*/ undefined);
return symbol && <GenericType>getTypeOfGlobalSymbol(symbol, arity);
@ -13962,8 +13967,13 @@ namespace ts {
}
if (isSourceFile(container)) {
// look up in the source file's locals or exports
const parent = getSymbolOfNode(container);
return createAnonymousType(parent, container.commonJsModuleIndicator ? parent.exports : globals, emptyArray, emptyArray, createIndexInfo(anyType, /*isReadonly*/ false), undefined);
if (container.commonJsModuleIndicator) {
const fileSymbol = getSymbolOfNode(container);
return fileSymbol && getTypeOfSymbol(fileSymbol);
}
else {
return getGlobalGlobalType();
}
}
}
}