Skip to content

Commit

Permalink
Convert require calls to imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed May 27, 2022
1 parent cd2e1dd commit ff4135c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/cancellationToken/cancellationToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node"/>

import fs = require("fs");
import * as fs from "fs";

interface ServerCancellationToken {
isCancellationRequested(): boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/harness/findUpDir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { join, resolve, dirname } = require("path") as typeof import("path");
const { existsSync } = require("fs") as typeof import("fs");
import { existsSync } from "fs";
import { dirname, join, resolve } from "path";

// search directories upward to avoid hard-wired paths based on the
// build tree (same as scripts/build/findUpDir.js)
Expand Down
20 changes: 8 additions & 12 deletions src/harness/harnessGlobals.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -39,7 +35,7 @@ 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
4 changes: 2 additions & 2 deletions src/instrumenter/instrumenter.ts
Original file line number Diff line number Diff line change
@@ -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, `
Expand Down
8 changes: 4 additions & 4 deletions src/testRunner/externalCompileRunner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as del from "del";
import * as fs from "fs";
import * as path from "path";

import { compareStringsCaseSensitive, compareValues, flatten, stringContains } from "../compiler/core";
import { Debug } from "../compiler/debug";
import { comparePathsCaseSensitive } from "../compiler/path";
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/services/languageService.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsserver/session.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -17,8 +19,6 @@ import { NormalizedPath } from "../../../server/utilitiesPublic";
import { FileTextChanges, IndentStyle } from "../../../services/types";
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 = {
Expand Down
13 changes: 3 additions & 10 deletions src/typingsInstaller/nodeTypingsInstaller.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
}
Expand Down
3 changes: 2 additions & 1 deletion src/watchGuard/watchGuard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/// <reference types="node" />

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.
Expand Down

0 comments on commit ff4135c

Please sign in to comment.