diff --git a/src/parser.ts b/src/parser.ts index 197dc22..f3c0865 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -57,9 +57,28 @@ export class Parser { }; this.tree = root; this.parser(root); + // clean up nodes with error: 'File not found' + this.removeTreesWithError(this.tree); return this.tree; } + private removeTreesWithError(tree: Tree): void { + // base case + if(tree.children.length === 0) return; + // iterate over tree.children array to check for error. + for(let i = 0; i < tree.children.length; i++){ + // call removeTreesWithError on every tree in the children array + if(tree.children[i].children.length !== 0){ + this.removeTreesWithError(tree.children[i]); + } + if(tree.children[i].error && (tree.children[i].error === 'File not found' || tree.children[i].error === 'Error while processing this file/node')){ + // when an error is found, splice the tree out of the children array + tree.children.splice(i,1); + i--; // decrement to account for change in children array length + } + } + }; + public getTree(): Tree { return this.tree; } @@ -278,7 +297,7 @@ export class Parser { } }; - console.log('directive: ', directive); + // console.log('directive: ', directive); // Initial check for use of directives (ex: 'use client', 'use server', 'use strict') // Accounts for more than one directive for (let i = 0; i < directive.length; i++) { diff --git a/src/test/suite/parser.test.ts b/src/test/suite/parser.test.ts index 81fee91..23d6181 100644 --- a/src/test/suite/parser.test.ts +++ b/src/test/suite/parser.test.ts @@ -9,9 +9,11 @@ import * as vscode from 'vscode' describe('Parser Test Suite', () => { let parser, tree, file; + const fs = require('fs'); + // UNPARSED TREE TEST - describe('It initializes correctly', () => { + xdescribe('It initializes correctly', () => { beforeEach(() => { // Assign the test file and make new instance of Parser file = path.join(__dirname, '../test_cases/tc_0/index.js'); @@ -30,7 +32,7 @@ describe('Parser Test Suite', () => { }); // TEST 0: ONE CHILD - describe('It works for simple apps', () => { + xdescribe('It works for simple apps', () => { beforeEach(() => { file = path.join(__dirname, ''); parser = new Parser(file); @@ -47,6 +49,32 @@ describe('Parser Test Suite', () => { // }); }); + // TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT + describe('Catches bad imports', () => { + beforeEach(() => { + file = path.join(__dirname, '../../../../src/test/test_cases/tc_6/component/App.jsx'); + parser = new Parser(file); + tree = parser.parse(); + }); + + test("Child component with bad file path does not show up on the node tree", () => { + expect(tree.children.length).toBe(0); + }) + }) + + // TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR + describe('Parser should not work for components with syntax errors in the code', () => { + beforeEach(() => { + file = path.join(__dirname, '../../../../src/test/test_cases/tc_7/index.js'); + parser = new Parser(file); + tree = parser.parse(); + }); + + test("Parser stops parsing when there is a syntax error in a component", () => { + expect(tree.children.length).toBe(0); + }); + }) + // these are the 14 tests we need to test for // TEST 1: NESTED CHILDREN diff --git a/src/test/test_cases/tc_6/component/App.jsx b/src/test/test_cases/tc_6/component/App.jsx new file mode 100644 index 0000000..0f779ba --- /dev/null +++ b/src/test/test_cases/tc_6/component/App.jsx @@ -0,0 +1,13 @@ +import React from "react"; +import anotherApp from "./anotherApp"; // this is purposefully the wrong file path for anotherApp + +const App = () => { + return ( +
Hello from App.jsx
+Greetings from inside anotherApp
+Syntax Error
+Child of App with Syntax Error
+