Skip to content

Commit 39ff156

Browse files
committed
Changes to enable the module conversion script
* shorthand -> long for `factory` since the typeformer script doesn't know how to handle it. * Use setter to change `ts.sys` (similar to microsoft#35399). * Fix `loggedIO` with empty namespaces to indicate dependency (similar to 50603ed). * Move `Map` / `Set` stuff from `core.ts` to `corePublic.ts` since the types are there.
1 parent 506a95b commit 39ff156

File tree

6 files changed

+38
-25
lines changed

6 files changed

+38
-25
lines changed

src/compiler/core.ts

-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
/* @internal */
22
namespace ts {
3-
type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator<
4-
I extends ReadonlyESMap<infer K, infer V> ? [K, V] :
5-
I extends ReadonlySet<infer T> ? T :
6-
I extends readonly (infer T)[] ? T :
7-
I extends undefined ? undefined :
8-
never>;
9-
10-
function getCollectionImplementation<
11-
K1 extends MatchingKeys<typeof NativeCollections, () => any>,
12-
K2 extends MatchingKeys<typeof ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>>
13-
>(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> {
14-
// NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
15-
const constructor = NativeCollections[nativeFactory]() ?? ShimCollections?.[shimFactory](getIterator);
16-
if (constructor) return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>;
17-
throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`);
18-
}
19-
20-
export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim");
21-
export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim");
22-
233
export function getIterator<I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I): Iterator<
244
I extends ReadonlyESMap<infer K, infer V> ? [K, V] :
255
I extends ReadonlySet<infer T> ? T :

src/compiler/corePublic.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace ts {
113113
}
114114

115115
/* @internal */
116-
export namespace NativeCollections {
116+
namespace NativeCollections {
117117
declare const Map: MapConstructor | undefined;
118118
declare const Set: SetConstructor | undefined;
119119

@@ -135,4 +135,28 @@ namespace ts {
135135
return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined;
136136
}
137137
}
138-
}
138+
139+
/* @internal */
140+
export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim");
141+
/* @internal */
142+
export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim");
143+
144+
/* @internal */
145+
type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator<
146+
I extends ReadonlyESMap<infer K, infer V> ? [K, V] :
147+
I extends ReadonlySet<infer T> ? T :
148+
I extends readonly (infer T)[] ? T :
149+
I extends undefined ? undefined :
150+
never>;
151+
152+
/* @internal */
153+
function getCollectionImplementation<
154+
K1 extends MatchingKeys<typeof NativeCollections, () => any>,
155+
K2 extends MatchingKeys<typeof ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>>
156+
>(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> {
157+
// NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
158+
const constructor = NativeCollections[nativeFactory]() ?? ShimCollections?.[shimFactory](getIterator);
159+
if (constructor) return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>;
160+
throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`);
161+
}
162+
}

src/compiler/sys.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,11 @@ namespace ts {
19001900
return sys!;
19011901
})();
19021902

1903+
/*@internal*/
1904+
export function setSys(s: System) {
1905+
sys = s;
1906+
}
1907+
19031908
if (sys && sys.getEnvironmentVariable) {
19041909
setCustomPollingValues(sys);
19051910
Debug.setAssertionLevel(/^development$/i.test(sys.getEnvironmentVariable("NODE_ENV"))

src/compiler/transformer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ namespace ts {
566566
}
567567

568568
export const nullTransformationContext: TransformationContext = {
569-
factory,
569+
factory: factory, // eslint-disable-line object-shorthand
570570
getCompilerOptions: () => ({}),
571571
getEmitResolver: notImplemented,
572572
getEmitHost: notImplemented,

src/loggedIO/loggedIO.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Playback {
1+
namespace Playback { // eslint-disable-line one-namespace-per-file
22
interface FileInformation {
33
contents?: string;
44
contentsPath?: string;
@@ -445,3 +445,7 @@ namespace Playback {
445445
return wrapper;
446446
}
447447
}
448+
449+
// empty modules for the module migration script
450+
namespace ts.server { } // eslint-disable-line one-namespace-per-file
451+
namespace Harness { } // eslint-disable-line one-namespace-per-file

src/tsserver/webServer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace ts.server {
7878
};
7979
// Do this after sys has been set as findArguments is going to work only then
8080
const sys = server.createWebSystem(webHost, args, () => findArgument("--executingFilePath") || location + "");
81-
ts.sys = sys;
81+
setSys(sys);
8282
const localeStr = findArgument("--locale");
8383
if (localeStr) {
8484
validateLocaleAndSetLanguage(localeStr, sys);

0 commit comments

Comments
 (0)