11use crate :: ast_utils:: {
2- expand_ts_as_expr, get_jsx_attr, get_jsx_attr_value_as_string, is_jsx_elements_equal,
3- omit_jsx_attrs,
2+ get_jsx_attr, get_jsx_attr_value_as_string, is_jsx_elements_equal, omit_jsx_attrs,
43} ;
54use crate :: options:: LinguiOptions ;
6- use crate :: tokens:: { CaseOrOffset , IcuChoice , MsgToken } ;
5+ use crate :: tokens:: { CaseOrOffset , MsgArg , MsgToken } ;
76use std:: collections:: HashSet ;
8- use swc_core:: {
9- common:: { SyntaxContext , DUMMY_SP } ,
10- ecma:: ast:: * ,
11- } ;
7+ use swc_core:: { common:: DUMMY_SP , ecma:: ast:: * } ;
128
139fn is_numeric ( s : & str ) -> bool {
1410 !s. is_empty ( ) && s. bytes ( ) . all ( |b| b. is_ascii_digit ( ) )
@@ -87,13 +83,10 @@ pub struct MessageBuilder<'a> {
8783 components : Vec < ValueWithPlaceholder > ,
8884
8985 values : Vec < ValueWithPlaceholder > ,
90- values_indexed : Vec < ValueWithPlaceholder > ,
9186
9287 options : & ' a LinguiOptions ,
9388 elements_tracking : Vec < ( String , JSXOpeningElement ) > ,
9489 element_index : usize ,
95- // Counter for auto-generated numeric placeholders
96- numeric_index : usize ,
9790}
9891
9992impl < ' a > MessageBuilder < ' a > {
@@ -103,18 +96,16 @@ impl<'a> MessageBuilder<'a> {
10396 components_stack : Vec :: new ( ) ,
10497 components : Vec :: new ( ) ,
10598 values : Vec :: new ( ) ,
106- values_indexed : Vec :: new ( ) ,
10799 options,
108100 elements_tracking : Vec :: new ( ) ,
109101 element_index : 0 ,
110- numeric_index : 0 ,
111102 } ;
112103
113104 builder. process_tokens ( tokens) ;
114105 builder. into_args ( )
115106 }
116107
117- pub fn into_args ( mut self ) -> MessageBuilderResult {
108+ pub fn into_args ( self ) -> MessageBuilderResult {
118109 let message_str = self . message ;
119110
120111 let message = Box :: new ( Expr :: Lit ( Lit :: Str ( Str {
@@ -123,8 +114,6 @@ impl<'a> MessageBuilder<'a> {
123114 raw : None ,
124115 } ) ) ) ;
125116
126- self . values . append ( & mut self . values_indexed ) ;
127-
128117 let values = if self . values . is_empty ( ) {
129118 None
130119 } else {
@@ -165,9 +154,8 @@ impl<'a> MessageBuilder<'a> {
165154 self . push_msg ( & str) ;
166155 }
167156
168- MsgToken :: Expression ( val) => {
169- let placeholder = self . push_exp ( val) ;
170- self . push_msg ( & format ! ( "{{{placeholder}}}" ) ) ;
157+ MsgToken :: Arg ( arg) => {
158+ self . push_arg ( arg) ;
171159 }
172160
173161 MsgToken :: TagOpening ( val) => {
@@ -176,9 +164,6 @@ impl<'a> MessageBuilder<'a> {
176164 MsgToken :: TagClosing => {
177165 self . push_tag_closing ( ) ;
178166 }
179- MsgToken :: IcuChoice ( icu) => {
180- self . push_icu ( icu) ;
181- }
182167 }
183168 }
184169 }
@@ -295,97 +280,37 @@ impl<'a> MessageBuilder<'a> {
295280 }
296281 }
297282
298- fn next_numeric_index ( & mut self ) -> String {
299- let index = self . numeric_index . to_string ( ) ;
300- self . numeric_index += 1 ;
301- index
302- }
283+ fn push_arg ( & mut self , arg : MsgArg ) {
284+ let placeholder = arg. name . clone ( ) ;
303285
304- fn push_exp ( & mut self , mut exp : Box < Expr > ) -> String {
305- exp = expand_ts_as_expr ( exp) ;
286+ self . values . push ( ValueWithPlaceholder {
287+ placeholder : placeholder. clone ( ) ,
288+ value : arg. value ,
289+ } ) ;
306290
307- match exp. as_ref ( ) {
308- Expr :: Ident ( ident) => {
309- self . values . push ( ValueWithPlaceholder {
310- placeholder : ident. sym . to_string ( ) . clone ( ) ,
311- value : exp. clone ( ) ,
312- } ) ;
291+ if let Some ( format) = arg. format {
292+ self . push_msg ( & format ! ( "{{{placeholder}, {format}," ) ) ;
313293
314- ident. sym . to_string ( )
315- }
316- Expr :: Object ( object) => {
317- if let Some ( PropOrSpread :: Prop ( prop) ) = object. props . first ( ) {
318- // {foo}
319- if let Some ( short) = prop. as_shorthand ( ) {
320- self . values_indexed . push ( ValueWithPlaceholder {
321- placeholder : short. sym . to_string ( ) ,
322- value : Box :: new ( Expr :: Ident ( Ident {
323- span : DUMMY_SP ,
324- sym : short. sym . clone ( ) ,
325- ctxt : SyntaxContext :: empty ( ) ,
326- optional : false ,
327- } ) ) ,
328- } ) ;
329-
330- return short. sym . to_string ( ) ;
331- }
332- // {foo: bar}
333- if let Prop :: KeyValue ( kv) = prop. as_ref ( ) {
334- if let PropName :: Ident ( ident) = & kv. key {
335- self . values_indexed . push ( ValueWithPlaceholder {
336- placeholder : ident. sym . to_string ( ) ,
337- value : kv. value . clone ( ) ,
338- } ) ;
339-
340- return ident. sym . to_string ( ) ;
294+ if let Some ( cases) = arg. cases {
295+ for choice in cases {
296+ match choice {
297+ // produce offset:{number}
298+ CaseOrOffset :: Offset ( val) => {
299+ self . push_msg ( & format ! ( " offset:{val}" ) ) ;
300+ }
301+ CaseOrOffset :: Case ( choice) => {
302+ let key = choice. key ;
303+ self . push_msg ( & format ! ( " {key} {{" ) ) ;
304+ self . process_tokens ( choice. tokens ) ;
305+ self . push_msg ( "}" ) ;
341306 }
342307 }
343308 }
344-
345- // fallback for {...spread} or {}
346- let index = self . next_numeric_index ( ) ;
347-
348- self . values_indexed . push ( ValueWithPlaceholder {
349- placeholder : index. clone ( ) ,
350- value : exp. clone ( ) ,
351- } ) ;
352-
353- index
354309 }
355- _ => {
356- let index = self . next_numeric_index ( ) ;
357310
358- self . values_indexed . push ( ValueWithPlaceholder {
359- placeholder : index. clone ( ) ,
360- value : exp. clone ( ) ,
361- } ) ;
362-
363- index
364- }
365- }
366- }
367-
368- fn push_icu ( & mut self , icu : IcuChoice ) {
369- let value_placeholder = self . push_exp ( icu. value ) ;
370- let method = icu. format ;
371- self . push_msg ( & format ! ( "{{{value_placeholder}, {method}," ) ) ;
372-
373- for choice in icu. cases {
374- match choice {
375- // produce offset:{number}
376- CaseOrOffset :: Offset ( val) => {
377- self . push_msg ( & format ! ( " offset:{val}" ) ) ;
378- }
379- CaseOrOffset :: Case ( choice) => {
380- let key = choice. key ;
381-
382- self . push_msg ( & format ! ( " {key} {{" ) ) ;
383- self . process_tokens ( choice. tokens ) ;
384- self . push_msg ( "}" ) ;
385- }
386- }
311+ self . push_msg ( "}" ) ;
312+ } else {
313+ self . push_msg ( & format ! ( "{{{placeholder}}}" ) ) ;
387314 }
388-
389- self . push_msg ( "}" ) ;
390315 }
391316}
0 commit comments