Skip to content

Commit e3080a1

Browse files
authored
Merge pull request #12 from oslabs-beta/check-is-client
updated: check-is-client
2 parents 7d132fc + ef4035d commit e3080a1

File tree

3 files changed

+91
-16
lines changed

3 files changed

+91
-16
lines changed

src/panel.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ function createPanel(context) {
4848
// null,
4949
// vscode.Disposable
5050
// );
51-
52-
5351
}
5452

5553
// getNonce generates a new random string each time ext is used to prevent external injection of foreign code into the html

src/parser.js

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,103 @@ class Parser {
289289
// input: ast.program.body
290290
// output: boolean
291291
getCallee(body) {
292-
// does useStore count as a client component functionality?
293-
const hooksArray = ['useState', 'useContext', 'useRef', 'useImperativeHandle', 'useNavigate', 'useLayoutEffect', 'useInsertionEffect', 'useMemo', 'useCallback', 'useTransition', 'useDeferredValue', 'useEffect', 'useReducer', 'useDispatch', 'useActions', 'useSelector', 'bindActionCreators'];
292+
const defaultErr = (err,) => {
293+
return {
294+
method: 'Error in getCallee method of Parser:',
295+
log: err,
296+
}
297+
};
294298

295-
//! console.log('ast.program.body', body);
299+
// console.log('ast.program.body', body);
296300
const bodyCallee = body.filter((item) => item.type === 'VariableDeclaration');
297-
const calleeArr = bodyCallee[0].declarations[0].init.body.body // gives us an array of callee nodes
298-
299-
console.log('calleArr:', calleeArr);
300-
for (let i = 0; i < calleeArr.length; i++) {
301-
if (calleeArr[i].type === 'VariableDeclaration') {
302-
if (hooksArray.includes(calleeArr[i].declarations[0].init.callee.name) || calleeArr[i].declarations[0].init.callee.name.startsWith('use')) {
303-
return true;
301+
if (bodyCallee.length === 0) return false;
302+
// console.log('bodyCallee', bodyCallee);
303+
// console.log('bodyCallee.length', bodyCallee.length)
304+
305+
const calleeHelper = (item) => {
306+
const hooksObj = {
307+
useState: 0,
308+
useContext: 0,
309+
useRef: 0,
310+
useImperativeHandle: 0,
311+
useNavigate: 0,
312+
useLocation: 0,
313+
useLayoutEffect: 0,
314+
useInsertionEffect: 0,
315+
useMemo: 0,
316+
useCallback: 0,
317+
useTransition: 0,
318+
useDeferredValue: 0,
319+
useEffect: 0,
320+
useReducer: 0,
321+
useDispatch: 0,
322+
useActions: 0,
323+
useSelector: 0,
324+
bindActionCreators: 0,
325+
}
326+
if (item.type === 'VariableDeclaration') {
327+
try {
328+
let calleeName = item.declarations[0]?.init?.callee?.name;
329+
if (hooksObj.hasOwnProperty(calleeName) || (typeof calleeName === 'string' && calleeName.startsWith('use'))) {
330+
return true;
331+
}
332+
}
333+
catch (err) {
334+
const error = defaultErr(err);
335+
console.error(error.method, '\n', error.log);
304336
}
305337
}
306-
if (calleeArr[i].type === 'ExpressionStatement') {
307-
if (hooksArray.includes(calleeArr[i].expression.callee.name) || calleeArr[i].expression.callee.name.startsWith('use')) {
308-
return true;
338+
else if (item.type === 'ExpressionStatement') {
339+
try {
340+
const calleeName = item.expression?.callee?.name;
341+
if (calleeName === undefined) return false;
342+
if (hooksObj.hasOwnProperty(calleeName) || (typeof calleeName === 'string' && calleeName.startsWith('use'))) {
343+
return true;
344+
}
345+
}
346+
catch (err) {
347+
const error = defaultErr(err);
348+
console.error(error.method, '\n', error.log);
309349
}
310350
}
351+
return false;
352+
}
353+
354+
if (bodyCallee.length === 1) {
355+
const calleeArr = bodyCallee[0].declarations[0]?.init?.body?.body;
356+
if (calleeArr === undefined) return false;
357+
358+
// console.log('calleArr:', calleeArr);
359+
let checkTrue = false;
360+
for (let i = 0; i < calleeArr.length; i++) {
361+
if (checkTrue) return true;
362+
checkTrue = calleeHelper(calleeArr[i]);
363+
}
364+
return checkTrue;
365+
}
366+
else if (bodyCallee.length > 1) {
367+
let calleeArr;
368+
for (let i = 0; i < bodyCallee.length; i++) {
369+
try {
370+
if (bodyCallee[i].declarations[0]?.init?.body?.body) {
371+
calleeArr = bodyCallee[i].declarations[0].init.body.body;
372+
console.log('calleeArr from body', calleeArr);
373+
}
374+
}
375+
catch (err) {
376+
const error = defaultErr(err);
377+
console.error(error.method, '\n', error.log);
378+
}
379+
}
380+
381+
if (calleeArr === undefined) return false;
382+
let checkTrue = false;
383+
for (let i = 0; i < calleeArr.length; i++) {
384+
if (checkTrue) return true;
385+
checkTrue = calleeHelper(calleeArr[i]);
386+
}
387+
return checkTrue;
311388
}
312-
return false;
313389
}
314390

315391
// Finds JSX React Components in current file

src/webview/Flow.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const OverviewFlow = () => {
2626
);
2727

2828
useEffect(() => {
29+
// does not work currently
2930
window.addEventListener('message', (e) => {
3031
const msg = e.data;
3132
switch (msg.type) {

0 commit comments

Comments
 (0)