From 1b9ab04d4b4694eb32402daf83f8df5f13df23ec Mon Sep 17 00:00:00 2001 From: Veetaha Date: Fri, 28 Feb 2020 23:56:17 +0200 Subject: vscode: migrate to more type-safe assert impl --- editors/code/src/installation/download_file.ts | 3 +-- editors/code/src/installation/server.ts | 3 +-- editors/code/src/util.ts | 12 ++++++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index 319cb995c..ee8949d61 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts @@ -2,8 +2,7 @@ import fetch from "node-fetch"; import * as fs from "fs"; import * as stream from "stream"; import * as util from "util"; -import { strict as assert } from "assert"; -import { log } from "../util"; +import { log, assert } from "../util"; const pipeline = util.promisify(stream.pipeline); diff --git a/editors/code/src/installation/server.ts b/editors/code/src/installation/server.ts index cb5e56844..6a6cf4f8c 100644 --- a/editors/code/src/installation/server.ts +++ b/editors/code/src/installation/server.ts @@ -1,13 +1,12 @@ import * as vscode from "vscode"; import * as path from "path"; -import { strict as assert } from "assert"; import { promises as dns } from "dns"; import { spawnSync } from "child_process"; import { BinarySource } from "./interfaces"; import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info"; import { downloadArtifact } from "./download_artifact"; -import { log } from "../util"; +import { log, assert } from "../util"; export async function ensureServerBinary(source: null | BinarySource): Promise { if (!source) { diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 68c2a94d0..acf78898b 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -1,22 +1,26 @@ import * as lc from "vscode-languageclient"; import * as vscode from "vscode"; +import { strict as nodeAssert } from "assert"; -let enabled: boolean = false; +export function assert(condition: unknown, explanation: string): asserts condition { + nodeAssert(condition, explanation); +} export const log = { + enabled: true, debug(message?: any, ...optionalParams: any[]): void { - if (!enabled) return; + if (!log.enabled) return; // eslint-disable-next-line no-console console.log(message, ...optionalParams); }, error(message?: any, ...optionalParams: any[]): void { - if (!enabled) return; + if (!log.enabled) return; debugger; // eslint-disable-next-line no-console console.error(message, ...optionalParams); }, setEnabled(yes: boolean): void { - enabled = yes; + log.enabled = yes; } }; -- cgit v1.2.3 From b88887e70e0936132b03d8db065b7cf7db175f7b Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 29 Feb 2020 00:02:19 +0200 Subject: vscode: add error loging on failed assertion --- editors/code/src/util.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index acf78898b..203494459 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -3,7 +3,12 @@ import * as vscode from "vscode"; import { strict as nodeAssert } from "assert"; export function assert(condition: unknown, explanation: string): asserts condition { - nodeAssert(condition, explanation); + try { + nodeAssert(condition, explanation); + } catch (err) { + log.error(`Assertion failed:`, explanation); + throw err; + } } export const log = { -- cgit v1.2.3 From 9cf25770542dfbf35c710f7c214f65ecaad39100 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 29 Feb 2020 00:07:29 +0200 Subject: vscode: rename nodeAssert -> nativeAssert --- editors/code/src/util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 203494459..733bdc8c5 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -1,10 +1,10 @@ import * as lc from "vscode-languageclient"; import * as vscode from "vscode"; -import { strict as nodeAssert } from "assert"; +import { strict as nativeAssert } from "assert"; export function assert(condition: unknown, explanation: string): asserts condition { try { - nodeAssert(condition, explanation); + nativeAssert(condition, explanation); } catch (err) { log.error(`Assertion failed:`, explanation); throw err; -- cgit v1.2.3 From 3ad0574d7e3f1af1e28f7f5cbc085faa30759e47 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 29 Feb 2020 00:26:22 +0200 Subject: vscode: add equality assertion --- editors/code/src/installation/download_artifact.ts | 4 ++-- editors/code/src/util.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/editors/code/src/installation/download_artifact.ts b/editors/code/src/installation/download_artifact.ts index 8ed99bf0a..c60014c8c 100644 --- a/editors/code/src/installation/download_artifact.ts +++ b/editors/code/src/installation/download_artifact.ts @@ -1,10 +1,10 @@ import * as vscode from "vscode"; import * as path from "path"; import { promises as fs } from "fs"; -import { strict as assert } from "assert"; import { ArtifactReleaseInfo } from "./interfaces"; import { downloadFile } from "./download_file"; +import { assert } from "../util"; /** * Downloads artifact from given `downloadUrl`. @@ -19,7 +19,7 @@ export async function downloadArtifact( installationDir: string, displayName: string, ) { - await fs.mkdir(installationDir).catch(err => assert.strictEqual( + await fs.mkdir(installationDir).catch(err => assert.eq( err?.code, "EEXIST", `Couldn't create directory "${installationDir}" to download ` + diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 733bdc8c5..5a5f11a10 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -11,6 +11,16 @@ export function assert(condition: unknown, explanation: string): asserts conditi } } +assert.eq = (bibba: unknown, bobba: T, explanation: string): asserts bibba is T => { + try { + nativeAssert.strictEqual(bibba, bobba, explanation); + } catch (err) { + log.error(`Equality assertion failed:`, explanation); + throw err; + } +} + + export const log = { enabled: true, debug(message?: any, ...optionalParams: any[]): void { -- cgit v1.2.3 From e7bf99e6fdddc1ec66611a0b145d796f17c46039 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 29 Feb 2020 00:33:34 +0200 Subject: vscode: add dat semicolon --- editors/code/src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 5a5f11a10..5c89bc62c 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -18,7 +18,7 @@ assert.eq = (bibba: unknown, bobba: T, explanation: string): asserts bibba is log.error(`Equality assertion failed:`, explanation); throw err; } -} +}; export const log = { -- cgit v1.2.3 From 6dc598fa6331de6124a255527e37804d82372fdc Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 29 Feb 2020 00:46:48 +0200 Subject: vscode: simplify assert.eq() to assert() as per matklad --- editors/code/src/installation/download_artifact.ts | 7 +++---- editors/code/src/util.ts | 12 +----------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/editors/code/src/installation/download_artifact.ts b/editors/code/src/installation/download_artifact.ts index c60014c8c..97e4d67c2 100644 --- a/editors/code/src/installation/download_artifact.ts +++ b/editors/code/src/installation/download_artifact.ts @@ -19,11 +19,10 @@ export async function downloadArtifact( installationDir: string, displayName: string, ) { - await fs.mkdir(installationDir).catch(err => assert.eq( - err?.code, - "EEXIST", + await fs.mkdir(installationDir).catch(err => assert( + err?.code === "EEXIST", `Couldn't create directory "${installationDir}" to download ` + - `${artifactFileName} artifact: ${err.message}` + `${artifactFileName} artifact: ${err?.message}` )); const installationPath = path.join(installationDir, artifactFileName); diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 5c89bc62c..f56c6bada 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -2,7 +2,7 @@ import * as lc from "vscode-languageclient"; import * as vscode from "vscode"; import { strict as nativeAssert } from "assert"; -export function assert(condition: unknown, explanation: string): asserts condition { +export function assert(condition: boolean, explanation: string): asserts condition { try { nativeAssert(condition, explanation); } catch (err) { @@ -11,16 +11,6 @@ export function assert(condition: unknown, explanation: string): asserts conditi } } -assert.eq = (bibba: unknown, bobba: T, explanation: string): asserts bibba is T => { - try { - nativeAssert.strictEqual(bibba, bobba, explanation); - } catch (err) { - log.error(`Equality assertion failed:`, explanation); - throw err; - } -}; - - export const log = { enabled: true, debug(message?: any, ...optionalParams: any[]): void { -- cgit v1.2.3