Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revise validate-extension-inputs.js to detect problems with percent i… #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"sync:help": "npm run push:help && npm run pull:help",
"test": "npm run lint:js && npm run validate:editor && npm run validate:www && npm run build && npm run lint:json",
"update": "scripts/update-translations.sh",
"validate:blocks": "babel-node scripts/validate-translations ./editor/blocks/",
"validate:extensions": "babel-node scripts/validate-translations ./editor/extensions/ && babel-node scripts/validate-extension-inputs",
"validate:blocks": "babel-node scripts/validate-translations ./editor/blocks/ && babel-node scripts/validate-extension-inputs ./editor/blocks/",
"validate:extensions": "babel-node scripts/validate-translations ./editor/extensions/ && babel-node scripts/validate-extension-inputs ./editor/extensions/",
"validate:interface": "babel-node scripts/validate-translations ./editor/interface/",
"validate:paint": "babel-node scripts/validate-translations ./editor/paint-editor/",
"validate:editor": "npm run validate:blocks && npm run validate:extensions && npm run validate:interface && npm run validate:paint",
Expand Down
77 changes: 55 additions & 22 deletions scripts/validate-extension-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,76 @@ import path from 'path';
import async from 'async';
import assert from 'assert';
import locales from '../src/supported-locales.js';
const args = process.argv.slice(2);

// Globals
const JSON_DIR = path.join(process.cwd(), '/editor/extensions');
const JSON_DIR = path.resolve(args[0]);
const source = JSON.parse(fs.readFileSync(`${JSON_DIR}/en.json`));

// Matches everything inside brackets, and the brackets themselves.
// e.g. matches '[MOTOR_ID]', '[POWER]' from 'altera a potência de [MOTOR_ID] para [POWER]'
const blockInputRegex = /\[.+?\]/g;
const blockPercentRegex = /%\d+/g;
const blockPercentWithQuotesRegex = /"%\d+"|“%\d+”|'%\d+'|「%\d+」|«%\d+»|„%\d+”/g;

let numTotalErrors = 0;

const validateExtensionInputs = (translationData, locale) => {
let numLocaleErrors = 0;
for (const block of Object.keys(translationData)) {
const englishBlockInputs = source[block].match(blockInputRegex);
if (englishBlockInputs) {
// If null (meaning no matches), that means that English block inputs exist but translated ones don't.
// Coerce it to an empty array so that the assertion below fails, instead of getting the less-helpful error
// that we can't call Array.includes on null.
const translatedBlockInputs = translationData[block].match(blockInputRegex) || [];

for (const input of englishBlockInputs) {
// Currently there are enough errors here that it would be tedious
// to fix an error, rerun this tool to find the next error, and repeat.
// So, catch the assertion error and add to the number of total errors.
// This allows all errors to be displayed when the command is run,
// rather than just the first encountered.
try {
assert(
translatedBlockInputs.includes(input),

`Block '${block}' in locale '${locale}' does not include input ${input}:\n` +
translationData[block]
);
} catch (err) {
numLocaleErrors++;
console.error(err.message + '\n'); // eslint-disable-line no-console
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.error(err.message + '\n'); // eslint-disable-line no-console
process.stdout.write(err.message + '\n');

It's better to use process.stdout for this type of script (also doesn't trigger eslint)

}
}
}

const englishBlockPercents = source[block].match(blockPercentRegex);
if (englishBlockPercents) {
const translatedBlockPercents = translationData[block].match(blockPercentRegex) || [];
for (const percentInput of englishBlockPercents) {
try {
assert(
translatedBlockPercents.includes(percentInput),
`Block '${block}' in locale '${locale}' does not include input ${percentInput}:\n` +
translationData[block]
);
} catch (err) {
numLocaleErrors++;
console.error(err.message + '\n'); // eslint-disable-line no-console
}
}
}

if (!englishBlockInputs) continue;

// If null (meaning no matches), that means that English block inputs exist but translated ones don't.
// Coerce it to an empty array so that the assertion below fails, instead of getting the less-helpful error
// that we can't call Array.includes on null.
const translatedBlockInputs = translationData[block].match(blockInputRegex) || [];

for (const input of englishBlockInputs) {
// Currently there are enough errors here that it would be tedious to fix an error, rerun this tool
// to find the next error, and repeat. So, catch the assertion error and add to the number of total errors.
// This allows all errors to be displayed when the command is run, rather than just the first encountered.
try {
assert(
translatedBlockInputs.includes(input),

`Block '${block}' in locale '${locale}' does not include input ${input}:\n` +
translationData[block]
);
} catch (err) {
numLocaleErrors++;
console.error(err.message + '\n'); // eslint-disable-line no-console
// do not add to list of errors, print warning only
const englishBlockPercentsWithQuotes = source[block].match(blockPercentWithQuotesRegex);
if (englishBlockPercentsWithQuotes) {
const translatedBlockPercentsWithQuotes = translationData[block].match(blockPercentWithQuotesRegex) || [];
if (englishBlockPercentsWithQuotes.length !== translatedBlockPercentsWithQuotes.length) {
/* eslint-disable no-console */
console.error(`Warning (nonfatal): Block '${block}' in locale '${locale}'` +
' might not have correct number of quoted inputs in:\n' + translationData[block] + '\n');
/* eslint-enable no-console */
}
}
}
Expand Down