@@ -57,9 +57,28 @@ export class Parser {
5757 } ;
5858 this . tree = root ;
5959 this . parser ( root ) ;
60+ // clean up nodes with error: 'File not found'
61+ this . removeTreesWithError ( this . tree ) ;
6062 return this . tree ;
6163 }
6264
65+ private removeTreesWithError ( tree : Tree ) : void {
66+ // base case
67+ if ( tree . children . length === 0 ) return ;
68+ // iterate over tree.children array to check for error.
69+ for ( let i = 0 ; i < tree . children . length ; i ++ ) {
70+ // call removeTreesWithError on every tree in the children array
71+ if ( tree . children [ i ] . children . length !== 0 ) {
72+ this . removeTreesWithError ( tree . children [ i ] ) ;
73+ }
74+ if ( tree . children [ i ] . error && ( tree . children [ i ] . error === 'File not found' || tree . children [ i ] . error === 'Error while processing this file/node' ) ) {
75+ // when an error is found, splice the tree out of the children array
76+ tree . children . splice ( i , 1 ) ;
77+ i -- ; // decrement to account for change in children array length
78+ }
79+ }
80+ } ;
81+
6382 public getTree ( ) : Tree {
6483 return this . tree ! ;
6584 }
@@ -159,9 +178,6 @@ export class Parser {
159178 if ( componentTree . parentList . includes ( componentTree . filePath ) ) {
160179 return ;
161180 }
162- // if (typeof componentTree.parentList === 'string' && componentTree.parentList.includes(componentTree.filePath)) {
163- // return;
164- // }
165181
166182 // Create abstract syntax tree of current component tree file
167183 let ast : babel . ParseResult < File > ;
@@ -290,18 +306,14 @@ export class Parser {
290306 }
291307
292308 // Second check for use of React/Redux hooks
293- // console.log('body:', body);
294309 // Checks for components declared using 'const'
295310 const bodyCallee = body . filter ( ( item ) => item . type === 'VariableDeclaration' ) ;
296- // console.log('bodyCall: ', bodyCallee);
297311
298312 // Checks for components declared using 'export default function'
299313 const exportCallee = body . filter ( ( item ) => item . type === 'ExportDefaultDeclaration' ) ;
300- // console.log('exprt: ', exportCallee);
301314
302315 // Checks for components declared using 'function'
303316 const functionCallee = body . filter ( ( item ) => item . type === 'FunctionDeclaration' ) ;
304- // console.log('func: ', functionCallee);
305317
306318 // Helper function
307319 const calleeHelper = ( item ) => {
@@ -355,114 +367,6 @@ export class Parser {
355367 return false ;
356368 }
357369
358- // 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- // }
382-
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;
390- // }
391-
392-
393- // Calling helper function for exportCallee array with length of 1 or more
394- // if (exportCallee.length === 1) {
395- // const calleeArr = exportCallee[0].declaration.body?.body;
396- // if (calleeArr === undefined) return false;
397-
398- // let checkTrue = false;
399- // for (let i = 0; i < calleeArr.length; i++) {
400- // if (checkTrue) return true;
401- // checkTrue = calleeHelper(calleeArr[i]);
402- // }
403- // return checkTrue;
404- // } else if (exportCallee.length > 1) {
405- // let calleeArr: [] = [];
406- // for (let i = 0; i < exportCallee.length; i++) {
407- // try {
408- // if (exportCallee[i].declarations[0]?.init?.body?.body) {
409- // calleeArr = exportCallee[i].declarations[0].init.body.body;
410- // }
411- // }
412- // catch (err) {
413- // const error = defaultErr(err);
414- // console.error(error.method, '\n', error.log);
415- // }
416- // }
417-
418- // if (calleeArr === undefined) return false;
419- // let checkTrue = false;
420- // for (let i = 0; i < calleeArr.length; i++) {
421- // if (checkTrue) return true;
422- // checkTrue = calleeHelper(calleeArr[i]);
423- // }
424- // return checkTrue;
425- // }
426-
427- // console.log('hello');
428- // // Calling helper function for bodyCallee array with length of 1 or more
429- // if (bodyCallee.length === 1) {
430- // console.log('body in length: ', bodyCallee);
431- // const calleeArr = bodyCallee[0].declarations[0]?.init?.body?.body;
432- // console.log('calle: ', calleeArr);
433- // if (calleeArr === undefined) return false;
434-
435- // let checkTrue = false;
436- // for (let i = 0; i < calleeArr.length; i++) {
437- // if (checkTrue) return true;
438- // console.log('i:', calleeArr[i])
439- // checkTrue = calleeHelper(calleeArr[i]);
440- // }
441- // return checkTrue;
442- // } else if (bodyCallee.length > 1) {
443- // let calleeArr: [] = [];
444- // for (let i = 0; i < bodyCallee.length; i++) {
445- // try {
446- // if (bodyCallee[i].declarations[0]?.init?.body?.body) {
447- // calleeArr = bodyCallee[i].declarations[0].init.body.body;
448- // }
449- // }
450- // catch (err) {
451- // const error = defaultErr(err);
452- // console.error(error.method, '\n', error.log);
453- // }
454- // }
455-
456- // if (calleeArr === undefined) return false;
457- // let checkTrue = false;
458- // for (let i = 0; i < calleeArr.length; i++) {
459- // if (checkTrue) return true;
460- // checkTrue = calleeHelper(calleeArr[i]);
461- // }
462- // return checkTrue;
463- // }
464- // if (!bodyCallee && !exportCallee && !functionCallee) return false;
465-
466370 // Process Function Declarations
467371 for ( const func of functionCallee ) {
468372 const calleeArr = func . body ?. body ;
0 commit comments