/** * Base Validator Abstract Class * * Abstract base class that all validators extend. Provides common utility methods * and enforces the IValidator interface contract. * * @module validators/base-validator */ import { IValidator, RuleCategory, RuleSeverity, RuleValidationContext, RuleValidationResult } from "../types.js"; /** * Abstract base class for all validators. * Subclasses must implement the abstract properties and validate method. * * @example * ```typescript * export class MyValidator extends BaseValidator { * readonly id = 'my-validator'; * readonly ruleId = 'my-rule'; * readonly category = 'code-quality'; * readonly severity = 'error'; * * async validate(context: RuleValidationContext): Promise { * // Implementation here * } * } * ``` */ export declare abstract class BaseValidator implements IValidator { /** Unique identifier for this validator instance */ abstract readonly id: string; /** The rule ID this validator validates */ abstract readonly ruleId: string; /** Category for organizing validators */ abstract readonly category: RuleCategory; /** Severity level of violations */ abstract readonly severity: RuleSeverity; /** * Perform validation on the given context. * Must be implemented by subclasses. * * @param context - The validation context containing code and operation info * @returns Promise resolving to validation result */ abstract validate(context: RuleValidationContext): Promise; /** * Extract function body for validation analysis. * Searches for a function by name and extracts its body. * * @param code - The source code to search * @param functionName - Name of the function to extract * @returns The function body or null if not found */ protected extractFunctionBody(code: string, functionName: string): string | null; /** * Calculate maximum nesting depth in code. * Counts opening and closing braces/brackets to determine depth. * * @param code - The source code to analyze * @returns Maximum nesting depth (0 for flat code) */ protected calculateMaxNesting(code: string): number; /** * Check if code contains a specific pattern. * * @param code - The source code to check * @param pattern - RegExp pattern to search for * @returns True if pattern found, false otherwise */ protected hasPattern(code: string, pattern: RegExp): boolean; /** * Create a successful validation result. * * @param message - Success message * @returns RuleValidationResult with passed: true */ protected createSuccessResult(message: string): RuleValidationResult; /** * Create a failed validation result. * * @param message - Failure message * @param suggestions - Optional list of suggestions * @param fixes - Optional list of automated fixes * @param severity - Optional severity level (defaults to this.severity) * @returns RuleValidationResult with passed: false */ protected createFailureResult(message: string, suggestions?: string[], fixes?: RuleValidationResult["fixes"], severity?: RuleSeverity): RuleValidationResult; }