Skip to content
21 changes: 20 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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++) {
Expand Down
32 changes: 30 additions & 2 deletions src/test/suite/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/test/test_cases/tc_6/component/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react";
import anotherApp from "./anotherApp"; // this is purposefully the wrong file path for anotherApp

const App = () => {
return (
<div>
<p>Hello from App.jsx</p>
<anotherApp />
</div>
)
};

export default App;
9 changes: 9 additions & 0 deletions src/test/test_cases/tc_6/index.js
Original file line number Diff line number Diff line change
@@ -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(<App />);
9 changes: 9 additions & 0 deletions src/test/test_cases/tc_6/otherComponent/anotherApp.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

export const anotherApp = () => {
return (
<div>
<p>Greetings from inside anotherApp</p>
</div>
)
}
13 changes: 13 additions & 0 deletions src/test/test_cases/tc_7/components/App.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<p>Syntax Error</p>
<ChildApp />
</div>
)

}
11 changes: 11 additions & 0 deletions src/test/test_cases/tc_7/components/ChildApp.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<p>Child of App with Syntax Error</p>
</div>
)

}
8 changes: 8 additions & 0 deletions src/test/test_cases/tc_7/index.js
Original file line number Diff line number Diff line change
@@ -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(<App />);