Skip to content

Commit 2050027

Browse files
committed
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.
1 parent 59e2617 commit 2050027

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ namespace ts {
23662366
// this.foo assignment in a source file
23672367
// Bind this property in the global namespace or in the exports if in commonjs
23682368
if ((thisContainer as SourceFile).commonJsModuleIndicator) {
2369-
declareSymbol(file.symbol.exports, file.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
2369+
declareSymbol(thisContainer.symbol.exports, thisContainer.symbol, node, SymbolFlags.Property | SymbolFlags.ExportValue, SymbolFlags.None);
23702370
}
23712371
else {
23722372
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);

src/compiler/checker.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ namespace ts {
419419
let deferredGlobalAsyncIteratorType: GenericType;
420420
let deferredGlobalAsyncIterableIteratorType: GenericType;
421421
let deferredGlobalTemplateStringsArrayType: ObjectType;
422+
let deferredGlobalGlobalType: ObjectType;
422423

423424
let deferredNodes: Node[];
424425
let deferredUnusedIdentifierNodes: Node[];
@@ -7612,6 +7613,10 @@ namespace ts {
76127613
return deferredGlobalIterableIteratorType || (deferredGlobalIterableIteratorType = getGlobalType("IterableIterator" as __String, /*arity*/ 1, reportErrors)) || emptyGenericType;
76137614
}
76147615

7616+
function getGlobalGlobalType() {
7617+
return deferredGlobalGlobalType || (deferredGlobalGlobalType = createAnonymousType(undefined, globals, emptyArray, emptyArray, createIndexInfo(anyType, /*isReadonly*/ false), undefined)) || emptyObjectType;
7618+
}
7619+
76157620
function getGlobalTypeOrUndefined(name: __String, arity = 0): ObjectType {
76167621
const symbol = getGlobalSymbol(name, SymbolFlags.Type, /*diagnostic*/ undefined);
76177622
return symbol && <GenericType>getTypeOfGlobalSymbol(symbol, arity);
@@ -13962,8 +13967,13 @@ namespace ts {
1396213967
}
1396313968
if (isSourceFile(container)) {
1396413969
// look up in the source file's locals or exports
13965-
const parent = getSymbolOfNode(container);
13966-
return createAnonymousType(parent, container.commonJsModuleIndicator ? parent.exports : globals, emptyArray, emptyArray, createIndexInfo(anyType, /*isReadonly*/ false), undefined);
13970+
if (container.commonJsModuleIndicator) {
13971+
const fileSymbol = getSymbolOfNode(container);
13972+
return fileSymbol && getTypeOfSymbol(fileSymbol);
13973+
}
13974+
else {
13975+
return getGlobalGlobalType();
13976+
}
1396713977
}
1396813978
}
1396913979
}

0 commit comments

Comments
 (0)