@@ -45,7 +45,6 @@ impl PeepholeReplaceKnownMethods {
4545
4646 let Expression :: StaticMemberExpression ( member) = & call_expr. callee else { return } ;
4747 if let Expression :: StringLiteral ( string_lit) = & member. object {
48- #[ expect( clippy:: match_same_arms) ]
4948 let replacement = match member. property . name . as_str ( ) {
5049 "toLowerCase" | "toUpperCase" | "trim" => {
5150 let transformed_value =
@@ -75,7 +74,6 @@ impl PeepholeReplaceKnownMethods {
7574 string_lit,
7675 ctx,
7776 ) ,
78- // TODO: Implement the rest of the string methods
7977 "substring" | "slice" => Self :: try_fold_string_substring_or_slice (
8078 call_expr. span ,
8179 call_expr,
@@ -88,10 +86,13 @@ impl PeepholeReplaceKnownMethods {
8886 "charCodeAt" => {
8987 Self :: try_fold_string_char_code_at ( call_expr. span , call_expr, string_lit, ctx)
9088 }
91- "replace" => {
92- Self :: try_fold_string_replace ( call_expr. span , call_expr, string_lit, ctx)
93- }
94- "replaceAll" => None ,
89+ "replace" | "replaceAll" => Self :: try_fold_string_replace_or_string_replace_all (
90+ call_expr. span ,
91+ call_expr,
92+ member,
93+ string_lit,
94+ ctx,
95+ ) ,
9596 _ => None ,
9697 } ;
9798
@@ -246,9 +247,10 @@ impl PeepholeReplaceKnownMethods {
246247 NumberBase :: Decimal ,
247248 ) ) )
248249 }
249- fn try_fold_string_replace < ' a > (
250+ fn try_fold_string_replace_or_string_replace_all < ' a > (
250251 span : Span ,
251252 call_expr : & CallExpression < ' a > ,
253+ member : & StaticMemberExpression < ' a > ,
252254 string_lit : & StringLiteral < ' a > ,
253255 ctx : & mut TraverseCtx < ' a > ,
254256 ) -> Option < Expression < ' a > > {
@@ -275,8 +277,15 @@ impl PeepholeReplaceKnownMethods {
275277 return None ;
276278 }
277279
278- let result =
279- string_lit. value . as_str ( ) . cow_replacen ( search_value. as_ref ( ) , & replace_value, 1 ) ;
280+ let result = match member. property . name . as_str ( ) {
281+ "replace" => {
282+ string_lit. value . as_str ( ) . cow_replacen ( search_value. as_ref ( ) , & replace_value, 1 )
283+ }
284+ "replaceAll" => {
285+ string_lit. value . as_str ( ) . cow_replace ( search_value. as_ref ( ) , & replace_value)
286+ }
287+ _ => unreachable ! ( ) ,
288+ } ;
280289
281290 Some ( ctx. ast . expression_from_string_literal ( ctx. ast . string_literal ( span, result) ) )
282291 }
@@ -456,7 +465,6 @@ mod test {
456465 }
457466
458467 #[ test]
459- #[ ignore]
460468 fn test_fold_string_replace_all ( ) {
461469 fold ( "x = 'abcde'.replaceAll('bcd','c')" , "x = 'ace'" ) ;
462470 fold ( "x = 'abcde'.replaceAll('c','xxx')" , "x = 'abxxxde'" ) ;
0 commit comments