Skip to content

Commit 9c17c4d

Browse files
committed
added testing for lazy loading import statements
1 parent fe1605d commit 9c17c4d

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

package/astParser.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
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+
// Helper function to recursively traverse through the user's codebase
7+
// INSERT HERE
78

89
module.exports = file => {
910
// Initialize empty object to store the setters and getter
1011
const hookState = {};
1112
const ast = JSXParser.parse(file).body;
13+
console.log('AST Tree', ast);
1214
// Iterate through AST of every function declaration
1315
// Check within each function declaration if there are hook declarations
1416
ast.forEach(func => {
15-
const { body } = func.body;
17+
const { body } = func.body;
1618
const statements = [];
1719
// Traverse through the function's funcDecs and Expression Statements
1820
body.forEach(program => {

package/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
2525
}
2626
});
2727

28-
module.exports = linkFiber;
28+
function visitor() {
29+
return './Components/BoardGame.js';
30+
}
31+
32+
// module.exports = linkFiber;
33+
module.exports = {
34+
reactime: linkFiber,
35+
visitor,
36+
};

package/linkFiber.js

Lines changed: 18 additions & 16 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;
@@ -24,8 +24,9 @@ module.exports = (snap, mode) => {
2424
}
2525

2626
function changeSetState(component) {
27+
console.log('Component', component);
2728
// check that setState hasn't been changed yet
28-
if (component.setState.linkFiberChanged) return;
29+
if (component.setState.linkFiberChanged === true) return;
2930
// make a copy of setState
3031
const oldSetState = component.setState.bind(component);
3132
// replace component's setState so developer doesn't change syntax
@@ -46,12 +47,12 @@ module.exports = (snap, mode) => {
4647

4748
function changeUseState(component) {
4849
if (component.queue.dispatch.linkFiberChanged) return;
49-
// store the original dispatch function definition
50-
const oldDispatch = component.queue.dispatch.bind(component.queue);;
50+
// store the original dispatch function definition
51+
const oldDispatch = component.queue.dispatch.bind(component.queue);
5152
// redefine the dispatch function so we can inject our code
5253
component.queue.dispatch = (fiber, queue, action) => {
5354
// don't do anything if state is locked
54-
if (mode.locked && !mode.jumping) return;
55+
if (mode.locked && !mode.jumping) return;
5556
oldDispatch(fiber, queue, action);
5657
setTimeout(() => {
5758
updateSnapShotTree();
@@ -63,19 +64,19 @@ module.exports = (snap, mode) => {
6364

6465
// Helper function to traverse through the memoized state
6566
function traverseHooks(memoizedState) {
66-
// Declare variables and assigned to 0th index and an empty object, respectively
67+
// Declare variables and assigned t return nextComponent;o 0th index and an empty object, respectively
6768
const memoized = {};
68-
let index = 0;
69-
astHooks = Object.values(astHooks);
69+
let index = 0;
70+
astHooks = Object.values(astHooks);
7071
// while memoizedState is truthy, save the value to the object
7172
while (memoizedState) {
7273
changeUseState(memoizedState);
73-
//memoized[astHooks[index]] = memoizedState.memoizedState;
74-
memoized[astHooks[index]] = memoizedState.memoizedState;
74+
// memoized[astHooks[index]] = memoizedState.memoizedState;
75+
memoized[astHooks[index]] = memoizedState.memoizedState;
7576
// Reassign memoizedState to its next value
7677
memoizedState = memoizedState.next;
7778
// Increment the index by 2
78-
index += 2;
79+
index += 2;
7980
}
8081
return memoized;
8182
}
@@ -100,7 +101,7 @@ module.exports = (snap, mode) => {
100101
}
101102
// Check if the component uses hooks
102103
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
103-
// Add a traversed property and initialize to the evaluated result
104+
// Add a traversed property and initialize to the evaluated result
104105
// of invoking traverseHooks, and reassign nextTree
105106
memoizedState.traversed = traverseHooks(memoizedState);
106107
nextTree = tree.appendChild(memoizedState);
@@ -125,16 +126,17 @@ module.exports = (snap, mode) => {
125126
} = container;
126127
// only assign internal rootp if it actually exists
127128
fiberRoot = _internalRoot || _reactRootContainer;
128-
// If hooks are implemented, traverse through the source code
129+
// If hooks are implemented, traverse through the source code
129130
// Save the getter/setter combo for timeJump
130131
if (entryFile) {
131132
astHooks = astParser(entryFile);
132-
saveState(astHooks);
133+
console.log('Ast Hooks', astHooks);
134+
saveState(astHooks);
133135
}
134136
updateSnapShotTree();
135137
// send the initial snapshot once the content script has started up
136138
window.addEventListener('message', ({ data: { action } }) => {
137139
if (action === 'contentScriptStarted') sendSnapshot();
138140
});
139-
}
141+
};
140142
};

package/masterState.js

Lines changed: 3 additions & 3 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 = [];
@@ -8,7 +10,5 @@ module.exports = {
810
masterState.push(state[key]);
911
}
1012
},
11-
returnState: () => {
12-
return masterState;
13-
},
13+
returnState: () => masterState,
1414
};

package/timeJump.js

Lines changed: 3 additions & 2 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,7 +27,7 @@ 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+
const hooks = returnState();
3031
// while loop through the memoize tree
3132
while (current) {
3233
current.queue.dispatch(target.state[hooks[index]]);

0 commit comments

Comments
 (0)