Skip to content

Commit 7607313

Browse files
committed
fix: 🐛 handle undefined file paths in component parser
1 parent 5960ed4 commit 7607313

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/parser/component/component.parser.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ describe('ComponentParser', () => {
180180

181181
expect(parseComponent(ast, 'foo.component.ts').cva).toBeTruthy();
182182
});
183+
184+
it('should be able to parse a component with an empty template', () => {
185+
const ast = tsquery.ast(`
186+
@Component({
187+
selector: 'my-test-comp',
188+
template: '',
189+
styles: []
190+
})
191+
export class MyTestComponent implements ControlValueAccessor{}
192+
`);
193+
194+
jest.spyOn(fs, 'readFileSync').mockReturnValue('');
195+
196+
expect(parseComponent(ast, 'foo.component.ts').template).toBe('');
197+
});
183198
});

src/parser/component/component.parser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ function isCva(ast: ts.SourceFile): boolean {
4848
);
4949
}
5050

51-
function fetchFileContent(filePath: string, componentFilePath: string): string {
51+
function fetchFileContent(filePath: string | undefined, componentFilePath: string): string {
52+
if (!filePath) {
53+
return '';
54+
}
5255
const filePathFragements = componentFilePath.split('/');
5356
filePathFragements.pop();
5457
const componentDirectory = filePathFragements.join('/');

0 commit comments

Comments
 (0)