Skip to content

Commit 2fba018

Browse files
feat(macro): default missing other in select macros to empty
1 parent 93eae50 commit 2fba018

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/macro_utils.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl MacroCtx {
254254
icu_format: &str,
255255
) -> Vec<CaseOrOffset> {
256256
// todo: there might be more props then real choices. Id for example
257-
let mut choices: Vec<CaseOrOffset> = Vec::with_capacity(props.len());
257+
let mut choices: Vec<CaseOrOffset> = Vec::with_capacity(props.len() + 1);
258258

259259
for prop_or_spread in props {
260260
if let PropOrSpread::Prop(prop) = prop_or_spread {
@@ -282,6 +282,17 @@ impl MacroCtx {
282282
}
283283
}
284284

285+
let no_other_case = !choices
286+
.iter()
287+
.any(|c| matches!(c, CaseOrOffset::Case(case) if case.key == "other"));
288+
289+
if no_other_case {
290+
choices.push(CaseOrOffset::Case(ChoiceCase {
291+
tokens: vec![],
292+
key: "other".into(),
293+
}));
294+
}
295+
285296
choices
286297
}
287298
}

src/tests/js_icu.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,24 @@ to!(
269269
});
270270
"#
271271
);
272+
273+
to!(
274+
js_should_handle_missing_other_in_select,
275+
r#"
276+
import { select } from '@lingui/macro'
277+
select(value, {
278+
offset: "..",
279+
any: "..",
280+
});
281+
"#,
282+
r#"
283+
import { i18n as $_i18n } from "@lingui/core";
284+
$_i18n._({
285+
id: "QHtFym",
286+
message: "{value, select, offset {..} any {..} other {}}",
287+
values: {
288+
value: value
289+
}
290+
});
291+
"#
292+
);

src/tests/jsx_icu.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ to!(
251251
"#
252252
);
253253

254+
to!(
255+
jsx_select_without_other,
256+
r#"
257+
import { Select } from '@lingui/macro';
258+
<Select
259+
value={count}
260+
_male="He"
261+
_female={`She`}
262+
/>;
263+
"#,
264+
r#"
265+
import { Trans as Trans_ } from "@lingui/react";
266+
<Trans_ message={"{count, select, male {He} female {She} other {} id={"Imwef9"} values={{
267+
count: count
268+
}} />;
269+
"#
270+
);
271+
254272
to!(
255273
jsx_select_with_expressions_in_cases,
256274
r#"

0 commit comments

Comments
 (0)