From 9ef6ce2b4c96666b600489e2b2bc5b2479ffcb62 Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 30 Jul 2024 02:13:44 +0900 Subject: [PATCH 1/4] :muscle: separate modules types and error classes --- errors.ts | 28 ++++++++++++++++++++++++++++ mod.ts | 10 ++-------- denops.ts => types.ts | 29 ++++++++--------------------- 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 errors.ts rename denops.ts => types.ts (89%) diff --git a/errors.ts b/errors.ts new file mode 100644 index 0000000..987fe26 --- /dev/null +++ b/errors.ts @@ -0,0 +1,28 @@ +/** + * This module provides error classes for [denops.vim]. + * + * [denops.vim]: https://github.com/vim-denops/denops.vim + * + * @module + */ + +/** + * Batch error raised when one of the functions fails during batch process. + */ +export class BatchError extends Error { + /** + * A result list that is successfully completed prior to the error. + */ + readonly results: unknown[]; + + constructor(message: string, results: unknown[]) { + super(message); + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, BatchError); + } + + this.name = this.constructor.name; + this.results = results; + } +} diff --git a/mod.ts b/mod.ts index 9f1ec4a..7834e1f 100644 --- a/mod.ts +++ b/mod.ts @@ -23,11 +23,5 @@ * @module */ -export { BatchError } from "./denops.ts"; -export type { - Context, - Denops, - Dispatcher, - Entrypoint, - Meta, -} from "./denops.ts"; +export * from "./errors.ts"; +export type * from "./types.ts"; diff --git a/denops.ts b/types.ts similarity index 89% rename from denops.ts rename to types.ts index d0872ed..74f1862 100644 --- a/denops.ts +++ b/types.ts @@ -1,3 +1,11 @@ +/** + * This module provides type interfaces for [denops.vim]. + * + * [denops.vim]: https://github.com/vim-denops/denops.vim + * + * @module + */ + /** * API dispatcher */ @@ -34,27 +42,6 @@ export interface Meta { readonly platform: "windows" | "mac" | "linux"; } -/** - * Batch error raised when one of the functions fails during batch process. - */ -export class BatchError extends Error { - /** - * A result list that is successfully completed prior to the error. - */ - readonly results: unknown[]; - - constructor(message: string, results: unknown[]) { - super(message); - - if (Error.captureStackTrace) { - Error.captureStackTrace(this, BatchError); - } - - this.name = this.constructor.name; - this.results = results; - } -} - /** * Denops is a facade instance visible from each denops plugin. */ From 59c789107be8623aa36b86b7b172faff2a346144 Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 30 Jul 2024 02:40:53 +0900 Subject: [PATCH 2/4] :herb: add errors tests --- deno.jsonc | 1 + errors_test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 errors_test.ts diff --git a/deno.jsonc b/deno.jsonc index 9054ca8..1e67cc9 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -11,6 +11,7 @@ "update:commit": "deno task -q update --commit --pre-commit=fmt,lint" }, "imports": { + "@std/assert": "jsr:@std/assert@^1.0.1", "jsr:@denops/core": "./mod.ts" } } diff --git a/errors_test.ts b/errors_test.ts new file mode 100644 index 0000000..2d4ef2b --- /dev/null +++ b/errors_test.ts @@ -0,0 +1,42 @@ +import { + assert, + assertEquals, + assertInstanceOf, + assertMatch, +} from "@std/assert"; + +import { BatchError } from "./errors.ts"; + +Deno.test("BatchError", async (t) => { + await t.step(".constructor()", async (t) => { + await t.step("constructs an instance", () => { + const actual = new BatchError("foo", ["bar", 1, true]); + assertInstanceOf(actual, BatchError); + }); + }); + await t.step(".name getter", async (t) => { + await t.step("returns 'BatchError'", () => { + const actual = new BatchError("foo", ["bar", 1, true]); + assertEquals(actual.name, "BatchError"); + }); + }); + await t.step(".message getter", async (t) => { + await t.step("returns an error message", () => { + const actual = new BatchError("foo", ["bar", 1, true]); + assertEquals(actual.message, "foo"); + }); + }); + await t.step(".stack getter", async (t) => { + await t.step("returns an error stack trace", () => { + const actual = new BatchError("foo", ["bar", 1, true]); + assert(actual.stack); + assertMatch(actual.stack, /\bat .*errors_test\.ts:\d+:\d+\n/); + }); + }); + await t.step(".results getter", async (t) => { + await t.step("returns a results array", () => { + const actual = new BatchError("foo", ["bar", 1, true]); + assertEquals(actual.results, ["bar", 1, true]); + }); + }); +}); From 5c2d4459353eb668606a8fe5e37e6cd65794e2cf Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 30 Jul 2024 02:45:15 +0900 Subject: [PATCH 3/4] :muscle: remove unnecessary codes `Error.captureStackTrace()` is v8 method that provide a stack trace for custom (NOT extends from `Error`) error object. It is not necessary if the class extends from `Error`. --- errors.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/errors.ts b/errors.ts index 987fe26..611cbcf 100644 --- a/errors.ts +++ b/errors.ts @@ -18,10 +18,6 @@ export class BatchError extends Error { constructor(message: string, results: unknown[]) { super(message); - if (Error.captureStackTrace) { - Error.captureStackTrace(this, BatchError); - } - this.name = this.constructor.name; this.results = results; } From bad30fe4363bb1c1355261e20e7092758209485d Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 30 Jul 2024 02:54:44 +0900 Subject: [PATCH 4/4] :muscle: define `name` on prototype It is recommended that the `name` property be defined as a string on the prototype. --- errors.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/errors.ts b/errors.ts index 611cbcf..b405b7f 100644 --- a/errors.ts +++ b/errors.ts @@ -10,6 +10,10 @@ * Batch error raised when one of the functions fails during batch process. */ export class BatchError extends Error { + static { + this.prototype.name = "BatchError"; + } + /** * A result list that is successfully completed prior to the error. */ @@ -18,7 +22,6 @@ export class BatchError extends Error { constructor(message: string, results: unknown[]) { super(message); - this.name = this.constructor.name; this.results = results; } }