🛡️ fix: Validate Translation Contracts Against English (#14914)

This commit is contained in:
Danny Avila 2026-08-16 22:55:05 -04:00 • committed by GitHub
parent 0680df8629
commit 5ca667c258
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -55,21 +55,16 @@ function placeholders(value) {
return (value.match(placeholderPattern) ?? []).sort().join('\u0000');
}
function boundaryWhitespace(value) {
return [value.match(/^\s*/u)?.[0] ?? '', value.match(/\s*$/u)?.[0] ?? ''].join('\u0000');
}
function compareFile(relative, base, current) {
const baseValues = flatten(base);
const currentValues = flatten(current);
for (const [key, baseValue] of baseValues) {
for (const [key] of baseValues) {
if (!currentValues.has(key)) {
errors.push(`${relative}: deleted key ${key}`);
continue;
}
const currentValue = currentValues.get(key);
if (typeof baseValue !== 'string' || typeof currentValue !== 'string') continue;
if (placeholders(baseValue) !== placeholders(currentValue)) {
errors.push(`${relative}: changed placeholders for ${key}`);
}
if (currentValue !== baseValue && currentValue !== currentValue.trim()) {
errors.push(`${relative}: introduced leading/trailing whitespace for ${key}`);
}
}
}
@ -106,8 +101,13 @@ if (currentSet.has(englishRelative)) {
const source = englishValues.get(key);
const baseline = baselineValuesByFile.get(relative)?.get(key);
const changedSinceBaseline = baseline === undefined || baseline !== value;
if (changedSinceBaseline && typeof source === 'string' && typeof value === 'string' && placeholders(source) !== placeholders(value)) {
errors.push(`${relative}: placeholder mismatch with English for ${key}`);
if (changedSinceBaseline && typeof source === 'string' && typeof value === 'string') {
if (placeholders(source) !== placeholders(value)) {
errors.push(`${relative}: placeholder mismatch with English for ${key}`);
}
if (value !== value.trim() && boundaryWhitespace(source) !== boundaryWhitespace(value)) {
errors.push(`${relative}: introduced leading/trailing whitespace for ${key}`);
}
}
}
}