Change fluent_messages macro to expect _ slugs instead of - slugs
For the most part, the macro actually worked with _ slugs, but the prefix_something -> prefix::something conversion was not implemented. We don't want to accept - slugs for consistency reasons. We thus error if a name is found with - inside. This ensures a consistent style.
This commit is contained in:
parent
75d78b9362
commit
ca16a8d76c
5 changed files with 65 additions and 6 deletions
|
@ -189,13 +189,25 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
||||||
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
|
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(ident_span);
|
||||||
|
|
||||||
// `typeck-foo-bar` => `foo_bar` (in `typeck.ftl`)
|
if name.contains('-') {
|
||||||
// `const-eval-baz` => `baz` (in `const_eval.ftl`)
|
Diagnostic::spanned(
|
||||||
|
ident_span,
|
||||||
|
Level::Error,
|
||||||
|
format!("name `{name}` contains a '-' character"),
|
||||||
|
)
|
||||||
|
.help("replace any '-'s with '_'s")
|
||||||
|
.emit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// `typeck_foo_bar` => `foo_bar` (in `typeck.ftl`)
|
||||||
|
// `const_eval_baz` => `baz` (in `const_eval.ftl`)
|
||||||
|
// `const-eval-hyphen-having` => `hyphen_having` (in `const_eval.ftl`)
|
||||||
|
// 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(
|
let snake_name = Ident::new(
|
||||||
// FIXME: should probably trim prefix, not replace all occurrences
|
// FIXME: should probably trim prefix, not replace all occurrences
|
||||||
&name
|
&name.replace('-', "_").replace(&format!("{}_", res.ident), ""),
|
||||||
.replace(&format!("{}-", res.ident).replace('_', "-"), "")
|
|
||||||
.replace('-', "_"),
|
|
||||||
span,
|
span,
|
||||||
);
|
);
|
||||||
constants.extend(quote! {
|
constants.extend(quote! {
|
||||||
|
@ -212,6 +224,16 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if attr_name.contains('-') {
|
||||||
|
Diagnostic::spanned(
|
||||||
|
ident_span,
|
||||||
|
Level::Error,
|
||||||
|
format!("attribute `{attr_name}` contains a '-' character"),
|
||||||
|
)
|
||||||
|
.help("replace any '-'s with '_'s")
|
||||||
|
.emit();
|
||||||
|
}
|
||||||
|
|
||||||
constants.extend(quote! {
|
constants.extend(quote! {
|
||||||
pub const #snake_name: crate::SubdiagnosticMessage =
|
pub const #snake_name: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(
|
crate::SubdiagnosticMessage::FluentAttr(
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
some_slug = hi
|
||||||
|
.label-has-hyphens = test
|
|
@ -0,0 +1 @@
|
||||||
|
this-slug-has-hyphens = hi
|
|
@ -55,6 +55,24 @@ mod duplicate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod slug_with_hyphens {
|
||||||
|
use super::fluent_messages;
|
||||||
|
|
||||||
|
fluent_messages! {
|
||||||
|
slug_with_hyphens => "./slug-with-hyphens.ftl",
|
||||||
|
//~^ ERROR name `this-slug-has-hyphens` contains a '-' character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod label_with_hyphens {
|
||||||
|
use super::fluent_messages;
|
||||||
|
|
||||||
|
fluent_messages! {
|
||||||
|
label_with_hyphens => "./label-with-hyphens.ftl",
|
||||||
|
//~^ ERROR attribute `label-has-hyphens` contains a '-' character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod valid {
|
mod valid {
|
||||||
use super::fluent_messages;
|
use super::fluent_messages;
|
||||||
|
|
||||||
|
|
|
@ -41,5 +41,21 @@ help: previously defined in this resource
|
||||||
LL | a => "./duplicate-a.ftl",
|
LL | a => "./duplicate-a.ftl",
|
||||||
| ^
|
| ^
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: name `this-slug-has-hyphens` contains a '-' character
|
||||||
|
--> $DIR/test.rs:62:9
|
||||||
|
|
|
||||||
|
LL | slug_with_hyphens => "./slug-with-hyphens.ftl",
|
||||||
|
| ^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: replace any '-'s with '_'s
|
||||||
|
|
||||||
|
error: attribute `label-has-hyphens` contains a '-' character
|
||||||
|
--> $DIR/test.rs:71:9
|
||||||
|
|
|
||||||
|
LL | label_with_hyphens => "./label-with-hyphens.ftl",
|
||||||
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: replace any '-'s with '_'s
|
||||||
|
|
||||||
|
error: aborting due to 6 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue