Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(harper.js): added ability configure `Linters` + unified tests

Elijah Potter97f1d3b235ea
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 0f8f74f69..cd243f4e4 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -89,7 +89,10 @@ impl Linter {
}
pub fn get_lint_config_as_object(&self) -> JsValue {
- serde_wasm_bindgen::to_value(&self.lint_group.config).unwrap()
+ // Important for downstream JSON serialization
+ let serializer = serde_wasm_bindgen::Serializer::json_compatible();
+
+ self.lint_group.config.serialize(&serializer).unwrap()
}
pub fn set_lint_config_from_object(&mut self, object: JsValue) -> Result<(), String> {
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
new file mode 100644
index 000000000..dc17f90f5
--- /dev/null
+++ b/packages/harper.js/src/Linter.test.ts
@@ -0,0 +1,110 @@
+import { expect, test } from 'vitest';
+import WorkerLinter from './WorkerLinter';
+import LocalLinter from './LocalLinter';
+
+const linters = {
+ WorkerLinter: WorkerLinter,
+ LocalLinter: LocalLinter
+};
+
+for (const [linterName, Linter] of Object.entries(linters)) {
+ test(`${linterName} detects repeated words`, async () => {
+ const linter = new Linter();
+
+ const lints = await linter.lint('The the problem is...');
+
+ expect(lints.length).toBe(1);
+ });
+
+ test(`${linterName} detects repeated words with multiple synchronous requests`, async () => {
+ const linter = new Linter();
+
+ const promises = [
+ linter.lint('The problem is that that...'),
+ linter.lint('The problem is...'),
+ linter.lint('The the problem is...')
+ ];
+
+ const results = [];
+
+ for (const promise of promises) {
+ results.push(await promise);
+ }
+
+ expect(results[0].length).toBe(1);
+ expect(results[0][0].suggestions().length).toBe(1);
+ expect(results[1].length).toBe(0);
+ expect(results[2].length).toBe(1);
+ });
+
+ test(`${linterName} detects repeated words with concurrent requests`, async () => {
+ const linter = new Linter();
+
+ const promises = [
+ linter.lint('The problem is that that...'),
+ linter.lint('The problem is...'),
+ linter.lint('The the problem is...')
+ ];
+
+ const results = await Promise.all(promises);
+
+ expect(results[0].length).toBe(1);
+ expect(results[0][0].suggestions().length).toBe(1);
+ expect(results[1].length).toBe(0);
+ expect(results[2].length).toBe(1);
+ });
+
+ test(`${linterName} detects lorem ipsum paragraph as not english`, async () => {
+ const linter = new Linter();
+
+ const result = await linter.isLikelyEnglish(
+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
+ );
+
+ expect(result).toBeTypeOf('boolean');
+ expect(result).toBe(false);
+ });
+
+ test(`${linterName} can run setup without issues`, async () => {
+ const linter = new Linter();
+
+ await linter.setup();
+ });
+
+ test(`${linterName} contains configuration option for repetition`, async () => {
+ const linter = new Linter();
+
+ const lintConfig = await linter.getLintConfig();
+ expect(lintConfig).toHaveProperty('repeated_words');
+ });
+}
+
+test('Linters have the same config format', async () => {
+ const configs = [];
+
+ for (const Linter of Object.values(linters)) {
+ const linter = new Linter();
+
+ configs.push(await linter.getLintConfig());
+ }
+
+ for (const config of configs) {
+ expect(config).toEqual(configs[0]);
+ expect(config).toBeTypeOf('object');
+ }
+});
+
+test('Linters have the same JSON config format', async () => {
+ const configs = [];
+
+ for (const Linter of Object.values(linters)) {
+ const linter = new Linter();
+
+ configs.push(await linter.getLintConfigAsJSON());
+ }
+
+ for (const config of configs) {
+ expect(config).toEqual(configs[0]);
+ expect(config).toBeTypeOf('string');
+ }
+});
diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.ts
index a34300cc5..2b697a374 100644
--- a/packages/harper.js/src/Linter.ts
+++ b/packages/harper.js/src/Linter.ts
@@ -1,4 +1,5 @@
import type { Lint, Span, Suggestion } from 'wasm';
+import { LintConfig } from './main';
/** A interface for an object that can perform linting actions. */
export default interface Linter {
@@ -16,4 +17,16 @@ export default interface Linter {
/** Determine which parts of a given string are intended to be English, returning those bits.
* The algorithm can be described as "proof of concept" and as such does not work terribly well.*/
isolateEnglish(text: string): Promise<string>;
+
+ /** Get the linter's current configuration. */
+ getLintConfig(): Promise<LintConfig>;
+
+ /** Set the linter's current configuration. */
+ setLintConfig(config: LintConfig): Promise<void>;
+
+ /** Get the linter's current configuration as JSON. */
+ getLintConfigAsJSON(): Promise<string>;
+
+ /** Set the linter's current configuration from JSON. */
+ setLintConfigWithJSON(config: string): Promise<void>;
}
diff --git a/packages/harper.js/src/LocalLinter.test.ts b/packages/harper.js/src/LocalLinter.test.ts
deleted file mode 100644
index f70641cb4..000000000
--- a/packages/harper.js/src/LocalLinter.test.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { expect, test } from 'vitest';
-import LocalLinter from './LocalLinter';
-
-test('detects repeated words', async () => {
- const linter = new LocalLinter();
-
- const lints = await linter.lint('The the problem is...');
-
- expect(lints.length).toBe(1);
-});
diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.ts
index a73b47456..36b4ca097 100644
--- a/packages/harper.js/src/LocalLinter.ts
+++ b/packages/harper.js/src/LocalLinter.ts
@@ -1,11 +1,19 @@
import type { Lint, Span, Suggestion, Linter as WasmLinter } from 'wasm';
import Linter from './Linter';
import loadWasm from './loadWasm';
+import { LintConfig } from './main';
/** A Linter that runs in the current JavaScript context (meaning it is allowed to block the event loop). */
export default class LocalLinter implements Linter {
private inner: WasmLinter | undefined;
+ /** Initialize the WebAssembly and construct the inner Linter. */
+ private async initialize(): Promise<void> {
+ const wasm = await loadWasm();
+ wasm.setup();
+ this.inner = wasm.Linter.new();
+ }
+
async setup(): Promise<void> {
await this.initialize();
this.inner!.lint('');
@@ -36,10 +44,27 @@ export default class LocalLinter implements Linter {
return this.inner!.isolate_english(text);
}
- /// Initialize the WebAssembly and construct the inner Linter.
- private async initialize(): Promise<void> {
- const wasm = await loadWasm();
- wasm.setup();
- this.inner = wasm.Linter.new();
+ async getLintConfig(): Promise<LintConfig> {
+ await this.initialize();
+
+ return this.inner!.get_lint_config_as_object();
+ }
+
+ async setLintConfig(config: LintConfig): Promise<void> {
+ await this.initialize();
+
+ this.inner!.set_lint_config_from_object(config);
+ }
+
+ async getLintConfigAsJSON(): Promise<string> {
+ await this.initialize();
+
+ return this.inner!.get_lint_config_as_json();
+ }
+
+ async setLintConfigWithJSON(config: string): Promise<void> {
+ await this.initialize();
+
+ this.inner!.set_lint_config_from_json(config);
}
}
diff --git a/packages/harper.js/src/WorkerLinter.test.ts b/packages/harper.js/src/WorkerLinter.test.ts
deleted file mode 100644
index b456eacf9..000000000
--- a/packages/harper.js/src/WorkerLinter.test.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { expect, test } from 'vitest';
-import WorkerLinter from './WorkerLinter';
-
-test('detects repeated words', async () => {
- const linter = new WorkerLinter();
-
- const lints = await linter.lint('The the problem is...');
-
- expect(lints.length).toBe(1);
-});
-
-test('detects repeated words with multiple synchronous requests', async () => {
- const linter = new WorkerLinter();
-
- const promises = [
- linter.lint('The problem is that that...'),
- linter.lint('The problem is...'),
- linter.lint('The the problem is...')
- ];
-
- const results = [];
-
- for (const promise of promises) {
- results.push(await promise);
- }
-
- expect(results[0].length).toBe(1);
- expect(results[0][0].suggestions().length).toBe(1);
- expect(results[1].length).toBe(0);
- expect(results[2].length).toBe(1);
-});
-
-test('detects repeated words with concurrent requests', async () => {
- const linter = new WorkerLinter();
-
- const promises = [
- linter.lint('The problem is that that...'),
- linter.lint('The problem is...'),
- linter.lint('The the problem is...')
- ];
-
- const results = await Promise.all(promises);
-
- expect(results[0].length).toBe(1);
- expect(results[0][0].suggestions().length).toBe(1);
- expect(results[1].length).toBe(0);
- expect(results[2].length).toBe(1);
-});
-
-test('detects lorem ipsum paragraph as not english', async () => {
- const linter = new WorkerLinter();
-
- const result = await linter.isLikelyEnglish(
- 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
- );
-
- expect(result).toBeTypeOf('boolean');
- expect(result).toBe(false);
-});
-
-test('can run setup without issues', async () => {
- const linter = new WorkerLinter();
-
- await linter.setup();
-});
diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.ts
index d9b609da7..9c1134bde 100644
--- a/packages/harper.js/src/WorkerLinter/index.ts
+++ b/packages/harper.js/src/WorkerLinter/index.ts
@@ -3,6 +3,7 @@ import type { Lint, Suggestion, Span } from 'wasm';
import Linter from '../Linter';
import Worker from './worker.js?worker&inline';
import { getWasmUri } from '../loadWasm';
+import { LintConfig } from '../main';
/** The data necessary to complete a request once the worker has responded. */
type RequestItem = {
@@ -75,6 +76,22 @@ export default class WorkerLinter implements Linter {
return await this.rpc('isolateEnglish', [text]);
}
+ async getLintConfig(): Promise<LintConfig> {
+ return JSON.parse(await this.getLintConfigAsJSON());
+ }
+
+ async setLintConfig(config: LintConfig): Promise<void> {
+ return await this.setLintConfigWithJSON(JSON.stringify(config));
+ }
+
+ async getLintConfigAsJSON(): Promise<string> {
+ return await this.rpc('getLintConfigAsJSON', []);
+ }
+
+ async setLintConfigWithJSON(config: string): Promise<void> {
+ return await this.rpc('setLintConfigWithJSON', [config]);
+ }
+
/** Run a procedure on the remote worker. */
private async rpc(procName: string, args: any[]): Promise<any> {
const promise = new Promise((resolve, reject) => {
diff --git a/packages/harper.js/src/main.ts b/packages/harper.js/src/main.ts
index 9f1ccd564..983925321 100644
--- a/packages/harper.js/src/main.ts
+++ b/packages/harper.js/src/main.ts
@@ -10,3 +10,5 @@ export enum SuggestionKind {
Replace = 0,
Remove = 1
}
+
+export type LintConfig = Record<string, boolean | undefined>;