Skip to content

Commit ab9120a

Browse files
committed
refactored astParser to account for nested function declarations
2 parents f1760d4 + b90a22d commit ab9120a

File tree

5 files changed

+96
-74
lines changed

5 files changed

+96
-74
lines changed

package-lock.json

Lines changed: 31 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/astParser.js

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

5-
// Helper function to recursively traverse through the user's codebase
6-
// INSERT HERE
6+
const hookState = {};
7+
8+
function getHookNames()
79

810
module.exports = file => {
911
// 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);
22-
});
12+
let ast = JSXParser.parse(file);
13+
while (Object.hasOwnProperty.call(ast, 'body')) {
14+
// All module exports will always start off as a single 'FunctionDeclaration' type
15+
// Traverse down .body once before invoking parsing logic and will loop through any .body after
16+
ast = ast.body;
17+
console.log('AST Tree', ast);
18+
// Iterate through AST of every function declaration
19+
// Check within each function declaration if there are hook declarations
20+
ast.forEach(functionDec => {
21+
const { body } = functionDec.body;
22+
const statements = [];
23+
// Traverse through the function's funcDecs and Expression Statements
24+
body.forEach(program => {
25+
// Hook Declarations will only be under 'VariableDeclaration' type
26+
if (program.type === 'VariableDeclaration') {
27+
program.declarations.forEach(dec => {
28+
statements.push(dec.id.name);
29+
});
30+
}
31+
});
32+
// Iterate through the array and determine getter/setters based on pattern
33+
for (let i = 0; i < statements.length; i += 1) {
34+
if (statements[i].match(/_use/)) {
35+
hookState[statements[i]] = statements[i + 2];
36+
}
2337
}
2438
});
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];
29-
}
30-
}
31-
});
39+
}
3240
// Return the object with setters and getters
3341
return hookState;
3442
};

package/linkFiber.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// links component state tree to library
55
// changes the setState method to also update our snapshot
66
const Tree = require('./tree');
7-
const astParser = require('./astParser');
8-
const { saveState } = require('./masterState');
7+
const astParser = require('./astParser');
8+
const { saveState } = require('./masterState');
99

