11import { Graphviz } from "@hpcc-js/wasm-graphviz" ;
2- import type Parser from "web-tree-sitter" ;
2+ import type { Parser , Node as SyntaxNode , Tree } from "web-tree-sitter" ;
33import {
44 type Language ,
55 functionNodeTypes ,
@@ -23,10 +23,10 @@ export async function initializeParsers(): Promise<Parsers> {
2323}
2424
2525export function getFirstFunction (
26- tree : Parser . Tree ,
26+ tree : Tree ,
2727 language : Language ,
28- ) : Parser . SyntaxNode | null {
29- let functionNode : Parser . SyntaxNode | null = null ;
28+ ) : SyntaxNode | null {
29+ let functionNode : SyntaxNode | null = null ;
3030 const cursor = tree . walk ( ) ;
3131 const visitNode = ( ) => {
3232 if ( functionNodeTypes [ language ] . includes ( cursor . nodeType ) ) {
@@ -61,8 +61,11 @@ export interface TestResults {
6161
6262export function runTest ( record : TestFuncRecord ) : TestResults [ ] {
6363 const tree = parsers [ record . language ] . parse ( record . code ) ;
64+ if ( ! tree ) {
65+ throw new Error ( `Failed to parse code from ${ record } ` ) ;
66+ }
6467 const testFunc : TestFunction = {
65- function : getFirstFunction ( tree , record . language ) as Parser . SyntaxNode ,
68+ function : getFirstFunction ( tree , record . language ) as SyntaxNode ,
6669 language : record . language ,
6770 name : record . name ,
6871 reqs : record . reqs ,
@@ -84,10 +87,10 @@ export function processRecord(
8487 options : RenderOptions ,
8588) : { dot ?: string ; ast : string ; svg ?: string ; error ?: Error } {
8689 const tree = parsers [ record . language ] . parse ( record . code ) ;
87- const functionSyntax = getFirstFunction (
88- tree ,
89- record . language ,
90- ) as Parser . SyntaxNode ;
90+ if ( ! tree ) {
91+ throw new Error ( `Failed to parse code from ${ record } ` ) ;
92+ }
93+ const functionSyntax = getFirstFunction ( tree , record . language ) as SyntaxNode ;
9194
9295 const ast = functionSyntax . toString ( ) ;
9396
0 commit comments