From 69277306c93bbd13e3fda8e995336b2861ef61e3 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 11 Jan 2022 17:30:57 -0500 Subject: [PATCH] Fix detecting an existing `Map`/`Set` This didn't affect compilation to CJS since that sets `exports.Map` instead of creating a global. --- src/compiler/corePublic.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/compiler/corePublic.ts b/src/compiler/corePublic.ts index f15b438c02f..61f60451752 100644 --- a/src/compiler/corePublic.ts +++ b/src/compiler/corePublic.ts @@ -114,16 +114,21 @@ namespace ts { /* @internal */ namespace NativeCollections { - declare const Map: MapConstructor | undefined; - declare const Set: SetConstructor | undefined; + declare const self: any; + + const globals = typeof globalThis !== "undefined" ? globalThis : + typeof global !== "undefined" ? global : + typeof self !== "undefined" ? self : + undefined; /** * Returns the native Map implementation if it is available and compatible (i.e. supports iteration). */ export function tryGetNativeMap(): MapConstructor | undefined { // Internet Explorer's Map doesn't support iteration, so don't use it. + const gMap = globals?.Map; // eslint-disable-next-line no-in-operator - return typeof Map !== "undefined" && "entries" in Map.prototype && new Map([[0, 0]]).size === 1 ? Map : undefined; + return typeof gMap !== "undefined" && "entries" in gMap.prototype && new gMap([[0, 0]]).size === 1 ? gMap : undefined; } /** @@ -131,8 +136,9 @@ namespace ts { */ export function tryGetNativeSet(): SetConstructor | undefined { // Internet Explorer's Set doesn't support iteration, so don't use it. + const gSet = globals?.Set; // eslint-disable-next-line no-in-operator - return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined; + return typeof gSet !== "undefined" && "entries" in gSet.prototype && new gSet([0]).size === 1 ? gSet : undefined; } }