@@ -54,6 +54,8 @@ impl PeepholeReplaceKnownMethods {
5454 let Some ( string_lit) = Self :: get_string_literal ( mem_expr. object ( ) ) else { return } ;
5555 let Some ( method_name) = mem_expr. static_property_name ( ) else { return } ;
5656
57+ let arguments = & call_expr. arguments ;
58+
5759 #[ expect( clippy:: match_same_arms) ]
5860 let replacement = match method_name {
5961 "toLowerCase" | "toUpperCase" | "trim" => {
@@ -76,15 +78,13 @@ impl PeepholeReplaceKnownMethods {
7678 call_expr. span ,
7779 string_lit,
7880 method_name,
79- & call_expr . arguments ,
81+ arguments,
8082 ctx,
8183 ) ,
8284 // TODO: Implement the rest of the string methods
8385 "substr" => None ,
8486 "substring" | "slice" => None ,
85- "charAt" => {
86- Self :: try_fold_string_char_at ( call_expr. span , call_expr, string_lit, ctx)
87- }
87+ "charAt" => Self :: try_fold_string_char_at ( call_expr. span , string_lit, arguments, ctx) ,
8888 "charCodeAt" => None ,
8989 "replace" => None ,
9090 "replaceAll" => None ,
@@ -133,11 +133,11 @@ impl PeepholeReplaceKnownMethods {
133133
134134 fn try_fold_string_char_at < ' a > (
135135 span : Span ,
136- call_expr : & CallExpression < ' a > ,
137- string_lit : & StringLiteral < ' a > ,
136+ string_lit : & str ,
137+ arguments : & oxc_allocator :: Vec < Argument < ' a > > ,
138138 ctx : & mut TraverseCtx < ' a > ,
139139 ) -> Option < Expression < ' a > > {
140- let char_at_index: Option < f64 > = match call_expr . arguments . first ( ) {
140+ let char_at_index: Option < f64 > = match arguments. first ( ) {
141141 Some ( Argument :: NumericLiteral ( numeric_lit) ) => Some ( numeric_lit. value ) ,
142142 Some ( Argument :: UnaryExpression ( unary_expr) )
143143 if unary_expr. operator == UnaryOperator :: UnaryNegation =>
@@ -151,11 +151,7 @@ impl PeepholeReplaceKnownMethods {
151151 _ => return None ,
152152 } ;
153153
154- let result = & string_lit
155- . value
156- . as_str ( )
157- . char_at ( char_at_index)
158- . map_or ( String :: new ( ) , |v| v. to_string ( ) ) ;
154+ let result = string_lit. char_at ( char_at_index) . map_or ( String :: new ( ) , |v| v. to_string ( ) ) ;
159155
160156 return Some ( ctx. ast . expression_from_string_literal ( ctx. ast . string_literal ( span, result) ) ) ;
161157 }
@@ -435,7 +431,7 @@ mod test {
435431 // fold("x = '\\ud834\udd1e'.charAt(1)", "x = '\\udd1e'");
436432
437433 // Template strings
438- fold_same ( "x = `abcdef`.charAt(0)" ) ;
434+ fold ( "x = `abcdef`.charAt(0)" , "x = 'a' ") ;
439435 fold_same ( "x = `abcdef ${abc}`.charAt(0)" ) ;
440436 }
441437
0 commit comments