Skip to content

Commit 7cf710c

Browse files
bors[bot]Veetaha
andauthored
Merge #3372
3372: vscode: migrate to more type-safe assert impl r=matklad a=Veetaha https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions Co-authored-by: Veetaha <[email protected]>
2 parents cc477ed + 6dc598f commit 7cf710c

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

editors/code/src/installation/download_artifact.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as vscode from "vscode";
22
import * as path from "path";
33
import { promises as fs } from "fs";
4-
import { strict as assert } from "assert";
54

65
import { ArtifactReleaseInfo } from "./interfaces";
76
import { downloadFile } from "./download_file";
7+
import { assert } from "../util";
88

99
/**
1010
* Downloads artifact from given `downloadUrl`.
@@ -19,11 +19,10 @@ export async function downloadArtifact(
1919
installationDir: string,
2020
displayName: string,
2121
) {
22-
await fs.mkdir(installationDir).catch(err => assert.strictEqual(
23-
err?.code,
24-
"EEXIST",
22+
await fs.mkdir(installationDir).catch(err => assert(
23+
err?.code === "EEXIST",
2524
`Couldn't create directory "${installationDir}" to download ` +
26-
`${artifactFileName} artifact: ${err.message}`
25+
`${artifactFileName} artifact: ${err?.message}`
2726
));
2827

2928
const installationPath = path.join(installationDir, artifactFileName);

editors/code/src/installation/download_file.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import fetch from "node-fetch";
22
import * as fs from "fs";
33
import * as stream from "stream";
44
import * as util from "util";
5-
import { strict as assert } from "assert";
6-
import { log } from "../util";
5+
import { log, assert } from "../util";
76

87
const pipeline = util.promisify(stream.pipeline);
98

editors/code/src/installation/server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as vscode from "vscode";
22
import * as path from "path";
3-
import { strict as assert } from "assert";
43
import { promises as dns } from "dns";
54
import { spawnSync } from "child_process";
65

76
import { BinarySource } from "./interfaces";
87
import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info";
98
import { downloadArtifact } from "./download_artifact";
10-
import { log } from "../util";
9+
import { log, assert } from "../util";
1110

1211
export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> {
1312
if (!source) {

editors/code/src/util.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
import * as lc from "vscode-languageclient";
22
import * as vscode from "vscode";
3+
import { strict as nativeAssert } from "assert";
34

4-
let enabled: boolean = false;
5+
export function assert(condition: boolean, explanation: string): asserts condition {
6+
try {
7+
nativeAssert(condition, explanation);
8+
} catch (err) {
9+
log.error(`Assertion failed:`, explanation);
10+
throw err;
11+
}
12+
}
513

614
export const log = {
15+
enabled: true,
716
debug(message?: any, ...optionalParams: any[]): void {
8-
if (!enabled) return;
17+
if (!log.enabled) return;
918
// eslint-disable-next-line no-console
1019
console.log(message, ...optionalParams);
1120
},
1221
error(message?: any, ...optionalParams: any[]): void {
13-
if (!enabled) return;
22+
if (!log.enabled) return;
1423
debugger;
1524
// eslint-disable-next-line no-console
1625
console.error(message, ...optionalParams);
1726
},
1827
setEnabled(yes: boolean): void {
19-
enabled = yes;
28+
log.enabled = yes;
2029
}
2130
};
2231

0 commit comments

Comments
 (0)