@@ -36,21 +36,28 @@ export const getScriptsNode = (packageJsonRootNode: SgRoot) =>
36
36
*/
37
37
export const getNodeJsUsage = ( packageJsonRootNode : SgRoot ) =>
38
38
getScriptsNode ( packageJsonRootNode )
39
- . flatMap ( ( node ) =>
40
- node . findAll ( {
41
- rule : {
42
- kind : "string_content" ,
43
- // we can use bash sub-parsing but it's will be overkill
44
- regex : "\\bnode(\\.exe)?\\b" ,
45
- inside : {
39
+ . flatMap ( ( node ) =>
40
+ node . findAll ( {
41
+ rule : {
46
42
kind : "string" ,
43
+ regex : "\\bnode(\\.exe)?\\b" ,
47
44
inside : {
45
+ field : "value" ,
48
46
kind : "pair" ,
49
- }
47
+ } ,
48
+ } ,
49
+ } ) . map ( ( n ) => {
50
+ const raw = n . text ( ) ;
51
+ let unquoted = raw ;
52
+ if ( unquoted . startsWith ( '"' ) && unquoted . endsWith ( '"' ) ) {
53
+ unquoted = unquoted . slice ( 1 , - 1 ) ;
50
54
}
51
- }
52
- } )
53
- ) ;
55
+ return {
56
+ node : n ,
57
+ text : ( ) => unquoted ,
58
+ } ;
59
+ } )
60
+ ) ;
54
61
55
62
/**
56
63
* Replace Node.js arguments in the "scripts" node of a package.json AST.
@@ -59,30 +66,25 @@ export const getNodeJsUsage = (packageJsonRootNode: SgRoot) =>
59
66
* @param edits An array to collect the edits made.
60
67
*/
61
68
export const replaceNodeJsArgs = ( packageJsonRootNode : SgRoot , argsToValues : Record < string , string > , edits : Edit [ ] ) => {
62
- for ( const nodeJsUsageNode of getNodeJsUsage ( packageJsonRootNode ) ) {
63
- const text = nodeJsUsageNode . text ( ) ;
69
+ for ( const usage of getNodeJsUsage ( packageJsonRootNode ) ) {
70
+ const text = usage . text ( ) ;
64
71
const bashAST = astGrep . parse ( "bash" , text ) . root ( ) ;
65
-
66
- const command = bashAST . findAll ( {
67
- rule : { kind : "command" }
68
- } ) ;
69
-
72
+ const command = bashAST . findAll ( { rule : { kind : "command" } } ) ;
70
73
for ( const cmd of command ) {
71
74
const args = cmd . findAll ( {
72
75
rule : {
73
76
kind : "word" ,
74
77
not : {
75
- inside : {
76
- kind : "command_name"
77
- }
78
- }
79
- }
78
+ inside : { kind : "command_name" } ,
79
+ } ,
80
+ } ,
80
81
} ) ;
81
-
82
82
for ( const arg of args ) {
83
- const newValue = argsToValues [ arg . text ( ) ] ;
84
-
85
- if ( newValue ) edits . push ( arg . replace ( newValue ) ) ;
83
+ const oldArg = arg . text ( ) ;
84
+ const newValue = argsToValues [ oldArg ] ;
85
+ if ( newValue ) {
86
+ edits . push ( arg . replace ( newValue ) ) ;
87
+ }
86
88
}
87
89
}
88
90
}
0 commit comments