Skip to content

Commit 0bd1996

Browse files
Merge pull request #926 from Automattic/fix-cache
hotfix(core): properly store spans in `PatternLinter` cache
2 parents 647336f + 4c5805f commit 0bd1996

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

harper-core/src/linting/lint_group.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ impl Linter for LintGroup {
383383
let config_hash = self.hasher_builder.hash_one(&self.config);
384384
let key = (chunk_chars.into(), config_hash);
385385

386-
if let Some(hit) = self.chunk_pattern_cache.get(&key) {
387-
results.extend(hit.iter().cloned());
386+
let mut chunk_results = if let Some(hit) = self.chunk_pattern_cache.get(&key) {
387+
hit.clone()
388388
} else {
389389
let mut pattern_lints = Vec::new();
390390

@@ -394,10 +394,21 @@ impl Linter for LintGroup {
394394
}
395395
}
396396

397+
// Make the spans relative to the chunk start
398+
for lint in &mut pattern_lints {
399+
lint.span.pull_by(chunk_span.start);
400+
}
401+
397402
self.chunk_pattern_cache.put(key, pattern_lints.clone());
403+
pattern_lints
404+
};
398405

399-
results.append(&mut pattern_lints);
406+
// Bring the spans back into document-space
407+
for lint in &mut chunk_results {
408+
lint.span.push_by(chunk_span.start);
400409
}
410+
411+
results.append(&mut chunk_results);
401412
}
402413

403414
results

harper-core/src/span.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::ops::Range;
22

33
use serde::{Deserialize, Serialize};
44

5+
use crate::CharStringExt;
6+
57
/// A window in a [`char`] sequence.
68
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
79
pub struct Span {
@@ -57,7 +59,14 @@ impl Span {
5759

5860
/// Get the associated content. Will panic if any aspect is invalid.
5961
pub fn get_content<'a>(&self, source: &'a [char]) -> &'a [char] {
60-
self.try_get_content(source).unwrap()
62+
match self.try_get_content(source) {
63+
Some(v) => v,
64+
None => panic!(
65+
"Could not get position {:?} within \"{}\"",
66+
self,
67+
source.to_string()
68+
),
69+
}
6170
}
6271

6372
pub fn get_content_string(&self, source: &[char]) -> String {

0 commit comments

Comments
 (0)