-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add new optional tidy check to check if ftl error messages are unused #147191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+232
−138
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
use std::collections::HashMap; | ||
use std::path::Path; | ||
|
||
use fluent_syntax::ast::{Entry, Expression, InlineExpression, PatternElement}; | ||
use fluent_syntax::parser; | ||
use regex::Regex; | ||
|
||
use crate::diagnostics::{CheckId, DiagCtx, RunningCheck}; | ||
|
||
pub fn check(root_path: &Path, diag_ctx: DiagCtx) { | ||
let path = &root_path.join("compiler"); | ||
let mut check = diag_ctx.start_check(CheckId::new("error_messages").path(path)); | ||
let regex = Regex::new(r#"(?:(?:#\[(diag|label|suggestion|note|help|help_once|multipart_suggestion)\()|(?:fluent(_generated)?::))\s*(?<id>[a-z_][a-zA-Z0-9_]+)\s*[\n,\)};]"#).unwrap(); | ||
|
||
for dir in std::fs::read_dir(path).unwrap() { | ||
let Ok(dir) = dir else { continue }; | ||
let dir = dir.path(); | ||
let messages_file = dir.join("messages.ftl"); | ||
|
||
if messages_file.is_file() { | ||
check_if_messages_are_used(&mut check, &dir.join("src"), &messages_file, ®ex); | ||
} | ||
} | ||
} | ||
|
||
fn check_fluent_ast<'a>(ids: &mut HashMap<&'a str, bool>, elem: &PatternElement<&'a str>) { | ||
if let PatternElement::Placeable { expression } = elem { | ||
match expression { | ||
Expression::Inline(InlineExpression::MessageReference { id, .. }) => { | ||
*ids.entry(&id.name).or_default() = true; | ||
} | ||
Expression::Select { variants, .. } => { | ||
for variant in variants { | ||
for elem in &variant.value.elements { | ||
check_fluent_ast(ids, elem); | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
fn check_if_messages_are_used( | ||
check: &mut RunningCheck, | ||
src_path: &Path, | ||
messages_file: &Path, | ||
regex: &Regex, | ||
) { | ||
// First we retrieve all error messages ID. | ||
let content = std::fs::read_to_string(messages_file).expect("failed to read file"); | ||
let resource = | ||
parser::parse(content.as_str()).expect("Errors encountered while parsing a resource."); | ||
|
||
let mut ids: HashMap<&str, bool> = HashMap::new(); | ||
for entry in &resource.body { | ||
if let Entry::Message(msg) = entry { | ||
let id: &str = &msg.id.name; | ||
if !ids.contains_key(&id) { | ||
ids.insert(id, false); | ||
} | ||
if let Some(value) = &msg.value { | ||
for elem in &value.elements { | ||
check_fluent_ast(&mut ids, elem); | ||
} | ||
} | ||
for attr in &msg.attributes { | ||
for elem in &attr.value.elements { | ||
check_fluent_ast(&mut ids, elem); | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert!(!ids.is_empty()); | ||
|
||
let skip = |f: &Path, is_dir: bool| !is_dir && !f.extension().is_some_and(|ext| ext == "rs"); | ||
crate::walk::walk(src_path, skip, &mut |_path: &_, content: &str| { | ||
for cap in regex.captures_iter(content) { | ||
let id = &cap["id"]; | ||
if let Some(found) = ids.get_mut(id) { | ||
// Error message IDs can be used more than once. | ||
*found = true; | ||
} | ||
} | ||
}); | ||
const TO_IGNORE: &[&str] = &[ | ||
// FIXME: #114050 | ||
"hir_typeck_option_result_asref", | ||
]; | ||
for (id, found) in ids { | ||
if !found && !TO_IGNORE.iter().any(|to_ignore| *to_ignore == id) { | ||
check.error(format!("unused message ID `{id}` from `{}`", messages_file.display())); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This checks all fluent messages, right? Many of them are not errors but lints. So something like "fluent_messages" or so is probably a better name for this module.
EDIT: There's already several checks called
fluent_something
sofluent_messages
is probably not specific enough.