@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
109 lines • 3.81 kB
TypeScript
/**
* Create a type checker instance
*/
export declare function createTypeChecker(options?: TypeCheckerOptions): TemplateTypeChecker;
/**
* Check a template file
*/
export declare function checkTemplateFile(filePath: string, options?: TypeCheckerOptions): TypeCheckResult;
/**
* Check a template string
*/
export declare function checkTemplate(template: string, options?: TypeCheckerOptions): TypeCheckResult;
/**
* Format type check errors for display
*/
export declare function formatTypeErrors(result: TypeCheckResult): string;
/**
* Type definition for a variable or expression
*/
export declare interface TypeDefinition {
kind: 'primitive' | 'object' | 'array' | 'function' | 'union' | 'intersection' | 'literal'
name?: string
value?: PrimitiveType | string
properties?: Record<string, TypeDefinition>
elementType?: TypeDefinition
params?: Array<{ name: string, type: TypeDefinition, optional?: boolean }>
returnType?: TypeDefinition
types?: TypeDefinition[]
}
/**
* Type error reported by the checker
*/
export declare interface TypeCheckError {
message: string
file: string
line: number
column: number
expression: string
expected?: string
received?: string
severity: 'error' | 'warning'
}
/**
* Type check result
*/
export declare interface TypeCheckResult {
valid: boolean
errors: TypeCheckError[]
warnings: TypeCheckError[]
variables: Map<string, TypeDefinition>
}
/**
* Options for the type checker
*/
export declare interface TypeCheckerOptions {
strict?: boolean
allowAny?: boolean
checkPropertyAccess?: boolean
checkFunctionCalls?: boolean
includeBuiltins?: boolean
customTypes?: Record<string, TypeDefinition>
baseDir?: string
}
/**
* Primitive types supported by the type checker
*/
export type PrimitiveType = | 'string'
| 'number'
| 'boolean'
| 'null'
| 'undefined'
| 'any'
| 'unknown'
| 'void'
| 'never'
/**
* Template Type Checker class
*/
export declare class TemplateTypeChecker {
private options: TypeCheckerOptions;
private variables: Map<string, TypeDefinition>;
private errors: TypeCheckError[];
private warnings: TypeCheckError[];
private currentFile: string;
constructor(options?: TypeCheckerOptions);
checkFile(filePath: string): TypeCheckResult;
checkTemplate(template: string, filePath?: string): TypeCheckResult;
private analyzeScript(script: string, scriptOffset: number): void;
private extractTypedVariables(line: string, lineNum: number): void;
private extractInterfaces(line: string, lines: string[], index: number, lineNum: number): void;
private extractTypeAliases(line: string, lineNum: number): void;
private parseTypeAnnotation(annotation: string): TypeDefinition;
private inferType(value: string): TypeDefinition;
private parseParams(paramsStr: string): Array<{ name: string, type: TypeDefinition, optional?: boolean }>;
private checkExpressions(template: string): void;
private checkExpression(expression: string, line: number, column: number): void;
private checkDirectives(template: string): void;
private resolvePropertyAccess(expr: string): TypeDefinition;
private isLiteral(expr: string): boolean;
private extractObjectLiteral(expr: string): Record<string, string>;
private splitByComma(str: string): string[];
private getLineNumber(position: number, template?: string): number;
private getColumn(position: number, template: string): number;
private addError(message: string, line: number, column: number, expression: string, expected?: string, received?: string): void;
private addWarning(message: string, line: number, column: number, expression: string, expected?: string, received?: string): void;
private getResult(): TypeCheckResult;
addVariable(name: string, type: TypeDefinition): void;
reset(): void;
}