Skip to content

Commit 7423c69

Browse files
authored
Merge pull request #31142 from Microsoft/binderPerf
Fix binder performance regression
2 parents 47d9081 + 57a8ee1 commit 7423c69

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

src/compiler/checker.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ namespace ts {
9494

9595
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
9696
globalThisSymbol.exports = globals;
97-
globalThisSymbol.valueDeclaration = createNode(SyntaxKind.Identifier) as Identifier;
98-
(globalThisSymbol.valueDeclaration as Identifier).escapedText = "globalThis" as __String;
9997
globals.set(globalThisSymbol.escapedName, globalThisSymbol);
10098

10199
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);
@@ -926,7 +924,12 @@ namespace ts {
926924
recordMergedSymbol(target, source);
927925
}
928926
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+
}
930933
}
931934
else { // error
932935
const isEitherEnum = !!(target.flags & SymbolFlags.Enum || source.flags & SymbolFlags.Enum);
@@ -30456,6 +30459,14 @@ namespace ts {
3045630459
continue;
3045730460
}
3045830461
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+
}
3045930470
mergeSymbolTable(globals, file.locals!);
3046030471
}
3046130472
if (file.jsGlobalAugmentations) {
@@ -30501,6 +30512,7 @@ namespace ts {
3050130512
getSymbolLinks(undefinedSymbol).type = undefinedWideningType;
3050230513
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments" as __String, /*arity*/ 0, /*reportErrors*/ true);
3050330514
getSymbolLinks(unknownSymbol).type = errorType;
30515+
getSymbolLinks(globalThisSymbol).type = createObjectType(ObjectFlags.Anonymous, globalThisSymbol);
3050430516

3050530517
// Initialize special types
3050630518
globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/conformance/es2019/globalThisCollision.js(1,5): error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
2+
3+
4+
==== tests/cases/conformance/es2019/globalThisCollision.js (1 errors) ====
5+
var globalThis;
6+
~~~~~~~~~~
7+
!!! error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/es2019/globalThisCollision.js ===
2+
var globalThis;
3+
>globalThis : Symbol(globalThis, Decl(globalThisCollision.js, 0, 3))
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/es2019/globalThisCollision.js ===
2+
var globalThis;
3+
>globalThis : any
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @filename: globalThisCollision.js
5+
var globalThis;

0 commit comments

Comments
 (0)