Skip to content

Commit a78342a

Browse files
authored
Move most harness globals into namespaces (microsoft#35530)
* Move most harness globals into namespaces * Remove forward declaration from `createMapShim` and move all `Map` declarations into one file * A small pile of more changes to get the harness transforming
1 parent 2b567b2 commit a78342a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1353
-1294
lines changed

src/compiler/core.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,8 @@
22
/* @internal */
33
namespace ts {
44

5-
/**
6-
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
7-
*/
8-
export function tryGetNativeMap(): MapConstructor | undefined {
9-
// Internet Explorer's Map doesn't support iteration, so don't use it.
10-
// Natives
11-
// NOTE: TS doesn't strictly allow in-line declares, but if we suppress the error, the declaration
12-
// is still used for typechecking _and_ correctly elided, which is out goal, as this prevents us from
13-
// needing to pollute an outer scope with a declaration of `Map` just to satisfy the checks in this function
14-
//@ts-ignore
15-
declare const Map: (new <T>() => Map<T>) | undefined;
16-
// eslint-disable-next-line no-in-operator
17-
return typeof Map !== "undefined" && "entries" in Map.prototype ? Map : undefined;
18-
}
19-
205
export const emptyArray: never[] = [] as never[];
216

22-
export const Map: MapConstructor = tryGetNativeMap() || (() => {
23-
// NOTE: createMapShim will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
24-
if (typeof createMapShim === "function") {
25-
return createMapShim();
26-
}
27-
throw new Error("TypeScript requires an environment that provides a compatible native Map implementation.");
28-
})();
29-
307
/** Create a new map. */
318
export function createMap<T>(): Map<T> {
329
return new Map<T>();

src/compiler/corePublic.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ namespace ts {
4646
new <T>(): Map<T>;
4747
}
4848

49+
/**
50+
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
51+
*/
52+
/* @internal */
53+
export function tryGetNativeMap(): MapConstructor | undefined {
54+
// Internet Explorer's Map doesn't support iteration, so don't use it.
55+
// Natives
56+
// NOTE: TS doesn't strictly allow in-line declares, but if we suppress the error, the declaration
57+
// is still used for typechecking _and_ correctly elided, which is out goal, as this prevents us from
58+
// needing to pollute an outer scope with a declaration of `Map` just to satisfy the checks in this function
59+
//@ts-ignore
60+
declare const Map: (new <T>() => Map<T>) | undefined;
61+
// eslint-disable-next-line no-in-operator
62+
return typeof Map !== "undefined" && "entries" in Map.prototype ? Map : undefined;
63+
}
64+
65+
/* @internal */
66+
export const Map: MapConstructor = tryGetNativeMap() || (() => {
67+
// NOTE: createMapShim will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
68+
if (typeof createMapShim === "function") {
69+
return createMapShim();
70+
}
71+
throw new Error("TypeScript requires an environment that provides a compatible native Map implementation.");
72+
})();
73+
4974
/** ES6 Iterator type. */
5075
export interface Iterator<T> {
5176
next(): { value: T, done?: false } | { value: never, done: true };
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/harness/fakes.ts renamed to src/harness/fakesHosts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace fakes {
3939
public readFile(path: string) {
4040
try {
4141
const content = this.vfs.readFileSync(path, "utf8");
42-
return content === undefined ? undefined : utils.removeByteOrderMark(content);
42+
return content === undefined ? undefined : Utils.removeByteOrderMark(content);
4343
}
4444
catch {
4545
return undefined;
@@ -48,7 +48,7 @@ namespace fakes {
4848

4949
public writeFile(path: string, data: string, writeByteOrderMark?: boolean): void {
5050
this.vfs.mkdirpSync(vpath.dirname(path));
51-
this.vfs.writeFileSync(path, writeByteOrderMark ? utils.addUTF8ByteOrderMark(data) : data);
51+
this.vfs.writeFileSync(path, writeByteOrderMark ? Utils.addUTF8ByteOrderMark(data) : data);
5252
}
5353

5454
public deleteFile(path: string) {
@@ -289,7 +289,7 @@ namespace fakes {
289289
}
290290

291291
public writeFile(fileName: string, content: string, writeByteOrderMark: boolean) {
292-
if (writeByteOrderMark) content = utils.addUTF8ByteOrderMark(content);
292+
if (writeByteOrderMark) content = Utils.addUTF8ByteOrderMark(content);
293293
this.sys.writeFile(fileName, content);
294294

295295
const document = new documents.TextDocument(fileName, content);

src/harness/fourslash.ts renamed to src/harness/fourslashImpl.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
namespace FourSlash {
2-
ts.disableIncrementalParsing = false;
3-
42
import ArrayOrSingle = FourSlashInterface.ArrayOrSingle;
53

64
export const enum FourSlashTestType {

src/harness/harnessGlobals.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
/* eslint-disable no-var */
33

44
// this will work in the browser via browserify
5-
var _chai: typeof chai = require("chai");
6-
var assert: typeof _chai.assert = _chai.assert;
5+
declare var assert: typeof _chai.assert;
6+
var _chai: typeof import("chai") = require("chai");
7+
globalThis.assert = _chai.assert;
78
{
89
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
910
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
@@ -27,4 +28,7 @@ var assert: typeof _chai.assert = _chai.assert;
2728
}
2829
};
2930
}
30-
/* eslint-enable no-var */
31+
/* eslint-enable no-var */
32+
// empty ts namespace so this file is included in the `ts.ts` namespace file generated by the module swapover
33+
// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation
34+
namespace ts {}

0 commit comments

Comments
 (0)