Skip to content

Commit b9a4a3a

Browse files
committed
Some require -> import conversions
1 parent 9a78868 commit b9a4a3a

File tree

9 files changed

+31
-46
lines changed

9 files changed

+31
-46
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 { join, resolve, dirname } from "path";
2+
import { existsSync } from "fs";
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

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { isArray } from "./ts";
2+
import * as chai from "chai";
3+
24
declare global {
35
// Block scoped definitions work poorly for global variables, temporarily enable var
46
/* eslint-disable no-var */
57

68
// this will work in the browser via browserify
7-
var assert: typeof _chai.assert;
8-
}
9-
declare global {
10-
var expect: typeof _chai.expect;
9+
var assert: typeof chai.assert;
10+
var expect: typeof chai.expect;
1111
}
12-
var _chai: typeof import("chai") = require("chai");
13-
globalThis.assert = _chai.assert;
12+
13+
globalThis.assert = chai.assert;
1414
{
1515
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
1616
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
@@ -39,7 +39,4 @@ globalThis.assert = _chai.assert;
3939
}
4040
};
4141
}
42-
globalThis.expect = _chai.expect;
43-
/* eslint-enable no-var */
44-
// 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
42+
globalThis.expect = chai.expect;

src/instrumenter/instrumenter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs = require("fs");
2-
import path = require("path");
1+
import * as fs from "fs";
2+
import * as path from "path";
33

