Skip to content

Commit 3666e2a

Browse files
committed
Fix spell checking function names in Rust trait implementations (#225)
Skip spell checking function names in `impl Trait for Type` blocks, since those names are dictated by the trait, not the implementor.
1 parent bda888c commit 3666e2a

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

crates/codebook/src/queries/rust.scm

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
(function_item
2-
name: (identifier) @identifier)
1+
; Function names - excluding trait implementations (issue #225)
2+
; Functions in non-trait impl blocks
3+
(impl_item
4+
!trait
5+
body: (declaration_list
6+
(function_item
7+
name: (identifier) @identifier)))
8+
; Functions in trait definitions
9+
(trait_item
10+
body: (declaration_list
11+
(function_item
12+
name: (identifier) @identifier)))
13+
; Top-level functions
14+
(source_file (function_item name: (identifier) @identifier))
15+
; Functions in modules
16+
(mod_item body: (declaration_list (function_item name: (identifier) @identifier)))
17+
; Nested functions (inside blocks)
18+
(block (function_item name: (identifier) @identifier))
319
(parameter
420
pattern: (identifier) @identifier)
521
(let_declaration

crates/codebook/tests/test_rust.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,67 @@ fn test_rust_struct() {
154154
assert_eq!(result.locations, expect.locations);
155155
}
156156
}
157+
158+
#[test]
159+
fn test_rust_trait_impl_function_names_not_checked() {
160+
// https://github.com/blopker/codebook/issues/225
161+
// Function names in `impl Trait for Type` blocks should not be spell-checked
162+
// because the names are dictated by the trait, not the implementor.
163+
utils::init_logging();
164+
let processor = utils::get_processor();
165+
let sample_text = r#"
166+
struct MyType;
167+
168+
impl SomeTrait for MyType {
169+
fn spelling_erorr(self) {
170+
// This comment has a typo: tset
171+
}
172+
}
173+
"#;
174+
let binding = processor
175+
.spell_check(sample_text, Some(LanguageType::Rust), None)
176+
.to_vec();
177+
let misspelled: Vec<&str> = binding.iter().map(|r| r.word.as_str()).collect();
178+
println!("Misspelled words: {misspelled:?}");
179+
// "erorr" in the function name should NOT be flagged (name dictated by trait)
180+
assert!(
181+
!misspelled.contains(&"erorr"),
182+
"Function names in trait impl blocks should not be spell-checked"
183+
);
184+
// But comments inside the impl block should still be checked
185+
assert!(
186+
misspelled.contains(&"tset"),
187+
"Comments inside trait impl blocks should still be spell-checked"
188+
);
189+
}
190+
191+
#[test]
192+
fn test_rust_regular_impl_function_names_checked() {
193+
// Regular impl blocks (not trait implementations) should still be spell-checked
194+
utils::init_logging();
195+
let processor = utils::get_processor();
196+
let sample_text = r#"
197+
struct MyType;
198+
199+
impl MyType {
200+
fn spelling_erorr(self) {}
201+
}
202+
203+
fn top_level_erorr() {}
204+
"#;
205+
let binding = processor
206+
.spell_check(sample_text, Some(LanguageType::Rust), None)
207+
.to_vec();
208+
let misspelled: Vec<&str> = binding.iter().map(|r| r.word.as_str()).collect();
209+
println!("Misspelled words: {misspelled:?}");
210+
assert!(
211+
misspelled.contains(&"erorr"),
212+
"Expected 'erorr' to be flagged in regular impl and top-level functions"
213+
);
214+
let erorr = binding.iter().find(|r| r.word == "erorr").unwrap();
215+
assert_eq!(
216+
erorr.locations.len(),
217+
2,
218+
"Expected 'erorr' flagged in both regular impl and top-level function"
219+
);
220+
}

0 commit comments

Comments
 (0)