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 | 10x 10x 10x 10x 2x 71x 8x 58x 58x 457x 457x 457x 457x 58x 8x 10x 10x 10x 10x 10x 3x 3x 3x 10x 4x 4x 28x 15x 15x 21x 21x 21x 15x 28x 13x 17x 17x 17x 17x 10x 28x 28x 28x 28x 28x 28x 8x 8x 3x 3x | /**
* @fds-uif/core - State Reference Validation
*
* Verifies that every `stateClasses` entry references an actual state in
* the UIF's `states` array. Wrong references are common typos (`'hovered'`
* vs `'hover'`), so this validator also produces "did you mean?"
* suggestions via Levenshtein distance, which the orchestrator surfaces in
* the diagnostic message.
*
* The Levenshtein helper is co-located here because state-reference is its
* only consumer; promoting it to `shared.ts` would mislead readers about
* its scope.
*/
import { UifInvalidStateReferenceError } from '../errors.js';
// ============================================================================
// Levenshtein "did you mean?" helpers
// ============================================================================
/**
* Calculate Levenshtein distance between two strings, used for "did you
* mean?" suggestions on misspelled state references. Optimized with early
* termination when distance exceeds the caller-provided threshold.
*/
function levenshteinDistance(a: string, b: string, maxDistance?: number): number {
Iif (a === b) return 0;
const aLen = a.length;
const bLen = b.length;
if (maxDistance !== undefined && Math.abs(aLen - bLen) > maxDistance) {
return maxDistance + 1;
}
// Single-row dynamic programming table for O(min(a, b)) memory.
const row: number[] = Array.from({ length: bLen + 1 }, (_, i) => i);
for (let i = 1; i <= aLen; i++) {
let prev = i;
for (let j = 1; j <= bLen; j++) {
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
const current = Math.min(
row[j] + 1, // deletion
prev + 1, // insertion
row[j - 1] + cost, // substitution
);
row[j - 1] = prev;
prev = current;
}
row[bLen] = prev;
}
return row[bLen];
}
/**
* Find the closest match for a string from a list of candidates. Returns
* `undefined` when the closest candidate is further than `maxDistance`
* edits away, so unrelated strings don't produce noisy false suggestions.
*/
function findClosestMatch(target: string, candidates: string[], maxDistance = 2): string | undefined {
const targetLower = target.toLowerCase();
let bestMatch: string | undefined;
let bestDistance = maxDistance + 1;
for (const candidate of candidates) {
const distance = levenshteinDistance(targetLower, candidate.toLowerCase(), bestDistance);
if (distance < bestDistance) {
bestDistance = distance;
bestMatch = candidate;
Iif (distance === 0) break;
}
}
return bestDistance <= maxDistance ? bestMatch : undefined;
}
// ============================================================================
// State reference validation
// ============================================================================
/**
* Result of state reference validation.
*/
export interface StateReferenceValidationResult {
/** Whether all state references are valid */
valid: boolean;
/** Invalid state references with suggestions */
errors: Array<{
state: string;
path: string;
suggestion?: string;
}>;
/** Available states for reference */
availableStates: string[];
}
/**
* Validate that stateClasses reference valid states.
*
* @param uif - The UIF definition to validate
* @returns Array of invalid state references (for backwards compatibility)
*/
export function validateStateReferences(uif: unknown): string[] {
const result = validateStateReferencesDetailed(uif);
return result.errors.map((e) => e.state);
}
function collectStateNames(states: unknown): string[] {
if (!Array.isArray(states)) return [];
const names: string[] = [];
for (const state of states) {
Eif (typeof state === 'object' && state !== null) {
const name = (state as Record<string, unknown>).name;
Eif (typeof name === 'string') names.push(name);
}
}
return names;
}
function checkStateClassReferences(
stateClasses: unknown,
stateNames: string[],
errors: StateReferenceValidationResult['errors'],
): void {
if (!Array.isArray(stateClasses)) return;
for (let i = 0; i < stateClasses.length; i++) {
const sc = stateClasses[i];
Iif (typeof sc !== 'object' || sc === null) continue;
const stateRef = (sc as Record<string, unknown>).state;
if (typeof stateRef === 'string' && !stateNames.includes(stateRef)) {
errors.push({
state: stateRef,
path: `stateClasses[${i}]`,
suggestion: findClosestMatch(stateRef, stateNames),
});
}
}
}
/**
* Validate state references with detailed results, including available
* states and "did you mean?" suggestions for typos.
*
* @param uif - The UIF definition to validate
* @returns Detailed validation result with suggestions
*/
export function validateStateReferencesDetailed(uif: unknown): StateReferenceValidationResult {
Iif (typeof uif !== 'object' || uif === null) {
return { valid: true, errors: [], availableStates: [] };
}
const record = uif as Record<string, unknown>;
const errors: StateReferenceValidationResult['errors'] = [];
const stateNames = collectStateNames(record.states);
checkStateClassReferences(record.stateClasses, stateNames, errors);
return { valid: errors.length === 0, errors, availableStates: stateNames };
}
/**
* Validate state references and throw if invalid.
*
* @param uif - The UIF definition to validate
* @throws {UifInvalidStateReferenceError} If invalid state references are found
*/
export function validateStateReferencesOrThrow(uif: unknown): void {
const result = validateStateReferencesDetailed(uif);
if (!result.valid && result.errors.length > 0) {
const firstError = result.errors[0];
throw new UifInvalidStateReferenceError(
firstError.path,
firstError.state,
result.availableStates,
firstError.suggestion,
);
}
}
|