Skip to content

Commit c82a117

Browse files
committed
Debugged and refactored AST, linkFiber and timeJump
1 parent 6a953bf commit c82a117

File tree

3 files changed

+64
-74
lines changed

3 files changed

+64
-74
lines changed

package/astParser.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
const acorn = require('acorn');
22
const jsx = require('acorn-jsx');
3-
43
const JSXParser = acorn.Parser.extend(jsx());
54

6-
const hookState = {};
5+
// Helper function to recursively traverse through the user's codebase
6+
// INSERT HERE
77

8-
module.exports = jsFile => {
9-
// console.log('ast inside', jsFile);
10-
const ast = JSXParser.parse(jsFile).body;
11-
// Iterating through AST of every function declaration
12-
ast.forEach(astFuncDec => {
13-
// Checking within each function declaration if there are hook declarations
14-
// console.log('App.js', astFuncDec.body.body);
15-
const fd = astFuncDec.body.body;
16-
// console.log(astFuncDec.body.body);
17-
// Traversing through the function's funcDecs and Expression Statements
18-
const allVarDeclarations = [];
19-
fd.forEach(astProgramBody => {
20-
if (astProgramBody.type === 'VariableDeclaration') {
21-
astProgramBody.declarations.forEach(declarations => {
22-
allVarDeclarations.push(declarations.id.name);
8+
module.exports = file => {
9+
// Initialize empty object to store the setters and getter
10+
const hookState = {};
11+
const ast = JSXParser.parse(file).body;
12+
// Iterate through AST of every function declaration
13+
// Check within each function declaration if there are hook declarations
14+
ast.forEach(func => {
15+
const { body } = func.body;
16+
const statements = [];
17+
// Traverse through the function's funcDecs and Expression Statements
18+
body.forEach(program => {
19+
if (program.type === 'VariableDeclaration') {
20+
program.declarations.forEach(dec => {
21+
statements.push(dec.id.name);
2322
});
2423
}
2524
});
26-
// console.log(allVarDeclarations);
27-
// Iterate through the array
28-
for (let varDec = 0; varDec < allVarDeclarations.length; varDec += 1) {
29-
// Determine getter/setters based on pattern
30-
if (allVarDeclarations[varDec].match(/_useState/)) {
31-
// Map the 4 elements together
32-
hookState[allVarDeclarations[varDec]] = allVarDeclarations[varDec + 2];
33-
// hookState[allVarDeclarations[varDec + 1]] = allVarDeclarations[varDec + 2];
25+
// Iterate through the array and determine getter/setters based on pattern
26+
for (let i = 0; i < statements.length; i += 1) {
27+
if (statements[i].match(/_use/)) {
28+
hookState[statements[i]] = statements[i + 2];
3429
}
3530
}
3631
});
32+
// Return the object with setters and getters
3733
return hookState;
3834
};

package/linkFiber.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const astParser = require('./astParser.js');
88

99
module.exports = (snap, mode) => {
1010
let fiberRoot = null;
11+
let astHooks;
1112

1213
function sendSnapshot() {
1314
// don't send messages while jumping or while paused
@@ -24,14 +25,12 @@ module.exports = (snap, mode) => {
2425
function changeSetState(component) {
2526
// check that setState hasn't been changed yet
2627
if (component.setState.linkFiberChanged) return;
27-
2828
// make a copy of setState
2929
const oldSetState = component.setState.bind(component);
30-
3130
// replace component's setState so developer doesn't change syntax
3231
// component.setState = newSetState.bind(component);
3332
component.setState = (state, callback = () => { }) => {
34-
// dont do anything if state is locked
33+
// don't do anything if state is locked
3534
// UNLESS we are currently jumping through time
3635
if (mode.locked && !mode.jumping) return;
3736
// continue normal setState functionality, except add sending message middleware
@@ -46,15 +45,14 @@ module.exports = (snap, mode) => {
4645

4746
function changeUseState(component) {
4847
if (component.queue.dispatch.linkFiberChanged) return;
49-
// storing the original dispatch function definition somewhere
50-
const oldDispatch = component.queue.dispatch.bind(component.queue);
51-
// redefining the dispatch function so we can inject our code
52-
component.queue.dispatch = function (fiber, queue, action) {
53-
console.log('mode', mode);
54-
if (mode.locked && !mode.jumping) return;
48+
// store the original dispatch function definition
49+
const oldDispatch = component.queue.dispatch.bind(component.queue);;
50+
// redefine the dispatch function so we can inject our code
51+
component.queue.dispatch = (fiber, queue, action) => {
52+
// don't do anything if state is locked
53+
if (mode.locked && !mode.jumping) return;
5554
oldDispatch(fiber, queue, action);
5655
setTimeout(() => {
57-
console.log('Updating the snapshot tree after an action has been dispatched');
5856
updateSnapShotTree();
5957
sendSnapshot();
6058
}, 100);
@@ -63,19 +61,22 @@ module.exports = (snap, mode) => {
6361
}
6462

6563
// Helper function to traverse through the memoized state
64+
// TODO: WE NEED TO CLEAN IT UP A BIT
6665
function traverseHooks(memoizedState) {
6766
// Declare variables and assigned to 0th index and an empty object, respectively
68-
let index = 0;
69-
const memoizedObj = {};
67+
const memoized = {};
68+
let index = 0;
69+
astHooks = Object.values(astHooks);
7070
// while memoizedState is truthy, save the value to the object
71-
while (memoizedState) {
71+
while (memoizedState && astHooks) {
7272
changeUseState(memoizedState);
73-
// Increment the index by 1
74-
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
73+
memoized[astHooks[index]] = memoizedState.memoizedState;
7574
// Reassign memoizedState to its next value
7675
memoizedState = memoizedState.next;
76+
// Increment the index by 2
77+
index += 2;
7778
}
78-
return memoizedObj;
79+
return memoized;
7980
}
8081

8182
function createTree(currentFiber, tree = new Tree('root')) {
@@ -97,9 +98,9 @@ module.exports = (snap, mode) => {
9798
changeSetState(stateNode);
9899
}
99100
// Check if the component uses hooks
100-
// TODO: Refactor the conditionals - think about the edge case where a stateful
101-
// component might have a key called 'baseState' in the state
102101
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
102+
// Add a traversed property and initialize to the evaluated result
103+
// of invoking traverseHooks, and reassign nextTree
103104
memoizedState.traversed = traverseHooks(memoizedState);
104105
nextTree = tree.appendChild(memoizedState);
105106
}
@@ -113,24 +114,23 @@ module.exports = (snap, mode) => {
113114

114115
function updateSnapShotTree() {
115116
const { current } = fiberRoot;
116-
console.log('current', current);
117117
snap.tree = createTree(current);
118118
}
119-
return {
120-
_(container, ENTRYFILE) {
121-
const {
122-
_reactRootContainer: { _internalRoot },
123-
_reactRootContainer,
124-
} = container;
125-
// only assign internal rootp if it actually exists
126-
fiberRoot = _internalRoot || _reactRootContainer;
127-
updateSnapShotTree();
128-
// send the initial snapshot once the content script has started up
129-
window.addEventListener('message', ({ data: { action } }) => {
130-
if (action === 'contentScriptStarted') sendSnapshot();
131-
});
132-
const astEntryFile = astParser(ENTRYFILE);
133-
console.log('ENTRYFILE into ast', astEntryFile);
134-
},
135-
};
119+
120+
return (container, entryFile) => {
121+
const {
122+
_reactRootContainer: { _internalRoot },
123+
_reactRootContainer,
124+
} = container;
125+
// only assign internal rootp if it actually exists
126+
fiberRoot = _internalRoot || _reactRootContainer;
127+
// If hooks are implemented, traverse through the source code
128+
if (entryFile) astHooks = astParser(entryFile);
129+
130+
updateSnapShotTree();
131+
// send the initial snapshot once the content script has started up
132+
window.addEventListener('message', ({ data: { action } }) => {
133+
if (action === 'contentScriptStarted') sendSnapshot();
134+
});
135+
}
136136
};

package/timeJump.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,19 @@ module.exports = (origin, mode) => {
2020
jump(child, coords.concat(i));
2121
});
2222
});
23-
}
24-
else {
23+
} else {
2524
// if component uses hooks
26-
// variable for current location
27-
let currLocation = originNode.component;
28-
// state no
29-
let stateNum = 1;
30-
console.log('component', originNode.component);
31-
// while loop through the memoize tree
32-
while (currLocation) {
33-
currLocation.queue.dispatch(target.state[`state${stateNum}`]);
34-
currLocation = currLocation.next;
35-
stateNum += 1;
25+
let current = originNode.component;
26+
let index = 1;
27+
// Iterate through the memoized tree
28+
while (current) {
29+
current.queue.dispatch(target.state[`state${index++}`]);
30+
current = current.next;
3631
}
3732
}
3833
}
3934

4035
return target => {
41-
console.log('im a target', target);
4236
// setting mode disables setState from posting messages to window
4337
mode.jumping = true;
4438
jump(target);

0 commit comments

Comments
 (0)