Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and add tests for BatchError #12

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
27 changes: 27 additions & 0 deletions errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 {
static {
this.prototype.name = "BatchError";
}
Comment on lines +13 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using this in a static context.

Using this in a static context can be confusing. Use the class name instead.

-    this.prototype.name = "BatchError";
+    BatchError.prototype.name = "BatchError";
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static {
this.prototype.name = "BatchError";
}
static {
BatchError.prototype.name = "BatchError";
}
Tools
Biome

[error] 14-14: Using this in a static context can be confusing.

this refers to the class.
Unsafe fix: Use the class name instead.

(lint/complexity/noThisInStatic)


/**
* A result list that is successfully completed prior to the error.
*/
readonly results: unknown[];

constructor(message: string, results: unknown[]) {
super(message);

this.results = results;
}
}
42 changes: 42 additions & 0 deletions errors_test.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
});
});
10 changes: 2 additions & 8 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
29 changes: 8 additions & 21 deletions denops.ts → types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* This module provides type interfaces for [denops.vim].
*
* [denops.vim]: https://github.com/vim-denops/denops.vim
*
* @module
*/

/**
* API dispatcher
*/
Expand Down Expand Up @@ -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.
*/
Expand Down