Skip to content

Commit 0cd4245

Browse files
committed
wip tool to find dangling message IDs in messages_en.json
Run with: node tools/check-messages-en.js TODO: - Incorporate into tools/test, perhaps to run only when --all-files requested - Type-check this file with Flow? - Think of any more ways to keep non-UI strings out of possibleUiStringLiterals - See discussion: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/Strings.20in.20messages_en.2Ejson.20but.20not.20in.20the.20app/near/1425315 Add flow-parser at <0.159.0. We'd like a later version -- probably ideal would be to sync it with flow-bin, which is ^0.162.0 right now -- but facebook/flow@5de4ea57e, released in v0.159.0, was a change that isn't yet handled by any version of ast-types [1]. A fix for ast-types is open as the issue benjamn/ast-types#728 and PR benjamn/ast-types#727. [1] I get this error output: /Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/types.js:664 throw new Error("did not recognize object of type " + ^ Error: did not recognize object of type "PropertyDefinition" at Object.getFieldNames (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/types.js:664:19) at visitChildren (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:184:36) at Visitor.PVp.visitWithoutReset (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:166:20) at NodePath.each (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path.js:87:26) at visitChildren (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:178:18) at Visitor.PVp.visitWithoutReset (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:166:20) at visitChildren (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:203:25) at Visitor.PVp.visitWithoutReset (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:166:20) at visitChildren (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:203:25) at Visitor.PVp.visitWithoutReset (/Users/chrisbobbe/dev/zulip-mobile/node_modules/ast-types/lib/path-visitor.js:166:20)
1 parent 40934bf commit 0cd4245

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"@types/react-native": "~0.67.6",
9494
"@vusion/webfonts-generator": "^0.8.0",
9595
"babel-plugin-transform-flow-enums": "^0.0.2",
96+
"ast-types": "^0.15.2",
9697
"core-js": "^3.1.4",
9798
"deep-freeze": "^0.0.1",
9899
"eslint": "^8.15.0",
@@ -107,6 +108,7 @@
107108
"eslint-plugin-react-hooks": "^4.5.0",
108109
"flow-bin": "^0.170.0",
109110
"flow-coverage-report": "^0.8.0",
111+
"flow-parser": "<0.159.0",
110112
"flow-typed": "^3.3.1",
111113
"hermes-eslint": "^0.9.0",
112114
"immutable-devtools": "^0.1.5",
@@ -124,6 +126,7 @@
124126
"prettier-eslint-cli": "^6.0.1",
125127
"react-native-cli": "^2.0.1",
126128
"react-test-renderer": "17.0.2",
129+
"recast": "^0.21.2",
127130
"redux-mock-store": "^1.5.1",
128131
"rollup": "^2.26.5",
129132
"sqlite3": "^5.0.2",

tools/check-messages-en.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { namedTypes: n, visit } = require('ast-types');
4+
const flowParser = require('flow-parser');
5+
const { parse } = require('recast');
6+
7+
const messages_en = require('../static/translations/messages_en.json');
8+
9+
// Make a list of files that might contain UI strings, by recursing in src/.
10+
const possibleUIStringFilePaths = [];
11+
const kSrcDirName = 'src/';
12+
function walk(dir, _dirName = '') {
13+
let dirent;
14+
// eslint-disable-next-line no-cond-assign
15+
while ((dirent = dir.readSync())) {
16+
// To reduce false negatives, `continue` when nothing in `dirent` can
17+
// cause UI strings to appear in the app.
18+
19+
if (dirent.isFile()) {
20+
// Non-JS code, and Flow type definitions in .js.flow files.
21+
if (!dirent.name.endsWith('.js')) {
22+
continue;
23+
}
24+
25+
possibleUIStringFilePaths.push(path.join(kSrcDirName, _dirName, dirent.name));
26+
} else if (dirent.isDirectory()) {
27+
const subdirName = path.join(_dirName, dirent.name);
28+
29+
// Test code.
30+
if (subdirName.endsWith('__tests__')) {
31+
continue;
32+
}
33+
34+
walk(fs.opendirSync(path.join(kSrcDirName, subdirName)), subdirName);
35+
} else {
36+
// Something we don't expect to find under src/, probably containing
37+
// no UI strings. (symlinks? fifos, sockets, devices??)
38+
continue;
39+
}
40+
}
41+
}
42+
walk(fs.opendirSync(kSrcDirName));
43+
44+
const parseOptions = {
45+
parser: {
46+
parse(src) {
47+
return flowParser.parse(src, {
48+
// Comments can't cause UI strings to appear in the app; ignore them.
49+
all_comments: false,
50+
comments: false,
51+
52+
// We plan to use Flow enums; the parser shouldn't crash on them.
53+
enums: true,
54+
55+
// Set `tokens: true` just to work around a mysterious error.
56+
//
57+
// From the doc for this option:
58+
//
59+
// > include a list of all parsed tokens in a top-level tokens
60+
// > property
61+
//
62+
// We don't actually want this list of tokens. String literals do
63+
// get represented in the list, but as tokens, i.e., meaningful
64+
// chunks of the literal source code. They come with surrounding
65+
// quotes, escape syntax, etc:
66+
//
67+
// 'doesn\'t'
68+
// "doesn't"
69+
//
70+
// What we really want is the *value* of a string literal:
71+
//
72+
// doesn't
73+
//
74+
// and we get that from the AST.
75+
//
76+
// Anyway, we set `true` for this because otherwise I've been seeing
77+
// `parse` throw an error:
78+
//
79+
// Error: Line 72: Invalid regular expression: missing /
80+
//
81+
// TODO: Debug and/or file an issue upstream.
82+
tokens: true,
83+
});
84+
},
85+
},
86+
};
87+
88+
// Look at all files in possibleUIStringFilePaths, and collect all string
89+
// literals that might represent UI strings.
90+
const possibleUiStringLiterals = new Set();
91+
possibleUIStringFilePaths.forEach(filePath => {
92+
const source = fs.readFileSync(filePath).toString();
93+
const ast = parse(source, parseOptions);
94+
95+
visit(ast, {
96+
// Find nodes with type "Literal" in the AST.
97+
/* eslint-disable no-shadow */
98+
visitLiteral(path) {
99+
// To reduce false negatives, return false when `path` definitely
100+
// doesn't represent a string literal for a UI string in the app.
101+
102+
const { value } = path.value;
103+
104+
// Non-string literals: numbers, booleans, etc.
105+
if (typeof value !== 'string') {
106+
return false;
107+
}
108+
109+
// String literals like 'react' in lines like
110+
// import React from 'react';
111+
if (n.ImportDeclaration.check(path.parent.value)) {
112+
return false;
113+
}
114+
115+
possibleUiStringLiterals.add(value);
116+
117+
return this.traverse(path);
118+
},
119+
});
120+
});
121+
122+
// Check each key ("message ID" in formatjs's lingo) against
123+
// possibleUiStringLiterals, and make a list of any that aren't found.
124+
const danglingMessageIds = Object.keys(messages_en).filter(
125+
messageId => !possibleUiStringLiterals.has(messageId),
126+
);
127+
128+
if (danglingMessageIds.length > 0) {
129+
console.warn(
130+
"Found message IDs in static/translations/messages_en.json that don't seem to be used in the app:",
131+
);
132+
console.warn(danglingMessageIds);
133+
}

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,13 @@ [email protected]:
32163216
dependencies:
32173217
tslib "^2.0.1"
32183218

