Skip to content

Commit 1afc08d

Browse files
authored
Rollup merge of #147396 - GuillaumeGomez:fluent-tidy-improvements, r=kobzol
Fluent tidy improvements Follow-up of #147345 and of #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 6332a76 + 831cdf3 commit 1afc08d

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

‎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
usecrate::diagnostics::{CheckId,DiagCtx,RunningCheck};
@@ -24,30 +26,31 @@ fn check_alphabetic(
2426
check:&mutRunningCheck,
2527
all_defined_msgs:&mutHashMap<String,String>,
2628
){
27-
letmut matches = message().captures_iter(fluent).peekable();
28-
whileletSome(m) = matches.next(){
29-
let name = m.get(1).unwrap();
30-
ifletSome(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+
letOk(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+
letmut prev:Option<&str> = None;
3834

39-
ifletSome(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+
ifletEntry::Message(msg) = entry {
37+
let name:&str = msg.id.name;
38+
ifletSome(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+
ifletSome(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)