|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// TODO: Type-check this file |
| 4 | + |
| 5 | +// Sadly our auto-format doesn't seem to run eslint on this file; give in to |
| 6 | +// Prettier here. |
| 7 | +// TODO: debug |
| 8 | +/* eslint-disable operator-linebreak */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests messages_en.json for strings that seem to not appear in the UI. |
| 12 | + */ |
| 13 | + |
| 14 | +const fs = require('fs'); |
| 15 | +const path = require('path'); |
| 16 | +const { namedTypes: n, visit } = require('ast-types'); |
| 17 | +const flowParser = require('flow-parser'); |
| 18 | +const { parse } = require('recast'); |
| 19 | + |
| 20 | +const messages_en = require('../static/translations/messages_en.json'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Make a list of files that might contain UI strings, by recursing in src/. |
| 24 | + */ |
| 25 | +function getPossibleUiStringFilePaths() { |
| 26 | + const result = []; |
| 27 | + const kSrcDirName = 'src/'; |
| 28 | + function walk(dir, _dirName = '') { |
| 29 | + let dirent; |
| 30 | + // eslint-disable-next-line no-cond-assign |
| 31 | + while ((dirent = dir.readSync())) { |
| 32 | + // To reduce false negatives, `continue` when nothing in `dirent` can |
| 33 | + // cause UI strings to appear in the app. |
| 34 | + |
| 35 | + if (dirent.isFile()) { |
| 36 | + if (!dirent.name.endsWith('.js')) { |
| 37 | + // Non-JS code, and Flow type definitions in .js.flow files. |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + result.push(path.join(kSrcDirName, _dirName, dirent.name)); |
| 42 | + } else if (dirent.isDirectory()) { |
| 43 | + const subdirName = path.join(_dirName, dirent.name); |
| 44 | + |
| 45 | + // e.g., …/__tests__ and …/__flow-tests__ |
| 46 | + if (subdirName.endsWith('tests__')) { |
| 47 | + // Test code. |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + walk(fs.opendirSync(path.join(kSrcDirName, subdirName)), subdirName); |
| 52 | + } else { |
| 53 | + // Something we don't expect to find under src/, probably containing |
| 54 | + // no UI strings. (symlinks? fifos, sockets, devices??) |
| 55 | + continue; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + walk(fs.opendirSync(kSrcDirName)); |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +const parseOptions = { |
| 64 | + parser: { |
| 65 | + parse(src) { |
| 66 | + return flowParser.parse(src, { |
| 67 | + // Comments can't cause UI strings to appear in the app; ignore them. |
| 68 | + all_comments: false, |
| 69 | + comments: false, |
| 70 | + |
| 71 | + // We use Flow enums; the parser shouldn't crash on them. |
| 72 | + enums: true, |
| 73 | + |
| 74 | + // Set `tokens: true` just to work around a mysterious error. |
| 75 | + // |
| 76 | + // From the doc for this option: |
| 77 | + // |
| 78 | + // > include a list of all parsed tokens in a top-level tokens |
| 79 | + // > property |
| 80 | + // |
| 81 | + // We don't actually want this list of tokens. String literals do |
| 82 | + // get represented in the list, but as tokens, i.e., meaningful |
| 83 | + // chunks of the literal source code. They come with surrounding |
| 84 | + // quotes, escape syntax, etc: |
| 85 | + // |
| 86 | + // 'doesn\'t' |
| 87 | + // "doesn't" |
| 88 | + // |
| 89 | + // What we really want is the *value* of a string literal: |
| 90 | + // |
| 91 | + // doesn't |
| 92 | + // |
| 93 | + // and we get that from the AST. |
| 94 | + // |
| 95 | + // Anyway, we set `true` for this because otherwise I've been seeing |
| 96 | + // `parse` throw an error: |
| 97 | + // |
| 98 | + // Error: Line 72: Invalid regular expression: missing / |
| 99 | + // |
| 100 | + // TODO: Debug and/or file an issue upstream. |
| 101 | + tokens: true, |
| 102 | + }); |
| 103 | + }, |
| 104 | + }, |
| 105 | +}; |
| 106 | + |
| 107 | +/** |
| 108 | + * Look at all given files and collect all strings that might be UI strings. |
| 109 | + * |
| 110 | + * The result will include non-UI strings because we can't realistically |
| 111 | + * filter them all out. That means, when the caller checks messages_en |
| 112 | + * against these strings, it could get false negatives: perhaps messages_en |
| 113 | + * has a string 'message-empty' (why would it, though), and that string |
| 114 | + * won't be flagged because it appears as an enum value in ComposeBox. |
| 115 | + * |
| 116 | + * To reduce these false negatives, we filter out low-hanging fruit, like |
| 117 | + * the string 'foo' in `import Foo from 'foo'`. |
| 118 | + */ |
| 119 | +function getPossibleUiStrings(possibleUiStringFilePaths) { |
| 120 | + const result = new Set(); |
| 121 | + possibleUiStringFilePaths.forEach(filePath => { |
| 122 | + const source = fs.readFileSync(filePath).toString(); |
| 123 | + const ast = parse(source, parseOptions); |
| 124 | + |
| 125 | + visit(ast, { |
| 126 | + // Find nodes with type "Literal" in the AST. |
| 127 | + /* eslint-disable no-shadow */ |
| 128 | + visitLiteral(path) { |
| 129 | + const { value } = path.value; |
| 130 | + |
| 131 | + if ( |
| 132 | + // (Non-string literals include numbers, booleans, etc.) |
| 133 | + typeof value === 'string' && |
| 134 | + // This string isn't like 'react' in `import React from 'react'`. |
| 135 | + !n.ImportDeclaration.check(path.parent.value) |
| 136 | + ) { |
| 137 | + result.add(value); |
| 138 | + } |
| 139 | + |
| 140 | + // A literal is always a leaf, right? No need to call this.traverse. |
| 141 | + return false; |
| 142 | + }, |
| 143 | + |
| 144 | + // Find nodes with type "TemplateLiteral" in the AST. We sometimes use |
| 145 | + // template literals in UI strings for readability. |
| 146 | + /* eslint-disable no-shadow */ |
| 147 | + visitTemplateLiteral(path) { |
| 148 | + if ( |
| 149 | + // Translatable UI strings are unlikely to contain |
| 150 | + // sub-expressions. |
| 151 | + // |
| 152 | + // Also, if a template literal has nontrivial sub-expressions, we |
| 153 | + // can't reasonably interpret them here anyway. |
| 154 | + path.value.quasis.length === 1 && |
| 155 | + path.value.expressions.length === 0 |
| 156 | + ) { |
| 157 | + result.add(path.value.quasis[0].value.cooked); |
| 158 | + } |
| 159 | + |
| 160 | + return this.traverse(path); |
| 161 | + }, |
| 162 | + }); |
| 163 | + }); |
| 164 | + return result; |
| 165 | +} |
| 166 | + |
| 167 | +function main() { |
| 168 | + const possibleUiStrings = getPossibleUiStrings(getPossibleUiStringFilePaths()); |
| 169 | + |
| 170 | + // Check each key ("message ID" in formatjs's lingo) against |
| 171 | + // possibleUiStrings, and make a list of any that aren't found. |
| 172 | + const danglingMessageIds = Object.keys(messages_en).filter( |
| 173 | + messageId => !possibleUiStrings.has(messageId), |
| 174 | + ); |
| 175 | + |
| 176 | + if (danglingMessageIds.length > 0) { |
| 177 | + console.error( |
| 178 | + "Found message IDs in static/translations/messages_en.json that don't seem to be used in the app:", |
| 179 | + ); |
| 180 | + console.error(danglingMessageIds); |
| 181 | + process.exit(1); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +main(); |
0 commit comments