From 0759eb8190a60ae1157986b50be62b96dd0fe1dd Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Tue, 16 Apr 2024 12:22:49 +0300 Subject: [PATCH] do the javascript Adding types doesn't actually make `undefined`, `null` and `""` different things. --- package.json | 2 -- src/zigSetup.ts | 16 ++++++++-------- src/zigUtil.ts | 8 ++++---- src/zls.ts | 14 +++++++------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 94ff73d..5b9164c 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,6 @@ "zig.path": { "scope": "machine-overridable", "type": "string", - "default": null, "description": "Set a custom path to the Zig binary. The string \"zig\" means lookup zig in PATH." }, "zig.checkForUpdate": { @@ -160,7 +159,6 @@ "zig.zls.path": { "scope": "machine-overridable", "type": "string", - "default": null, "description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. The string \"zls\" means lookup ZLS in PATH.", "format": "path" }, diff --git a/src/zigSetup.ts b/src/zigSetup.ts index 8ab9314..e5f5b3c 100644 --- a/src/zigSetup.ts +++ b/src/zigSetup.ts @@ -191,7 +191,7 @@ async function checkUpdate(context: vscode.ExtensionContext) { async function getUpdatedVersion(context: vscode.ExtensionContext): Promise { const configuration = vscode.workspace.getConfiguration("zig"); - const zigPath = configuration.get("path", null); + const zigPath = configuration.get("path"); const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath; if (!zigPath?.startsWith(zigBinPath)) return null; @@ -233,14 +233,14 @@ export async function setupZig(context: vscode.ExtensionContext) { const zigConfig = vscode.workspace.getConfiguration("zig"); const initialSetupDone = zigConfig.get("initialSetupDone", false); - const zigPath = zigConfig.get("path"); - if (zigPath === "" || (initialSetupDone && zigPath === null)) { + const zigPath = zigConfig.get("path"); + if (zigPath === "" && initialSetupDone) { await zigConfig.update("path", "zig", true); } const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - const zlsPath = zlsConfig.get("path"); - if (zlsPath === "" || (initialSetupDone && zlsPath === null)) { + const zlsPath = zlsConfig.get("path"); + if (zlsPath === "" && initialSetupDone) { await zlsConfig.update("path", "zls", true); } } @@ -276,7 +276,7 @@ export async function setupZig(context: vscode.ExtensionContext) { async function initialSetup(context: vscode.ExtensionContext): Promise { const zigConfig = vscode.workspace.getConfiguration("zig"); - if (zigConfig.get("path", null) === null) { + if (!zigConfig.get("path")) { const zigResponse = await vscode.window.showInformationMessage( "Zig path hasn't been set, do you want to specify the path or install Zig?", { modal: true }, @@ -287,7 +287,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise switch (zigResponse) { case "Install": await selectVersionAndInstall(context); - const zigPath = vscode.workspace.getConfiguration("zig").get("path", null); + const zigPath = vscode.workspace.getConfiguration("zig").get("path"); if (!zigPath) return false; break; case "Specify path": @@ -314,7 +314,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (zlsConfig.get("path", null) === null) { + if (!zlsConfig.get("path")) { const zlsResponse = await vscode.window.showInformationMessage( "We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to install it?", { modal: true }, diff --git a/src/zigUtil.ts b/src/zigUtil.ts index 3d6cee4..2c1e85d 100644 --- a/src/zigUtil.ts +++ b/src/zigUtil.ts @@ -49,8 +49,8 @@ export function handleConfigOption(input: string): string { return input; } -export function getExePath(exePath: string | null, exeName: string, optionName: string): string { - if (exePath === null) { +export function getExePath(exePath: string | null | undefined, exeName: string, optionName: string): string { + if (!exePath) { exePath = which.sync(exeName, { nothrow: true }); } else { // allow passing predefined variables @@ -64,7 +64,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName: } let message; - if (exePath === null) { + if (!exePath) { message = `Could not find ${exeName} in PATH`; } else if (!fs.existsSync(exePath)) { message = `\`${optionName}\` ${exePath} does not exist`; @@ -82,7 +82,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName: export function getZigPath(): string { const configuration = vscode.workspace.getConfiguration("zig"); - const zigPath = configuration.get("path", null); + const zigPath = configuration.get("path"); const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH return getExePath(exePath, "zig", "zig.path"); } diff --git a/src/zls.ts b/src/zls.ts index 6386532..57f8d65 100644 --- a/src/zls.ts +++ b/src/zls.ts @@ -80,7 +80,7 @@ export async function stopClient() { // returns the file system path to the zls executable export function getZLSPath(): string { const configuration = vscode.workspace.getConfiguration("zig.zls"); - const zlsPath = configuration.get("path", null); + const zlsPath = configuration.get("path"); const exePath = zlsPath !== "zls" ? zlsPath : null; // the string "zls" means lookup in PATH return getExePath(exePath, "zls", "zig.zls.path"); } @@ -201,7 +201,7 @@ async function getVersionIndex(): Promise { // checks whether there is newer version on master async function checkUpdate(context: vscode.ExtensionContext) { const configuration = vscode.workspace.getConfiguration("zig.zls"); - const zlsPath = configuration.get("path", null); + const zlsPath = configuration.get("path"); const zlsBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath; if (!zlsPath?.startsWith(zlsBinPath)) return; @@ -238,7 +238,7 @@ export async function install(context: vscode.ExtensionContext, ask: boolean) { } // Zig 0.9.0 was the first version to have a tagged zls release if (semver.lt(zigVersion, "0.9.0")) { - if (zlsConfiguration.get("path", null) !== null) { + if (zlsConfiguration.get("path")) { void vscode.window.showErrorMessage(`ZLS is not available for Zig version ${zigVersion.version}`); } await zlsConfiguration.update("path", undefined, true); @@ -343,8 +343,8 @@ async function installVersion(context: vscode.ExtensionContext, version: semver. } function checkInstalled(): boolean { - const zlsPath = vscode.workspace.getConfiguration("zig.zls").get("path", null); - if (zlsPath === null) { + const zlsPath = vscode.workspace.getConfiguration("zig.zls").get("path"); + if (!zlsPath) { void vscode.window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.", { modal: true, }); @@ -395,7 +395,7 @@ export async function activate(context: vscode.ExtensionContext) { ) { await stopClient(); const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (zlsConfig.get("path", null) !== null) { + if (zlsConfig.get("path")) { await startClient(); } } @@ -403,7 +403,7 @@ export async function activate(context: vscode.ExtensionContext) { ); const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (zlsConfig.get("path", null) === null) return; + if (!zlsConfig.get("path")) return; if (zlsConfig.get("checkForUpdate") && (await shouldCheckUpdate(context, "zlsUpdate"))) { await checkUpdate(context); }