3219+
[email protected], ast-types@^0.15.2:
3220+
version "0.15.2"
3221+
resolved "https://registry.yarnpkg.com/ast-types/-/ast-types-0.15.2.tgz#39ae4809393c4b16df751ee563411423e85fb49d"
3222+
integrity sha512-c27loCv9QkZinsa5ProX751khO9DJl/AcB5c2KNtA6NRvHKS0PgLfcftz72KVq504vB0Gku5s2kUZzDBvQWvHg==
3223+
dependencies:
3224+
tslib "^2.0.1"
3225+
32193226
"ast-types@npm:@gregprice/ast-types@^0.15.3-0.tsflower.5":
32203227
version "0.15.3-0.tsflower.5"
32213228
resolved "https://registry.yarnpkg.com/@gregprice/ast-types/-/ast-types-0.15.3-0.tsflower.5.tgz#59bfaf5b776dc8bf3e9de7d94d5a9546f3a1b6df"
@@ -5823,6 +5830,11 @@ flow-parser@0.*:
58235830
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.192.0.tgz#e2aa03e0c6a844c4d6ccdb4af2bc83cc589d9c8c"
58245831
integrity sha512-FLyei0ikf4ab9xlg+05WNmdpOODiH9XVBuw7iI9OZyjIo+cX2L2OUPTovjbWLYLlI41oGTcprbKdB/f9XwBnKw==
58255832

5833+
flow-parser@<0.159.0:
5834+
version "0.158.0"
5835+
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.158.0.tgz#d845f167c722babe880110fc3681c44f21823399"
5836+
integrity sha512-0hMsPkBTRrkII/0YiG9ehOxFXy4gOWdk8RSRze5WbfeKAQpL5kC2K4BmumyTfU9o5gr7/llgElF3UpSSrjzQAA==
5837+
58265838
flow-parser@^0.121.0:
58275839
version "0.121.0"
58285840
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.121.0.tgz#9f9898eaec91a9f7c323e9e992d81ab5c58e618f"
@@ -10453,6 +10465,16 @@ recast@^0.20.4:
1045310465
source-map "~0.6.1"
1045410466
tslib "^2.0.1"
1045510467

10468+
recast@^0.21.2:
10469+
version "0.21.2"
10470+
resolved "https://registry.yarnpkg.com/recast/-/recast-0.21.2.tgz#f1995f76397da403e8ed9beafb01a6ff8c29ed10"
10471+
integrity sha512-jUR1+NtaBQdKDqBJ+qxwMm5zR8aLSNh268EfgAbY+EP4wcNEWb6hZFhFeYjaYanwgDahx5t47CH8db7X2NfKdQ==
10472+
dependencies:
10473+
ast-types "0.15.2"
10474+
esprima "~4.0.0"
10475+
source-map "~0.6.1"
10476+
tslib "^2.0.1"
10477+
1045610478
"recast@npm:@gregprice/recast@^0.21.2-0.tsflower.8":
1045710479
version "0.21.2-0.tsflower.8"
1045810480
resolved "https://registry.yarnpkg.com/@gregprice/recast/-/recast-0.21.2-0.tsflower.8.tgz#576b7329a3cb6fcd1e406ebded7daf3cd5d9f573"

0 commit comments

Comments
 (0)