Skip to content

Commit f581b14

Browse files
committed
AST implementation fixed using elementType prop
1 parent 6828d73 commit f581b14

File tree

4 files changed

+14
-72
lines changed

4 files changed

+14
-72
lines changed

package/astParser.js

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

6-
// Helper function to recursively traverse AST of a specified component for all hook declarations
7-
function getHookNames(ast) {
5+
// Helper function to grab the getters/setters from `elementType`
6+
module.exports = elementType => {
87
// Initialize empty object to store the setters and getter
8+
let ast = JSXParser.parse(elementType);
99
const hookState = {};
1010
// All module exports will always start off as a single 'FunctionDeclaration' type
1111
while (Object.hasOwnProperty.call(ast, 'body')) {
@@ -35,19 +35,3 @@ function getHookNames(ast) {
3535
}
3636
return hookState;
3737
}
38-
39-
module.exports = file => {
40-
// Create an empty object to allow all invocations of getHookNames to consolidate
41-
let allHookNames = {};
42-
const ast = JSXParser.parse(file);
43-
// console.log('Original File', file.toString());
44-
// console.log('Original AST', ast);
45-
// Upsert any new/updated {_hookType#: hookName} pairs
46-
allHookNames = {
47-
...allHookNames,
48-
...getHookNames(ast),
49-
};
50-
51-
// Return the object with setters and getters
52-
return allHookNames;
53-
};

package/linkFiber.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
/* eslint-disable no-param-reassign */
44
// links component state tree to library
55
// changes the setState method to also update our snapshot
6-
7-
// import loadable from '@loadable/component';
8-
96
const Tree = require('./tree');
107
const astParser = require('./astParser');
118
const { saveState } = require('./masterState');
@@ -55,9 +52,8 @@ module.exports = (snap, mode) => {
5552
component.queue.dispatch = (fiber, queue, action) => {
5653
// don't do anything if state is locked
5754
if (mode.locked && !mode.jumping) return;
58-
// oldDispatch(fiber, queue, action);
55+
oldDispatch(fiber, queue, action);
5956
setTimeout(() => {
60-
oldDispatch(fiber, queue, action);
6157
updateSnapShotTree();
6258
sendSnapshot();
6359
}, 100);
@@ -87,12 +83,13 @@ module.exports = (snap, mode) => {
8783

8884
function createTree(currentFiber, tree = new Tree('root')) {
8985
if (!currentFiber) return tree;
90-
86+
9187
const {
9288
sibling,
9389
stateNode,
9490
child,
9591
memoizedState,
92+
elementType,
9693
} = currentFiber;
9794

9895
let nextTree = tree;
@@ -105,8 +102,11 @@ module.exports = (snap, mode) => {
105102
}
106103
// Check if the component uses hooks
107104
if (memoizedState && Object.hasOwnProperty.call(memoizedState, 'baseState')) {
108-
// Add a traversed property and initialize to the evaluated result
109-
// of invoking traverseHooks, and reassign nextTree
105+
// Traverse through the currentFiber and extract the getters/setters
106+
astHooks = astParser(elementType);
107+
saveState(astHooks);
108+
// Create a traversed property and assign to the evaluated result of
109+
// invoking traverseHooks with memoizedState
110110
memoizedState.traversed = traverseHooks(memoizedState);
111111
nextTree = tree.appendChild(memoizedState);
112112
}
@@ -124,20 +124,14 @@ module.exports = (snap, mode) => {
124124
snap.tree = createTree(current);
125125
}
126126

127-
return (container, entryFile) => {
127+
return container => {
128128
const {
129129
_reactRootContainer: { _internalRoot },
130130
_reactRootContainer,
131131
} = container;
132132
// only assign internal rootp if it actually exists
133133
fiberRoot = _internalRoot || _reactRootContainer;
134-
// If hooks are implemented, traverse through the source code
135-
// Save the getter/setter combo for timeJump
136-
if (entryFile) {
137-
astHooks = astParser(entryFile);
138-
// console.log('Ast Hooks', astHooks);
139-
saveState(astHooks);
140-
}
134+
141135
updateSnapShotTree();
142136
// send the initial snapshot once the content script has started up
143137
window.addEventListener('message', ({ data: { action } }) => {

package/package-lock.json

Lines changed: 1 addition & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extension/contentScript.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ chrome.runtime.onMessage.addListener(request => {
2222
const { action } = request;
2323
switch (action) {
2424
case 'jumpToSnap':
25-
console.log('jumpped to snap', request);
2625
chrome.runtime.sendMessage(request);
2726
window.postMessage(request);
2827
break;

0 commit comments

Comments
 (0)