1- import type { SgRoot } from " @codemod.com/jssg-types/src/main" ;
2- import type Js from '@codemod.com/jssg-types/src/langs/javascript' ;
1+ import type Js from ' @codemod.com/jssg-types/src/langs/javascript'
2+ import type { SgRoot } from '@codemod.com/jssg-types/src/main'
33
44async function transform ( root : SgRoot < Js > ) : Promise < string > {
5- const rootNode = root . root ( ) ;
6-
5+ const rootNode = root . root ( )
6+
77 // Helper function to find the request parameter name
88 function findRequestParamName ( node : any ) : string {
99 // Start from the call expression and traverse up to find function parameters
10- let current = node ;
10+ let current = node
1111 while ( current ) {
12- const parent = current . parent ( ) ;
13- if ( ! parent ) break ;
12+ const parent = current . parent ( )
13+ if ( ! parent ) break
1414 // Check if we're in a function declaration or arrow function
1515 if ( parent . kind ( ) === 'function_declaration' || parent . kind ( ) === 'arrow_function' ) {
16- const params = parent . field ( 'parameters' ) ;
16+ const params = parent . field ( 'parameters' )
1717
1818 if ( params && params . children ( ) . length > 0 ) {
19- const firstParam = params . children ( ) [ 1 ] ;
19+ const firstParam = params . children ( ) [ 1 ]
2020 if ( firstParam . kind ( ) === 'required_parameter' ) {
21- const pattern = firstParam . field ( 'pattern' ) ;
21+ const pattern = firstParam . field ( 'pattern' )
2222 if ( pattern && pattern . kind ( ) === 'identifier' ) {
23- return pattern . text ( ) ;
23+ return pattern . text ( )
2424 }
2525 }
2626 }
2727 }
2828
29- current = parent ;
29+ current = parent
3030 }
31- return 'req' ; // default fallback
31+ return 'req' // default fallback
3232 }
3333
3434 // Find all redirect and location
3535 const nodes = rootNode . findAll ( {
3636 rule : {
3737 any : [
3838 {
39- pattern : " $OBJ.redirect($ARG)" ,
39+ pattern : ' $OBJ.redirect($ARG)' ,
4040 } ,
4141 {
42- pattern : "$OBJ.location($ARG)" ,
43- } ]
44- }
45- } ) ;
42+ pattern : '$OBJ.location($ARG)' ,
43+ } ,
44+ ] ,
45+ } ,
46+ } )
4647
4748 const edits = nodes . reduce ( ( acc : any [ ] , node : any ) => {
48- const requestParamName = findRequestParamName ( node ) ;
49- const obj = node . getMatch ( " OBJ" ) ;
50- const arg = node . getMatch ( " ARG" ) ;
49+ const requestParamName = findRequestParamName ( node )
50+ const obj = node . getMatch ( ' OBJ' )
51+ const arg = node . getMatch ( ' ARG' )
5152
5253 // Only transform when the argument is the literal 'back' (single or double quotes)
53- const argText = arg && typeof arg . text === 'function' ? arg . text ( ) : null ;
54- if ( argText !== "'back'" && argText !== '"back"' && argText !== " ‘back’" && argText !== " “back”" ) {
55- return acc ; // skip this node, no edit
54+ const argText = arg && typeof arg . text === 'function' ? arg . text ( ) : null
55+ if ( argText !== "'back'" && argText !== '"back"' && argText !== ' ‘back’' && argText !== ' “back”' ) {
56+ return acc // skip this node, no edit
5657 }
5758
5859 // Case: obj.redirect('back') or obj.location('back')
59- const objText = obj ?. text ( ) ;
60- const methodName = node . text ( ) . includes ( '.redirect(' ) ? 'redirect' : 'location' ;
61- acc . push ( node . replace ( `${ objText } .${ methodName } (${ requestParamName } .get("Referrer") || "/")` ) ) ;
62- return acc ;
63- } , [ ] as any [ ] ) ;
60+ const objText = obj ?. text ( )
61+ const methodName = node . text ( ) . includes ( '.redirect(' ) ? 'redirect' : 'location'
62+ acc . push ( node . replace ( `${ objText } .${ methodName } (${ requestParamName } .get("Referrer") || "/")` ) )
63+ return acc
64+ } , [ ] as any [ ] )
6465
65- const newSource = rootNode . commitEdits ( edits ) ;
66- return newSource ;
66+ const newSource = rootNode . commitEdits ( edits )
67+ return newSource
6768}
6869
69- export default transform ;
70+ export default transform
0 commit comments