Skip to content

Commit 6a953bf

Browse files
committed
refactored to include astParser.js to access client source code
1 parent 989a6f7 commit 6a953bf

File tree

7 files changed

+116
-29
lines changed

7 files changed

+116
-29
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"David Chai",
2727
"Josh Kim",
2828
"Ruthba Anam",
29-
"Ryan Dang",
29+
"Ryan Dang",
3030
"Sierra Swaby",
3131
"Yujin Kang"
3232
],
@@ -59,6 +59,8 @@
5959
"webpack-cli": "^3.3.6"
6060
},
6161
"dependencies": {
62+
"acorn": "^7.1.0",
63+
"acorn-jsx": "^5.0.2",
6264
"d3": "^3.5.17",
6365
"immer": "^3.2.0",
6466
"jsondiffpatch": "^0.3.11",

package/astParser.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const acorn = require('acorn');
2+
const jsx = require('acorn-jsx');
3+
4+
const JSXParser = acorn.Parser.extend(jsx());
5+
6+
const hookState = {};
7+
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);
23+
});
24+
}
25+
});
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];
34+
}
35+
}
36+
});
37+
return hookState;
38+
};

package/index.js

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

28-
module.exports = linkFiber;
28+
module.exports = linkFiber;

package/linkFiber.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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.js');
78

89
module.exports = (snap, mode) => {
910
let fiberRoot = null;
@@ -29,7 +30,7 @@ module.exports = (snap, mode) => {
2930

3031
// replace component's setState so developer doesn't change syntax
3132
// component.setState = newSetState.bind(component);
32-
component.setState = (state, callback = () => {}) => {
33+
component.setState = (state, callback = () => { }) => {
3334
// dont do anything if state is locked
3435
// UNLESS we are currently jumping through time
3536
if (mode.locked && !mode.jumping) return;
@@ -43,7 +44,7 @@ module.exports = (snap, mode) => {
4344
component.setState.linkFiberChanged = true;
4445
}
4546

46-
function changeUseState (component) {
47+
function changeUseState(component) {
4748
if (component.queue.dispatch.linkFiberChanged) return;
4849
// storing the original dispatch function definition somewhere
4950
const oldDispatch = component.queue.dispatch.bind(component.queue);
@@ -59,8 +60,8 @@ module.exports = (snap, mode) => {
5960
}, 100);
6061
};
6162
component.queue.dispatch.linkFiberChanged = true;
62-
};
63-
63+
}
64+
6465
// Helper function to traverse through the memoized state
6566
function traverseHooks(memoizedState) {
6667
// Declare variables and assigned to 0th index and an empty object, respectively
@@ -79,7 +80,7 @@ module.exports = (snap, mode) => {
7980

8081
function createTree(currentFiber, tree = new Tree('root')) {
8182
if (!currentFiber) return tree;
82-
83+
8384
const {
8485
sibling,
8586
stateNode,
@@ -99,7 +100,6 @@ module.exports = (snap, mode) => {
99100
// TODO: Refactor the conditionals - think about the edge case where a stateful
100101
// component might have a key called 'baseState' in the state
101102
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
102-
console.log("I'm not supposed to run", currentFiber);
103103
memoizedState.traversed = traverseHooks(memoizedState);
104104
nextTree = tree.appendChild(memoizedState);
105105
}
@@ -116,20 +116,21 @@ module.exports = (snap, mode) => {
116116
console.log('current', current);
117117
snap.tree = createTree(current);
118118
}
119-
120119
return {
121-
_(container) {
120+
_(container, ENTRYFILE) {
122121
const {
123122
_reactRootContainer: { _internalRoot },
124123
_reactRootContainer,
125124
} = container;
126-
// only assign internal root if it actually exists
125+
// only assign internal rootp if it actually exists
127126
fiberRoot = _internalRoot || _reactRootContainer;
128127
updateSnapShotTree();
129128
// send the initial snapshot once the content script has started up
130129
window.addEventListener('message', ({ data: { action } }) => {
131130
if (action === 'contentScriptStarted') sendSnapshot();
132131
});
132+
const astEntryFile = astParser(ENTRYFILE);
133+
console.log('ENTRYFILE into ast', astEntryFile);
133134
},
134135
};
135136
};

package/package-lock.json

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

package/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
"David Chai",
2828
"Josh Kim",
2929
"Ruthba Anam",
30-
"Ryan Dang",
30+
"Ryan Dang",
3131
"Sierra Swaby",
3232
"Yujin Kang"
3333
],
3434
"license": "MIT",
35-
"dependencies": {}
35+
"dependencies": {
36+
"acorn": "^7.1.0",
37+
"acorn-jsx": "^5.0.2"
38+
}
3639
}

0 commit comments

Comments
 (0)