Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(harper.js): added ability to ignore lints
Elijah Potter487bc59d4b01
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rsindex 8fd811f50..4e79533c0 100644--- a/harper-wasm/src/lib.rs+++ b/harper-wasm/src/lib.rs@@ -6,7 +6,7 @@ use std::sync::Arc;use harper_core::language_detection::is_doc_likely_english;use harper_core::linting::{LintGroup, LintGroupConfig, Linter as _};use harper_core::parsers::{IsolateEnglish, Markdown, Parser, PlainEnglish};-use harper_core::{remove_overlaps, Document, FstDictionary, FullDictionary, Lrc};+use harper_core::{remove_overlaps, Document, FstDictionary, FullDictionary, IgnoredLints, Lrc};use serde::{Deserialize, Serialize};use wasm_bindgen::prelude::wasm_bindgen;use wasm_bindgen::JsValue;@@ -43,15 +43,27 @@ make_serialize_fns_for!(Lint);make_serialize_fns_for!(Span);#[wasm_bindgen]+#[derive(Serialize, Deserialize, Debug, Copy, Clone)]pub enum Language {Plain,Markdown,}+impl Language {+ fn create_parser(&self) -> Box<dyn Parser> {+ match self {+ Language::Plain => Box::new(PlainEnglish),+ // TODO: Have a way to configure the Markdown parser+ Language::Markdown => Box::new(Markdown::default()),+ }+ }+}+#[wasm_bindgen]pub struct Linter {lint_group: LintGroup<Arc<FstDictionary>>,dictionary: Arc<FstDictionary>,+ ignored_lints: IgnoredLints,}#[wasm_bindgen]@@ -65,6 +77,7 @@ impl Linter {Self {lint_group: LintGroup::new(LintGroupConfig::default(), dictionary.clone()),dictionary,+ ignored_lints: IgnoredLints::default(),}}@@ -117,16 +130,22 @@ impl Linter {Ok(())}+ pub fn ignore_lint(&mut self, lint: Lint) {+ let document = Document::new_from_vec(+ lint.source.into(),+ &lint.language.create_parser(),+ &self.dictionary,+ );++ self.ignored_lints.ignore_lint(&lint.inner, &document);+ }+/// Perform the configured linting on the provided text.pub fn lint(&mut self, text: String, language: Language) -> Vec<Lint> {let source: Vec<_> = text.chars().collect();let source = Lrc::new(source);- let parser: Box<dyn Parser> = match language {- Language::Plain => Box::new(PlainEnglish),- // TODO: Have a way to configure the Markdown parser- Language::Markdown => Box::new(Markdown::default()),- };+ let parser = language.create_parser();let document = Document::new_from_vec(source.clone(), &parser, &FullDictionary::curated());@@ -134,9 +153,11 @@ impl Linter {remove_overlaps(&mut lints);+ self.ignored_lints.remove_ignored(&mut lints, &document);+lints.into_iter()- .map(|l| Lint::new(l, source.to_vec()))+ .map(|l| Lint::new(l, source.to_vec(), language)).collect()}}@@ -219,12 +240,21 @@ impl Suggestion {pub struct Lint {inner: harper_core::linting::Lint,source: Vec<char>,+ language: Language,}#[wasm_bindgen]impl Lint {- pub(crate) fn new(inner: harper_core::linting::Lint, source: Vec<char>) -> Self {- Self { inner, source }+ pub(crate) fn new(+ inner: harper_core::linting::Lint,+ source: Vec<char>,+ language: Language,+ ) -> Self {+ Self {+ inner,+ source,+ language,+ }}/// Get the content of the source material pointed to by [`Self::span`]diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.tsindex 10cfe916a..ea3b6169b 100644--- a/packages/harper.js/src/Linter.test.ts+++ b/packages/harper.js/src/Linter.test.ts@@ -131,6 +131,28 @@ for (const [linterName, Linter] of Object.entries(linters)) {expect(value).not.toBeNull();}});++ test(`${linterName} can ignore lints`, async () => {+ const linter = new Linter();+ 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);++ await linter.ignoreLint(firstRound[0]);++ const secondRound = await linter.lint(source);++ for (const lint of secondRound) {+ console.log(lint.message());+ }++ expect(secondRound.length).toBeLessThan(firstRound.length);+ });}test('Linters have the same config format', async () => {diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.tsindex 4976aa739..dfd802a98 100644--- a/packages/harper.js/src/Linter.ts+++ b/packages/harper.js/src/Linter.ts@@ -50,4 +50,7 @@ export default interface Linter {/** Convert a string to Chicago-style title case. */toTitleCase(text: string): Promise<string>;++ /** Ignore future instances of a lint from a previous linting run in future invocations. */+ ignoreLint(lint: Lint): Promise<void>;}diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.tsindex 03196248f..857888e03 100644--- a/packages/harper.js/src/LocalLinter.ts+++ b/packages/harper.js/src/LocalLinter.ts@@ -97,4 +97,10 @@ export default class LocalLinter implements Linter {await this.initialize();return this.inner!.get_lint_descriptions_as_json();}++ async ignoreLint(lint: Lint): Promise<void> {+ await this.initialize();++ this.inner!.ignore_lint(lint);+ }}diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.tsindex 675d1644f..aa28476ea 100644--- a/packages/harper.js/src/WorkerLinter/index.ts+++ b/packages/harper.js/src/WorkerLinter/index.ts@@ -113,6 +113,10 @@ export default class WorkerLinter implements Linter {return JSON.parse(await this.getDefaultLintConfigAsJSON()) as LintConfig;}+ async ignoreLint(lint: Lint): Promise<void> {+ return this.rpc('ignoreLint', [lint]);+ }+/** 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/vite.config.js b/packages/harper.js/vite.config.jsindex f23b88f49..dc7f19a56 100644--- a/packages/harper.js/vite.config.js+++ b/packages/harper.js/vite.config.js@@ -55,8 +55,7 @@ export default defineConfig({test: {browser: {provider: 'playwright',- enabled: true,- headless: true+ enabled: true}},assetsInclude: ['**/*.wasm']