Skip to content

Commit fb4d211

Browse files
authored
Merge pull request #38 from oslabs-beta/LK/fileNotFoundBug
Lk/file not found bug
2 parents 9e28334 + ab1073e commit fb4d211

File tree

8 files changed

+113
-3
lines changed

8 files changed

+113
-3
lines changed

src/parser.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -278,7 +297,7 @@ export class Parser {
278297
}
279298
};
280299

281-
console.log('directive: ', directive);
300+
// console.log('directive: ', directive);
282301
// Initial check for use of directives (ex: 'use client', 'use server', 'use strict')
283302
// Accounts for more than one directive
284303
for (let i = 0; i < directive.length; i++) {

src/test/suite/parser.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import * as vscode from 'vscode'
99

1010
describe('Parser Test Suite', () => {
1111
let parser, tree, file;
12+
const fs = require('fs');
13+
1214

1315
// UNPARSED TREE TEST
14-
describe('It initializes correctly', () => {
16+
xdescribe('It initializes correctly', () => {
1517
beforeEach(() => {
1618
// Assign the test file and make new instance of Parser
1719
file = path.join(__dirname, '../test_cases/tc_0/index.js');
@@ -30,7 +32,7 @@ describe('Parser Test Suite', () => {
3032
});
3133

3234
// TEST 0: ONE CHILD
33-
describe('It works for simple apps', () => {
35+
xdescribe('It works for simple apps', () => {
3436
beforeEach(() => {
3537
file = path.join(__dirname, '');
3638
parser = new Parser(file);
@@ -47,6 +49,32 @@ describe('Parser Test Suite', () => {
4749
// });
4850
});
4951

52+
// TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
53+
describe('Catches bad imports', () => {
54+
beforeEach(() => {
55+
file = path.join(__dirname, '../../../../src/test/test_cases/tc_6/component/App.jsx');
56+
parser = new Parser(file);
57+
tree = parser.parse();
58+
});
59+
60+
test("Child component with bad file path does not show up on the node tree", () => {
61+
expect(tree.children.length).toBe(0);
62+
})
63+
})
64+
65+
// TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR
66+
describe('Parser should not work for components with syntax errors in the code', () => {
67+
beforeEach(() => {
68+
file = path.join(__dirname, '../../../../src/test/test_cases/tc_7/index.js');
69+
parser = new Parser(file);
70+
tree = parser.parse();
71+
});
72+
73+
test("Parser stops parsing when there is a syntax error in a component", () => {
74+
expect(tree.children.length).toBe(0);
75+
});
76+
})
77+
5078
// these are the 14 tests we need to test for
5179

5280
// TEST 1: NESTED CHILDREN
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
import anotherApp from "./anotherApp"; // this is purposefully the wrong file path for anotherApp
3+
4+
const App = () => {
5+
return (
6+
<div>
7+
<p>Hello from App.jsx</p>
8+
<anotherApp />
9+
</div>
10+
)
11+
};
12+
13+
export default App;

src/test/test_cases/tc_6/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// !TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
2+
import React from 'react';
3+
import { createRoot } from 'react-dom/client';
4+
import App from './components/App.jsx';
5+
6+
// tests whether the parser still works when a component is given the wrong File path
7+
8+
const root = createRoot(document.getElementById('root'));
9+
root.render(<App />);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
3+
export const anotherApp = () => {
4+
return (
5+
<div>
6+
<p>Greetings from inside anotherApp</p>
7+
</div>
8+
)
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { Component } from 'react';
2+
import ChildApp from './ChildApp';
3+
4+
export const App = () => {
5+
this should not work when given to the parser
6+
return (
7+
<div>
8+
<p>Syntax Error</p>
9+
<ChildApp />
10+
</div>
11+
)
12+
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// this component will not show up in the children of App due to App's syntax error
2+
import React, { Component } from 'react';
3+
4+
export const ChildApp = () => {
5+
return (
6+
<div>
7+
<p>Child of App with Syntax Error</p>
8+
</div>
9+
)
10+
11+
}

src/test/test_cases/tc_7/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR
2+
3+
import React from 'react';
4+
import { createRoot } from 'react-dom/client';
5+
import App from './components/App.jsx';
6+
7+
const root = createRoot(document.getElementById('root'));
8+
root.render(<App />);

0 commit comments

Comments
 (0)