Skip to content

Commit 87eb35d

Browse files
authored
Merge pull request #18 from kiacolbert/parserString
Parser
2 parents 2863135 + e1b9f07 commit 87eb35d

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

src/browser/chrome/parser.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// function parser() {
2+
const esprima = require('esprima');
3+
const estraverse = require('estraverse');
4+
const escodegen = require('escodegen');
5+
const _ = require('lodash');
6+
7+
// declare functions to insert
8+
// TODO: Un-comment timeTravelTracker
9+
function useReducerReplacement() {
10+
const dispatcher = resolveDispatcher();
11+
function reducerWithTracker(state, action) {
12+
const newState = reducer(state, action);
13+
// timeTravelTracker[timeTravelTracker.length - 1].actionDispatched = true;
14+
window.postMessage({
15+
type: 'DISPATCH',
16+
data: {
17+
state: newState,
18+
action,
19+
},
20+
});
21+
return newState;
22+
}
23+
return dispatcher.useReducer(reducerWithTracker, initialArg, init);
24+
}
25+
function commitAllHostEffectsReplacement() {
26+
while (nextEffect !== null) {
27+
{
28+
setCurrentFiber(nextEffect);
29+
}
30+
recordEffect();
31+
32+
let {effectTag} = nextEffect;
33+
34+
if (effectTag & ContentReset) {
35+
commitResetTextContent(nextEffect);
36+
}
37+
38+
if (effectTag & Ref) {
39+
let current$$1 = nextEffect.alternate;
40+
if (current$$1 !== null) {
41+
commitDetachRef(current$$1);
42+
}
43+
}
44+
45+
// The following switch statement is only concerned about placement,
46+
// updates, and deletions. To avoid needing to add a case for every
47+
// possible bitmap value, we remove the secondary effects from the
48+
// effect tag and switch on that value.
49+
let primaryEffectTag = effectTag & (Placement | Update | Deletion);
50+
switch (primaryEffectTag) {
51+
case Placement:
52+
{
53+
// editbyme
54+
window.postMessage({
55+
type: 'EFFECT',
56+
data: {
57+
primaryEffectTag: 'PLACEMENT',
58+
effect: _.cloneDeep(nextEffect),
59+
},
60+
});
61+
62+
commitPlacement(nextEffect);
63+
// Clear the "placement" from effect tag so that we know that this is inserted, before
64+
// any life-cycles like componentDidMount gets called.
65+
// TODO: findDOMNode doesn't rely on this any more but isMounted
66+
// does and isMounted is deprecated anyway so we should be able
67+
// to kill this.
68+
nextEffect.effectTag &= ~Placement;
69+
break;
70+
}
71+
case PlacementAndUpdate:
72+
{
73+
// Placement
74+
commitPlacement(nextEffect);
75+
// Clear the "placement" from effect tag so that we know that this is inserted, before
76+
// any life-cycles like componentDidMount gets called.
77+
nextEffect.effectTag &= ~Placement;
78+
79+
// Update
80+
let _current = nextEffect.alternate;
81+
commitWork(_current, nextEffect);
82+
break;
83+
}
84+
case Update:
85+
{
86+
// editbyme
87+
window.postMessage({
88+
type: 'EFFECT',
89+
data: {
90+
primaryEffectTag: 'UPDATE',
91+
effect: _.cloneDeep(nextEffect),
92+
current: _.cloneDeep(nextEffect.alternate),
93+
},
94+
});
95+
96+
let _current2 = nextEffect.alternate;
97+
commitWork(_current2, nextEffect);
98+
break;
99+
}
100+
case Deletion:
101+
{
102+
// editbyme
103+
window.postMessage({
104+
type: 'EFFECT',
105+
data: {
106+
primaryEffectTag: 'DELETION',
107+
effect: _.cloneDeep(nextEffect),
108+
},
109+
});
110+
111+
commitDeletion(nextEffect);
112+
break;
113+
}
114+
}
115+
nextEffect = nextEffect.nextEffect;
116+
}
117+
118+
{
119+
resetCurrentFiber();
120+
}
121+
}
122+
123+
// traverse ast to find method and replace body with our node's body
124+
function traverseTree(replacementNode, functionName, ast) {
125+
estraverse.replace(ast, {
126+
enter(node) {
127+
if (node.type === 'FunctionDeclaration') {
128+
if (node.id.name === functionName) {
129+
node.body = replacementNode.body[0].body;
130+
}
131+
}
132+
},
133+
});
134+
}
135+
136+
const parseAndGenerate = (codeString) => {
137+
if (codeString.search('react')) {
138+
const ast = esprima.parseModule(codeString);
139+
// parse react-dom code
140+
if (codeString.search('react-dom')) {
141+
const injectableCommitAllHostEffects = esprima.parseScript(commitAllHostEffectsReplacement.toString());
142+
traverseTree(injectableCommitAllHostEffects, 'commitAllHostEffects', ast);
143+
} else {
144+
// parse react code
145+
const injectableUseReducer = esprima.parseScript(useReducerReplacement.toString());
146+
traverseTree(injectableUseReducer, 'useReducer', ast);
147+
}
148+
const code = escodegen.generate(ast);
149+
return code;
150+
}
151+
return -1;
152+
};
153+
154+
// }
155+
module.exports = parseAndGenerate;

0 commit comments

Comments
 (0)