-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add a tidy rule to check that fluent messages and attrs don't end in .
#126811
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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
Next
Next commit
Add a tidy rule to make sure that diagnostics don't end in periods
- Loading branch information
commit ea681ef2814b321177d1eee437b8867d31141754
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,84 @@ | ||
//! Checks that no Fluent messages or attributes end in periods (except ellipses) | ||
|
||
use fluent_syntax::ast::{Entry, PatternElement}; | ||
|
||
use crate::walk::{filter_dirs, walk}; | ||
use std::path::Path; | ||
|
||
fn filter_fluent(path: &Path) -> bool { | ||
if let Some(ext) = path.extension() { ext.to_str() != Some("ftl") } else { true } | ||
} | ||
|
||
/// Messages allowed to have `.` at their end. | ||
/// | ||
/// These should probably be reworked eventually. | ||
const ALLOWLIST: &[&str] = &[ | ||
"const_eval_long_running", | ||
"const_eval_validation_failure_note", | ||
"driver_impl_ice", | ||
"incremental_corrupt_file", | ||
"mir_build_pointer_pattern", | ||
]; | ||
|
||
fn check_period(filename: &str, contents: &str, bad: &mut bool) { | ||
if filename.contains("codegen") { | ||
// FIXME: Too many codegen messages have periods right now... | ||
return; | ||
} | ||
|
||
let (Ok(parse) | Err((parse, _))) = fluent_syntax::parser::parse(contents); | ||
for entry in &parse.body { | ||
if let Entry::Message(m) = entry { | ||
if ALLOWLIST.contains(&m.id.name) { | ||
continue; | ||
} | ||
|
||
if let Some(pat) = &m.value { | ||
if let Some(PatternElement::TextElement { value }) = pat.elements.last() { | ||
// We don't care about ellipses. | ||
if value.ends_with(".") && !value.ends_with("...") { | ||
let ll = find_line(contents, *value); | ||
let name = m.id.name; | ||
tidy_error!(bad, "{filename}:{ll}: message `{name}` ends in a period"); | ||
} | ||
} | ||
} | ||
|
||
for attr in &m.attributes { | ||
// Teach notes usually have long messages. | ||
if attr.id.name == "teach_note" { | ||
continue; | ||
} | ||
|
||
if let Some(PatternElement::TextElement { value }) = attr.value.elements.last() { | ||
if value.ends_with(".") && !value.ends_with("...") { | ||
let ll = find_line(contents, *value); | ||
let name = attr.id.name; | ||
tidy_error!(bad, "{filename}:{ll}: attr `{name}` ends in a period"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Evil cursed bad hack. Requires that `value` be a substr (in memory) of `contents`. | ||
fn find_line(haystack: &str, needle: &str) -> usize { | ||
for (ll, line) in haystack.lines().enumerate() { | ||
if line.as_ptr() > needle.as_ptr() { | ||
return ll; | ||
} | ||
} | ||
|
||
1 | ||
} | ||
|
||
pub fn check(path: &Path, bad: &mut bool) { | ||
walk( | ||
path, | ||
|path, is_dir| filter_dirs(path) || (!is_dir && filter_fluent(path)), | ||
&mut |ent, contents| { | ||
check_period(ent.path().to_str().unwrap(), contents, bad); | ||
}, | ||
); | ||
} |
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
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.
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.
not this PR's fault, but I find the inverted filter condition odd