11const esprima = require ( 'esprima' ) ;
22const estraverse = require ( 'estraverse' ) ;
33const escodegen = require ( 'escodegen' ) ;
4- const _ = require ( 'lodash' ) ;
54
65// declare functions to insert
76function useReducerReplacement ( ) {
@@ -103,9 +102,7 @@ function commitAllHostEffectsReplacement() {
103102 resetCurrentFiber ( ) ;
104103 }
105104}
106- // regex method signatures
107- const uRsig = new RegExp ( / \b ( u s e R e d u c e r ) \b \( r e d u c e r , i n i t i a l A r g , i n i t \) / ) ;
108- const cAHEsig = new RegExp ( / \b ( f u n c t i o n ) \b \s \b ( c o m m i t A l l H o s t E f f e c t s ) \b \( \) / , 'g' ) ;
105+
109106// method names
110107const USEREDUCER = 'useReducer' ;
111108const COMMITALLHOSTEFFECTS = 'commitAllHostEffects' ;
@@ -114,10 +111,8 @@ const reactLibraryPath = './node_modules/react/cjs/react.development.js';
114111const reactDOMLibraryPath = './node_modules/react-dom/cjs/react-dom.development.js' ;
115112// get replacer method
116113let injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
117- const injectableUseReducerString = escodegen . generate ( injectableUseReducer . body [ 0 ] . body ) ;
118114
119115let injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
120- const injectableCommitAllHostEffectsString = escodegen . generate ( injectableCommitAllHostEffects . body [ 0 ] . body ) ;
121116
122117// traverse ast to find method and replace body with our node's body
123118function traverseTree ( replacementNode , functionName , ast ) {
@@ -133,54 +128,28 @@ function traverseTree(replacementNode, functionName, ast) {
133128 } ,
134129 } ) ;
135130}
136- function stringParser ( string , newBody , methodSig ) {
137- const stack = [ ] ;
138- const foundMethod = methodSig . test ( string ) ;
139- let oldBody = '' ;
140- let output ;
141- for ( let i = methodSig . lastIndex ; i < string . length ; i ++ ) {
142- if ( foundMethod ) {
143- if ( string [ i ] === '{' ) {
144- stack . push ( string [ i ] ) ;
145- }
146- if ( stack . length > 0 && stack [ stack . length - 1 ] === '{' && string [ i ] === '}' ) {
147- stack . pop ( ) ;
148- oldBody += string [ i ] ;
149- output = string . replace ( oldBody , newBody ) ;
150- break ;
151- }
152- if ( stack . length > 0 ) {
153- oldBody += string [ i ] ;
154- }
155- }
156- }
157- return output ;
158- }
131+
159132function traverseBundledTree ( replacementNode , functionName , ast , library ) {
160- estraverse . replace ( ast , {
133+ estraverse . traverse ( ast , {
161134 enter ( node ) {
162135 if ( node . key && node . key . value === library ) {
163136 if ( node . value . body . body [ 1 ] . type === 'ExpressionStatement' ) {
164137 if ( node . value . body . body [ 1 ] . expression . callee . name === 'eval' ) {
165138 // create new ast
166- // const reactLib = esprima.parseScript(node.value.body.body[1].expression.arguments[0].value, { range: true, tokens: true, comment: true });
167139 const reactLib = esprima . parseScript ( node . value . body . body [ 1 ] . expression . arguments [ 0 ] . value ) ;
168- estraverse . replace ( reactLib , {
140+ estraverse . traverse ( reactLib , {
169141 enter ( libNode ) {
170142 if ( libNode . type === 'FunctionDeclaration' ) {
171143 if ( libNode . id . name === functionName ) {
172144 libNode . body = replacementNode . body [ 0 ] . body ;
173- console . log ( 'From parser. REPLACING!' , libNode . id . name ) ;
174- // return libNode;
145+ console . log ( 'From parser. REPLACING body!' , libNode . id . name ) ;
175146 }
176147 }
177148 } ,
178149 } ) ;
179- // reactLib = escodegen.attachComments(reactLib, reactLib.comments, reactLib.tokens);
180- node . value . body . body [ 1 ] . expression . arguments = escodegen . generate ( reactLib ) ;
181- console . log ( 'generated' ) ;
182- // node.value.body.body[1].expression.arguments[0].value = escodegen.generate(reactLib, { comment: true });
183- // node.value.body.body[1].expression.arguments[0].value = stringParser(node.value.body.body[1].expression.arguments[0].value, replacementNode, functionName);
150+ node . value . body . body [ 1 ] . expression . arguments [ 0 ] . value = escodegen . generate ( reactLib ) ;
151+ node . value . body . body [ 1 ] . expression . arguments [ 0 ] . raw = JSON . stringify ( escodegen . generate ( reactLib ) ) ;
152+ console . log ( 'arguments replaced' ) ;
184153 }
185154 }
186155 }
@@ -193,13 +162,8 @@ const parseAndGenerate = (codeString) => {
193162 const ast = esprima . parseModule ( codeString ) ;
194163 // Webpack bundle is wrapped in function call
195164 if ( ast . body [ 0 ] . expression . type === 'CallExpression' ) {
196- // if (ast.body[0].expression.arguments[0].properties[6].key.value ==='./node_modules/react/cjs/react.development.js'){
197- // const reactLib = esprima.parseModule(ast.body[0].expression.arguments[0].properties[6].value.body.body[1].expression.arguments[0].value);
198- // .value at end is a string
199165 traverseBundledTree ( injectableUseReducer , USEREDUCER , ast , reactLibraryPath ) ;
200166 traverseBundledTree ( injectableCommitAllHostEffects , COMMITALLHOSTEFFECTS , ast , reactDOMLibraryPath ) ;
201- // traverseBundledTree(injectableUseReducerString, uRsig, ast, reactLibraryPath);
202- // traverseBundledTree(injectableCommitAllHostEffectsString, cAHEsig, ast, reactDOMLibraryPath);
203167 } else {
204168 // parse react-dom code
205169 injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
0 commit comments