mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
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 <gerzoh1@gmail.com>
This commit is contained in:
commit
7cf710c66f
4 changed files with 19 additions and 13 deletions
|
@ -1,10 +1,10 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
import { strict as assert } from "assert";
|
|
||||||
|
|
||||||
import { ArtifactReleaseInfo } from "./interfaces";
|
import { ArtifactReleaseInfo } from "./interfaces";
|
||||||
import { downloadFile } from "./download_file";
|
import { downloadFile } from "./download_file";
|
||||||
|
import { assert } from "../util";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads artifact from given `downloadUrl`.
|
* Downloads artifact from given `downloadUrl`.
|
||||||
|
@ -19,11 +19,10 @@ export async function downloadArtifact(
|
||||||
installationDir: string,
|
installationDir: string,
|
||||||
displayName: string,
|
displayName: string,
|
||||||
) {
|
) {
|
||||||
await fs.mkdir(installationDir).catch(err => assert.strictEqual(
|
await fs.mkdir(installationDir).catch(err => assert(
|
||||||
err?.code,
|
err?.code === "EEXIST",
|
||||||
"EEXIST",
|
|
||||||
`Couldn't create directory "${installationDir}" to download ` +
|
`Couldn't create directory "${installationDir}" to download ` +
|
||||||
`${artifactFileName} artifact: ${err.message}`
|
`${artifactFileName} artifact: ${err?.message}`
|
||||||
));
|
));
|
||||||
|
|
||||||
const installationPath = path.join(installationDir, artifactFileName);
|
const installationPath = path.join(installationDir, artifactFileName);
|
||||||
|
|
|
@ -2,8 +2,7 @@ import fetch from "node-fetch";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import * as stream from "stream";
|
import * as stream from "stream";
|
||||||
import * as util from "util";
|
import * as util from "util";
|
||||||
import { strict as assert } from "assert";
|
import { log, assert } from "../util";
|
||||||
import { log } from "../util";
|
|
||||||
|
|
||||||
const pipeline = util.promisify(stream.pipeline);
|
const pipeline = util.promisify(stream.pipeline);
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { strict as assert } from "assert";
|
|
||||||
import { promises as dns } from "dns";
|
import { promises as dns } from "dns";
|
||||||
import { spawnSync } from "child_process";
|
import { spawnSync } from "child_process";
|
||||||
|
|
||||||
import { BinarySource } from "./interfaces";
|
import { BinarySource } from "./interfaces";
|
||||||
import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info";
|
import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info";
|
||||||
import { downloadArtifact } from "./download_artifact";
|
import { downloadArtifact } from "./download_artifact";
|
||||||
import { log } from "../util";
|
import { log, assert } from "../util";
|
||||||
|
|
||||||
export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> {
|
export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> {
|
||||||
if (!source) {
|
if (!source) {
|
||||||
|
|
|
@ -1,22 +1,31 @@
|
||||||
import * as lc from "vscode-languageclient";
|
import * as lc from "vscode-languageclient";
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
|
import { strict as nativeAssert } from "assert";
|
||||||
|
|
||||||
let enabled: boolean = false;
|
export function assert(condition: boolean, explanation: string): asserts condition {
|
||||||
|
try {
|
||||||
|
nativeAssert(condition, explanation);
|
||||||
|
} catch (err) {
|
||||||
|
log.error(`Assertion failed:`, explanation);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const log = {
|
export const log = {
|
||||||
|
enabled: true,
|
||||||
debug(message?: any, ...optionalParams: any[]): void {
|
debug(message?: any, ...optionalParams: any[]): void {
|
||||||
if (!enabled) return;
|
if (!log.enabled) return;
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(message, ...optionalParams);
|
console.log(message, ...optionalParams);
|
||||||
},
|
},
|
||||||
error(message?: any, ...optionalParams: any[]): void {
|
error(message?: any, ...optionalParams: any[]): void {
|
||||||
if (!enabled) return;
|
if (!log.enabled) return;
|
||||||
debugger;
|
debugger;
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(message, ...optionalParams);
|
console.error(message, ...optionalParams);
|
||||||
},
|
},
|
||||||
setEnabled(yes: boolean): void {
|
setEnabled(yes: boolean): void {
|
||||||
enabled = yes;
|
log.enabled = yes;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue