All files SyntaxKindDelegator.ts

92.85% Statements 13/14
75% Branches 3/4
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  1x                   1x 24x 20x 20x 4x   16x 16x 16x   12x  
import { SyntaxKindMap, SyntaxKindValidatorMap } from "./SyntaxKindDelegator.types";
import { SyntaxKindDelegator } from "./SyntaxKindMap";
import { Nodely } from "./types";
 
/**
 * Uses a syntax kind to delegate actions to allow for automatic type based on syntax kind.
 * @param node 
 * @param skMap 
 * @param defaultFN 
 * @returns 
 */
export const bySyntax = <T>(node: Nodely, skMap: Partial<SyntaxKindMap<T>>, defaultFN: (node: Nodely)=>T): T => {
	if(!node) return defaultFN(node);
	const k = node.getKind();
	if(!(k in SyntaxKindDelegator)) {
		return defaultFN(node);
	}
	Iif(!SyntaxKindDelegator[k as keyof SyntaxKindValidatorMap](node)) return defaultFN(node);
	const entry = skMap[k as keyof SyntaxKindMap<T>];
	if(!entry) return defaultFN(node);
	//@ts-ignore
	return entry(node, defaultFN);
}