1
Fork 0

Auto merge of #100675 - Xiretza:fluent-mandate-crate-prefix, r=davidtwco

fluent: mandate slug names to be prefixed by crate name

This is currently only convention, but not actively checked for.

Additionally, improve error messages to highlight the path of the offending fluent file rather than the identifier preceding it.

This will conflict with #100671, so I'll leave it as draft until that's merged.
This commit is contained in:
bors 2022-08-23 22:30:07 +00:00
commit 87991d5f5d
11 changed files with 82 additions and 31 deletions

View file

@ -187,11 +187,11 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
for entry in resource.entries() {
let span = res.ident.span();
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
let _ = previous_defns.entry(name.to_string()).or_insert(ident_span);
let _ = previous_defns.entry(name.to_string()).or_insert(path_span);
if name.contains('-') {
Diagnostic::spanned(
ident_span,
path_span,
Level::Error,
format!("name `{name}` contains a '-' character"),
)
@ -205,11 +205,23 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
// The last case we error about above, but we want to fall back gracefully
// so that only the error is being emitted and not also one about the macro
// failing.
let snake_name = Ident::new(
// FIXME: should probably trim prefix, not replace all occurrences
&name.replace('-', "_").replace(&format!("{}_", res.ident), ""),
span,
);
let crate_prefix = format!("{}_", res.ident);
let snake_name = name.replace('-', "_");
let snake_name = match snake_name.strip_prefix(&crate_prefix) {
Some(rest) => Ident::new(rest, span),
None => {
Diagnostic::spanned(
path_span,
Level::Error,
format!("name `{name}` does not start with the crate name"),
)
.help(format!("prepend `{crate_prefix}` to the slug name: `{crate_prefix}{snake_name}`"))
.emit();
Ident::new(&snake_name, span)
}
};
constants.extend(quote! {
pub const #snake_name: crate::DiagnosticMessage =
crate::DiagnosticMessage::FluentIdentifier(
@ -226,7 +238,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
if attr_name.contains('-') {
Diagnostic::spanned(
ident_span,
path_span,
Level::Error,
format!("attribute `{attr_name}` contains a '-' character"),
)
@ -249,7 +261,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
match e {
FluentError::Overriding { kind, id } => {
Diagnostic::spanned(
ident_span,
path_span,
Level::Error,
format!("overrides existing {}: `{}`", kind, id),
)