Skip to content

Commit 15edfcc

Browse files
committed
Convert require calls to imports
Although the existing require calls would work fine, it's more consistent to make them actual imports and let them be converted at compile/bundle time. It also lets us emit ESM if needed. There are still conditional requires present for things like the node System, but that's a more difficult problem to solve.
1 parent 0034428 commit 15edfcc

File tree

8 files changed

+23
-33
lines changed

8 files changed

+23
-33
lines changed

src/cancellationToken/cancellationToken.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="node"/>
22

3-
import fs = require("fs");
3+
import * as fs from "fs";
44

55
interface ServerCancellationToken {
66
isCancellationRequested(): boolean;

src/harness/findUpDir.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { join, resolve, dirname } = require("path") as typeof import("path");
2-
const { existsSync } = require("fs") as typeof import("fs");
1+
import { existsSync } from "fs";
2+
import { dirname, join, resolve } from "path";
33

44
// search directories upward to avoid hard-wired paths based on the
55
// build tree (same as scripts/build/findUpDir.js)

src/harness/harnessGlobals.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import * as ts from "./_namespaces/ts";
1+
import * as chai from "chai";
22

3-
// Block scoped definitions work poorly for global variables, temporarily enable var
4-
/* eslint-disable no-var */
3+
import * as ts from "./_namespaces/ts";
54

65
// this will work in the browser via browserify
76
declare global {
87
// Module transform: converted from ambient declaration
9-
var assert: typeof _chai.assert;
8+
var assert: typeof chai.assert; // eslint-disable-line no-var
109
}
1110
declare global {
1211
// Module transform: converted from ambient declaration
13-
var expect: typeof _chai.expect;
12+
var expect: typeof chai.expect; // eslint-disable-line no-var
1413
}
15-
var _chai: typeof import("chai") = require("chai");
16-
globalThis.assert = _chai.assert;
14+
globalThis.assert = chai.assert;
1715
{
1816
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
1917
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
@@ -39,7 +37,7 @@ globalThis.assert = _chai.assert;
3937
}
4038
};
4139
}
42-
globalThis.expect = _chai.expect;
43-
/* eslint-enable no-var */
40+
globalThis.expect = chai.expect;
41+
4442
// empty ts namespace so this file is included in the `ts.ts` namespace file generated by the module swapover
45-
// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation
43+
// This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation

src/testRunner/externalCompileRunner.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import * as del from "del";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
15
import * as ts from "./_namespaces/ts";
26
import { Baseline, IO, isWorker, RunnerBase, TestRunnerKind } from "./_namespaces/Harness";
37

4-
const fs: typeof import("fs") = require("fs");
5-
const path: typeof import("path") = require("path");
6-
const del: typeof import("del") = require("del");
7-
88
interface ExecResult {
99
stdout: Buffer;
1010
stderr: Buffer;

src/testRunner/unittests/services/languageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { expect } from "chai";
2+
13
import * as ts from "../../_namespaces/ts";
24

3-
const _chai: typeof import("chai") = require("chai");
4-
const expect: typeof _chai.expect = _chai.expect;
55
describe("unittests:: services:: languageService", () => {
66
const files: {[index: string]: string} = {
77
"foo.ts": `import Vue from "./vue";

src/testRunner/unittests/tsserver/session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { expect } from "chai";
2+
13
import * as ts from "../../_namespaces/ts";
24
import * as Harness from "../../_namespaces/Harness";
35
import * as Utils from "../../_namespaces/Utils";
46

5-
const _chai: typeof import("chai") = require("chai");
6-
const expect: typeof _chai.expect = _chai.expect;
77
let lastWrittenToHost: string;
88
const noopFileWatcher: ts.FileWatcher = { close: ts.noop };
99
const mockHost: ts.server.ServerHost = {

src/typingsInstaller/nodeTypingsInstaller.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
14
import {
25
installNpmPackages, Log, RequestCompletedAction, TypingsInstaller,
36
} from "./_namespaces/ts.server.typingsInstaller";
@@ -11,16 +14,6 @@ import {
1114
MapLike, normalizeSlashes, stringContains, sys, toPath, version,
1215
} from "./_namespaces/ts";
1316

14-
const fs: {
15-
appendFileSync(file: string, content: string): void
16-
} = require("fs");
17-
18-
const path: {
19-
join(...parts: string[]): string;
20-
dirname(path: string): string;
21-
basename(path: string, extension?: string): string;
22-
} = require("path");
23-
2417
class FileLog implements Log {
2518
constructor(private logFile: string | undefined) {
2619
}

src/watchGuard/watchGuard.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/// <reference types="node" />
1+
import * as fs from "fs";
22

33
if (process.argv.length < 3) {
44
process.exit(1);
55
}
66
const directoryName = process.argv[2];
7-
const fs: { watch(directoryName: string, options: any, callback: () => {}): any } = require("fs");
87
// main reason why we need separate process to check if it is safe to watch some path
98
// is to guard against crashes that cannot be intercepted with protected blocks and
109
// code in tsserver already can handle normal cases, like non-existing folders.

0 commit comments

Comments
 (0)