Skip to content

Commit 92fa73c

Browse files
committed
changed logic in parser.ts to skip a conditional if no body array is found based on the type. all the tests in tc_14 are now passing.
1 parent b4c7088 commit 92fa73c

File tree

2 files changed

+137
-94
lines changed

2 files changed

+137
-94
lines changed

src/parser.ts

Lines changed: 137 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,18 @@ export class Parser {
291291

292292
// Second check for use of React/Redux hooks
293293

294+
console.log('body:', body);
294295
// Checks for components declared using 'const'
295296
const bodyCallee = body.filter((item) => item.type === 'VariableDeclaration');
296-
// console.log('body: ', bodyCallee);
297+
console.log('bodyCall: ', bodyCallee);
297298

298299
// Checks for components declared using 'export default function'
299300
const exportCallee = body.filter((item) => item.type === 'ExportDefaultDeclaration');
300-
// console.log('exprt: ', exportCallee);
301+
console.log('exprt: ', exportCallee);
301302

302303
// Checks for components declared using 'function'
303304
const functionCallee = body.filter((item) => item.type === 'FunctionDeclaration');
304-
// console.log('func: ', functionCallee);
305+
console.log('func: ', functionCallee);
305306

306307
// Helper function
307308
const calleeHelper = (item) => {
@@ -356,105 +357,150 @@ export class Parser {
356357
}
357358

358359
// Calling helper function for functionCallee array with length of 1 or more
359-
if (functionCallee.length === 1) {
360-
const calleeArr = functionCallee[0].body?.body;
361-
if (calleeArr === undefined) return false;
362-
363-
let checkTrue = false;
364-
for (let i = 0; i < calleeArr.length; i++) {
365-
if (checkTrue) return true;
366-
checkTrue = calleeHelper(calleeArr[i]);
367-
}
368-
return checkTrue;
369-
} else if (functionCallee.length > 1) {
370-
let calleeArr: [] = [];
371-
for (let i = 0; i < functionCallee.length; i++) {
372-
try {
373-
if (functionCallee[i].declarations[0]?.init?.body?.body) {
374-
calleeArr = functionCallee[i].declarations[0].init.body.body;
375-
}
376-
}
377-
catch (err) {
378-
const error = defaultErr(err);
379-
console.error(error.method, '\n', error.log);
380-
}
381-
}
360+
// if (functionCallee.length === 1) {
361+
// const calleeArr = functionCallee[0].body?.body;
362+
// if (calleeArr === undefined) return false;
363+
364+
// let checkTrue = false;
365+
// for (let i = 0; i < calleeArr.length; i++) {
366+
// if (checkTrue) return true;
367+
// checkTrue = calleeHelper(calleeArr[i]);
368+
// }
369+
// return checkTrue;
370+
// } else if (functionCallee.length > 1) {
371+
// let calleeArr: [] = [];
372+
// for (let i = 0; i < functionCallee.length; i++) {
373+
// try {
374+
// if (functionCallee[i].declarations[0]?.init?.body?.body) {
375+
// calleeArr = functionCallee[i].declarations[0].init.body.body;
376+
// }
377+
// }
378+
// catch (err) {
379+
// const error = defaultErr(err);
380+
// console.error(error.method, '\n', error.log);
381+
// }
382+
// }
383+
384+
// if (calleeArr === undefined) return false;
385+
// let checkTrue = false;
386+
// for (let i = 0; i < calleeArr.length; i++) {
387+
// if (checkTrue) return true;
388+
// checkTrue = calleeHelper(calleeArr[i]);
389+
// }
390+
// return checkTrue;
391+
// }
382392

383-
if (calleeArr === undefined) return false;
384-
let checkTrue = false;
385-
for (let i = 0; i < calleeArr.length; i++) {
386-
if (checkTrue) return true;
387-
checkTrue = calleeHelper(calleeArr[i]);
388-
}
389-
return checkTrue;
390393

391394
// Calling helper function for exportCallee array with length of 1 or more
392-
} else if (exportCallee.length === 1) {
393-
const calleeArr = exportCallee[0].declaration.body?.body;
394-
if (calleeArr === undefined) return false;
395-
396-
let checkTrue = false;
397-
for (let i = 0; i < calleeArr.length; i++) {
398-
if (checkTrue) return true;
399-
checkTrue = calleeHelper(calleeArr[i]);
400-
}
401-
return checkTrue;
402-
} else if (exportCallee.length > 1) {
403-
let calleeArr: [] = [];
404-
for (let i = 0; i < exportCallee.length; i++) {
405-
try {
406-
if (exportCallee[i].declarations[0]?.init?.body?.body) {
407-
calleeArr = exportCallee[i].declarations[0].init.body.body;
408-
}
409-
}
410-
catch (err) {
411-
const error = defaultErr(err);
412-
console.error(error.method, '\n', error.log);
413-
}
414-
}
395+
// if (exportCallee.length === 1) {
396+
// const calleeArr = exportCallee[0].declaration.body?.body;
397+
// if (calleeArr === undefined) return false;
398+
399+
// let checkTrue = false;
400+
// for (let i = 0; i < calleeArr.length; i++) {
401+
// if (checkTrue) return true;
402+
// checkTrue = calleeHelper(calleeArr[i]);
403+
// }
404+
// return checkTrue;
405+
// } else if (exportCallee.length > 1) {
406+
// let calleeArr: [] = [];
407+
// for (let i = 0; i < exportCallee.length; i++) {
408+
// try {
409+
// if (exportCallee[i].declarations[0]?.init?.body?.body) {
410+
// calleeArr = exportCallee[i].declarations[0].init.body.body;
411+
// }
412+
// }
413+
// catch (err) {
414+
// const error = defaultErr(err);
415+
// console.error(error.method, '\n', error.log);
416+
// }
417+
// }
418+
419+
// if (calleeArr === undefined) return false;
420+
// let checkTrue = false;
421+
// for (let i = 0; i < calleeArr.length; i++) {
422+
// if (checkTrue) return true;
423+
// checkTrue = calleeHelper(calleeArr[i]);
424+
// }
425+
// return checkTrue;
426+
// }
415427

416-
if (calleeArr === undefined) return false;
417-
let checkTrue = false;
418-
for (let i = 0; i < calleeArr.length; i++) {
419-
if (checkTrue) return true;
420-
checkTrue = calleeHelper(calleeArr[i]);
421-
}
422-
return checkTrue;
428+
// console.log('hello');
429+
// // Calling helper function for bodyCallee array with length of 1 or more
430+
// if (bodyCallee.length === 1) {
431+
// console.log('body in length: ', bodyCallee);
432+
// const calleeArr = bodyCallee[0].declarations[0]?.init?.body?.body;
433+
// console.log('calle: ', calleeArr);
434+
// if (calleeArr === undefined) return false;
435+
436+
// let checkTrue = false;
437+
// for (let i = 0; i < calleeArr.length; i++) {
438+
// if (checkTrue) return true;
439+
// console.log('i:', calleeArr[i])
440+
// checkTrue = calleeHelper(calleeArr[i]);
441+
// }
442+
// return checkTrue;
443+
// } else if (bodyCallee.length > 1) {
444+
// let calleeArr: [] = [];
445+
// for (let i = 0; i < bodyCallee.length; i++) {
446+
// try {
447+
// if (bodyCallee[i].declarations[0]?.init?.body?.body) {
448+
// calleeArr = bodyCallee[i].declarations[0].init.body.body;
449+
// }
450+
// }
451+
// catch (err) {
452+
// const error = defaultErr(err);
453+
// console.error(error.method, '\n', error.log);
454+
// }
455+
// }
456+
457+
// if (calleeArr === undefined) return false;
458+
// let checkTrue = false;
459+
// for (let i = 0; i < calleeArr.length; i++) {
460+
// if (checkTrue) return true;
461+
// checkTrue = calleeHelper(calleeArr[i]);
462+
// }
463+
// return checkTrue;
464+
// }
465+
// if (!bodyCallee && !exportCallee && !functionCallee) return false;
423466

424-
// Calling helper function for bodyCallee array with length of 1 or more
425-
} else if (bodyCallee.length === 1) {
426-
const calleeArr = bodyCallee[0].declarations[0]?.init?.body?.body;
427-
if (calleeArr === undefined) return false;
467+
// Process Function Declarations
468+
for (const func of functionCallee) {
469+
const calleeArr = func.body?.body;
470+
if (!calleeArr) continue; // Skip if no body
428471

429-
let checkTrue = false;
430-
for (let i = 0; i < calleeArr.length; i++) {
431-
if (checkTrue) return true;
432-
checkTrue = calleeHelper(calleeArr[i]);
433-
}
434-
return checkTrue;
435-
} else if (bodyCallee.length > 1) {
436-
let calleeArr: [] = [];
437-
for (let i = 0; i < bodyCallee.length; i++) {
438-
try {
439-
if (bodyCallee[i].declarations[0]?.init?.body?.body) {
440-
calleeArr = bodyCallee[i].declarations[0].init.body.body;
441-
}
442-
}
443-
catch (err) {
444-
const error = defaultErr(err);
445-
console.error(error.method, '\n', error.log);
472+
for (const callee of calleeArr) {
473+
if (calleeHelper(callee)) {
474+
return true;
446475
}
447476
}
477+
}
448478

449-
if (calleeArr === undefined) return false;
450-
let checkTrue = false;
451-
for (let i = 0; i < calleeArr.length; i++) {
452-
if (checkTrue) return true;
453-
checkTrue = calleeHelper(calleeArr[i]);
479+
// Process Export Declarations
480+
for (const exportDecl of exportCallee) {
481+
const calleeArr = exportDecl.declaration.body?.body;
482+
if (!calleeArr) continue; // Skip if no body
483+
484+
for (const callee of calleeArr) {
485+
if (calleeHelper(callee)) {
486+
return true;
487+
}
488+
}
489+
}
490+
491+
// Process Body Declarations
492+
for (const bodyDecl of bodyCallee) {
493+
const calleeArr = bodyDecl.declarations[0]?.init?.body?.body;
494+
if (!calleeArr) continue; // Skip if no body
495+
496+
for (const callee of calleeArr) {
497+
if (calleeHelper(callee)) {
498+
return true;
499+
}
454500
}
455-
return checkTrue;
456501
}
457-
if (!bodyCallee && !exportCallee && !functionCallee) return false;
502+
503+
return false;
458504
}
459505

460506
// Finds JSX React Components in current file

src/test/suite/parser.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ describe('Parser Test Suite', () => {
9696
file = path.join(__dirname, '../../../../src/test/test_cases/tc_14/index.js');
9797
parser = new Parser(file);
9898
tree = parser.parse();
99-
// console.log('tree:', tree.children[0].children[0]);
10099
});
101100

102101
test('Root should be named index, it should have one children named App', () => {
@@ -106,8 +105,6 @@ describe('Parser Test Suite', () => {
106105
});
107106

108107
test('App should have three children, Component1 is a client component using hooks (variable declaration, export default declaration, and function declaration), Component2 is a client component using directives, and Component3 is not a client component', () => {
109-
// these first two doesnt work but the other tests in this test suite does
110-
// gives me false instead of true
111108
expect(tree.children[0].children[0]).toHaveProperty('name', 'Component1');
112109
expect(tree.children[0].children[0]).toHaveProperty('isClientComponent', true);
113110

0 commit comments

Comments
 (0)