Skip to content

Commit

Permalink
feat: add class for ToolCheckResult
Browse files Browse the repository at this point in the history
it is now easier to check if it's an error or not
  • Loading branch information
Totto16 committed Dec 10, 2024
1 parent 8d8b9f8 commit ef4a9d3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { checkHasError, extensionConfiguration, getOutputChannel } from "./utils";
import { extensionConfiguration, getOutputChannel } from "./utils";
import { ToolCheckFunc, Tool } from "./types";
import * as muon from "./tools/muon";

Expand Down Expand Up @@ -28,7 +28,7 @@ async function reloadFormatters(sourceRoot: string, context: vscode.ExtensionCon
const props = formatters[name];

const checkResult = await props.check();
if (checkHasError(checkResult)) {
if (checkResult.isError()) {
getOutputChannel().appendLine(`Failed to enable formatter ${name}: ${checkResult.error}`);
getOutputChannel().show(true);
return disposables;
Expand Down
4 changes: 2 additions & 2 deletions src/linters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { checkHasError, extensionConfiguration, getOutputChannel } from "./utils";
import { extensionConfiguration, getOutputChannel } from "./utils";
import { ExtensionConfiguration, LinterConfiguration, ToolCheckFunc, Tool } from "./types";
import * as muon from "./tools/muon";

Expand Down Expand Up @@ -39,7 +39,7 @@ async function reloadLinters(

const props = linters[name];
const checkResult = await props.check();
if (checkHasError(checkResult)) {
if (checkResult.isError()) {
getOutputChannel().appendLine(`Failed to enable linter ${name}: ${checkResult.error}`);
getOutputChannel().show(true);
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/tools/muon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";
import { ExecResult, exec, execFeed, extensionConfiguration, getOutputChannel } from "../utils";
import { Tool, type ToolCheckResult } from "../types";
import { Tool, ToolCheckResult } from "../types";
import { Version, type VersionArray } from "../version";

export async function lint(muon: Tool, root: string, document: vscode.TextDocument): Promise<vscode.Diagnostic[]> {
Expand Down Expand Up @@ -91,12 +91,12 @@ export async function check(): Promise<ToolCheckResult> {
} catch (e) {
const { error, stdout, stderr } = e as ExecResult;
console.log(error);
return { error: error!.message };
return ToolCheckResult.newError(error!.message);
}

const line1 = stdout.split("\n")[0].split(" ");
if (line1.length !== 2) {
return { error: `Invalid version string: ${line1}` };
return ToolCheckResult.newError(`Invalid version string: ${line1}`);
}

const ver = line1[1]
Expand All @@ -110,5 +110,5 @@ export async function check(): Promise<ToolCheckResult> {
return Number.parseInt(s);
}) as VersionArray;

return { tool: { path: muon_path, version: new Version(ver) } };
return ToolCheckResult.newTool({ path: muon_path, version: new Version(ver) });
}
41 changes: 40 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,46 @@ export type ToolCheckErrorResult = {
error: string;
};

export type ToolCheckResult = ToolCheckSuccessResult | ToolCheckErrorResult;
type ResultImpl = ToolCheckSuccessResult | ToolCheckErrorResult;

export class ToolCheckResult {
private readonly result: ResultImpl;

private constructor(result: ResultImpl) {
this.result = result;
}

static newError(error: string) {
return new ToolCheckResult({ error });
}

static newTool(tool: Tool) {
return new ToolCheckResult({ tool });
}

private hasErrorImpl(result: ResultImpl): result is ToolCheckErrorResult {
return !!result.error;
}

isError(): boolean {
return this.hasErrorImpl(this.result);
}

get error(): string {
if (!this.hasErrorImpl(this.result)) {
throw new Error("Wrong invocation of getter for 'error', check the state first");
}
return this.result.error;
}

get tool(): Tool {
if (this.hasErrorImpl(this.result)) {
throw new Error("Wrong invocation of getter for 'tool', check the state first");
}
return this.result.tool;
}
}

export type ToolCheckFunc = () => Promise<ToolCheckResult>;

export type LinterConfiguration = {
Expand Down
4 changes: 0 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,3 @@ export function whenFileExists(ctx: vscode.ExtensionContext, file: string, liste
export function mesonProgram(): string {
return which.sync(extensionConfiguration("mesonPath"));
}

export function checkHasError(result: ToolCheckResult): result is ToolCheckErrorResult {
return !!result.error;
}

0 comments on commit ef4a9d3

Please sign in to comment.