-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathlocalization-extractor.ts
422 lines (373 loc) · 13.7 KB
/
localization-extractor.ts
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
// *****************************************************************************
// Copyright (C) 2021 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import * as fs from 'fs-extra';
import * as ts from 'typescript';
import * as os from 'os';
import * as path from 'path';
import { glob } from 'glob';
import { promisify } from 'util';
import deepmerge = require('deepmerge');
import { Localization, sortLocalization } from './common';
const globPromise = promisify(glob);
export interface ExtractionOptions {
root?: string
output?: string
exclude?: string
logs?: string
/** List of globs matching the files to extract from. */
files?: string[]
merge?: boolean
quiet?: boolean
}
class SingleFileServiceHost implements ts.LanguageServiceHost {
private file: ts.IScriptSnapshot;
private lib: ts.IScriptSnapshot;
constructor(private options: ts.CompilerOptions, private filename: string, contents: string) {
this.file = ts.ScriptSnapshot.fromString(contents);
this.lib = ts.ScriptSnapshot.fromString('');
}
getCompilationSettings = () => this.options;
getScriptFileNames = () => [this.filename];
getScriptVersion = () => '1';
getScriptSnapshot = (name: string) => name === this.filename ? this.file : this.lib;
getCurrentDirectory = () => '';
getDefaultLibFileName = () => 'lib.d.ts';
}
class TypeScriptError extends Error {
constructor(message: string, node: ts.Node) {
super(buildErrorMessage(message, node));
}
}
function buildErrorMessage(message: string, node: ts.Node): string {
const source = node.getSourceFile();
const sourcePath = source.fileName;
const pos = source.getLineAndCharacterOfPosition(node.pos);
return `${sourcePath}(${pos.line + 1},${pos.character + 1}): ${message}`;
}
const tsOptions: ts.CompilerOptions = {
allowJs: true
};
export async function extract(options: ExtractionOptions): Promise<void> {
const cwd = path.resolve(process.env.INIT_CWD || process.cwd(), options.root ?? '');
const files: string[] = [];
await Promise.all((options.files ?? ['**/src/**/*.{ts,tsx}']).map(
async pattern => files.push(...await globPromise(pattern, { cwd }))
));
let localization: Localization = {};
const errors: string[] = [];
for (const file of files) {
const filePath = path.resolve(cwd, file);
const content = await fs.readFile(filePath, 'utf8');
const fileLocalization = await extractFromFile(file, content, errors, options);
localization = deepmerge(localization, fileLocalization);
}
if (errors.length > 0 && options.logs) {
await fs.writeFile(options.logs, errors.join(os.EOL));
}
const out = path.resolve(process.env.INIT_CWD || process.cwd(), options.output ?? '');
if (options.merge && await fs.pathExists(out)) {
const existing = await fs.readJson(out);
localization = deepmerge(existing, localization);
}
localization = sortLocalization(localization);
await fs.mkdirs(path.dirname(out));
await fs.writeJson(out, localization, {
spaces: 2
});
}
export async function extractFromFile(file: string, content: string, errors?: string[], options?: ExtractionOptions): Promise<Localization> {
const serviceHost = new SingleFileServiceHost(tsOptions, file, content);
const service = ts.createLanguageService(serviceHost);
const sourceFile = service.getProgram()!.getSourceFile(file)!;
const localization: Localization = {};
const localizationCalls = collect(sourceFile, node => isLocalizeCall(node));
for (const call of localizationCalls) {
try {
const extracted = extractFromLocalizeCall(call, options);
if (extracted) {
insert(localization, extracted);
}
} catch (err) {
const tsError = err as Error;
errors?.push(tsError.message);
if (!options?.quiet) {
console.log(tsError.message);
}
}
}
const localizedCommands = collect(sourceFile, node => isCommandLocalizeUtility(node));
for (const command of localizedCommands) {
try {
const extracted = extractFromLocalizedCommandCall(command, errors, options);
const label = extracted.label;
const category = extracted.category;
if (!isExcluded(options, label[0])) {
insert(localization, label);
}
if (category && !isExcluded(options, category[0])) {
insert(localization, category);
}
} catch (err) {
const tsError = err as Error;
errors?.push(tsError.message);
if (!options?.quiet) {
console.log(tsError.message);
}
}
}
return localization;
}
function isExcluded(options: ExtractionOptions | undefined, key: string): boolean {
return !!options?.exclude && key.startsWith(options.exclude);
}
function insert(localization: Localization, values: [string, string, ts.Node]): void {
const key = values[0];
const value = values[1];
const node = values[2];
const parts = key.split('/');
parts.forEach((part, i) => {
let entry = localization[part];
if (i === parts.length - 1) {
if (typeof entry === 'object') {
throw new TypeScriptError(`Multiple translation keys already exist at '${key}'`, node);
}
localization[part] = value;
} else {
if (typeof entry === 'string') {
throw new TypeScriptError(`String entry already exists at '${parts.splice(0, i + 1).join('/')}'`, node);
}
if (!entry) {
entry = {};
}
localization[part] = entry;
localization = entry;
}
});
}
function collect(n: ts.Node, fn: (node: ts.Node) => boolean): ts.Node[] {
const result: ts.Node[] = [];
function loop(node: ts.Node): void {
const stepResult = fn(node);
if (stepResult) {
result.push(node);
} else {
ts.forEachChild(node, loop);
}
}
loop(n);
return result;
}
function isLocalizeCall(node: ts.Node): boolean {
if (!ts.isCallExpression(node)) {
return false;
}
return node.expression.getText() === 'nls.localize';
}
function extractFromLocalizeCall(node: ts.Node, options?: ExtractionOptions): [string, string, ts.Node] | undefined {
if (!ts.isCallExpression(node)) {
throw new TypeScriptError('Invalid node type', node);
}
const args = node.arguments;
if (args.length < 2) {
throw new TypeScriptError('Localize call needs at least 2 arguments', node);
}
const key = extractString(args[0]);
const value = extractString(args[1]);
if (isExcluded(options, key)) {
return undefined;
}
return [key, value, args[1]];
}
function extractFromLocalizedCommandCall(node: ts.Node, errors?: string[], options?: ExtractionOptions): {
label: [string, string, ts.Node],
category?: [string, string, ts.Node]
} {
if (!ts.isCallExpression(node)) {
throw new TypeScriptError('Invalid node type', node);
}
const args = node.arguments;
if (args.length < 1) {
throw new TypeScriptError('Command localization call needs at least one argument', node);
}
const commandObj = args[0];
if (!ts.isObjectLiteralExpression(commandObj)) {
throw new TypeScriptError('First argument of "toLocalizedCommand" needs to be an object literal', node);
}
const properties = commandObj.properties;
const propertyMap = new Map<string, string>();
const relevantProps = ['id', 'label', 'category'];
let labelNode: ts.Node = node;
for (const property of properties) {
if (!property.name) {
continue;
}
if (!ts.isPropertyAssignment(property)) {
throw new TypeScriptError('Only property assignments in "toLocalizedCommand" are allowed', property);
}
if (!ts.isIdentifier(property.name)) {
throw new TypeScriptError('Only identifiers are allowed as property names in "toLocalizedCommand"', property);
}
const name = property.name.text;
if (!relevantProps.includes(property.name.text)) {
continue;
}
if (property.name.text === 'label') {
labelNode = property.initializer;
}
try {
const value = extractString(property.initializer);
propertyMap.set(name, value);
} catch (err) {
const tsError = err as Error;
errors?.push(tsError.message);
if (!options?.quiet) {
console.log(tsError.message);
}
}
}
let labelKey = propertyMap.get('id');
let categoryKey: string | undefined = undefined;
let categoryNode: ts.Node | undefined;
// We have an explicit label translation key
if (args.length > 1) {
try {
const labelOverrideKey = extractStringOrUndefined(args[1]);
if (labelOverrideKey) {
labelKey = labelOverrideKey;
labelNode = args[1];
}
} catch (err) {
const tsError = err as Error;
errors?.push(tsError.message);
if (!options?.quiet) {
console.log(tsError.message);
}
}
}
// We have an explicit category translation key
if (args.length > 2) {
try {
categoryKey = extractStringOrUndefined(args[2]);
categoryNode = args[2];
} catch (err) {
const tsError = err as Error;
errors?.push(tsError.message);
if (!options?.quiet) {
console.log(tsError.message);
}
}
}
if (!labelKey) {
throw new TypeScriptError('No label key found', node);
}
if (!propertyMap.get('label')) {
throw new TypeScriptError('No default label found', node);
}
let categoryLocalization: [string, string, ts.Node] | undefined = undefined;
const categoryLabel = propertyMap.get('category');
if (categoryKey && categoryLabel && categoryNode) {
categoryLocalization = [categoryKey, categoryLabel, categoryNode];
}
return {
label: [labelKey, propertyMap.get('label')!, labelNode],
category: categoryLocalization
};
}
function extractStringOrUndefined(node: ts.Expression): string | undefined {
if (node.getText() === 'undefined') {
return undefined;
}
return extractString(node);
}
function extractString(node: ts.Expression): string {
if (ts.isIdentifier(node)) {
const reference = followReference(node);
if (!reference) {
throw new TypeScriptError(`Could not resolve reference to '${node.text}'`, node);
}
node = reference;
}
if (ts.isTemplateLiteral(node)) {
throw new TypeScriptError(
"Template literals are not supported for localization. Please use the additional arguments of the 'nls.localize' function to format strings",
node
);
}
if (!ts.isStringLiteralLike(node)) {
throw new TypeScriptError(`'${node.getText()}' is not a string constant`, node);
}
return unescapeString(node.text);
}
function followReference(node: ts.Identifier): ts.Expression | undefined {
const scope = collectScope(node);
const next = scope.get(node.text);
if (next && ts.isIdentifier(next)) {
return followReference(next);
}
return next;
}
function collectScope(node: ts.Node, map: Map<string, ts.Expression> = new Map()): Map<string, ts.Expression> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const locals = (node as any)['locals'] as Map<string, ts.Symbol>;
if (locals) {
for (const [key, value] of locals.entries()) {
if (!map.has(key)) {
const declaration = value.valueDeclaration;
if (declaration && ts.isVariableDeclaration(declaration) && declaration.initializer) {
map.set(key, declaration.initializer);
}
}
}
}
if (node.parent) {
collectScope(node.parent, map);
}
return map;
}
function isCommandLocalizeUtility(node: ts.Node): boolean {
if (!ts.isCallExpression(node)) {
return false;
}
return node.expression.getText() === 'Command.toLocalizedCommand';
}
const unescapeMap: Record<string, string> = {
'\'': '\'',
'"': '"',
'\\': '\\',
'n': '\n',
'r': '\r',
't': '\t',
'b': '\b',
'f': '\f'
};
function unescapeString(str: string): string {
const result: string[] = [];
for (let i = 0; i < str.length; i++) {
const ch = str.charAt(i);
if (ch === '\\') {
if (i + 1 < str.length) {
const replace = unescapeMap[str.charAt(i + 1)];
if (replace !== undefined) {
result.push(replace);
i++;
continue;
}
}
}
result.push(ch);
}
return result.join('');
}