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

+ +
+ ) +}; + +export default App; \ No newline at end of file diff --git a/src/test/test_cases/tc_6/index.js b/src/test/test_cases/tc_6/index.js new file mode 100644 index 0000000..acfef2a --- /dev/null +++ b/src/test/test_cases/tc_6/index.js @@ -0,0 +1,9 @@ +// !TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT +import React from 'react'; +import { createRoot } from 'react-dom/client'; +import App from './components/App.jsx'; + +// tests whether the parser still works when a component is given the wrong File path + +const root = createRoot(document.getElementById('root')); +root.render(); \ No newline at end of file diff --git a/src/test/test_cases/tc_6/otherComponent/anotherApp.jsx b/src/test/test_cases/tc_6/otherComponent/anotherApp.jsx new file mode 100644 index 0000000..3d4d13d --- /dev/null +++ b/src/test/test_cases/tc_6/otherComponent/anotherApp.jsx @@ -0,0 +1,9 @@ +import React from "react"; + +export const anotherApp = () => { + return ( +
+

Greetings from inside anotherApp

+
+ ) +} \ No newline at end of file diff --git a/src/test/test_cases/tc_7/components/App.jsx b/src/test/test_cases/tc_7/components/App.jsx new file mode 100644 index 0000000..d3bfba1 --- /dev/null +++ b/src/test/test_cases/tc_7/components/App.jsx @@ -0,0 +1,13 @@ +import React, { Component } from 'react'; +import ChildApp from './ChildApp'; + +export const App = () => { + this should not work when given to the parser + return ( +
+

Syntax Error

+ +
+ ) + +} \ No newline at end of file diff --git a/src/test/test_cases/tc_7/components/ChildApp.jsx b/src/test/test_cases/tc_7/components/ChildApp.jsx new file mode 100644 index 0000000..402ffe7 --- /dev/null +++ b/src/test/test_cases/tc_7/components/ChildApp.jsx @@ -0,0 +1,11 @@ +// this component will not show up in the children of App due to App's syntax error +import React, { Component } from 'react'; + +export const ChildApp = () => { + return ( +
+

Child of App with Syntax Error

+
+ ) + +} \ No newline at end of file diff --git a/src/test/test_cases/tc_7/index.js b/src/test/test_cases/tc_7/index.js new file mode 100644 index 0000000..0293b7d --- /dev/null +++ b/src/test/test_cases/tc_7/index.js @@ -0,0 +1,8 @@ +//! TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR + +import React from 'react'; +import { createRoot } from 'react-dom/client'; +import App from './components/App.jsx'; + +const root = createRoot(document.getElementById('root')); +root.render(); \ No newline at end of file