1010
module.exports = (snap, mode) => {
1111
let fiberRoot = null;
@@ -46,13 +46,13 @@ module.exports = (snap, mode) => {
4646

4747
function changeUseState(component) {
4848
if (component.queue.dispatch.linkFiberChanged) return;
49-
// store the original dispatch function definition
49+
// store the original dispatch function definition
5050
const oldDispatch = component.queue.dispatch.bind(component.queue);
5151
// redefine the dispatch function so we can inject our code
5252
component.queue.dispatch = (fiber, queue, action) => {
5353
// don't do anything if state is locked
54-
if (mode.locked && !mode.jumping) return;
55-
//oldDispatch(fiber, queue, action);
54+
if (mode.locked && !mode.jumping) return;
55+
// oldDispatch(fiber, queue, action);
5656
setTimeout(() => {
5757
oldDispatch(fiber, queue, action);
5858
updateSnapShotTree();
@@ -66,17 +66,17 @@ module.exports = (snap, mode) => {
6666
function traverseHooks(memoizedState) {
6767
// Declare variables and assigned to 0th index and an empty object, respectively
6868
const memoized = {};
69-
let index = 0;
70-
astHooks = Object.values(astHooks);
69+
let index = 0;
70+
astHooks = Object.values(astHooks);
7171
// while memoizedState is truthy, save the value to the object
7272
while (memoizedState) {
7373
changeUseState(memoizedState);
74-
//memoized[astHooks[index]] = memoizedState.memoizedState;
75-
memoized[astHooks[index]] = memoizedState.memoizedState;
74+
// memoized[astHooks[index]] = memoizedState.memoizedState;
75+
memoized[astHooks[index]] = memoizedState.memoizedState;
7676
// Reassign memoizedState to its next value
7777
memoizedState = memoizedState.next;
7878
// Increment the index by 2
79-
index += 2;
79+
index += 2;
8080
}
8181
return memoized;
8282
}
@@ -100,8 +100,8 @@ module.exports = (snap, mode) => {
100100
changeSetState(stateNode);
101101
}
102102
// Check if the component uses hooks
103-
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
104-
// Add a traversed property and initialize to the evaluated result
103+
if (memoizedState && Object.hasOwnProperty.call(memoizedState, 'baseState')) {
104+
// Add a traversed property and initialize to the evaluated result
105105
// of invoking traverseHooks, and reassign nextTree
106106
memoizedState.traversed = traverseHooks(memoizedState);
107107
nextTree = tree.appendChild(memoizedState);
@@ -113,8 +113,8 @@ module.exports = (snap, mode) => {
113113

114114
return tree;
115115
}
116-
// runs when page initially loads
117-
// but skips 1st hook click
116+
// runs when page initially loads
117+
// but skips 1st hook click
118118
function updateSnapShotTree() {
119119
const { current } = fiberRoot;
120120
snap.tree = createTree(current);
@@ -127,16 +127,25 @@ module.exports = (snap, mode) => {
127127
} = container;
128128
// only assign internal rootp if it actually exists
129129
fiberRoot = _internalRoot || _reactRootContainer;
130-
// If hooks are implemented, traverse through the source code
130+
// If hooks are implemented, traverse through the source code
131131
// Save the getter/setter combo for timeJump
132132
if (entryFile) {
133133
astHooks = astParser(entryFile);
134-
saveState(astHooks);
134+
console.log('Ast Hooks', astHooks);
135+
saveState(astHooks);
135136
}
136-
updateSnapShotTree();
137+
updateSnapShotTree();
137138
// send the initial snapshot once the content script has started up
138139
window.addEventListener('message', ({ data: { action } }) => {
139140
if (action === 'contentScriptStarted') sendSnapshot();
140141
});
141-
}
142+
//
143+
if (astHooks) {
144+
function consoleLoggerTest(someString) {
145+
return someString;
146+
}
147+
const testFunction = consoleLoggerTest('Hello from reactime');
148+
return testFunction;
149+
}
150+
};
142151
};

package/masterState.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable guard-for-in */
2+
/* eslint-disable no-restricted-syntax */
13
// Export two functions that either saves the AST state object into an array
24
// or returns the array for use
35
const masterState = [];
@@ -7,7 +9,7 @@ module.exports = {
79
for (const key in state) {
810
masterState.push(state[key]);
911
}
10-
return masterState;
12+
return masterState;
1113
},
12-
returnState: () => masterState
14+
returnState: () => masterState,
1315
};

package/timeJump.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-param-reassign */
22
// traverses given tree by accessing children through coords array
3-
const { returnState } = require('./masterState');
3+
const { returnState } = require('./masterState');
44

55
function traverseTree(tree, coords) {
66
let curr = tree;
@@ -16,6 +16,7 @@ module.exports = (origin, mode) => {
1616
const originNode = traverseTree(origin.tree, coords);
1717
// set the state of the origin tree if the component is stateful
1818
if (originNode.component.setState) {
19+
console.log('if(originNode.component.setState):', originNode.component);
1920
originNode.component.setState(target.state, () => {
2021
// iterate through new children once state has been set
2122
target.children.forEach((child, i) => {
@@ -26,8 +27,8 @@ module.exports = (origin, mode) => {
2627
// if component uses hooks, traverse through the memoize tree
2728
let current = originNode.component;
2829
let index = 0;
29-
const hooks = returnState();
30-
// Iterate through the memoize tree
30+
const hooks = returnState();
31+
// while loop through the memoize tree
3132
while (current) {
3233
current.queue.dispatch(target.state[hooks[index]]);
3334
// Reassign the current value

0 commit comments

Comments
 (0)