Skip to content

Commit 1193141

Browse files
committed
Merged new parser script that deals with webpack bundled scripts.
2 parents b90eb96 + 0f8174c commit 1193141

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

src/browser/chrome/scripts/parser.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// function parser() {
21
const esprima = require('esprima');
32
const estraverse = require('estraverse');
43
const escodegen = require('escodegen');
@@ -49,10 +48,6 @@ function commitAllHostEffectsReplacement() {
4948
}
5049
}
5150

52-
// The following switch statement is only concerned about placement,
53-
// updates, and deletions. To avoid needing to add a case for every
54-
// possible bitmap value, we remove the secondary effects from the
55-
// effect tag and switch on that value.
5651
let primaryEffectTag = effectTag & (Placement | Update | Deletion);
5752
switch (primaryEffectTag) {
5853
case Placement:
@@ -64,23 +59,14 @@ function commitAllHostEffectsReplacement() {
6459
});
6560

6661
commitPlacement(nextEffect);
67-
// Clear the "placement" from effect tag so that we know that this is inserted, before
68-
// any life-cycles like componentDidMount gets called.
69-
// TODO: findDOMNode doesn't rely on this any more but isMounted
70-
// does and isMounted is deprecated anyway so we should be able
71-
// to kill this.
62+
7263
nextEffect.effectTag &= ~Placement;
7364
break;
7465
}
7566
case PlacementAndUpdate:
7667
{
77-
// Placement
7868
commitPlacement(nextEffect);
79-
// Clear the "placement" from effect tag so that we know that this is inserted, before
80-
// any life-cycles like componentDidMount gets called.
8169
nextEffect.effectTag &= ~Placement;
82-
83-
// Update
8470
let _current = nextEffect.alternate;
8571
commitWork(_current, nextEffect);
8672
break;
@@ -117,6 +103,16 @@ function commitAllHostEffectsReplacement() {
117103
resetCurrentFiber();
118104
}
119105
}
106+
// regex method signatures
107+
const uRsig = new RegExp(/\b(useReducer)\b\(reducer, initialArg, init\)/);
108+
const cAHEsig = new RegExp(/\b(function)\b\s\b(commitAllHostEffects)\b\(\)/, 'g');
109+
110+
// get replacer method bodies
111+
let injectableUseReducer = esprima.parseScript(useReducerReplacement.toString());
112+
let injectableUseReducerString = escodegen.generate(injectableUseReducer.body[0].body);
113+
114+
let injectableCommitAllHostEffects = esprima.parseScript(commitAllHostEffectsReplacement.toString());
115+
let injectableCommitAllHostEffectsString = escodegen.generate(injectableCommitAllHostEffects.body[0].body);
120116

121117
// traverse ast to find method and replace body with our node's body
122118
function traverseTree(replacementNode, functionName, ast) {
@@ -132,19 +128,49 @@ function traverseTree(replacementNode, functionName, ast) {
132128
},
133129
});
134130
}
135-
131+
function stringParser(string, newBody, methodSig) {
132+
let stack = [];
133+
const foundMethod = methodSig.test(string);
134+
let oldBody = '';
135+
let output;
136+
for (let i = methodSig.lastIndex; i < string.length; i++) {
137+
if (foundMethod) {
138+
if (string[i] === '{') {
139+
stack.push(string[i]);
140+
}
141+
if (stack.length > 0 && stack[stack.length - 1] === '{' && string[i] === '}') {
142+
stack.pop();
143+
oldBody += string[i];
144+
output = string.replace(oldBody, newBody);
145+
break;
146+
}
147+
if (stack.length > 0) {
148+
oldBody += string[i];
149+
}
150+
}
151+
}
152+
return output;
153+
}
136154
const parseAndGenerate = (codeString) => {
137155
if (codeString.search('react') !== -1) {
138-
const ast = esprima.parseModule(codeString);
139-
156+
let ast;
157+
try {
158+
ast = esprima.parseModule(codeString);
159+
} catch (error) {
160+
// esprima throws parsing error webpack devtool setting generates code
161+
console.log('unable to use esprima parser');
162+
codeString = stringParser(codeString, injectableUseReducerString, uRsig);
163+
codeString = stringParser(codeString, injectableCommitAllHostEffectsString, cAHEsig);
164+
return codeString;
165+
}
140166
// parse react-dom code
141-
const injectableCommitAllHostEffects = esprima.parseScript(commitAllHostEffectsReplacement.toString());
167+
injectableCommitAllHostEffects = esprima.parseScript(commitAllHostEffectsReplacement.toString());
142168
traverseTree(injectableCommitAllHostEffects, 'commitAllHostEffects', ast);
143169

144170
// parse react code
145-
const injectableUseReducer = esprima.parseScript(useReducerReplacement.toString());
171+
injectableUseReducer = esprima.parseScript(useReducerReplacement.toString());
146172
traverseTree(injectableUseReducer, 'useReducer', ast);
147-
173+
148174
const code = escodegen.generate(ast);
149175
console.log('returning code.');
150176
return code;

0 commit comments

Comments
 (0)