1- import type { Edit , Kinds , Range , SgNode , SgRoot } from '@codemod.com/jssg-types/main' ;
1+ import type { Edit , Range , SgNode , SgRoot } from '@codemod.com/jssg-types/main' ;
22import type Js from '@codemod.com/jssg-types/langs/javascript' ;
33import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path' ;
44import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call' ;
@@ -8,30 +8,37 @@ import { removeBinding } from '@nodejs/codemod-utils/ast-grep/remove-binding';
88
99export default function transform ( root : SgRoot < Js > ) : string | null {
1010 const rootNode = root . root ( ) ;
11- const bindingStatementFnTuples : [ string , SgNode < Js > , ( arg : string ) => string ] [ ] = [ ] ;
11+ const bindingStatementFnTuples : [
12+ string ,
13+ SgNode < Js > ,
14+ ( arg : string ) => string ,
15+ ] [ ] = [ ] ;
1216 const edits : Edit [ ] = [ ] ;
1317 const linesToRemove : Range [ ] = [ ] ;
1418
1519 const updates = [
1620 {
17- oldBind : "$.atob" ,
18- replaceFn : ( arg : string ) => `Buffer.from(${ arg } , 'base64').toString('binary')`
21+ oldBind : '$.atob' ,
22+ replaceFn : ( arg : string ) =>
23+ `Buffer.from(${ arg } , 'base64').toString('binary')` ,
1924 } ,
2025 {
21- oldBind : "$.btoa" ,
22- replaceFn : ( arg : string ) => `Buffer.from(${ arg } , 'binary').toString('base64')`
23- }
26+ oldBind : '$.btoa' ,
27+ replaceFn : ( arg : string ) =>
28+ `Buffer.from(${ arg } , 'binary').toString('base64')` ,
29+ } ,
2430 ] ;
2531
2632 const statements = [
2733 ...getNodeRequireCalls ( root , 'buffer' ) ,
28- ...getNodeImportStatements ( root , 'buffer' )
34+ ...getNodeImportStatements ( root , 'buffer' ) ,
2935 ] ;
3036
3137 for ( const statement of statements ) {
3238 for ( const update of updates ) {
3339 const binding = resolveBindingPath ( statement , update . oldBind ) ;
34- if ( binding ) bindingStatementFnTuples . push ( [ binding , statement , update . replaceFn ] ) ;
40+ if ( binding )
41+ bindingStatementFnTuples . push ( [ binding , statement , update . replaceFn ] ) ;
3542 }
3643 }
3744
@@ -43,32 +50,32 @@ export default function transform(root: SgRoot<Js>): string | null {
4350 // Check for calls to the specified binding
4451 const calls = rootNode . findAll ( {
4552 rule : {
46- pattern : `${ binding } ($ARG)`
47- }
53+ pattern : `${ binding } ($ARG)` ,
54+ } ,
4855 } ) ;
4956 // Check for any other calls, so as to not remove a statement that is still being used
5057 const otherCalls = rootNode . findAll ( {
5158 rule : {
5259 all : [
5360 {
54- pattern : 'buffer.$FN'
61+ pattern : 'buffer.$FN' ,
5562 } ,
5663 {
5764 not : {
58- pattern : '$.btoa($ARG)'
59- }
65+ pattern : '$.btoa($ARG)' ,
66+ } ,
6067 } ,
6168 {
6269 not : {
63- pattern : '$.atob($ARG)'
64- }
65- }
66- ]
67- }
70+ pattern : '$.atob($ARG)' ,
71+ } ,
72+ } ,
73+ ] ,
74+ } ,
6875 } ) ;
6976
7077 for ( const call of calls ) {
71- const argMatch = call . getMatch ( " ARG" ) ;
78+ const argMatch = call . getMatch ( ' ARG' ) ;
7279 if ( argMatch ) edits . push ( call . replace ( fn ( argMatch . text ( ) ) ) ) ;
7380 }
7481
0 commit comments