From e4feeffd47a7f182e6fe6faec776cd0a8f5ffe45 Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Tue, 26 Jan 2021 21:05:05 -0500 Subject: [PATCH 1/2] revise validate-extension-inputs.js to detect problems with percent inputs --- package.json | 4 +- scripts/validate-extension-inputs.js | 64 ++++++++++++++++++---------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 9f86c4064..5471a71be 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/validate-extension-inputs.js b/scripts/validate-extension-inputs.js index 9a374e874..e396c95c6 100644 --- a/scripts/validate-extension-inputs.js +++ b/scripts/validate-extension-inputs.js @@ -10,14 +10,16 @@ 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; let numTotalErrors = 0; @@ -25,28 +27,46 @@ 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 + } + } + } - 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 + 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 + } } } } From 838efbf5f7744373d5ae03c602ae30de9c81b37c Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Wed, 27 Jan 2021 10:09:03 -0500 Subject: [PATCH 2/2] added warnings for inconsitent quote styles --- scripts/validate-extension-inputs.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/validate-extension-inputs.js b/scripts/validate-extension-inputs.js index e396c95c6..677ae8828 100644 --- a/scripts/validate-extension-inputs.js +++ b/scripts/validate-extension-inputs.js @@ -20,6 +20,7 @@ const source = JSON.parse(fs.readFileSync(`${JSON_DIR}/en.json`)); // 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; @@ -69,6 +70,18 @@ const validateExtensionInputs = (translationData, locale) => { } } } + + // 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 */ + } + } } if (numLocaleErrors > 0) {