1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Disallow template strings to t function in verify-locales script (#7694)

This commit is contained in:
Erik Marks 2019-12-12 08:49:52 -08:00 committed by GitHub
parent 69def1fcab
commit f9eac81a85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -161,12 +161,25 @@ async function verifyEnglishLocale (fix = false) {
const englishLocale = await getLocale('en')
const javascriptFiles = await findJavascriptFiles(path.resolve(__dirname, '..', 'ui'))
const regex = /'(\w+)'|"(\w+)"/g
// match "t(`...`)" because constructing message keys from template strings
// prevents this script from finding the messages, and then inappropriately
// deletes them
const templateStringRegex = /\bt\(`.*`\)/g
const templateUsage = []
// match the keys from the locale file
const keyRegex = /'(\w+)'|"(\w+)"/g
const usedMessages = new Set()
for await (const fileContents of getFileContents(javascriptFiles)) {
for (const match of matchAll.call(fileContents, regex)) {
for (const match of matchAll.call(fileContents, keyRegex)) {
usedMessages.add(match[1] || match[2])
}
const templateMatches = fileContents.match(templateStringRegex)
if (templateMatches) {
// concat doesn't work here for some reason
templateMatches.forEach(match => templateUsage.push(match))
}
}
// never consider these messages as unused
@ -176,16 +189,24 @@ async function verifyEnglishLocale (fix = false) {
const unusedMessages = englishMessages
.filter(message => !messageExceptions.includes(message) && !usedMessages.has(message))
if (unusedMessages.length) {
console.log(`**en**: ${unusedMessages.length} unused messages`)
log.info(`Messages not present in UI:`)
unusedMessages.forEach(function (key) {
log.info(` - [ ] ${key}`)
})
} else {
}
if (templateUsage.length) {
log.info(`Forbidden use of template strings in 't' function:`)
templateUsage.forEach(function (occurrence) {
log.info(` - ${occurrence}`)
})
}
if (!unusedMessages.length && !templateUsage.length) {
log.info('Full coverage : )')
return false
return false // failed === false
}
if (unusedMessages.length > 0 && fix) {
@ -196,7 +217,7 @@ async function verifyEnglishLocale (fix = false) {
await writeLocale('en', newLocale)
}
return true
return true // failed === true
}
async function findJavascriptFiles (rootDir) {