-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tidy check that checks whether the fluent slugs only appear once
- Loading branch information
Showing
3 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Checks that all Fluent messages appear at least twice | ||
|
||
use crate::walk::{filter_dirs, walk}; | ||
use regex::Regex; | ||
use std::collections::HashMap; | ||
use std::path::Path; | ||
|
||
lazy_static::lazy_static! { | ||
static ref WORD: Regex = Regex::new(r"\w+").unwrap(); | ||
} | ||
|
||
fn filter_used_messages( | ||
contents: &str, | ||
msgs_not_appeared_yet: &mut HashMap<String, String>, | ||
msgs_appeared_only_once: &mut HashMap<String, String>, | ||
) { | ||
let mut matches = WORD.find_iter(contents); | ||
while let Some(name) = matches.next() { | ||
if let Some((name, filename)) = msgs_not_appeared_yet.remove_entry(name.as_str()) { | ||
// if one msg appears for the first time | ||
// remove it from `msgs_not_appeared_yet` and insert it into `msgs_appeared_only_once` | ||
msgs_appeared_only_once.insert(name, filename); | ||
} else { | ||
// if one msg appears for the second time | ||
// remove it from `msgs_appeared_only_once` | ||
msgs_appeared_only_once.remove(name.as_str()); | ||
} | ||
} | ||
} | ||
|
||
pub fn check(path: &Path, mut all_defined_msgs: HashMap<String, String>, bad: &mut bool) { | ||
let mut msgs_appear_only_once = HashMap::new(); | ||
walk(path, |path, _| filter_dirs(path), &mut |_, contents| { | ||
filter_used_messages(contents, &mut all_defined_msgs, &mut msgs_appear_only_once); | ||
}); | ||
|
||
for (name, filename) in msgs_appear_only_once { | ||
tidy_error!(bad, "{filename}: message `{}` is not used", name,); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters