Skip to content

Commit

Permalink
Eliminate builtinGlobals
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Mar 1, 2024
1 parent 886a41e commit 7b80d7a
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,9 +2255,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
var identityRelation = new Map<string, RelationComparisonResult>();
var enumRelation = new Map<string, RelationComparisonResult>();

var builtinGlobals = createSymbolTable();
builtinGlobals.set(undefinedSymbol.escapedName, undefinedSymbol);

// Extensions suggested for path imports when module resolution is node16 or higher.
// The first element of each tuple is the extension a file has.
// The second element of each tuple is the extension that should be used in a path import.
Expand Down Expand Up @@ -2720,24 +2717,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function addToSymbolTable(target: SymbolTable, source: SymbolTable, message: DiagnosticMessage) {
source.forEach((sourceSymbol, id) => {
const targetSymbol = target.get(id);
if (targetSymbol) {
// Error on redeclarations
forEach(targetSymbol.declarations, addDeclarationDiagnostic(unescapeLeadingUnderscores(id), message));
}
else {
target.set(id, sourceSymbol);
}
});

function addDeclarationDiagnostic(id: string, message: DiagnosticMessage) {
return (declaration: Declaration) => {
if (!isTypeDeclaration(declaration) && declaration.kind !== SyntaxKind.ClassDeclaration) {
diagnostics.add(createDiagnosticForNode(declaration, message, id));
function addUndefinedToGlobalsOrErrorOnRedeclaration() {
const name = undefinedSymbol.escapedName;
const targetSymbol = globals.get(name);
if (targetSymbol) {
forEach(targetSymbol.declarations, declaration => {
// checkTypeNameIsReserved will have added better diagnostics for undefined.
if (!isTypeDeclaration(declaration)) {
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0, unescapeLeadingUnderscores(name)));
}
};
});
}
else {
globals.set(name, undefinedSymbol);
}
}

Expand Down Expand Up @@ -48754,7 +48746,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (!isExternalOrCommonJsModule(file)) {
// It is an error for a non-external-module (i.e. script) to declare its own `globalThis`.
// We can't use `builtinGlobals` for this due to synthetic expando-namespace generation in JS files.
const fileGlobalThisSymbol = file.locals!.get("globalThis" as __String);
if (fileGlobalThisSymbol?.declarations) {
for (const declaration of fileGlobalThisSymbol.declarations) {
Expand Down Expand Up @@ -48800,8 +48791,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

// Setup global builtins
addToSymbolTable(globals, builtinGlobals, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0);
addUndefinedToGlobalsOrErrorOnRedeclaration();

getSymbolLinks(undefinedSymbol).type = undefinedWideningType;
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments" as __String, /*arity*/ 0, /*reportErrors*/ true);
Expand Down

0 comments on commit 7b80d7a

Please sign in to comment.