Skip to content

Commit

Permalink
Merge pull request #393 from vim-denops/unknownutil-v4
Browse files Browse the repository at this point in the history
📦 Update unknownutil to v4
  • Loading branch information
lambdalisue authored Aug 3, 2024
2 parents cd628cd + 56debde commit 0608102
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 49 deletions.
7 changes: 5 additions & 2 deletions denops/@denops-private/denops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
type Dispatcher,
type Meta,
} from "jsr:@denops/core@^7.0.0";
import { ensure, is } from "jsr:@core/unknownutil@^3.18.1";
import { ensure } from "jsr:@core/unknownutil@^4.0.0/ensure";
import { isTupleOf } from "jsr:@core/unknownutil@^4.0.0/is/tuple-of";
import { isArray } from "jsr:@core/unknownutil@^4.0.0/is/array";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import type { Host as HostOrigin } from "./host.ts";
import type { Service as ServiceOrigin } from "./service.ts";

const isBatchReturn = is.TupleOf([is.Array, is.String] as const);
const isBatchReturn = isTupleOf([isArray, isString] as const);

export type Host = Pick<HostOrigin, "redraw" | "call" | "batch">;

Expand Down
4 changes: 2 additions & 2 deletions denops/@denops-private/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is } from "jsr:@core/unknownutil@^3.18.1";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import {
fromErrorObject,
isErrorObject,
Expand All @@ -16,7 +16,7 @@ export function errorSerializer(err: unknown): unknown {
}

export function errorDeserializer(err: unknown): unknown {
if (is.String(err)) {
if (isString(err)) {
const obj = tryOr(() => JSON.parse(err), undefined);
if (isErrorObject(obj)) {
return fromErrorObject(obj);
Expand Down
8 changes: 4 additions & 4 deletions denops/@denops-private/error_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { assert, assertEquals, assertInstanceOf } from "jsr:@std/assert@^1.0.1";
import { is } from "jsr:@core/unknownutil@^3.18.1";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import { errorDeserializer, errorSerializer } from "./error.ts";

Deno.test("errorSerializer", async (t) => {
await t.step("serializes Error", () => {
const err = new Error("error message");
const ret = errorSerializer(err);
assert(is.String(ret));
assert(isString(ret));
assertEquals({ ...JSON.parse(ret), stack: "stack" }, {
attributes: {},
message: "error message",
Expand All @@ -19,7 +19,7 @@ Deno.test("errorSerializer", async (t) => {
await t.step("serializes String", () => {
const err = "error message";
const ret = errorSerializer(err);
assert(is.String(ret));
assert(isString(ret));
assertEquals(ret, "error message");
});

Expand All @@ -29,7 +29,7 @@ Deno.test("errorSerializer", async (t) => {
message: "error message",
};
const ret = errorSerializer(err);
assert(is.String(ret));
assert(isString(ret));
assertEquals(JSON.parse(ret), {
type: "error",
message: "error message",
Expand Down
24 changes: 15 additions & 9 deletions denops/@denops-private/host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ensure, is, type Predicate } from "jsr:@core/unknownutil@^3.18.1";
import type { Predicate } from "jsr:@core/unknownutil@^4.0.0/type";
import { ensure } from "jsr:@core/unknownutil@^4.0.0/ensure";
import { asOptional } from "jsr:@core/unknownutil@^4.0.0/as/optional";
import { isArray } from "jsr:@core/unknownutil@^4.0.0/is/array";
import { isParametersOf } from "jsr:@core/unknownutil@^4.0.0/is/parameters-of";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import { isUnknown } from "jsr:@core/unknownutil@^4.0.0/is/unknown";

/**
* Host (Vim/Neovim) which is visible from Service
Expand Down Expand Up @@ -94,15 +100,15 @@ export function invoke(
}

const serviceMethodArgs = {
load: is.ParametersOf([is.String, is.String] as const),
unload: is.ParametersOf([is.String] as const),
reload: is.ParametersOf([is.String] as const),
interrupt: is.ParametersOf([is.OptionalOf(is.Unknown)] as const),
dispatch: is.ParametersOf([is.String, is.String, is.Array] as const),
dispatchAsync: is.ParametersOf(
[is.String, is.String, is.Array, is.String, is.String] as const,
load: isParametersOf([isString, isString] as const),
unload: isParametersOf([isString] as const),
reload: isParametersOf([isString] as const),
interrupt: isParametersOf([asOptional(isUnknown)] as const),
dispatch: isParametersOf([isString, isString, isArray] as const),
dispatchAsync: isParametersOf(
[isString, isString, isArray, isString, isString] as const,
),
close: is.ParametersOf([] as const),
close: isParametersOf([] as const),
} as const satisfies {
[K in keyof ServiceForInvoke]: Predicate<Parameters<ServiceForInvoke[K]>>;
};
24 changes: 15 additions & 9 deletions denops/@denops-private/host/nvim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ensure, is } from "jsr:@core/unknownutil@^3.18.1";
import { ensure } from "jsr:@core/unknownutil@^4.0.0/ensure";
import { isArray } from "jsr:@core/unknownutil@^4.0.0/is/array";
import { isNull } from "jsr:@core/unknownutil@^4.0.0/is/null";
import { isNumber } from "jsr:@core/unknownutil@^4.0.0/is/number";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import { isTupleOf } from "jsr:@core/unknownutil@^4.0.0/is/tuple-of";
import { isUnionOf } from "jsr:@core/unknownutil@^4.0.0/is/union-of";
import { Client, Session } from "jsr:@lambdalisue/messagepack-rpc@^2.4.0";
import { errorDeserializer, errorSerializer } from "../error.ts";
import { getVersionOr } from "../version.ts";
Expand Down Expand Up @@ -27,8 +33,8 @@ export class Neovim implements Host {
}
return invoke(
this.#service,
ensure(method, is.String),
ensure(args, is.Array),
ensure(method, isString),
ensure(args, isArray),
);
},

Expand Down Expand Up @@ -130,16 +136,16 @@ export class Neovim implements Host {
// nvim_call_function throws a special error object
// https://github.com/neovim/neovim/blob/5dc0bdfe98b59bb03226167ed541d17cc5af30b1/src/nvim/api/vimscript.c#L260
// https://github.com/neovim/neovim/blob/5dc0bdfe98b59bb03226167ed541d17cc5af30b1/src/nvim/api/private/defs.h#L63-L66
const isNvimErrorObject = is.TupleOf([is.Number, is.String] as const);
const isNvimErrorObject = isTupleOf([isNumber, isString] as const);

// nvim_call_atomics returns a tuple of [return values, error details]
const isNvimCallAtomicReturn = is.TupleOf(
const isNvimCallAtomicReturn = isTupleOf(
[
is.Array,
is.OneOf([
is.Null,
isArray,
isUnionOf([
isNull,
// the index, the error type, the error message
is.TupleOf([is.Number, is.Number, is.String] as const),
isTupleOf([isNumber, isNumber, isString] as const),
]),
] as const,
);
21 changes: 13 additions & 8 deletions denops/@denops-private/host/vim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ensure, is } from "jsr:@core/unknownutil@^3.18.1";
import { ensure } from "jsr:@core/unknownutil@^4.0.0/ensure";
import { isArray } from "jsr:@core/unknownutil@^4.0.0/is/array";
import { isLiteralOf } from "jsr:@core/unknownutil@^4.0.0/is/literal-of";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import { isTupleOf } from "jsr:@core/unknownutil@^4.0.0/is/tuple-of";
import { isUnknown } from "jsr:@core/unknownutil@^4.0.0/is/unknown";
import {
Client,
type Message,
Expand Down Expand Up @@ -120,16 +125,16 @@ export class Vim implements Host {
}
}

const isCallReturn = is.TupleOf([is.Unknown, is.String] as const);
const isCallReturn = isTupleOf([isUnknown, isString] as const);

const isBatchReturn = is.TupleOf([is.Array, is.String] as const);
const isBatchReturn = isTupleOf([isArray, isString] as const);

const isVoidMessage = is.TupleOf([is.LiteralOf("void")] as const);
const isVoidMessage = isTupleOf([isLiteralOf("void")] as const);

const isInvokeMessage = is.TupleOf(
const isInvokeMessage = isTupleOf(
[
is.LiteralOf("invoke"),
is.String,
is.Array,
isLiteralOf("invoke"),
isString,
isArray,
] as const,
);
2 changes: 1 addition & 1 deletion denops/@denops-private/host_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
assertSpyCalls,
stub,
} from "jsr:@std/testing@^1.0.0-rc.5/mock";
import { AssertError } from "jsr:@core/unknownutil@^3.18.1";
import { AssertError } from "jsr:@core/unknownutil@^4.0.0/assert";
import { unimplemented } from "jsr:@lambdalisue/errorutil@^1.1.0";
import { invoke, type Service } from "./host.ts";

Expand Down
15 changes: 9 additions & 6 deletions denops/@denops-private/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { Meta } from "jsr:@denops/core@^7.0.0";
import { is, type Predicate } from "jsr:@core/unknownutil@^3.18.1";
import type { Predicate } from "jsr:@core/unknownutil@^4.0.0/type";
import { isLiteralOneOf } from "jsr:@core/unknownutil@^4.0.0/is/literal-one-of";
import { isObjectOf } from "jsr:@core/unknownutil@^4.0.0/is/object-of";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";

export const isMeta: Predicate<Meta> = is.ObjectOf({
mode: is.LiteralOneOf(["release", "debug", "test"] as const),
host: is.LiteralOneOf(["vim", "nvim"] as const),
version: is.String,
platform: is.LiteralOneOf(["windows", "mac", "linux"] as const),
export const isMeta: Predicate<Meta> = isObjectOf({
mode: isLiteralOneOf(["release", "debug", "test"] as const),
host: isLiteralOneOf(["vim", "nvim"] as const),
version: isString,
platform: isLiteralOneOf(["windows", "mac", "linux"] as const),
});
19 changes: 12 additions & 7 deletions denops/@denops-private/version_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { assert, assertEquals } from "jsr:@std/assert@^1.0.1";
import { resolvesNext, stub } from "jsr:@std/testing@^1.0.0-rc.5/mock";
import type { SemVer } from "jsr:@std/semver@^0.224.3/types";
import { is, type Predicate } from "jsr:@core/unknownutil@^3.18.1";
import type { Predicate } from "jsr:@core/unknownutil@^4.0.0/type";
import { isArrayOf } from "jsr:@core/unknownutil@^4.0.0/is/array-of";
import { isNumber } from "jsr:@core/unknownutil@^4.0.0/is/number";
import { isObjectOf } from "jsr:@core/unknownutil@^4.0.0/is/object-of";
import { isString } from "jsr:@core/unknownutil@^4.0.0/is/string";
import { isUnionOf } from "jsr:@core/unknownutil@^4.0.0/is/union-of";
import { getVersionOr } from "./version.ts";

Deno.test("getVersionOr()", async (t) => {
Expand Down Expand Up @@ -53,12 +58,12 @@ Deno.test("getVersionOr()", async (t) => {
});
});

const isSemVer = is.ObjectOf({
major: is.Number,
minor: is.Number,
patch: is.Number,
prerelease: is.ArrayOf(is.UnionOf([is.String, is.Number])),
build: is.ArrayOf(is.String),
const isSemVer = isObjectOf({
major: isNumber,
minor: isNumber,
patch: isNumber,
prerelease: isArrayOf(isUnionOf([isString, isNumber])),
build: isArrayOf(isString),
}) satisfies Predicate<SemVer>;

async function isInsideWorkTree(): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion denops/@denops-private/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
readableStreamFromWorker,
writableStreamFromWorker,
} from "jsr:@lambdalisue/workerio@^4.0.1";
import { ensure } from "jsr:@core/unknownutil@^3.18.1";
import { ensure } from "jsr:@core/unknownutil@^4.0.0/ensure";
import { pop } from "jsr:@lambdalisue/streamtools@^1.0.0";
import { asyncSignal } from "jsr:@milly/async-signal@^1.0.0";
import type { Meta } from "jsr:@denops/core@^7.0.0";
Expand Down

0 comments on commit 0608102

Please sign in to comment.