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 Aug 25, 2022
1 parent fdafae4 commit 3b3f987
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 34 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
18 changes: 8 additions & 10 deletions src/harness/harnessGlobals.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import * as ts from "./_namespaces/ts";
import * as chai from "chai";

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

// 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;
}
declare global {
// Module transform: converted from ambient declaration
var expect: typeof _chai.expect;
var expect: typeof chai.expect;
}
var _chai: typeof import("chai") = require("chai");
globalThis.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 +37,7 @@ globalThis.assert = _chai.assert;
}
};
}
globalThis.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
// 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,10 +1,10 @@
import * as del from "del";
import * as fs from "fs";
import * as path from "path";

import * as ts from "./_namespaces/ts";
import { Baseline, IO, isWorker, RunnerBase, TestRunnerKind } from "./_namespaces/Harness";

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,7 +1,7 @@
import { expect } from "chai";

import * as ts from "../../_namespaces/ts";

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,9 +1,9 @@
import { expect } from "chai";

import * as ts from "../../_namespaces/ts";
import * as Harness from "../../_namespaces/Harness";
import * as Utils from "../../_namespaces/Utils";

const _chai: typeof import("chai") = require("chai");
const expect: typeof _chai.expect = _chai.expect;
let lastWrittenToHost: string;
const noopFileWatcher: ts.FileWatcher = { close: ts.noop };
const mockHost: ts.server.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 {
installNpmPackages, Log, RequestCompletedAction, TypingsInstaller,
} from "./_namespaces/ts.server.typingsInstaller";
Expand All @@ -11,16 +14,6 @@ import {
MapLike, normalizeSlashes, stringContains, sys, toPath, version,
} from "./_namespaces/ts";

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 3b3f987

Please sign in to comment.