Skip to content

Commit cd63ba7

Browse files
authored
Rollup merge of rust-lang#147396 - GuillaumeGomez:fluent-tidy-improvements, r=kobzol
Fluent tidy improvements Follow-up of rust-lang#147345 and of rust-lang#147191. It uses `fluent_syntax` to parse `fluent` files (but not for blessing, not even sure how the current one works). I also added an `assert` to ensure we never go to previous situation where the `fluent` files were actually not checked at all. cc `@Kivooeo` r? kobzol
2 parents c917e02 + 831cdf3 commit cd63ba7

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/tools/tidy/src/fluent_alphabetical.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::fs::OpenOptions;
55
use std::io::Write;
66
use std::path::Path;
77

8+
use fluent_syntax::ast::Entry;
9+
use fluent_syntax::parser;
810
use regex::Regex;
911

1012
use crate::diagnostics::{CheckId, DiagCtx, RunningCheck};
@@ -24,30 +26,31 @@ fn check_alphabetic(
2426
check: &mut RunningCheck,
2527
all_defined_msgs: &mut HashMap<String, String>,
2628
) {
27-
let mut matches = message().captures_iter(fluent).peekable();
28-
while let Some(m) = matches.next() {
29-
let name = m.get(1).unwrap();
30-
if let Some(defined_filename) = all_defined_msgs.get(name.as_str()) {
31-
check.error(format!(
32-
"{filename}: message `{}` is already defined in {defined_filename}",
33-
name.as_str(),
34-
));
35-
}
29+
let Ok(resource) = parser::parse(fluent) else {
30+
panic!("Errors encountered while parsing fluent file `{filename}`");
31+
};
3632

37-
all_defined_msgs.insert(name.as_str().to_owned(), filename.to_owned());
33+
let mut prev: Option<&str> = None;
3834

39-
if let Some(next) = matches.peek() {
40-
let next = next.get(1).unwrap();
41-
if name.as_str() > next.as_str() {
35+
for entry in &resource.body {
36+
if let Entry::Message(msg) = entry {
37+
let name: &str = msg.id.name;
38+
if let Some(defined_filename) = all_defined_msgs.get(name) {
4239
check.error(format!(
43-
"{filename}: message `{}` appears before `{}`, but is alphabetically later than it
44-
run `./x.py test tidy --bless` to sort the file correctly",
45-
name.as_str(),
46-
next.as_str()
40+
"{filename}: message `{name}` is already defined in {defined_filename}",
4741
));
42+
} else {
43+
all_defined_msgs.insert(name.to_string(), filename.to_owned());
4844
}
49-
} else {
50-
break;
45+
if let Some(prev) = prev
46+
&& prev > name
47+
{
48+
check.error(format!(
49+
"{filename}: message `{prev}` appears before `{name}`, but is alphabetically \
50+
later than it. Run `./x.py test tidy --bless` to sort the file correctly",
51+
));
52+
}
53+
prev = Some(name);
5154
}
5255
}
5356
}
@@ -115,5 +118,7 @@ pub fn check(path: &Path, bless: bool, diag_ctx: DiagCtx) {
115118
},
116119
);
117120

121+
assert!(!all_defined_msgs.is_empty());
122+
118123
crate::fluent_used::check(path, all_defined_msgs, diag_ctx);
119124
}

0 commit comments

Comments
 (0)