Skip to content

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

feat(harper.js): can export and import ignored lints

Elijah Potter12bfe0301c68
diff --git a/harper-core/src/ignored_lints.rs b/harper-core/src/ignored_lints.rs
index 07c97792f..66480fb72 100644
--- a/harper-core/src/ignored_lints.rs
+++ b/harper-core/src/ignored_lints.rs
@@ -1,11 +1,12 @@
use std::hash::{DefaultHasher, Hash, Hasher};
use hashbrown::HashSet;
+use serde::{Deserialize, Serialize};
use crate::{linting::Lint, Document};
/// A structure that keeps track of lints that have been ignored by users.
-#[derive(Debug, Default)]
+#[derive(Debug, Default, Serialize, Deserialize)]
pub struct IgnoredLints {
context_hashes: HashSet<u64>,
}
@@ -15,6 +16,11 @@ impl IgnoredLints {
Self::default()
}
+ /// Move entries from another instance to this one.
+ pub fn append(&mut self, other: Self) {
+ self.context_hashes.extend(other.context_hashes)
+ }
+
fn hash_lint_context(&self, lint: &Lint, document: &Document) -> u64 {
let problem_tokens = document.token_indices_intersecting(lint.span);
let prequel_tokens = lint
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 4e79533c0..5a7ed8e55 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -160,6 +160,20 @@ impl Linter {
.map(|l| Lint::new(l, source.to_vec(), language))
.collect()
}
+
+ /// Export the linter's ignored lints as a privacy-respecting JSON list of hashes.
+ pub fn export_ignored_lints(&self) -> String {
+ serde_json::to_string(&self.ignored_lints).unwrap()
+ }
+
+ /// Import into the linter's ignored lints from a privacy-respecting JSON list of hashes.
+ pub fn import_ignored_lints(&mut self, json: String) -> Result<(), String> {
+ let list: IgnoredLints = serde_json::from_str(&json).map_err(|err| err.to_string())?;
+
+ self.ignored_lints.append(list);
+
+ Ok(())
+ }
}
impl Default for Linter {
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index ea3b6169b..84fd96bde 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -137,9 +137,6 @@ for (const [linterName, Linter] of Object.entries(linters)) {
const source = 'This is an test.';
const firstRound = await linter.lint(source);
- for (const lint of firstRound) {
- console.log(lint.message());
- }
expect(firstRound.length).toBeGreaterThanOrEqual(1);
@@ -147,11 +144,30 @@ for (const [linterName, Linter] of Object.entries(linters)) {
const secondRound = await linter.lint(source);
- for (const lint of secondRound) {
- console.log(lint.message());
+ expect(secondRound.length).toBeLessThan(firstRound.length);
+ });
+
+ test(`${linterName} can reimport ignored lints.`, async () => {
+ const source = 'This is an test of xporting lints.';
+
+ const firstLinter = new Linter();
+
+ const firstLints = await firstLinter.lint(source);
+
+ for (const lint of firstLints) {
+ await firstLinter.ignoreLint(lint);
}
- expect(secondRound.length).toBeLessThan(firstRound.length);
+ const exported = await firstLinter.exportIgnoredLints();
+
+ /// Create a new instance and reimport the lints.
+ const secondLinter = new Linter();
+ await secondLinter.importIgnoredLints(exported);
+
+ const secondLints = await secondLinter.lint(source);
+
+ expect(firstLints.length).toBeGreaterThan(secondLints.length);
+ expect(secondLints.length).toBe(0);
});
}
diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.ts
index dfd802a98..ec440135f 100644
--- a/packages/harper.js/src/Linter.ts
+++ b/packages/harper.js/src/Linter.ts
@@ -53,4 +53,11 @@ export default interface Linter {
/** Ignore future instances of a lint from a previous linting run in future invocations. */
ignoreLint(lint: Lint): Promise<void>;
+
+ /** Export the ignored lints to a JSON list of privacy-respecting hashes. */
+ exportIgnoredLints(): Promise<string>;
+
+ /** Import ignored lints from a JSON list to the linter.
+ * This function appends to the existing lints, if any. */
+ importIgnoredLints(json: string): Promise<void>;
}
diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.ts
index 857888e03..7d9611e87 100644
--- a/packages/harper.js/src/LocalLinter.ts
+++ b/packages/harper.js/src/LocalLinter.ts
@@ -103,4 +103,16 @@ export default class LocalLinter implements Linter {
this.inner!.ignore_lint(lint);
}
+
+ async exportIgnoredLints(): Promise<string> {
+ await this.initialize();
+
+ return this.inner!.export_ignored_lints();
+ }
+
+ async importIgnoredLints(json: string): Promise<void> {
+ await this.initialize();
+
+ return this.inner!.import_ignored_lints(json);
+ }
}
diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.ts
index aa28476ea..f5159ae6c 100644
--- a/packages/harper.js/src/WorkerLinter/index.ts
+++ b/packages/harper.js/src/WorkerLinter/index.ts
@@ -117,6 +117,14 @@ export default class WorkerLinter implements Linter {
return this.rpc('ignoreLint', [lint]);
}
+ async exportIgnoredLints(): Promise<string> {
+ return this.rpc('exportIgnoredLints', []);
+ }
+
+ async importIgnoredLints(json: string): Promise<void> {
+ return this.rpc('importIgnoredLints', [json]);
+ }
+
/** Run a procedure on the remote worker. */
private async rpc(procName: string, args: any[]): Promise<any> {
const promise = new Promise((resolve, reject) => {