All files SyntaxKindDelegator.types.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x                                                                           1x
import { Node, SyntaxKind as SK } from "ts-morph";
import { Nodely } from "./types";
import { SyntaxKindTypeMap } from "./SyntaxKindMap";
 
/**
 * Each syntax kind we are utilizing has a validation method provided by ts-morph. luckily they all appear to utilize the same syntax
 * These validator along with checking for potential parse errors will coerce the generic Node type to a more accurate type.
 */
export type SyntaxKindValidator<T extends Node> = (node: Nodely) => node is T;
 
/**
 * Just a signature that allows a typed but open ended callback.
 */
export type SyntaxKindDelegateDefaultAction<T> = (node: Nodely)=>T;
 
/**
 * Every syntax kind can be provided this action which automatically types it. additionally if tiered support is necessary and you might encounter a null you can call the fallback callback.
 */
export type SyntaxKindDelegateAction<T extends Node, R> = (node: T, defaultFN: SyntaxKindDelegateDefaultAction<R>) => R
/**
 * For the delegator method to work it needs delegates which will know how to consume the generic Node and utilize it as a specialized node based on its syntax Kind. 
 * 
 */
export interface SyntaxKindDelegate<T extends Node> {
	validate: SyntaxKindValidator<T>
}
 
export type TypeByKind<T extends keyof SyntaxKindTypeMap> = SyntaxKindTypeMap[T]
export type SyntaxKindValidatorMap = {
	[k in keyof SyntaxKindTypeMap]: SyntaxKindValidator<SyntaxKindTypeMap[k]>
}
 
export type SyntaxKindMap<T> = {
	[k in keyof SyntaxKindTypeMap]: SyntaxKindDelegateAction<SyntaxKindTypeMap[k], T>
}
 
export type SKindMap<T> = Partial<SyntaxKindMap<T>>
 
export default SK; //this will make it easier to alias in other files later