44
function instrumentForRecording(fn: string, tscPath: string) {
55
instrument(tscPath, `

src/testRunner/externalCompileRunner.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { RunnerBase, IO, isWorker, Baseline, TestRunnerKind } from "./Harness";
22
import { Debug, Version, flatten, comparePathsCaseSensitive, compareValues, compareStringsCaseSensitive, stringContains } from "./ts";
3-
const fs: typeof import("fs") = require("fs");
4-
const path: typeof import("path") = require("path");
5-
const del: typeof import("del") = require("del");
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import * as del from "del";
66

77
interface ExecResult {
88
stdout: Buffer;

src/testRunner/unittests/services/languageService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { ScriptSnapshot, getDefaultLibFilePath, emptyArray, LanguageServiceHost,
22
import { libFile, createServerHost } from "../../ts.projectSystem";
33
import { projectRoot } from "../../ts.tscWatch";
44
import * as ts from "../../ts";
5-
const _chai: typeof import("chai") = require("chai");
6-
const expect: typeof _chai.expect = _chai.expect;
5+
import { expect } from "chai";
6+
77
describe("unittests:: services:: languageService", () => {
88
const files: {
99
[index: string]: string;

src/tsserver/nodeServer.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { LogLevel, getLogLevel, findArgument, StartInput, ServerHost, BaseLogger, Msg, stringifyIndented, RequireResult, ServerCancellationToken, nullCancellationToken, StartSessionOptions, Logger, ITypingsInstaller, ProjectService, Event, InstallPackageOptionsWithProject, InstallPackageRequest, Arguments, Project, TypingInstallerRequestUnion, createInstallTypingsRequest, TypesRegistryResponse, PackageInstalledResponse, SetTypings, InvalidateCachedTypings, BeginInstallTypes, EndInstallTypes, InitializationFailedResponse, EventTypesRegistry, ActionPackageInstalled, EventInitializationFailed, protocol, EventBeginInstallTypes, EventEndInstallTypes, ActionInvalidate, ActionSet, Session, nullTypingsInstaller, formatMessage, toEvent, hasArgument } from "./ts.server";
22
import { CharacterCodes, stripQuotes, LanguageServiceMode, Debug, MapLike, noop, getNodeMajorVersion, combinePaths, noopFileWatcher, WatchFileKind, resolveJSModule, validateLocaleAndSetLanguage, normalizeSlashes, directorySeparator, toFileNameLowerCase, getRootLength, DirectoryWatcherCallback, WatchOptions, FileWatcher, ESMap, ApplyCodeActionCommandResult, JsTyping, getDirectoryPath, TypeAcquisition, SortedReadonlyArray, getEntries, assertType, sys, tracing, startTracing, versionMajorMinor } from "./ts";
33
import * as ts from "./ts";
4+
import { execFileSync } from "child_process";
5+
46
/*@internal*/
57
interface LogOptions {
68
file?: string;
@@ -100,12 +102,6 @@ function parseServerMode(): LanguageServiceMode | string | undefined {
100102
/* @internal */
101103
export function initializeNodeSystem(): StartInput {
102104
const sys = Debug.checkDefined(ts.sys) as ServerHost;
103-
const childProcess: {
104-
execFileSync(file: string, args: string[], options: {
105-
stdio: "ignore";
106-
env: MapLike<string>;
107-
}): string | Buffer;
108-
} = require("child_process");
109105

110106
interface Stats {
111107
isFile(): boolean;
@@ -207,7 +203,7 @@ export function initializeNodeSystem(): StartInput {
207203
if (logger.hasLevel(LogLevel.verbose)) {
208204
logger.info(`Starting ${process.execPath} with args:${stringifyIndented(args)}`);
209205
}
210-
childProcess.execFileSync(process.execPath, args, { stdio: "ignore", env: { ELECTRON_RUN_AS_NODE: "1" } });
206+
execFileSync(process.execPath, args, { stdio: "ignore", env: { ELECTRON_RUN_AS_NODE: "1" } });
211207
status = true;
212208
if (logger.hasLevel(LogLevel.verbose)) {
213209
logger.info(`WatchGuard for path ${path} returned: OK`);

src/typingsInstaller/nodeTypingsInstaller.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { Log, TypingsInstaller, RequestCompletedAction, installNpmPackages } from "./ts.server.typingsInstaller";
22
import { nowString, InstallTypingHost, InitializationFailedResponse, Arguments, TypingInstallerRequestUnion, TypesRegistryResponse, EventTypesRegistry, PackageInstalledResponse, ActionPackageInstalled, TypingInstallerResponseUnion, findArgument, hasArgument } from "./ts.server";
3-
import { sys, MapLike, ESMap, getEntries, combinePaths, normalizeSlashes, toPath, createGetCanonicalFileName, stringContains, Debug, version, forEachAncestorDirectory, getDirectoryPath } from "./ts";
4-
import * as ts from "./ts";
5-
const fs: {
6-
appendFileSync(file: string, content: string): void;
7-
} = require("fs");
8-
9-
const path: {
10-
join(...parts: string[]): string;
11-
dirname(path: string): string;
12-
basename(path: string, extension?: string): string;
13-
} = require("path");
3+
import { sys, MapLike, Map, ESMap, getEntries, combinePaths, normalizeSlashes, toPath, createGetCanonicalFileName, stringContains, Debug, version, forEachAncestorDirectory, getDirectoryPath } from "./ts";
4+
import { appendFileSync } from "fs";
5+
import * as path from "path";
146

157
class FileLog implements Log {
168
constructor(private logFile: string | undefined) {
@@ -24,7 +16,7 @@ class FileLog implements Log {
2416
return;
2517

2618
try {
27-
fs.appendFileSync(this.logFile, `[${nowString()}] ${text}${sys.newLine}`);
19+
appendFileSync(this.logFile, `[${nowString()}] ${text}${sys.newLine}`);
2820
}
2921
catch (e) {
3022
this.logFile = undefined;
@@ -55,17 +47,17 @@ function loadTypesRegistryFile(typesRegistryFilePath: string, host: InstallTypin
5547
if (log.isEnabled()) {
5648
log.writeLine(`Types registry file '${typesRegistryFilePath}' does not exist`);
5749
}
58-
return new ts.Map<string, MapLike<string>>();
50+
return new Map<string, MapLike<string>>();
5951
}
6052
try {
6153
const content = JSON.parse(host.readFile(typesRegistryFilePath)!) as TypesRegistryFile;
62-
return new ts.Map(getEntries(content.entries));
54+
return new Map(getEntries(content.entries));
6355
}
6456
catch (e) {
6557
if (log.isEnabled()) {
6658
log.writeLine(`Error when loading types registry file '${typesRegistryFilePath}': ${(e as Error).message}, ${(e as Error).stack}`);
6759
}
68-
return new ts.Map<string, MapLike<string>>();
60+
return new Map<string, MapLike<string>>();
6961
}
7062
}
7163

src/watchGuard/watchGuard.ts

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

3+
import { watch } from "fs";
4+
35
if (process.argv.length < 3) {
46
process.exit(1);
57
}
68
const directoryName = process.argv[2];
7-
const fs: {
8-
watch(directoryName: string, options: any, callback: () => {}): any;
9-
} = require("fs");
9+
1010
// main reason why we need separate process to check if it is safe to watch some path
1111
// is to guard against crashes that cannot be intercepted with protected blocks and
1212
// code in tsserver already can handle normal cases, like non-existing folders.
1313
// This means that here we treat any result (success or exception) from fs.watch as success since it does not tear down the process.
1414
// The only case that should be considered as failure - when watchGuard process crashes.
1515
try {
16-
const watcher = fs.watch(directoryName, { recursive: true }, () => ({}));
16+
const watcher = watch(directoryName, { recursive: true }, () => ({}));
1717
watcher.close();
1818
}
1919
catch { /*ignore*/ }

0 commit comments

Comments
 (0)