Skip to content

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

fix(core): overlap removal did not respect priority (#2685)

Elijah Potter1d6d05e4ffef
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index a059cc2c5..4c37bcce9 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -78,6 +78,7 @@ pub fn remove_overlaps(lints: &mut Vec<Lint>) {
}
let mut remove_indices = VecDeque::new();
+ lints.sort_by_key(|l| l.priority);
lints.sort_by_key(|l| (l.span.start, !0 - l.span.end));
let mut cur = 0;
@@ -106,6 +107,7 @@ pub fn remove_overlaps_map<K: Ord>(lint_map: &mut BTreeMap<K, Vec<Lint>>) {
struct IndexedSpan {
rule_idx: usize,
lint_idx: usize,
+ priority: u8,
start: usize,
end: usize,
}
@@ -119,6 +121,7 @@ pub fn remove_overlaps_map<K: Ord>(lint_map: &mut BTreeMap<K, Vec<Lint>>) {
for (rule_idx, (_, lints)) in lint_map.iter().enumerate() {
for (lint_idx, lint) in lints.iter().enumerate() {
spans.push(IndexedSpan {
+ priority: lint.priority,
rule_idx,
lint_idx,
start: lint.span.start,
@@ -127,6 +130,7 @@ pub fn remove_overlaps_map<K: Ord>(lint_map: &mut BTreeMap<K, Vec<Lint>>) {
}
}
+ spans.sort_by_key(|span| span.priority);
spans.sort_by_key(|span| (span.start, usize::MAX - span.end));
let mut cur = 0;
@@ -154,6 +158,14 @@ pub fn remove_overlaps_map<K: Ord>(lint_map: &mut BTreeMap<K, Vec<Lint>>) {
#[cfg(test)]
mod tests {
+ use std::hash::DefaultHasher;
+ use std::hash::{Hash, Hasher};
+
+ use itertools::Itertools;
+ use quickcheck_macros::quickcheck;
+
+ use crate::linting::Lint;
+ use crate::remove_overlaps_map;
use crate::spell::FstDictionary;
use crate::{
Dialect, Document,
@@ -175,4 +187,31 @@ mod tests {
assert_eq!(lints.len(), 3);
}
+
+ #[quickcheck]
+ fn overlap_removals_have_equivalent_behavior(s: String) {
+ let doc = Document::new_plain_english_curated(&s);
+ let mut linter = LintGroup::new_curated(FstDictionary::curated(), Dialect::American);
+
+ let mut lint_map = linter.organized_lints(&doc);
+ let mut lint_flat: Vec<_> = lint_map.values().flatten().cloned().collect();
+
+ remove_overlaps_map(&mut lint_map);
+ remove_overlaps(&mut lint_flat);
+
+ let post_removal_flat: Vec<_> = lint_map.values().flatten().cloned().collect();
+
+ fn hash_lint(lint: &Lint) -> u64 {
+ let mut hasher = DefaultHasher::new();
+ lint.hash(&mut hasher);
+ hasher.finish()
+ }
+
+ // We want to ignore ordering, so let us hash these first and sort them.
+ let lint_flat_hashes: Vec<_> = lint_flat.iter().map(hash_lint).sorted().collect();
+ let post_removal_flat_hashes: Vec<_> =
+ post_removal_flat.iter().map(hash_lint).sorted().collect();
+
+ assert_eq!(post_removal_flat_hashes, lint_flat_hashes);
+ }
}
diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rs
index ff0c7ffda..19e2da99b 100644
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@ -7,7 +7,7 @@ use crate::{Span, render_markdown::render_markdown};
use super::{LintKind, Suggestion};
/// An error found in text.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Hash)]
pub struct Lint {
/// The location in the source text the error lies.
/// Important for automatic lint resolution through [`Self::suggestions`].
diff --git a/harper-core/src/span.rs b/harper-core/src/span.rs
index bf3a8a50e..a8da9be72 100644
--- a/harper-core/src/span.rs
+++ b/harper-core/src/span.rs
@@ -15,7 +15,7 @@ use crate::Token;
/// behavior or panics.
///
/// Although specific to `harper.js`, [this page may clear up any questions you have](https://writewithharper.com/docs/harperjs/spans).
-#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq)]
+#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
pub struct Span<T> {
/// The start index of the span.
pub start: usize,
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 90c506e5a..2d98829a5 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -22,7 +22,7 @@ use serde_wasm_bindgen::Serializer;
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::wasm_bindgen;
-/// Setup the WebAssembly module's logging.
+/// Set up the WebAssembly module's logging.
#[wasm_bindgen(start)]
pub fn setup() {
console_error_panic_hook::set_once();
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index 53227e6b2..dbc725889 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -526,6 +526,35 @@ for (const [linterName, Linter] of Object.entries(linters)) {
await linter.dispose();
});
+
+ test(`${linterName} returns correct suggestion for 'ned' with organizedLints.`, async () => {
+ const linter = new Linter({ binary });
+
+ const source = "I don't ned it.";
+ const lints = await linter.organizedLints(source);
+ const flattened = Object.values(lints).flat();
+
+ expect(flattened).toHaveLength(1);
+
+ const suggestions = flattened[0].suggestions().map((s) => s.get_replacement_text());
+
+ expect(suggestions).toContain('need');
+
+ await linter.dispose();
+ });
+
+ test(`${linterName} returns correct suggestion for 'ned'.`, async () => {
+ const linter = new Linter({ binary });
+
+ const source = "I don't ned it.";
+ const lints = await linter.lint(source);
+
+ expect(lints).toHaveLength(1);
+ const suggestions = lints[0].suggestions().map((s) => s.get_replacement_text());
+ expect(suggestions).toContain('need');
+
+ await linter.dispose();
+ });
}
// Disabled because it significantly slows down CI