@@ -94,8 +94,6 @@ namespace ts {
94
94
95
95
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
96
96
globalThisSymbol.exports = globals;
97
- globalThisSymbol.valueDeclaration = createNode(SyntaxKind.Identifier) as Identifier;
98
- (globalThisSymbol.valueDeclaration as Identifier).escapedText = "globalThis" as __String;
99
97
globals.set(globalThisSymbol.escapedName, globalThisSymbol);
100
98
101
99
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);
@@ -926,7 +924,12 @@ namespace ts {
926
924
recordMergedSymbol(target, source);
927
925
}
928
926
else if (target.flags & SymbolFlags.NamespaceModule) {
929
- error(getNameOfDeclaration(source.declarations[0]), Diagnostics.Cannot_augment_module_0_with_value_exports_because_it_resolves_to_a_non_module_entity, symbolToString(target));
927
+ // Do not report an error when merging `var globalThis` with the built-in `globalThis`,
928
+ // as we will already report a "Declaration name conflicts..." error, and this error
929
+ // won't make much sense.
930
+ if (target !== globalThisSymbol) {
931
+ error(getNameOfDeclaration(source.declarations[0]), Diagnostics.Cannot_augment_module_0_with_value_exports_because_it_resolves_to_a_non_module_entity, symbolToString(target));
932
+ }
930
933
}
931
934
else { // error
932
935
const isEitherEnum = !!(target.flags & SymbolFlags.Enum || source.flags & SymbolFlags.Enum);
@@ -30456,6 +30459,14 @@ namespace ts {
30456
30459
continue;
30457
30460
}
30458
30461
if (!isExternalOrCommonJsModule(file)) {
30462
+ // It is an error for a non-external-module (i.e. script) to declare its own `globalThis`.
30463
+ // We can't use `builtinGlobals` for this due to synthetic expando-namespace generation in JS files.
30464
+ const fileGlobalThisSymbol = file.locals!.get("globalThis" as __String);
30465
+ if (fileGlobalThisSymbol) {
30466
+ for (const declaration of fileGlobalThisSymbol.declarations) {
30467
+ diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0, "globalThis"));
30468
+ }
30469
+ }
30459
30470
mergeSymbolTable(globals, file.locals!);
30460
30471
}
30461
30472
if (file.jsGlobalAugmentations) {
@@ -30501,6 +30512,7 @@ namespace ts {
30501
30512
getSymbolLinks(undefinedSymbol).type = undefinedWideningType;
30502
30513
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments" as __String, /*arity*/ 0, /*reportErrors*/ true);
30503
30514
getSymbolLinks(unknownSymbol).type = errorType;
30515
+ getSymbolLinks(globalThisSymbol).type = createObjectType(ObjectFlags.Anonymous, globalThisSymbol);
30504
30516
30505
30517
// Initialize special types
30506
30518
globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
0 commit comments