@@ -120,13 +120,12 @@ function commitAllHostEffectsReplacement() {
120120const 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 \) / ) ;
121121const 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' ) ;
122122// get replacer method bodies
123- const injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
124- // const injectableCommitAllHostEffects = injectableCommitAllHostEffects.body[0].body;
125- const injectableCommitAllHostEffectsString = escodegen . generate ( injectableCommitAllHostEffects . body [ 0 ] . body ) ;
126-
127123const injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
128124const injectableUseReducerString = escodegen . generate ( injectableUseReducer . body [ 0 ] . body ) ;
129125
126+ const injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
127+ // const injectableCommitAllHostEffects = injectableCommitAllHostEffects.body[0].body;
128+ const injectableCommitAllHostEffectsString = escodegen . generate ( injectableCommitAllHostEffects . body [ 0 ] . body ) ;
130129// traverse ast to find method and replace body with our node's body
131130function traverseTree ( replacementNode , functionName , ast ) {
132131 console . log ( 'traverse called' ) ;
@@ -141,17 +140,47 @@ function traverseTree(replacementNode, functionName, ast) {
141140 } ,
142141 } ) ;
143142}
144-
143+ function stringParser ( string , newBody , methodSig ) {
144+ let stack = [ ] ;
145+ const foundMethod = methodSig . test ( string ) ;
146+ let oldBody = '' ;
147+ let output ;
148+ for ( let i = methodSig . lastIndex ; i < string . length ; i ++ ) {
149+ if ( foundMethod ) {
150+ if ( string [ i ] === '{' ) {
151+ stack . push ( string [ i ] ) ;
152+ }
153+ if ( stack . length > 0 && stack [ stack . length - 1 ] === '{' && string [ i ] === '}' ) {
154+ stack . pop ( ) ;
155+ oldBody += string [ i ] ;
156+ output = string . replace ( oldBody , newBody ) ;
157+ break ;
158+ }
159+ if ( stack . length > 0 ) {
160+ oldBody += string [ i ] ;
161+ }
162+ }
163+ }
164+ return output ;
165+ }
145166const parseAndGenerate = ( codeString ) => {
146167 if ( codeString . search ( 'react' ) !== - 1 ) {
147- const ast = esprima . parseModule ( codeString ) ;
148-
168+ let ast ;
169+ try {
170+ ast = esprima . parseModule ( codeString ) ;
171+ } catch ( error ) {
172+ // exprima throws parsing error webpack devtool setting generates code
173+ console . log ( 'unable to use esprima parser' ) ;
174+ codeString = stringParser ( codeString , injectableUseReducerString , uRsig ) ;
175+ codeString = stringParser ( codeString , injectableCommitAllHostEffectsString , cAHEsig ) ;
176+ return codeString ;
177+ }
149178 // parse react-dom code
150- const injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
179+ injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
151180 traverseTree ( injectableCommitAllHostEffects , 'commitAllHostEffects' , ast ) ;
152181
153182 // parse react code
154- const injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
183+ injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
155184 traverseTree ( injectableUseReducer , 'useReducer' , ast ) ;
156185
157186 const code = escodegen . generate ( ast ) ;
0 commit comments