Skip to content

Commit

Permalink
📦 Use unknownutil v3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Jul 11, 2023
1 parent 62b1d22 commit 2f73ce3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
9 changes: 3 additions & 6 deletions denops/@denops-private/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
isObject,
isString,
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
import { is } from "https://deno.land/x/unknownutil@v3.2.0/mod.ts#^";

export function errorSerializer(err: unknown): unknown {
if (err instanceof Error) {
Expand All @@ -15,10 +12,10 @@ export function errorSerializer(err: unknown): unknown {
}

export function errorDeserializer(err: unknown): unknown {
if (isString(err)) {
if (is.String(err)) {
try {
const obj = JSON.parse(err);
if (isObject(obj) && isString(obj.message)) {
if (is.Record(obj) && is.String(obj.message)) {
return Object.assign(new Error(obj.message), obj);
}
} catch {
Expand Down
5 changes: 3 additions & 2 deletions denops/@denops-private/host/invoker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { is } from "https://deno.land/x/unknownutil@v3.2.0/mod.ts#^";
import { Service } from "../service.ts";
import type { Meta } from "../../@denops/mod.ts";

Expand Down Expand Up @@ -80,8 +81,8 @@ export class Invoker {
}
}

export function isInvokerMethod(value: string): value is keyof Invoker {
return value in Invoker.prototype;
export function isInvokerMethod(value: unknown): value is keyof Invoker {
return is.String(value) && value in Invoker.prototype;
}

// https://github.com/vim-denops/denops.vim/issues/112
Expand Down
14 changes: 5 additions & 9 deletions denops/@denops-private/host/nvim.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
assertArray,
assertString,
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
import { assert, is } from "https://deno.land/x/unknownutil@v3.2.0/mod.ts#^";
import {
Client,
Session,
Expand Down Expand Up @@ -62,11 +59,10 @@ export class Neovim implements Host {
},

async invoke(method: unknown, args: unknown): Promise<unknown> {
assertString(method);
assertArray(args);
if (!isInvokerMethod(method)) {
throw new Error(`Method '${method}' is not defined in the invoker`);
}
assert(method, isInvokerMethod, {
message: `method '${method}' must be a key of Invoker`,
});
assert(args, is.Array, { message: `args '${args}' must be an array` });
// deno-lint-ignore no-explicit-any
return await (invoker[method] as any)(...args);
},
Expand Down
31 changes: 15 additions & 16 deletions denops/@denops-private/worker/script.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
assertObject,
assertString,
isObject,
isString,
} from "https://deno.land/x/unknownutil@v2.1.1/mod.ts#^";
import { assert, is } from "https://deno.land/x/unknownutil@v3.2.0/mod.ts#^";
import {
Client,
Session,
Expand Down Expand Up @@ -104,20 +99,20 @@ async function main(
}

function isMeta(v: unknown): v is Meta {
if (!isObject(v)) {
if (!is.Record(v)) {
return false;
}
if (!isString(v.mode) || !["release", "debug", "test"].includes(v.mode)) {
if (!is.String(v.mode) || !["release", "debug", "test"].includes(v.mode)) {
return false;
}
if (!isString(v.host) || !["vim", "nvim"].includes(v.host)) {
if (!is.String(v.host) || !["vim", "nvim"].includes(v.host)) {
return false;
}
if (!isString(v.version)) {
if (!is.String(v.version)) {
return false;
}
if (
!isString(v.platform) || !["windows", "mac", "linux"].includes(v.platform)
!is.String(v.platform) || !["windows", "mac", "linux"].includes(v.platform)
) {
return false;
}
Expand All @@ -129,11 +124,15 @@ patchConsole(`(${worker.name})`);

// Wait startup arguments and start 'main'
worker.addEventListener("message", (event: MessageEvent<unknown>) => {
assertObject(event.data);
assertString(event.data.scriptUrl);
if (!isMeta(event.data.meta)) {
throw new Error(`Invalid 'meta' is passed: ${event.data.meta}`);
}
assert(event.data, is.Record, {
message: `event.data '${event.data}' must be Record`,
});
assert(event.data.scriptUrl, is.String, {
message: `event.data.scriptUrl '${event.data.scriptUrl}' must be String`,
});
assert(event.data.meta, isMeta, {
message: `event.data.meta '${event.data.meta}' must be Meta`,
});
const { scriptUrl, meta } = event.data;
main(scriptUrl, meta).catch((e) => {
console.error(
Expand Down

0 comments on commit 2f73ce3

Please sign in to comment.