Skip to content

Fix detecting an existing Map/Set - #47409

Merged
Eli Barzilay (elibarzilay) merged 1 commit into
microsoft:mainfrom
elibarzilay:fix-map-set-shims
Jan 24, 2022
Merged

Eli Barzilay (elibarzilay) merged 1 commit into
microsoft:mainfrom
elibarzilay:fix-map-set-shims

Conversation

@elibarzilay

Copy link
Copy Markdown
Contributor

This didn't affect compilation to CJS since that sets exports.Map
instead of creating a global.

@rbuckton

Copy link
Copy Markdown
Contributor

The point of the declare const Map on the preceding line is to force the lookup of Map to look at the globally scoped Map (rather than ts.Map due to namespace merging). How did this become an error for CJS emit?

@elibarzilay

Copy link
Copy Markdown
Contributor Author

The problem is not in the CJS output. If I set module to es6, the resulting code is:

var NativeCollections;
(function (NativeCollections) {
    function tryGetNativeMap() {
        return typeof Map !== "undefined" && ...;
    }
    ...
})(NativeCollections || (NativeCollections = {}));
export var Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim");
```

@elibarzilay

Copy link
Copy Markdown
Contributor Author

Ron Buckton (@rbuckton), does that make sense?

Comment thread src/compiler/corePublic.ts Outdated
Comment thread src/compiler/corePublic.ts Outdated
@elibarzilay
Eli Barzilay (elibarzilay) force-pushed the fix-map-set-shims branch 2 times, most recently from c1beaa1 to bb2334f Compare January 24, 2022 19:36

@rbuckton Ron Buckton (rbuckton) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few more small issues I noticed, after which this should be ready to merge.

Comment thread src/compiler/corePublic.ts Outdated
Comment on lines +119 to +122
const globals = typeof globalThis !== "undefined" ? globalThis
: typeof global !== "undefined" ? global
: typeof self !== "undefined" ? self
: undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting nit:

Suggested change
const globals = typeof globalThis !== "undefined" ? globalThis
: typeof global !== "undefined" ? global
: typeof self !== "undefined" ? self
: undefined;
const globals = typeof globalThis !== "undefined" ? globalThis :
typeof global !== "undefined" ? global :
typeof self !== "undefined" ? self :
undefined;

Comment thread src/compiler/corePublic.ts Outdated
*/
export function tryGetNativeMap(): MapConstructor | undefined {
// Internet Explorer's Map doesn't support iteration, so don't use it.
const gMap = globals.Map;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

globals is possibly undefined.

Suggested change
const gMap = globals.Map;
const gMap = globals?.Map;

Comment thread src/compiler/corePublic.ts Outdated
*/
export function tryGetNativeSet(): SetConstructor | undefined {
// Internet Explorer's Set doesn't support iteration, so don't use it.
const gSet = globals.Set;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

globals is possibly undefined.

Suggested change
const gSet = globals.Set;
const gSet = globals?.Set;

This didn't affect compilation to CJS since that sets `exports.Map`
instead of creating a global.
@elibarzilay
Eli Barzilay (elibarzilay) merged commit 6927730 into microsoft:main Jan 24, 2022
@elibarzilay
Eli Barzilay (elibarzilay) deleted the fix-map-set-shims branch January 24, 2022 20:47
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants