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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 1x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 228x 228x 248x 248x 414x 414x 410x 414x 248x 829x 829x 248x 1x 1x 248x 8x 8x 248x 414x 414x 414x 414x 414x 411x 414x 410x 410x 414x 22x 18x 22x 4x 4x 4x 22x 414x 248x 410x 410x 410x 410x 410x 410x 6x 3x 3x 3x 3x 3x 3x 3x 410x 14x 14x 8x 14x 14x 232x 211x 4x 4x 1x 4x 4x 211x 301x 1x 1x 1x 1x 1x 301x 206x 207x 410x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 1x 1x 1x 1x 1x 248x 2x 2x 2x 2x 2x 248x 1x 1x 122x 122x 122x 122x 122x 1x 196x 196x 196x 196x 196x 196x 196x 196x 196x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x | import React from 'react';
/**
* Configuration for dependency validation rules
*/
export interface DependencyValidationRule {
/** Function name to validate */
functionName: string;
/** Expected number of dependencies */
expectedCount?: number;
/** Expected specific dependencies (functions or values) */
expectedDeps?: React.DependencyList;
/** Custom validation function */
validator?: (deps: React.DependencyList, context?: unknown) => boolean;
/** Error message when validation fails */
errorMessage?: string;
/** Whether to validate in production */
validateInProduction?: boolean;
}
/**
* Configuration for the dependency validator
*/
export interface DependencyValidatorConfig {
/** Whether validation is enabled */
enabled?: boolean;
/** Environment override - if true, validates in all environments */
forceEnabled?: boolean;
/** Custom validation rules */
rules?: DependencyValidationRule[];
/** Default error message prefix */
errorPrefix?: string;
/** Whether to throw errors or just log warnings */
throwErrors?: boolean;
}
/**
* Class-based dependency validator that can be configured for different use cases
* Designed to be used in production by different classes with custom validation rules
*/
export class DependencyValidator {
private config: Required<DependencyValidatorConfig>;
private rules: Map<string, DependencyValidationRule> = new Map();
constructor(config: DependencyValidatorConfig = {}) {
this.config = {
enabled: true,
forceEnabled: false,
rules: [],
errorPrefix: 'Dependency validation failed',
throwErrors: true,
...config
};
// Register default rules
this.registerDefaultRules();
// Register custom rules
if (config.rules) {
config.rules.forEach(rule => this.registerRule(rule));
}
}
/**
* Check if validation should run
*/
private shouldValidate(): boolean {
if (!this.config.enabled) return false;
if (this.config.forceEnabled) return true;
return process.env.NODE_ENV !== 'production';
}
/**
* Register a custom validation rule
*/
registerRule(rule: DependencyValidationRule): void {
this.rules.set(rule.functionName, rule);
}
/**
* Remove a validation rule
*/
unregisterRule(functionName: string): void {
this.rules.delete(functionName);
}
/**
* Get a registered rule
*/
getRule(functionName: string): DependencyValidationRule | undefined {
return this.rules.get(functionName);
}
/**
* Validate dependencies for a specific function
*/
validate(
functionName: string,
deps: React.DependencyList,
context?: unknown
): boolean {
if (!this.shouldValidate()) return true;
const rule = this.rules.get(functionName);
if (!rule) return true; // No rule defined, skip validation
try {
return this.validateAgainstRule(rule, deps, context);
} catch (error) {
if (this.config.throwErrors) {
throw error;
} else {
console.warn(`${this.config.errorPrefix}:`, error.message);
return false;
}
}
}
/**
* Validate dependencies against a specific rule
*/
private validateAgainstRule(
rule: DependencyValidationRule,
deps: React.DependencyList,
context?: unknown
): boolean {
// Skip if no dependencies to validate
if (!deps || deps.length === 0) return true;
// Custom validator takes precedence
if (rule.validator) {
if (!rule.validator(deps, context)) {
throw new Error(
rule.errorMessage ||
`${this.config.errorPrefix}: Custom validation failed for ${rule.functionName}`
);
}
return true;
}
// Check expected count
if (rule.expectedCount !== undefined && deps.length !== rule.expectedCount) {
throw new Error(
rule.errorMessage ||
`${this.config.errorPrefix}: ${rule.functionName} expected ${rule.expectedCount} dependencies but got ${deps.length}`
);
}
// Check specific expected dependencies
if (rule.expectedDeps) {
if (deps.length !== rule.expectedDeps.length) {
throw new Error(
rule.errorMessage ||
`${this.config.errorPrefix}: ${rule.functionName} expected ${rule.expectedDeps.length} dependencies but got ${deps.length}`
);
}
for (let i = 0; i < deps.length; i++) {
if (deps[i] !== rule.expectedDeps[i]) {
throw new Error(
rule.errorMessage ||
`${this.config.errorPrefix}: ${rule.functionName} dependency at index ${i} doesn't match expected value`
);
}
}
}
return true;
}
/**
* Register default rules for backward compatibility
*/
private registerDefaultRules(): void {
// Rule for functions that should have no dependencies
this.registerRule({
functionName: 'updateParam',
expectedCount: 0,
errorMessage: `CRITICAL: useCallback for updateParam has dependencies that will cause infinite loops. The dependencies array must remain [] (empty) to prevent unnecessary re-renders.`,
validateInProduction: false
});
this.registerRule({
functionName: 'getParam',
expectedCount: 0,
errorMessage: `CRITICAL: useCallback for getParam has dependencies that will cause infinite loops. The dependencies array must remain [] (empty) to prevent unnecessary re-renders.`,
validateInProduction: false
});
}
/**
* Create a validator instance for a specific class/context
*/
static forClass(className: string, config?: DependencyValidatorConfig): DependencyValidator {
return new DependencyValidator({
errorPrefix: `Dependency validation failed in ${className}`,
...config
});
}
/**
* Create a validator that can run in production
*/
static forProduction(config?: Omit<DependencyValidatorConfig, 'forceEnabled'>): DependencyValidator {
return new DependencyValidator({
forceEnabled: true,
...config
});
}
}
/**
* Utility function to create common validation rules
*/
export const ValidationRules = {
/**
* Rule for functions that should have no dependencies
*/
noDependencies: (functionName: string): DependencyValidationRule => ({
functionName,
expectedCount: 0,
errorMessage: `CRITICAL: useCallback for ${functionName} has dependencies that will cause infinite loops. The dependencies array must remain [] (empty) to prevent unnecessary re-renders.`,
validateInProduction: false
}),
/**
* Rule for functions with specific dependencies
*/
specificDependencies: (
functionName: string,
expectedDeps: React.DependencyList,
errorMessage?: string
): DependencyValidationRule => ({
functionName,
expectedDeps,
errorMessage: errorMessage || `CRITICAL: useCallback for ${functionName} must have exactly the expected dependencies to prevent infinite loops.`,
validateInProduction: false
}),
/**
* Custom validation rule
*/
custom: (
functionName: string,
validator: (deps: React.DependencyList, context?: unknown) => boolean,
errorMessage?: string
): DependencyValidationRule => ({
functionName,
validator,
errorMessage,
validateInProduction: false
})
};
|