From 27845480334bf2a006806f8cb44b641d5212e44f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 26 May 2022 17:00:27 -0700 Subject: [PATCH] Convert require calls to imports --- src/cancellationToken/cancellationToken.ts | 2 +- src/harness/findUpDir.ts | 4 ++-- src/harness/harnessGlobals.ts | 20 ++++++++----------- src/instrumenter/instrumenter.ts | 4 ++-- src/testRunner/externalCompileRunner.ts | 8 ++++---- .../unittests/services/languageService.ts | 4 ++-- src/testRunner/unittests/tsserver/session.ts | 4 ++-- src/typingsInstaller/nodeTypingsInstaller.ts | 13 +++--------- src/watchGuard/watchGuard.ts | 3 ++- 9 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/cancellationToken/cancellationToken.ts b/src/cancellationToken/cancellationToken.ts index aaa19750b8710..cab50efb801d4 100644 --- a/src/cancellationToken/cancellationToken.ts +++ b/src/cancellationToken/cancellationToken.ts @@ -1,6 +1,6 @@ /// -import fs = require("fs"); +import * as fs from 'fs'; interface ServerCancellationToken { isCancellationRequested(): boolean; diff --git a/src/harness/findUpDir.ts b/src/harness/findUpDir.ts index 621e2dff1b219..1471ccf863ffe 100644 --- a/src/harness/findUpDir.ts +++ b/src/harness/findUpDir.ts @@ -1,6 +1,6 @@ -const { join, resolve, dirname } = require("path") as typeof import("path"); -const { existsSync } = require("fs") as typeof import("fs"); +import { join, resolve, dirname } from "path"; +import { existsSync } from "fs"; // search directories upward to avoid hard-wired paths based on the // build tree (same as scripts/build/findUpDir.js) diff --git a/src/harness/harnessGlobals.ts b/src/harness/harnessGlobals.ts index 3a776f25f4b9c..aecefc3e7fa5b 100644 --- a/src/harness/harnessGlobals.ts +++ b/src/harness/harnessGlobals.ts @@ -1,19 +1,15 @@ -import { isArray } from "../compiler/core"; +import * as chai from "chai"; -// Block scoped definitions work poorly for global variables, temporarily enable var -/* eslint-disable no-var */ +import { isArray } from "../compiler/core"; // this will work in the browser via browserify declare global { // Module transform: converted from ambient declaration - var assert: typeof _chai.assert; + var assert: typeof chai.assert; + var expect: typeof chai.expect; } -declare global { - // Module transform: converted from ambient declaration - var expect: typeof _chai.expect; -} -var _chai: typeof import("chai") = require("chai"); -assert = _chai.assert; + +globalThis.assert = chai.assert; { // chai's builtin `assert.isFalse` is featureful but slow - we don't use those features, // so we'll just overwrite it as an alterative to migrating a bunch of code off of chai @@ -39,8 +35,8 @@ assert = _chai.assert; } }; } -expect = _chai.expect; -/* eslint-enable no-var */ +globalThis.expect = chai.expect; + // empty ts namespace so this file is included in the `ts.ts` namespace file generated by the module swapover // This way, everything that ends up importing `ts` downstream also imports this file and picks up its augmentation export { }; diff --git a/src/instrumenter/instrumenter.ts b/src/instrumenter/instrumenter.ts index c7cbb8d3ec855..641d1734561e4 100644 --- a/src/instrumenter/instrumenter.ts +++ b/src/instrumenter/instrumenter.ts @@ -1,5 +1,5 @@ -import fs = require("fs"); -import path = require("path"); +import * as fs from "fs"; +import * as path from "path"; function instrumentForRecording(fn: string, tscPath: string) { instrument(tscPath, ` diff --git a/src/testRunner/externalCompileRunner.ts b/src/testRunner/externalCompileRunner.ts index cff64f9d2cf35..e09a98e20a7bc 100644 --- a/src/testRunner/externalCompileRunner.ts +++ b/src/testRunner/externalCompileRunner.ts @@ -1,3 +1,7 @@ +import * as fs from "fs"; +import * as path from "path"; +import * as del from "del"; + import { compareStringsCaseSensitive, compareValues, flatten, stringContains } from "../compiler/core"; import { Debug } from "../compiler/debug"; import { comparePathsCaseSensitive } from "../compiler/path"; @@ -6,10 +10,6 @@ import { Baseline, IO } from "../harness/harnessIO"; import { RunnerBase, TestRunnerKind } from "../harness/runnerbase"; import { isWorker } from "./runner"; -const fs: typeof import("fs") = require("fs"); -const path: typeof import("path") = require("path"); -const del: typeof import("del") = require("del"); - interface ExecResult { stdout: Buffer; stderr: Buffer; diff --git a/src/testRunner/unittests/services/languageService.ts b/src/testRunner/unittests/services/languageService.ts index a2f957f776bf7..384a580c0e630 100644 --- a/src/testRunner/unittests/services/languageService.ts +++ b/src/testRunner/unittests/services/languageService.ts @@ -1,3 +1,5 @@ +import { expect } from "chai" + import { getParsedCommandLineOfConfigFile } from "../../../compiler/commandLineParser"; import { emptyArray, noop, returnTrue } from "../../../compiler/core"; import { Map } from "../../../compiler/corePublic"; @@ -9,8 +11,6 @@ import * as ts from "../../_namespaces/ts"; import { projectRoot } from "../tscWatch/helpers"; import { createServerHost, libFile } from "../tsserver/helpers"; -const _chai: typeof import("chai") = require("chai"); -const expect: typeof _chai.expect = _chai.expect; describe("unittests:: services:: languageService", () => { const files: {[index: string]: string} = { "foo.ts": `import Vue from "./vue"; diff --git a/src/testRunner/unittests/tsserver/session.ts b/src/testRunner/unittests/tsserver/session.ts index ee385551fb48f..ab7b2cc0bd023 100644 --- a/src/testRunner/unittests/tsserver/session.ts +++ b/src/testRunner/unittests/tsserver/session.ts @@ -1,3 +1,5 @@ +import { expect } from "chai"; + import { AnyFunction, noop, returnUndefined } from "../../../compiler/core"; import { Map, version } from "../../../compiler/corePublic"; import { Debug } from "../../../compiler/debug"; @@ -20,8 +22,6 @@ import { FileTextChanges } from "../../../services/types"; import * as ts from "../../_namespaces/ts"; import { createHasErrorMessageLogger, nullLogger } from "./helpers"; -const _chai: typeof import("chai") = require("chai"); -const expect: typeof _chai.expect = _chai.expect; let lastWrittenToHost: string; const noopFileWatcher: FileWatcher = { close: noop }; const mockHost: ServerHost = { diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 1320a9a8a5825..eb8feab955a11 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -1,3 +1,6 @@ +import * as fs from "fs"; +import * as path from "path"; + import { createGetCanonicalFileName, getEntries, stringContains } from "../compiler/core"; import { ESMap, Map, MapLike, version } from "../compiler/corePublic"; import { Debug } from "../compiler/debug"; @@ -14,16 +17,6 @@ import { installNpmPackages, Log, RequestCompletedAction, TypingsInstaller, } from "../typingsInstallerCore/typingsInstaller"; -const fs: { - appendFileSync(file: string, content: string): void -} = require("fs"); - -const path: { - join(...parts: string[]): string; - dirname(path: string): string; - basename(path: string, extension?: string): string; -} = require("path"); - class FileLog implements Log { constructor(private logFile: string | undefined) { } diff --git a/src/watchGuard/watchGuard.ts b/src/watchGuard/watchGuard.ts index 3835540d9c990..b66338f42eb0a 100644 --- a/src/watchGuard/watchGuard.ts +++ b/src/watchGuard/watchGuard.ts @@ -1,10 +1,11 @@ /// +import * as fs from "fs"; + if (process.argv.length < 3) { process.exit(1); } const directoryName = process.argv[2]; -const fs: { watch(directoryName: string, options: any, callback: () => {}): any } = require("fs"); // main reason why we need separate process to check if it is safe to watch some path // is to guard against crashes that cannot be intercepted with protected blocks and // code in tsserver already can handle normal cases, like non-existing folders.