Skip to content

Commit

Permalink
util: fix async bugs with find missing translations (#5699)
Browse files Browse the repository at this point in the history
The "code" missing translations finder wasn't properly async and so
would continue to accumulate missing translations as the rest of the
coverage report continued.

The find missing translations script (standalone) handles this by not
ending automatically, but the coverage report would (for example) list 4
entries but then say 21 errors as the the counting of the errors came
after the entries were written.
  • Loading branch information
quisquous authored Jul 16, 2023
1 parent 4c9f82f commit 09e223b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions util/find_missing_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ const findAllJavascriptFiles = (filter?: string): string[] => {
};

// Print missing translations in |file| for |locales|
// TODO: this should just leverage eval
const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func: ErrorFuncType) => {
const parseJavascriptFile = async (
file: string,
inputLocales: readonly Lang[],
func: ErrorFuncType,
) => {
const locales = new Set(inputLocales);

const lineCounter = ((i = 0) => () => i++)();

const lineReader = readline.createInterface({
input: fs.createReadStream(file),
});
Expand All @@ -83,11 +84,13 @@ const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func:
const fixmeRe = /\/\/ FIX-?ME/;
const ignoreRe = /cactbot-ignore-missing-translations/;

lineReader.on('line', (line, idx = lineCounter()) => {
for await (const line of lineReader) {
lineNumber++;
// Immediately exit if the file is auto-generated
if (line.match('// Auto-generated')) {
lineReader.close();
lineReader.removeAllListeners();
return;
}

// Any time we encounter what looks like a new object, start over.
Expand All @@ -96,18 +99,16 @@ const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func:
const m = line.match(openObjRe);
if (m) {
openMatch = m;
// idx is zero-based, but line numbers are not.
lineNumber = idx + 1;
keys = [];
fixme = [];
foundIgnore = false;
return;
continue;
}

// If we're not inside an object, keep looking for the start of one.
const openMatchValue = openMatch?.[1];
if (!openMatch || openMatchValue === undefined)
return;
continue;

// If this object is ended with the same indentation,
// then we've probably maybe found the end of this object.
Expand All @@ -121,7 +122,7 @@ const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func:
const openStr = openMatch[2];

if (openStr === undefined)
return;
continue;

// Only some locales care about zoneRegex, so special case.
if (openStr === 'zoneRegex: {')
Expand All @@ -131,7 +132,7 @@ const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func:
func(file, lineNumber, 'code', Array.from(missingKeys), openStr);
}
openMatch = undefined;
return;
continue;
}

if (line.match(ignoreRe))
Expand All @@ -149,7 +150,7 @@ const parseJavascriptFile = (file: string, inputLocales: readonly Lang[], func:
fixme.push(lang);
}
}
});
}
};

export const findMissingTranslations = async (
Expand All @@ -166,6 +167,6 @@ export const findMissingTranslations = async (
func,
);
}
parseJavascriptFile(file, locales, func);
await parseJavascriptFile(file, locales, func);
}
};

0 comments on commit 09e223b

Please sign in to comment.