forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.mts
More file actions
623 lines (524 loc) · 18 KB
/
Copy pathcheck.mts
File metadata and controls
623 lines (524 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import { argv, exit, stderr, stdout } from 'node:process';
import { fileURLToPath } from 'node:url';
import { formatWithOptions, parseArgs, styleText } from 'node:util';
import { baseLanguage, getLanguagePlurals, getResourceLanguages, readContent, readResource, writeResource } from './common.mts';
type TaskOptions = {
fix?: boolean;
};
let errorCount = 0;
const describeTask =
(
task: string,
fn: () => AsyncGenerator<{
lint: (reportError: (format?: any, ...param: any[]) => void) => Promise<void>;
fix?: (throwError: (format?: any, ...param: any[]) => void) => Promise<void>;
}>,
) =>
async (options: TaskOptions) => {
const stdoutSupportsColor = styleText('blue', `.`, { stream: stdout }) !== '.';
const stderrSupportsColor = styleText('blue', `.`, { stream: stderr }) !== '.';
const throwError = (format?: any, ...param: any[]) => {
throw new Error(formatWithOptions({ colors: stdoutSupportsColor }, format, ...param));
};
const reportError = (format?: any, ...param: any[]) => {
console.error(
styleText('red', '✘', { stream: stderr }),
styleText('gray', `${task}:`, { stream: stderr }),
formatWithOptions({ colors: stderrSupportsColor }, format, ...param),
);
errorCount++;
};
for await (const result of fn()) {
if (!result) continue;
if (options.fix) {
try {
if (!result.fix) {
await result.lint(throwError);
continue;
}
await result.fix(throwError);
console.log(styleText('blue', '✔', { stream: stdout }), styleText('gray', `${task}:`, { stream: stdout }), 'fixes applied');
} catch (error) {
console.error(
styleText('red', '✘', { stream: stdout }),
styleText('gray', `${task}:`, { stream: stdout }),
error instanceof Error ? error.message : error,
);
console.error(styleText('gray', ` cannot apply fixes automatically, run without --fix to see all errors`, { stream: stdout }));
errorCount++;
}
} else {
await result.lint(reportError);
}
}
};
/**
* Sort keys of the base language (en) alphabetically and write back the sorted resource file if necessary
*/
const sortBaseKeys = describeTask('sort-base-keys', async function* () {
const baseResource = await readResource(baseLanguage);
const keys = Object.keys(baseResource);
const sortedKeys = keys.toSorted((a, b) => a.toLowerCase().localeCompare(b.toLowerCase(), 'en'));
if (keys.join(',') === sortedKeys.join(',')) return;
yield {
lint: async (reportError) => {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const beforeKey = keys.at(i - 1);
const j = sortedKeys.indexOf(key);
const expectedBeforeKey = sortedKeys.at(j - 1);
if (beforeKey !== expectedBeforeKey) {
if (expectedBeforeKey) {
reportError('%o should be after %o', keys[i], expectedBeforeKey);
} else {
reportError('%o should be the first key', keys[i]);
}
}
}
},
fix: async () => {
const sortedResource: Record<string, unknown> = {};
for (const key of sortedKeys) {
sortedResource[key] = baseResource[key];
}
await writeResource(baseLanguage, sortedResource);
},
};
});
/**
* Apply the order of the base language (en) to all other languages
*/
const sortKeys = describeTask('sort-keys', async function* () {
const baseResource = await readResource(baseLanguage);
const baseKeys = new Set(Object.keys(baseResource));
const languages = await getResourceLanguages();
for (const language of languages) {
if (language === baseLanguage) continue;
const resource = await readResource(language);
const resourceKeys = new Set(Object.keys(resource));
const extraKeys = resourceKeys.difference(baseKeys);
const sortedResource: Record<string, unknown> = {};
for (const key of baseKeys) {
if (!resourceKeys.has(key)) continue;
sortedResource[key] = resource[key];
}
for (const key of extraKeys) {
sortedResource[key] = resource[key];
}
if (Object.keys(resource).join(',') === Object.keys(sortedResource).join(',')) continue;
yield {
lint: async (reportError) => {
const keys = Object.keys(resource);
const sortedKeys = Object.keys(sortedResource);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (extraKeys.has(key)) continue;
const j = sortedKeys.indexOf(key);
const expectedBeforeKey = sortedKeys.at(j - 1);
const beforeKey = keys.at(i - 1);
if (beforeKey !== expectedBeforeKey) {
if (expectedBeforeKey) {
reportError('%s: %o should be after %o', language, keys[i], expectedBeforeKey);
} else {
reportError('%s: %o should be the first key', language, keys[i]);
}
}
}
},
fix: async () => {
await writeResource(language, sortedResource);
},
};
}
});
/**
* Wipes extra keys from all language files that are not present in the base language (en)
*/
const wipeExtraKeys = describeTask('wipe-extra-keys', async function* () {
const baseResource = await readResource(baseLanguage);
const baseKeys = new Set(Object.keys(baseResource));
const languages = await getResourceLanguages();
for (const language of languages) {
if (language === baseLanguage) continue;
const resource = await readResource(language);
const resourceKeys = new Set(Object.keys(resource));
if (resourceKeys.difference(baseKeys).size === 0) continue;
yield {
lint: async (reportError) => {
const extraKeys = resourceKeys.difference(baseKeys);
for (const key of extraKeys) {
reportError('%s: has extra key %o', language, key);
}
},
fix: async () => {
const wipedResource: Record<string, unknown> = {};
// Traversing the own resource keys to preserve the original order
for (const key of Object.keys(resource)) {
if (!baseKeys.has(key)) continue;
wipedResource[key] = resource[key];
}
await writeResource(language, wipedResource);
},
};
}
});
/**
* Wipes invalid plural forms from all language files (only "zero", "one", "two", "few", "many", "other" are valid)
*/
const wipeInvalidPlurals = describeTask('wipe-invalid-plurals', async function* () {
const languages = await getResourceLanguages();
for (const language of languages) {
const resource = await readResource(language);
const plurals = getLanguagePlurals(language).concat(['zero']); // 'zero' is special in i18next
for (const [key, translation] of Object.entries(resource)) {
if (typeof translation !== 'object' || !translation) continue;
const translationPlurals = Object.keys(translation);
for (const plural of translationPlurals) {
if (!plurals.includes(plural)) {
yield {
lint: async (reportError) => {
reportError('%s: key %o has invalid plural form %o', language, key, plural);
},
fix: async () => {
const fixedResource: Record<string, unknown> = { ...resource };
fixedResource[key] = Object.fromEntries(Object.entries(translation).filter(([p]) => plurals.includes(p)));
await writeResource(language, fixedResource);
},
};
}
}
}
}
});
/**
* Finds missing plural forms in all language files
*/
const findMissingPlurals = describeTask('find-missing-plurals', async function* () {
const languages = await getResourceLanguages();
for (const language of languages) {
if (language === baseLanguage) continue;
const resource = await readResource(language);
const baseResource = await readResource(baseLanguage);
const plurals = getLanguagePlurals(language);
for (const [key, translation] of Object.entries(baseResource)) {
if (typeof translation !== 'object' || !translation) continue;
if (!(key in resource)) continue;
const translationPlurals = Object.keys(translation);
const resourceTranslation = resource[key];
if (typeof resourceTranslation !== 'object' || !resourceTranslation) continue;
for (const plural of translationPlurals) {
if (!plurals.includes(plural)) continue;
if (plural in resourceTranslation) continue;
yield {
lint: async (reportError) => {
reportError('%s: key %o is missing plural form %o', language, key, plural);
},
};
}
}
}
});
function* listTranslations(resource: Record<string, unknown>) {
for (const [key, translation] of Object.entries(resource)) {
if (typeof translation === 'string') {
yield { key, translation } as const;
continue;
}
if (typeof translation === 'object' && translation) {
for (const [plural, pluralTranslation] of Object.entries(translation)) {
if (typeof pluralTranslation !== 'string') continue;
yield { key, plural, translation: pluralTranslation } as const;
}
}
}
}
const replaceDoubleUnderscorePlaceholders = describeTask('replace-2-underscores', async function* () {
const languages = await getResourceLanguages();
const placeholderRegex = /__(.*?)__/g;
const identifierRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
for (const language of languages) {
const resource = await readResource(language);
for (const { key, plural, translation } of listTranslations(resource)) {
const matches = Array.from(translation.matchAll(placeholderRegex));
if (!matches.length) continue;
for (const match of matches) {
if (!identifierRegex.test(match[1])) {
yield {
lint: async (reportError) => {
if (plural) {
reportError('%s: key %o (plural %o) has invalid placeholder %o', language, key, plural, match[0]);
} else {
reportError('%s: key %o has invalid placeholder %o', language, key, match[0]);
}
},
};
continue;
}
yield {
lint: async (reportError) => {
if (plural) {
reportError('%s: key %o (plural %o) has placeholder %o, should be %o', language, key, plural, match[0], `{{${match[1]}}}`);
} else {
reportError('%s: key %o has placeholder %o, should be %o', language, key, match[0], `{{${match[1]}}}`);
}
},
fix: async () => {
const fixedResource = { ...resource };
if (plural) {
fixedResource[key] = {
...(fixedResource[key] as Record<string, string>),
[plural]: translation.replace(placeholderRegex, `{{${match[1]}}}`),
};
} else {
fixedResource[key] = translation.replace(placeholderRegex, `{{${match[1]}}}`);
}
await writeResource(language, fixedResource);
},
};
}
}
}
});
const trimEndOfFile = describeTask('trim-eof', async function* () {
const languages = await getResourceLanguages();
for (const language of languages) {
const content = await readContent(language);
const trimmedContent = content.replace(/\s+$/g, '');
if (trimmedContent.length === content.length) continue;
yield {
lint: async (reportError) => {
reportError('%s: has trailing whitespace at end of file', language);
},
fix: async () => {
await writeResource(language, JSON.parse(trimmedContent));
},
};
}
});
const extractPlaceholders = (translation: string): Set<string> => {
const placeholders = new Set<string>();
const placeholderRegex = /{{(.+?)(,.*?)?}}/g;
let match;
while ((match = placeholderRegex.exec(translation)) !== null) {
placeholders.add(match[1]);
}
return placeholders;
};
const encodedKey = (key: string, plural?: string) => (plural ? `${key}|${plural}` : key);
/**
* Finds translations that are missing placeholders present in the base language (en)
*/
const missingPlaceholders = describeTask('missing-placeholders', async function* () {
const baseResource = await readResource(baseLanguage);
const baseTranslations = listTranslations(baseResource);
const basePlaceholdersByEncodedKey = new Map<string, Set<string>>();
for (const { key: baseKey, plural: basePlural, translation: baseTranslation } of baseTranslations) {
basePlaceholdersByEncodedKey.set(encodedKey(baseKey, basePlural), extractPlaceholders(baseTranslation));
}
const languages = await getResourceLanguages();
for (const language of languages) {
if (language === baseLanguage) continue;
const resource = await readResource(language);
const translations = listTranslations(resource);
for (const { key, plural, translation } of translations) {
const basePlaceholders = basePlaceholdersByEncodedKey.get(encodedKey(key, plural));
if (!basePlaceholders) continue;
const placeholders = extractPlaceholders(translation);
for (const basePlaceholder of basePlaceholders) {
if (placeholders.has(basePlaceholder)) continue;
yield {
lint: async (reportError) => {
if (plural) {
reportError('%s: key %o (plural %o) is missing placeholder %o', language, key, plural, basePlaceholder);
return;
}
reportError('%s: key %o is missing placeholder %o', language, key, basePlaceholder);
},
};
}
}
}
});
/**
* Finds translations that have extra placeholders not present in the base language (en)
*/
const extraPlaceholders = describeTask('extra-placeholders', async function* () {
const baseResource = await readResource(baseLanguage);
const baseTranslations = listTranslations(baseResource);
const basePlaceholdersByEncodedKey = new Map<string, Set<string>>();
for (const { key: baseKey, plural: basePlural, translation: baseTranslation } of baseTranslations) {
basePlaceholdersByEncodedKey.set(encodedKey(baseKey, basePlural), extractPlaceholders(baseTranslation));
}
const languages = await getResourceLanguages();
for (const language of languages) {
if (language === baseLanguage) continue;
const resource = await readResource(language);
const translations = listTranslations(resource);
for (const { key, plural, translation } of translations) {
const basePlaceholders = basePlaceholdersByEncodedKey.get(encodedKey(key, plural));
if (!basePlaceholders) continue;
const placeholders = extractPlaceholders(translation);
for (const placeholder of placeholders) {
if (basePlaceholders.has(placeholder)) continue;
yield {
lint: async (reportError) => {
reportError('%s: key %o%s has extra placeholder %o', language, key, plural ? ` (plural ${plural})` : '', placeholder);
},
};
}
}
}
});
const findPositionalParams = describeTask('find-sprintf-params', async function* () {
const resource = await readResource(baseLanguage);
for (const { key, plural, translation } of listTranslations(resource)) {
if (!translation.includes('%s')) continue;
yield {
lint: async (reportError) => {
if (plural) {
reportError('key %o (plural %o) has positional parameter %o, should be named parameter like %o', key, plural, '%s', '{{param}}');
} else {
reportError('key %o has positional parameter %o, should be named parameter like %o', key, '%s', '{{param}}');
}
},
};
}
});
function* detectDuplicateJsonKeys(text: string) {
let pos = 0;
const skip = () => {
while (pos < text.length && ' \t\r\n'.includes(text[pos])) pos++;
};
const readString = (): string => {
pos++;
let s = '';
while (pos < text.length && text[pos] !== '"') {
if (text[pos] === '\\') {
s += text[pos++];
}
s += text[pos++];
}
pos++;
return s;
};
function* skipValue(): Generator<{ key: string; parentKey?: string }> {
skip();
if (text[pos] === '"') readString();
else if (text[pos] === '{') yield* readObject();
else if (text[pos] === '[') yield* readArray();
else while (pos < text.length && !',}]'.includes(text[pos]) && !' \t\r\n'.includes(text[pos])) pos++;
}
function* readObject(parentKey?: string) {
pos++;
skip();
const seen = new Set<string>();
while (pos < text.length && text[pos] !== '}') {
skip();
const key = readString();
skip();
pos++;
if (seen.has(key)) {
yield { key, parentKey };
}
seen.add(key);
yield* skipValue();
skip();
if (text[pos] === ',') pos++;
}
pos++;
}
function* readArray() {
pos++;
skip();
while (pos < text.length && text[pos] !== ']') {
yield* skipValue();
skip();
if (text[pos] === ',') pos++;
}
pos++;
}
skip();
if (pos < text.length && text[pos] === '{') yield* readObject();
}
const findDuplicateKeys = describeTask('find-duplicate-keys', async function* () {
const languages = await getResourceLanguages();
for (const language of languages) {
const content = await readContent(language);
for (const { key, parentKey } of detectDuplicateJsonKeys(content)) {
yield {
lint: async (reportError) => {
if (parentKey) {
reportError('%s: duplicate key %o in %o', language, key, parentKey);
} else {
reportError('%s: duplicate key %o', language, key);
}
},
};
}
}
});
/**
* Map of all available tasks
*/
const tasksByName = {
'sort-base-keys': sortBaseKeys,
'sort-keys': sortKeys,
'wipe-extra-keys': wipeExtraKeys,
'wipe-invalid-plurals': wipeInvalidPlurals,
'find-missing-plurals': findMissingPlurals,
'replace-2-underscores': replaceDoubleUnderscorePlaceholders,
'trim-eof': trimEndOfFile,
'find-sprintf-params': findPositionalParams,
'missing-placeholders': missingPlaceholders,
'extra-placeholders': extraPlaceholders,
'find-duplicate-keys': findDuplicateKeys,
} as const;
async function check({ fix, task }: { fix?: boolean; task?: string[] } = {}) {
// We're lenient by default excluding some non-critical tasks
const tasks = new Set<keyof typeof tasksByName>([
'sort-keys',
'wipe-extra-keys',
'wipe-invalid-plurals',
'find-missing-plurals',
'replace-2-underscores',
'trim-eof',
'missing-placeholders',
'extra-placeholders',
'find-duplicate-keys',
]);
if (task?.length) {
tasks.clear();
task.filter((taskName): taskName is keyof typeof tasksByName => taskName in tasksByName).forEach((taskName) => tasks.add(taskName));
}
if (tasks.size === 0) {
throw new Error('No valid tasks selected.');
}
for (const taskName of tasks) {
const task = tasksByName[taskName];
await task({ fix });
}
if (errorCount > 0) {
throw new Error(`${errorCount} error(s) found.`);
}
}
if (import.meta.url.startsWith('file:')) {
const modulePath = fileURLToPath(import.meta.url);
if (argv[1] === modulePath) {
const { values } = parseArgs({
args: argv.slice(2),
options: {
fix: { type: 'boolean', short: 'f' },
task: {
type: 'string',
multiple: true,
short: 't',
choices: Object.keys(tasksByName),
},
},
});
check(values).catch((error) => {
console.error(error);
exit(1);
});
}
}