Skip to content

Commit 4d79271

Browse files
authored
Merge pull request #9 from oslabs-beta/rl-br
update astParser linkFiber
2 parents c9b3d81 + 0ed1632 commit 4d79271

File tree

4 files changed

+98
-59
lines changed

4 files changed

+98
-59
lines changed

package/astParser.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-inner-declarations */
12
const acorn = require('acorn'); // javascript parser
23
// eslint-disable-next-line import/newline-after-import
34
const jsx = require('acorn-jsx');
@@ -12,27 +13,43 @@ module.exports = elementType => {
1213
while (Object.hasOwnProperty.call(ast, 'body')) {
1314
// Traverse down .body once before invoking parsing logic and will loop through any .body after
1415
ast = ast.body;
15-
// Iterate through AST of every function declaration
16-
// Check within each function declaration if there are hook declarations
17-
ast.forEach(functionDec => {
18-
const { body } = functionDec.body;
19-
const statements = [];
20-
// Traverse through the function's funcDecs and Expression Statements
21-
body.forEach(program => {
22-
// Hook Declarations will only be under 'VariableDeclaration' type
23-
if (program.type === 'VariableDeclaration') {
24-
program.declarations.forEach(dec => {
25-
statements.push(dec.id.name);
16+
const statements = [];
17+
18+
function saveAstHooks(st) {
19+
st.forEach((el, i) => {
20+
if (el.match(/_use/)) hookState[el] = statements[i + 2];
21+
});
22+
}
23+
24+
function findHookDeclarations(astVal) {
25+
astVal.forEach(elem => {
26+
if (elem.type === 'VariableDeclaration') {
27+
elem.declarations.forEach(decClar => {
28+
statements.push(decClar.id.name);
2629
});
2730
}
2831
});
29-
// Iterate through the array and determine getter/setters based on pattern
30-
for (let i = 0; i < statements.length; i += 1) {
31-
if (statements[i].match(/_use/)) {
32-
hookState[statements[i]] = statements[i + 2];
33-
}
34-
}
35-
});
32+
}
33+
34+
// handle useState useContext
35+
if (ast[0].expression.body.body) {
36+
ast = ast[0].expression.body.body;
37+
// Hook Declarations will only be under 'VariableDeclaration' type
38+
findHookDeclarations(ast);
39+
// Iterate array and determine getter/setters based on pattern
40+
saveAstHooks(statements); // add key-value to hookState
41+
} else {
42+
// TODO test if this is needed, backward compatibility?
43+
// Iterate through AST of every function declaration
44+
// Check within each function declaration if there are hook declarations
45+
ast.forEach(functionDec => {
46+
const { body } = functionDec.body;
47+
// Traverse through the function's funcDecs and Expression Statements
48+
findHookDeclarations(body);
49+
// Iterate array and determine getter/setters based on pattern
50+
saveAstHooks(statements); // add key-value to hookState
51+
});
52+
}
3653
}
3754
return hookState;
3855
};

package/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ const mode = {
99
const linkFiber = require('./linkFiber')(snapShot, mode);
1010
const timeJump = require('./timeJump')(snapShot, mode);
1111

12-
window.addEventListener('message', ({ data: { action, payload } }) => { //runs automatically twice per second with inspectedElement
12+
window.addEventListener('message', ({ data: { action, payload } }) => {
13+
//runs automatically twice per second with inspectedElement
1314
switch (action) {
1415
case 'jumpToSnap':
1516
timeJump(payload);
1617
// Get the pathname from payload and add new entry to browser history
1718
// MORE: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
1819
if (payload.children[0].state && payload.children[0].state.location) {
1920
const route = payload.children[0].state.location.pathname;
20-
window.history.pushState(route);
21+
window.history.pushState('', '', route);
2122
}
2223
break;
2324
case 'setLock':

package/linkFiber.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = (snap, mode) => {
3131
const oldSetState = component.setState.bind(component);
3232
// replace component's setState so developer doesn't change syntax
3333
// component.setState = newSetState.bind(component);
34-
component.setState = (state, callback = () => { }) => {
34+
component.setState = (state, callback = () => {}) => {
3535
// don't do anything if state is locked
3636
// UNLESS we are currently jumping through time
3737
if (mode.locked && !mode.jumping) return;
@@ -70,16 +70,13 @@ module.exports = (snap, mode) => {
7070
let index = 0;
7171
astHooks = Object.values(astHooks);
7272
// while memoizedState is truthy, save the value to the object
73-
while (memoizedState && memoizedState.queue) { // prevents useEffect from crashing on load
74-
if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
75-
changeUseState(memoizedState);
76-
}
77-
// memoized[astHooks[index]] = memoizedState.memoizedState;
73+
while (memoizedState && memoizedState.queue) {
74+
changeUseState(memoizedState);
75+
7876
memoized[astHooks[index]] = memoizedState.memoizedState;
7977
// Reassign memoizedState to its next value
8078
memoizedState = memoizedState.next;
81-
// Increment the index by 2
82-
index += 2;
79+
index += 2; // Increment the index by 2
8380
}
8481
return memoized;
8582
}
@@ -104,7 +101,10 @@ module.exports = (snap, mode) => {
104101
changeSetState(stateNode);
105102
}
106103
// Check if the component uses hooks
107-
if (memoizedState && Object.hasOwnProperty.call(memoizedState, 'baseState')) {
104+
if (
105+
memoizedState &&
106+
Object.hasOwnProperty.call(memoizedState, 'baseState')
107+
) {
108108
// Traverse through the currentFiber and extract the getters/setters
109109
astHooks = astParser(elementType);
110110
saveState(astHooks);

src/extension/background.js

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ const firstSnapshotReceived = {};
88
// there will be the same number of objects in here as there are reactime tabs open for each user application being worked on
99
const tabsObj = {};
1010

11-
console.log("background scripts");
11+
console.log('background scripts');
1212

1313
function createTabObj(title) {
14-
1514
// update tabsObj
1615
return {
1716
title,
@@ -62,14 +61,17 @@ function changeCurrLocation(tabObj, rootNode, index) {
6261
return;
6362
// if not, recurse on each one of the children
6463
}
65-
rootNode.children.forEach(child => {
66-
changeCurrLocation(tabObj, child, index);
67-
});
64+
// added if (rootNode.children) { <=======
65+
if (rootNode.children) {
66+
rootNode.children.forEach(child => {
67+
changeCurrLocation(tabObj, child, index);
68+
});
69+
}
6870
}
6971

70-
7172
// establishing connection with devtools
72-
chrome.runtime.onConnect.addListener(port => { // port is one end of the connection - an object
73+
chrome.runtime.onConnect.addListener(port => {
74+
// port is one end of the connection - an object
7375

7476
// push every port connected to the ports array
7577
portsArr.push(port);
@@ -92,8 +94,9 @@ chrome.runtime.onConnect.addListener(port => { // port is one end of the connect
9294
}
9395
});
9496

95-
// receive snapshot from devtools and send it to contentScript -
96-
port.onMessage.addListener(msg => { // msg is action denoting a time jump in devtools
97+
// receive snapshot from devtools and send it to contentScript -
98+
port.onMessage.addListener(msg => {
99+
// msg is action denoting a time jump in devtools
97100

98101
// ---------------------------------------------------------------
99102
// message incoming from devTools should look like this:
@@ -143,7 +146,11 @@ chrome.runtime.onMessage.addListener((request, sender) => {
143146
let isReactTimeTravel = false;
144147

145148
// Filter out tabs that don't have reactime
146-
if (action === 'tabReload' || action === 'recordSnap' || action === 'jumpToSnap') {
149+
if (
150+
action === 'tabReload' ||
151+
action === 'recordSnap' ||
152+
action === 'jumpToSnap'
153+
) {
147154
isReactTimeTravel = true;
148155
} else return;
149156

@@ -175,10 +182,12 @@ chrome.runtime.onMessage.addListener((request, sender) => {
175182
tabsObj[tabId].index = 1;
176183

177184
// send a message to devtools
178-
portsArr.forEach(bg => bg.postMessage({
179-
action: 'initialConnectSnapshots',
180-
payload: tabsObj,
181-
}));
185+
portsArr.forEach(bg =>
186+
bg.postMessage({
187+
action: 'initialConnectSnapshots',
188+
payload: tabsObj,
189+
})
190+
);
182191
}
183192
reloaded[tabId] = true;
184193
break;
@@ -191,12 +200,17 @@ chrome.runtime.onMessage.addListener((request, sender) => {
191200
reloaded[tabId] = false;
192201

193202
tabsObj[tabId].snapshots.push(request.payload);
194-
sendToHierarchy(tabsObj[tabId], new Node(request.payload, tabsObj[tabId]));
203+
sendToHierarchy(
204+
tabsObj[tabId],
205+
new Node(request.payload, tabsObj[tabId])
206+
);
195207
if (portsArr.length > 0) {
196-
portsArr.forEach(bg => bg.postMessage({
197-
action: 'initialConnectSnapshots',
198-
payload: tabsObj,
199-
}));
208+
portsArr.forEach(bg =>
209+
bg.postMessage({
210+
action: 'initialConnectSnapshots',
211+
payload: tabsObj,
212+
})
213+
);
200214
}
201215
break;
202216
}
@@ -207,15 +221,20 @@ chrome.runtime.onMessage.addListener((request, sender) => {
207221
} else {
208222
tabsObj[tabId].snapshots.push(request.payload);
209223
//! INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN!!!!
210-
sendToHierarchy(tabsObj[tabId], new Node(request.payload, tabsObj[tabId]));
224+
sendToHierarchy(
225+
tabsObj[tabId],
226+
new Node(request.payload, tabsObj[tabId])
227+
);
211228
}
212229
// send message to devtools
213230
if (portsArr.length > 0) {
214-
portsArr.forEach(bg => bg.postMessage({
215-
action: 'sendSnapshots',
216-
payload: tabsObj,
217-
sourceTab,
218-
}));
231+
portsArr.forEach(bg =>
232+
bg.postMessage({
233+
action: 'sendSnapshots',
234+
payload: tabsObj,
235+
sourceTab,
236+
})
237+
);
219238
}
220239
break;
221240
}
@@ -228,10 +247,12 @@ chrome.runtime.onMessage.addListener((request, sender) => {
228247
chrome.tabs.onRemoved.addListener(tabId => {
229248
// tell devtools which tab to delete
230249
if (portsArr.length > 0) {
231-
portsArr.forEach(bg => bg.postMessage({
232-
action: 'deleteTab',
233-
payload: tabId,
234-
}));
250+
portsArr.forEach(bg =>
251+
bg.postMessage({
252+
action: 'deleteTab',
253+
payload: tabId,
254+
})
255+
);
235256
}
236257

237258
// delete the tab from the tabsObj
@@ -253,7 +274,7 @@ chrome.runtime.onInstalled.addListener(() => {
253274
// when context menu is clicked, listen for the menuItemId,
254275
// if user clicked on reactime, open the devtools window
255276
chrome.contextMenus.onClicked.addListener(({ menuItemId }) => {
256-
console.log("clicked menu");
277+
console.log('clicked menu');
257278
const options = {
258279
type: 'panel',
259280
left: 0,

0 commit comments

Comments
 (0)