@@ -6,7 +6,7 @@ export default function injector(code, script, keys) {
66}
77
88function rewrite ( code , script , keys ) {
9- // Throw error if useFusion is imported with an alias
9+ // Throw an error if useFusion is imported with an alias.
1010 checkAliasedUseFusion ( script . content ) ;
1111
1212 const originalContent = script . content ;
@@ -18,13 +18,13 @@ function rewrite(code, script, keys) {
1818 throw new Error ( "Multiple useFusion calls are not allowed." ) ;
1919 }
2020
21- // Check if the user has any useFusion call in their code .
21+ // Check if the user has any useFusion call.
2222 const hasUserCall = / u s e F u s i o n \s * \( / . test ( userCode ) ;
2323 let rewritten = "" ;
2424 let remainingKeys = keys . slice ( ) ;
2525
2626 if ( ! hasUserCall ) {
27- // No user call – insert an extra useFusion call that “imports” the placeholder.
27+ // No user call – insert an extra call for the placeholder without the third parameter .
2828 rewritten = wrapWithFusionImport (
2929 userCode ,
3030 "const {__exportedKeysAsCsv__} = useFusion([__exportedKeysAsCsv__], __props.fusion);"
@@ -43,24 +43,19 @@ function rewrite(code, script, keys) {
4343 // Parameterless call: assume the user wants all keys.
4444 rewritten = wrapWithFusionImport (
4545 userCode . replace ( regex , ( m , prefix ) => {
46- return `${ prefix } [${ keys . map ( k => `'${ k } '` ) . join ( ',' ) } ], __props.fusion)` ;
46+ return `${ prefix } [${ keys . map ( k => `'${ k } '` ) . join ( ',' ) } ], __props.fusion, true )` ;
4747 } )
4848 ) ;
4949 remainingKeys = [ ] ;
5050 } else {
5151 // User provided an explicit parameter array.
52- // Determine which keys they handled.
5352 const handledKeys = extractUseFusionParams ( userCode ) ;
5453 remainingKeys = handledKeys . includes ( "*" )
5554 ? [ ]
5655 : keys . filter ( key => ! handledKeys . includes ( key ) ) ;
5756
58- // Rewrite the call to append __props.fusion)
5957 const modifiedCall = modifyUseFusionCalls ( userCode , keys ) ;
60-
61- // For inline formatting (i.e. no newline in the array) we prepend an extra call
62- // to import the placeholder since the user is not importing all keys.
63- // For multiline formatting, we leave the call as-is.
58+ // For inline formatting (no newline in the array), we insert an extra call above the user call.
6459 if ( isMultiline ) {
6560 rewritten = wrapWithFusionImport ( modifiedCall ) ;
6661 } else {
@@ -74,7 +69,7 @@ function rewrite(code, script, keys) {
7469}
7570
7671function checkAliasedUseFusion ( code ) {
77- // Look for imports with curly braces and check if useFusion is aliased.
72+ // Look for import statements with curly braces and check if useFusion is aliased.
7873 const regex = / i m p o r t \s * { ( [ ^ } ] + ) } / gm;
7974 let match ;
8075 while ( ( match = regex . exec ( code ) ) !== null ) {
@@ -93,7 +88,7 @@ ${extraLine ? extraLine + "\n" : ""}${content}`;
9388}
9489
9590function extractUseFusionParams ( code ) {
96- // This regex handles both inline and multiline array formatting .
91+ // Modified regex to handle both inline and multiline arrays .
9792 const regex = / u s e F u s i o n \s * \( \s * \[ ( [ \s \S ] * ?) \] \s * \) / g;
9893 let handledKeys = [ ] ;
9994 let match ;
@@ -106,7 +101,7 @@ function extractUseFusionParams(code) {
106101 . filter ( Boolean ) ;
107102 handledKeys . push ( ...keysInCall ) ;
108103 }
109- // If a useFusion() call exists with no parameters, treat it as handling all keys.
104+ // If a useFusion call exists with no parameters, treat it as handling all keys.
110105 if ( handledKeys . length === 0 && / u s e F u s i o n \s * \( \s * \) / . test ( code ) ) {
111106 return [ "*" ] ;
112107 }
@@ -117,9 +112,9 @@ function modifyUseFusionCalls(code, allKeys) {
117112 const regex = / ( u s e F u s i o n \s * \( ) ( \s * \[ [ \s \S ] * ?\] ) ? \s * \) / g;
118113 return code . replace ( regex , ( match , prefix , params ) => {
119114 if ( ! params ) {
120- return `${ prefix } [${ allKeys . map ( k => `'${ k } '` ) . join ( ',' ) } ], __props.fusion)` ;
115+ return `${ prefix } [${ allKeys . map ( k => `'${ k } '` ) . join ( ',' ) } ], __props.fusion, true )` ;
121116 }
122- return `${ prefix } ${ params } , __props.fusion)` ;
117+ return `${ prefix } ${ params } , __props.fusion, true )` ;
123118 } ) ;
124119}
125120
0 commit comments