@twin.org/validate-locales 0.0.2-next.21 → 0.0.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,763 +0,0 @@
1
- 'use strict';
2
-
3
- var path = require('node:path');
4
- var node_url = require('node:url');
5
- var cliCore = require('@twin.org/cli-core');
6
- var promises = require('node:fs/promises');
7
- var core = require('@twin.org/core');
8
- var nameofTransformer = require('@twin.org/nameof-transformer');
9
- var glob = require('glob');
10
- var ts = require('typescript');
11
-
12
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
13
- function _interopNamespaceDefault(e) {
14
- var n = Object.create(null);
15
- if (e) {
16
- Object.keys(e).forEach(function (k) {
17
- if (k !== 'default') {
18
- var d = Object.getOwnPropertyDescriptor(e, k);
19
- Object.defineProperty(n, k, d.get ? d : {
20
- enumerable: true,
21
- get: function () { return e[k]; }
22
- });
23
- }
24
- });
25
- }
26
- n.default = e;
27
- return Object.freeze(n);
28
- }
29
-
30
- var glob__namespace = /*#__PURE__*/_interopNamespaceDefault(glob);
31
- var ts__namespace = /*#__PURE__*/_interopNamespaceDefault(ts);
32
-
33
- // Copyright 2024 IOTA Stiftung.
34
- // SPDX-License-Identifier: Apache-2.0.
35
- const ERROR_TYPES = [
36
- { name: "GeneralError", dynamicPropertyIndex: 2 },
37
- { name: "GuardError", dynamicPropertyIndex: -1 },
38
- { name: "ValidationError", dynamicPropertyIndex: -1 },
39
- { name: "FetchError", dynamicPropertyIndex: 3, inbuiltProperties: ["httpStatus"] },
40
- { name: "NotFoundError", dynamicPropertyIndex: 3, inbuiltProperties: ["notFoundId"] },
41
- { name: "AlreadyExistsError", dynamicPropertyIndex: 3, inbuiltProperties: ["existingId"] },
42
- { name: "UnauthorizedError", dynamicPropertyIndex: 2 },
43
- { name: "NotSupportedError", dynamicPropertyIndex: 3, inbuiltProperties: ["methodName"] },
44
- { name: "UnprocessableError", dynamicPropertyIndex: 2 },
45
- { name: "ConflictError", dynamicPropertyIndex: 4, inbuiltProperties: ["conflictId", "conflicts"] }
46
- ];
47
- const SKIP_FILES = ["**/models/**/*.ts"];
48
- const SKIP_LITERALS = [
49
- /\d+\.\d+\.\d+(-\w+(\.\w+)*)?$/, // Version string
50
- /^[^@]+@[^@]+\.[^@]+$/, // Email string
51
- /\.json$/i, // ending in .json
52
- /\.js$/i, // ending in .js
53
- /\.ts$/i, // ending in .ts
54
- /\.env$/i, // ending in .env
55
- /\.png$/i, // ending in .png
56
- /\.lock$/i, // ending in .lock
57
- /\.toml$/i, // ending in .toml
58
- /\.{3}/i, // ...
59
- /@twin\.org/ // starting with @twin.org
60
- ];
61
- const SKIP_METHODS = [/^generateRest/, /^generateSocket/];
62
- const CAPTURE_VARIABLES = [/ROUTES_SOURCE/];
63
- /**
64
- * Build the root command to be consumed by the CLI.
65
- * @param program The command to build on.
66
- */
67
- function buildCommandValidateLocales(program) {
68
- program
69
- .option(core.I18n.formatMessage("commands.validate-locales.options.source.param"), core.I18n.formatMessage("commands.validate-locales.options.source.description"), "src/**/*.ts")
70
- .option(core.I18n.formatMessage("commands.validate-locales.options.locales.param"), core.I18n.formatMessage("commands.validate-locales.options.locales.description"), "locales/**/*.json")
71
- .option(core.I18n.formatMessage("commands.validate-locales.options.ignoreFile.param"), core.I18n.formatMessage("commands.validate-locales.options.ignoreFile.description"), "locales/.validate-ignore")
72
- .action(async (opts) => {
73
- await actionCommandValidateLocales(opts);
74
- });
75
- }
76
- /**
77
- * Action the root command.
78
- * @param opts The options for the command.
79
- * @param opts.source The source glob.
80
- * @param opts.locales The locales glob.
81
- * @param opts.ignoreFile The ignore file path.
82
- */
83
- async function actionCommandValidateLocales(opts) {
84
- opts.source = path.resolve(opts.source);
85
- opts.locales = path.resolve(opts.locales);
86
- opts.ignoreFile = path.resolve(opts.ignoreFile);
87
- cliCore.CLIDisplay.value(core.I18n.formatMessage("commands.validate-locales.labels.source"), opts.source);
88
- cliCore.CLIDisplay.value(core.I18n.formatMessage("commands.validate-locales.labels.locales"), opts.locales);
89
- let ignore = [];
90
- if (await cliCore.CLIUtils.fileExists(opts.ignoreFile)) {
91
- cliCore.CLIDisplay.value(core.I18n.formatMessage("commands.validate-locales.labels.ignoreFile"), opts.ignoreFile);
92
- ignore = (await cliCore.CLIUtils.readLinesFile(opts.ignoreFile)) ?? [];
93
- }
94
- cliCore.CLIDisplay.break();
95
- const sources = glob__namespace.sync(opts.source.replace(/\\/g, "/"), { ignore: SKIP_FILES });
96
- const locales = glob__namespace.sync(opts.locales.replace(/\\/g, "/"));
97
- if (sources.length === 0) {
98
- cliCore.CLIDisplay.warning(core.I18n.formatMessage("commands.validate-locales.warnings.noSourceFiles"));
99
- }
100
- else if (locales.length === 0) {
101
- cliCore.CLIDisplay.warning(core.I18n.formatMessage("commands.validate-locales.warnings.noLocaleFiles"));
102
- }
103
- else {
104
- await validateLocales(sources, locales, ignore.filter(i => core.Is.stringValue(i) && !i.startsWith("#")).map(i => new RegExp(i)));
105
- }
106
- cliCore.CLIDisplay.break();
107
- cliCore.CLIDisplay.done();
108
- }
109
- /**
110
- * Validate the locales.
111
- * @param sourceFiles The source files.
112
- * @param localeFiles The locale files.
113
- * @param ignore The ignore list.
114
- */
115
- async function validateLocales(sourceFiles, localeFiles, ignore) {
116
- const localeEntries = [];
117
- let hasQuoteError = false;
118
- let hasUnused = false;
119
- let hasMissing = false;
120
- for (const localeFile of localeFiles) {
121
- const dictionary = await cliCore.CLIUtils.readJsonFile(localeFile);
122
- const locale = path.basename(localeFile, path.extname(localeFile));
123
- cliCore.CLIDisplay.task(core.I18n.formatMessage("commands.validate-locales.progress.validatingLocale", { localeFile }));
124
- cliCore.CLIDisplay.break();
125
- if (core.Is.object(dictionary)) {
126
- const mergedKeys = {};
127
- core.I18n.flattenTranslationKeys(dictionary, "", mergedKeys);
128
- for (const flatKey of Object.keys(mergedKeys)) {
129
- if (/'{.*?}'/.test(mergedKeys[flatKey])) {
130
- cliCore.CLIDisplay.errorMessage(core.I18n.formatMessage("error.validateLocales.usesSingleQuotes", {
131
- key: flatKey
132
- }));
133
- hasQuoteError = true;
134
- }
135
- localeEntries.push({
136
- locale,
137
- key: flatKey,
138
- value: mergedKeys[flatKey],
139
- propertyNames: core.I18n.getPropertyNames(mergedKeys[flatKey]),
140
- referenced: false
141
- });
142
- }
143
- }
144
- let missing = [];
145
- const captureVariables = {};
146
- for (const sourceFile of sourceFiles) {
147
- const source = await promises.readFile(sourceFile, "utf8");
148
- const sourceTs = ts__namespace.createSourceFile(sourceFile, source, ts__namespace.ScriptTarget.ESNext, true, ts__namespace.ScriptKind.TS);
149
- visit(sourceTs, sourceTs, localeEntries, missing, captureVariables);
150
- }
151
- missing = missing.filter(mr => !ignore.some(pattern => pattern.test(mr.key)));
152
- if (missing.length === 0) {
153
- cliCore.CLIDisplay.write(core.I18n.formatMessage("commands.validate-locales.labels.noMissingLocaleEntries"));
154
- cliCore.CLIDisplay.break();
155
- }
156
- else {
157
- hasMissing = true;
158
- for (const missingRef of missing) {
159
- if (missingRef.type === "key") {
160
- cliCore.CLIDisplay.errorMessage(core.I18n.formatMessage("error.validateLocales.missingLocaleEntry", {
161
- key: missingRef.key,
162
- source: missingRef.source,
163
- line: missingRef.line,
164
- column: missingRef.column
165
- }));
166
- }
167
- }
168
- }
169
- cliCore.CLIDisplay.break();
170
- for (const localeEntry of localeEntries) {
171
- if (ignore.some(pattern => pattern.test(localeEntry.key))) {
172
- localeEntry.referenced = true;
173
- }
174
- }
175
- if (localeEntries.filter(le => !le.referenced).length === 0) {
176
- cliCore.CLIDisplay.write(core.I18n.formatMessage("commands.validate-locales.labels.noUnusedLocaleEntries"));
177
- cliCore.CLIDisplay.break();
178
- }
179
- else {
180
- hasUnused = true;
181
- for (const localeEntry of localeEntries) {
182
- if (!localeEntry.referenced) {
183
- cliCore.CLIDisplay.errorMessage(core.I18n.formatMessage("error.validateLocales.unusedLocaleEntry", {
184
- key: localeEntry.key
185
- }));
186
- }
187
- }
188
- }
189
- }
190
- if (hasMissing || hasUnused || hasQuoteError) {
191
- throw new core.GeneralError("validateLocales", "validationFailed");
192
- }
193
- }
194
- /**
195
- * Visit the node.
196
- * @param sourceFile The TypeScript source file for position calculations.
197
- * @param node The node to visit.
198
- * @param localeEntries The locale entries.
199
- * @param missing The missing entries.
200
- * @param captureVariables The capture variables.
201
- */
202
- function visit(sourceFile, node, localeEntries, missing, captureVariables) {
203
- let handled = false;
204
- if (ts__namespace.isNewExpression(node) &&
205
- ts__namespace.isIdentifier(node.expression) &&
206
- ERROR_TYPES.some(errorType => errorType.name === node.expression.getText())) {
207
- processErrorType(sourceFile, node, node.expression.text, localeEntries, missing);
208
- handled = true;
209
- }
210
- else if (ts__namespace.isStringLiteral(node)) {
211
- processStringLiteral(sourceFile, node, localeEntries, missing);
212
- handled = true;
213
- }
214
- else if (ts__namespace.isTemplateExpression(node)) {
215
- processTemplateExpression(sourceFile, node, localeEntries, missing);
216
- handled = true;
217
- }
218
- else if (ts__namespace.isCallExpression(node)) {
219
- handled = processCallExpression(sourceFile, node, localeEntries, missing, captureVariables);
220
- }
221
- else if (ts__namespace.isFunctionDeclaration(node)) {
222
- handled = processFunctionDeclaration(sourceFile, node);
223
- }
224
- else if (ts__namespace.isVariableDeclaration(node)) {
225
- handled = processVariableDeclaration(sourceFile, node, localeEntries, missing, captureVariables);
226
- }
227
- else if (ts__namespace.isImportDeclaration(node) || ts__namespace.isExportDeclaration(node)) {
228
- // Don't care about string in imports/exports
229
- handled = true;
230
- }
231
- else if (ts__namespace.isPropertyAssignment(node)) {
232
- handled = processPropertyAssignment(sourceFile, node, localeEntries, missing);
233
- }
234
- if (!handled) {
235
- ts__namespace.forEachChild(node, child => visit(sourceFile, child, localeEntries, missing, captureVariables));
236
- }
237
- }
238
- /**
239
- * Process an error type node.
240
- * @param sourceFile The TypeScript source file for position calculations.
241
- * @param node The node to process.
242
- * @param errorType The error type.
243
- * @param localeEntries The locale entries.
244
- * @param missing The missing entries.
245
- */
246
- function processErrorType(sourceFile, node, errorType, localeEntries, missing) {
247
- const errType = ERROR_TYPES.find(e => e.name === errorType);
248
- if (core.Is.object(errType)) {
249
- const localeKey = localeFromClassAndMessage(node.arguments?.[0], node.arguments?.[1], "error");
250
- if (core.Is.stringValue(localeKey)) {
251
- const localeEntry = findAndReferenceLocale(localeEntries, localeKey);
252
- if (core.Is.object(localeEntry)) {
253
- if (errType.dynamicPropertyIndex !== -1 || core.Is.arrayValue(errType.inbuiltProperties)) {
254
- const usedProperties = errType.inbuiltProperties?.slice() ?? [];
255
- if (errType.dynamicPropertyIndex !== -1) {
256
- usedProperties.push(...getPropertiesFromNode(node.arguments?.[errType.dynamicPropertyIndex]));
257
- }
258
- checkPropertyUsage(sourceFile, node, localeEntry, localeKey, usedProperties, missing);
259
- }
260
- }
261
- else {
262
- missing.push({
263
- type: "key",
264
- key: localeKey,
265
- source: path.resolve(sourceFile.fileName),
266
- ...getSourcePosition(sourceFile, node)
267
- });
268
- }
269
- }
270
- return;
271
- }
272
- cliCore.CLIDisplay.errorMessage(core.I18n.formatMessage("error.validateLocales.unableToProcessContent", { content: node.getText() }));
273
- }
274
- /**
275
- * Find the locale entry if it exists.
276
- * @param localeEntries The locale entries.
277
- * @param entryToMatch The full key to check.
278
- * @returns The item if found, undefined otherwise.
279
- */
280
- function findAndReferenceLocale(localeEntries, entryToMatch) {
281
- if (core.Is.stringValue(entryToMatch)) {
282
- const found = localeEntries.find(le => le.key === entryToMatch);
283
- if (found) {
284
- found.referenced = true;
285
- return found;
286
- }
287
- }
288
- }
289
- /**
290
- * Process a string literal node.
291
- * @param sourceFile The TypeScript source file for position calculations.
292
- * @param node The node to process.
293
- * @param localeEntries The locale entries.
294
- * @param missing The missing entries.
295
- */
296
- function processStringLiteral(sourceFile, node, localeEntries, missing) {
297
- if (node.text.length > 3 &&
298
- node.text.includes(".") &&
299
- !/[ ()/]/.test(node.text) &&
300
- !/^\.|\.$/.test(node.text) &&
301
- !isSkipLiteral(node.text)) {
302
- const parts = node.text.split(".");
303
- if (parts.length > 1) {
304
- // First try and match the string as-is
305
- let localeEntry = findAndReferenceLocale(localeEntries, node.text);
306
- if (localeEntry) {
307
- localeEntry.referenced = true;
308
- const usedProperties = getPropertiesFromNode(node);
309
- checkPropertyUsage(sourceFile, node, localeEntry, node.text, usedProperties, missing);
310
- }
311
- if (!localeEntry && ["validation.", "common."].some(t => node.text.startsWith(t))) {
312
- localeEntry = findAndReferenceLocale(localeEntries, `error.${node.text}`);
313
- if (localeEntry) {
314
- localeEntry.referenced = true;
315
- const usedProperties = getPropertiesFromNode(node);
316
- checkPropertyUsage(sourceFile, node, localeEntry, `error.${node.text}`, usedProperties, missing);
317
- }
318
- }
319
- if (!localeEntry) {
320
- missing.push({
321
- type: "key",
322
- key: node.text,
323
- source: path.resolve(sourceFile.fileName),
324
- ...getSourcePosition(sourceFile, node)
325
- });
326
- }
327
- }
328
- }
329
- }
330
- /**
331
- * Process a template expression node.
332
- * @param sourceFile The TypeScript source file for position calculations.
333
- * @param node The node to process.
334
- * @param localeEntries The locale entries.
335
- * @param missing The missing entries.
336
- */
337
- function processTemplateExpression(sourceFile, node, localeEntries, missing) {
338
- // This case handles templates like `error.${nameof(Class)}.message`
339
- const templateParts = extractTemplatePartsWithExpressions(node);
340
- // Join all literal text parts to form a potential locale key
341
- if (hasValidTemplateContent(templateParts)) {
342
- const key = expandTemplateParts(templateParts);
343
- let localeEntry = findAndReferenceLocale(localeEntries, key);
344
- if (localeEntry) {
345
- const usedProperties = getPropertiesFromNode(node);
346
- checkPropertyUsage(sourceFile, node, localeEntry, localeEntry.key, usedProperties, missing);
347
- }
348
- else if (["validation.", "common."].some(t => key.startsWith(t))) {
349
- localeEntry = findAndReferenceLocale(localeEntries, `error.${key}`);
350
- if (localeEntry) {
351
- localeEntry.referenced = true;
352
- const usedProperties = getPropertiesFromNode(node.parent.parent);
353
- checkPropertyUsage(sourceFile, node, localeEntry, `error.${key}`, usedProperties, missing);
354
- }
355
- }
356
- }
357
- }
358
- /**
359
- * Process a call expression node.
360
- * @param sourceFile The TypeScript source file for position calculations.
361
- * @param node The node to process.
362
- * @param localeEntries The locale entries.
363
- * @param missing The missing entries.
364
- * @param captureVariables The capture variables.
365
- * @returns True if processed, false otherwise.
366
- */
367
- function processCallExpression(sourceFile, node, localeEntries, missing, captureVariables) {
368
- if (ts__namespace.isPropertyAccessExpression(node.expression)) {
369
- const functionName = node.expression.name.getText();
370
- if (functionName === "log" &&
371
- node.arguments.length === 1 &&
372
- ts__namespace.isObjectLiteralExpression(node.arguments[0])) {
373
- let level;
374
- let source;
375
- let message;
376
- let dataNames;
377
- for (const prop of node.arguments[0].properties) {
378
- if (ts__namespace.isPropertyAssignment(prop) && ts__namespace.isIdentifier(prop.name)) {
379
- if (prop.name.text === "source") {
380
- if (ts__namespace.isIdentifier(prop.initializer) && captureVariables[prop.initializer.text]) {
381
- source = captureVariables[prop.initializer.text];
382
- }
383
- else if (ts__namespace.isStringLiteral(prop.initializer)) {
384
- source = prop.initializer;
385
- }
386
- else if (ts__namespace.isPropertyAccessExpression(prop.initializer)) {
387
- source = prop.initializer;
388
- }
389
- }
390
- else if (prop.name.text === "message") {
391
- message = prop.initializer;
392
- }
393
- else if (prop.name.text === "data") {
394
- dataNames = getPropertiesFromNode(prop.initializer);
395
- }
396
- else if (prop.name.text === "level") {
397
- level = getExpandedText(prop.initializer);
398
- }
399
- }
400
- }
401
- const localeKey = localeFromClassAndMessage(source, message, level);
402
- if (core.Is.stringValue(localeKey)) {
403
- const localeEntry = findAndReferenceLocale(localeEntries, localeKey);
404
- if (core.Is.object(localeEntry)) {
405
- checkPropertyUsage(sourceFile, node, localeEntry, localeKey, dataNames ?? [], missing);
406
- }
407
- else {
408
- missing.push({
409
- type: "key",
410
- key: localeKey,
411
- source: path.resolve(sourceFile.fileName),
412
- ...getSourcePosition(sourceFile, node)
413
- });
414
- }
415
- }
416
- return true;
417
- }
418
- else if (functionName === "formatMessage" &&
419
- node.arguments.length === 2 &&
420
- ts__namespace.isTemplateExpression(node.arguments[0]) &&
421
- ts__namespace.isObjectLiteralExpression(node.arguments[1])) {
422
- const localeKey = getExpandedText(node.arguments[0]);
423
- if (core.Is.stringValue(localeKey)) {
424
- const dataNames = getPropertiesFromNode(node.arguments[1]);
425
- const localeEntry = findAndReferenceLocale(localeEntries, localeKey);
426
- if (core.Is.object(localeEntry)) {
427
- checkPropertyUsage(sourceFile, node, localeEntry, localeKey, dataNames ?? [], missing);
428
- }
429
- else {
430
- missing.push({
431
- type: "key",
432
- key: localeKey,
433
- source: path.resolve(sourceFile.fileName),
434
- ...getSourcePosition(sourceFile, node)
435
- });
436
- }
437
- }
438
- return true;
439
- }
440
- }
441
- return false;
442
- }
443
- /**
444
- * Process a function declaration node.
445
- * @param sourceFile The TypeScript source file for position calculations.
446
- * @param node The node to process.
447
- * @param localeEntries The locale entries.
448
- * @param missing The missing entries.
449
- * @returns True if processed, false otherwise.
450
- */
451
- function processFunctionDeclaration(sourceFile, node, localeEntries, missing) {
452
- if (core.Is.object(node.name) &&
453
- ts__namespace.isIdentifier(node.name) &&
454
- SKIP_METHODS.some(re => re.test(node.name?.text ?? ""))) {
455
- return true;
456
- }
457
- return false;
458
- }
459
- /**
460
- * Process a variable statement declaration node.
461
- * @param sourceFile The TypeScript source file for position calculations.
462
- * @param node The node to process.
463
- * @param localeEntries The locale entries.
464
- * @param missing The missing entries.
465
- * @param captureVariables The capture variables.
466
- * @returns True if processed, false otherwise.
467
- */
468
- function processVariableDeclaration(sourceFile, node, localeEntries, missing, captureVariables) {
469
- if (core.Is.object(node.name) &&
470
- core.Is.object(node.initializer) &&
471
- ts__namespace.isIdentifier(node.name) &&
472
- ts__namespace.isStringLiteral(node.initializer) &&
473
- CAPTURE_VARIABLES.some(re => re.test(node.name.getText()))) {
474
- captureVariables[node.name.getText()] = node.initializer;
475
- return true;
476
- }
477
- return false;
478
- }
479
- /**
480
- * Process a property assignment node.
481
- * @param sourceFile The TypeScript source file for position calculations.
482
- * @param node The node to process.
483
- * @param localeEntries The locale entries.
484
- * @param missing The missing entries.
485
- * @returns True if processed, false otherwise.
486
- */
487
- function processPropertyAssignment(sourceFile, node, localeEntries, missing) {
488
- if (core.Is.object(node.name) && ts__namespace.isIdentifier(node.name) && node.name.getText() === "message") {
489
- const localeKey = getExpandedText(node.initializer);
490
- if (core.Is.stringValue(localeKey)) {
491
- let localeEntry = findAndReferenceLocale(localeEntries, localeKey);
492
- if (!core.Is.object(localeEntry)) {
493
- localeEntry = findAndReferenceLocale(localeEntries, `error.${localeKey}`);
494
- }
495
- if (!core.Is.object(localeEntry)) {
496
- missing.push({
497
- type: "key",
498
- key: localeKey,
499
- source: path.resolve(sourceFile.fileName),
500
- ...getSourcePosition(sourceFile, node)
501
- });
502
- }
503
- return true;
504
- }
505
- }
506
- return false;
507
- }
508
- /**
509
- * Get the expanded text from a node.
510
- * @param node The node to get the text from.
511
- * @returns The expanded text.
512
- */
513
- function getExpandedText(node) {
514
- if (ts__namespace.isTemplateExpression(node)) {
515
- const templateParts = extractTemplatePartsWithExpressions(node);
516
- if (hasValidTemplateContent(templateParts)) {
517
- return expandTemplateParts(templateParts);
518
- }
519
- }
520
- else if (ts__namespace.isStringLiteral(node)) {
521
- if (hasValidTemplateContent([node.text])) {
522
- return node.text;
523
- }
524
- }
525
- return "";
526
- }
527
- /**
528
- * Extract parts from a template literal, splitting by ${} expressions.
529
- * @param node The template expression node.
530
- * @returns Array of parts including expressions and literal text.
531
- */
532
- function extractTemplatePartsWithExpressions(node) {
533
- const parts = [];
534
- // Start with the head (text before first ${})
535
- if (node.head.text) {
536
- parts.push(node.head.text);
537
- }
538
- // Process each template span (${expression}text)
539
- for (const span of node.templateSpans) {
540
- // Add the expression part as text
541
- parts.push(span.expression.getText());
542
- // Add the literal text after the expression
543
- if (span.literal.text) {
544
- parts.push(span.literal.text);
545
- }
546
- }
547
- return parts.filter(part => part.length > 0);
548
- }
549
- /**
550
- * Check if the content has any parts which contain elements which determine is is not an locale key.
551
- * @param templateParts The template parts to check.
552
- * @returns True if the template parts are valid, false otherwise.
553
- */
554
- function hasValidTemplateContent(templateParts) {
555
- return !templateParts.some(part => /[ #,/:=?|]/.test(part));
556
- }
557
- /**
558
- * Expand template parts into a single string, processing nameof expressions.
559
- * @param templateParts The template parts to expand.
560
- * @returns The expanded template string.
561
- */
562
- function expandTemplateParts(templateParts) {
563
- for (let i = 0; i < templateParts.length; i++) {
564
- templateParts[i] = expandTemplatePart(templateParts[i]);
565
- }
566
- return templateParts.join("");
567
- }
568
- /**
569
- * Expand template part into a single string, processing nameof expressions.
570
- * @param templatePart The template part to expand.
571
- * @returns The expanded template string.
572
- */
573
- function expandTemplatePart(templatePart) {
574
- if (templatePart.startsWith("nameof")) {
575
- const stripped = nameofTransformer.manual(templatePart).replace(/["']/g, "");
576
- templatePart = core.StringHelper.camelCase(stripped, true);
577
- }
578
- else if (templatePart.startsWith("StringHelper.")) {
579
- templatePart = templatePart.replace(/\.CLASS_NAME/g, "");
580
- templatePart = templatePart.replace(/StringHelper\.camelCase\((.*?)\)/, (_, i) => core.StringHelper.camelCase(i));
581
- templatePart = templatePart.replace(/StringHelper\.titleCase\((.*?)\)/, (_, i) => core.StringHelper.titleCase(i));
582
- templatePart = templatePart.replace(/StringHelper\.pascalCase\((.*?)\)/, (_, i) => core.StringHelper.pascalCase(i));
583
- templatePart = templatePart.replace(/StringHelper\.kebabCase\((.*?)\)/, (_, i) => core.StringHelper.kebabCase(i));
584
- templatePart = templatePart.replace(/StringHelper\.snakeCase\((.*?)\)/, (_, i) => core.StringHelper.snakeCase(i));
585
- }
586
- else if (templatePart.includes(".CLASS_NAME")) {
587
- templatePart = core.StringHelper.camelCase(templatePart.replace(/\.CLASS_NAME/g, ""));
588
- }
589
- templatePart = templatePart.replace(/["'`]/g, ""); // Remove quotes
590
- return templatePart;
591
- }
592
- /**
593
- * Check the property usage in a locale entry against the used properties.
594
- * @param sourceFile The TypeScript source file for position calculations.
595
- * @param node The node to check.
596
- * @param localeEntry The locale entry to check against.
597
- * @param key The key in the locale entry.
598
- * @param usedProperties The properties used in the code.
599
- * @param missing The missing entries.
600
- */
601
- function checkPropertyUsage(sourceFile, node, localeEntry, key, usedProperties, missing) {
602
- for (const propName of localeEntry.propertyNames) {
603
- const propIndex = usedProperties.indexOf(propName);
604
- if (propIndex === -1) {
605
- const position = getSourcePosition(sourceFile, node);
606
- cliCore.CLIDisplay.errorMessage(core.I18n.formatMessage("error.validateLocales.missingPropertyInLocale", {
607
- key,
608
- property: propName,
609
- source: path.resolve(sourceFile.fileName),
610
- line: position.line,
611
- column: position.column
612
- }));
613
- missing.push({
614
- type: "property",
615
- key: localeEntry.key,
616
- source: path.resolve(sourceFile.fileName),
617
- ...position
618
- });
619
- }
620
- }
621
- // We often pass additional properties in the error details to better inform the logging
622
- // so we don't want to perform the opposite check for parameters in the call but not in the locale entry
623
- }
624
- /**
625
- * Helper to get line and column position from a node.
626
- * @param sourceFile The TypeScript source file for position calculations.
627
- * @param n The node to get position for.
628
- * @returns Line and column (1-based).
629
- */
630
- function getSourcePosition(sourceFile, n) {
631
- const { line, character } = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile));
632
- return { line: line + 1, column: character + 1 };
633
- }
634
- /**
635
- * Get property names from a node.
636
- * @param node The node to get property names from.
637
- * @returns The property names.
638
- */
639
- function getPropertiesFromNode(node) {
640
- if (!node) {
641
- return [];
642
- }
643
- const props = [];
644
- // If this is an object literal then we can get the property names
645
- if (ts__namespace.isObjectLiteralExpression(node)) {
646
- // Get the properties of the object literal
647
- const properties = node.properties;
648
- for (const prop of properties) {
649
- if (ts__namespace.isPropertyAssignment(prop) && ts__namespace.isIdentifier(prop.name)) {
650
- // { property: value }
651
- props.push(prop.name.text);
652
- // { property: { nestedProperty: value } }
653
- if (ts__namespace.isObjectLiteralExpression(prop.initializer)) {
654
- // Recursively get properties from nested object literals
655
- const nestedProps = getPropertiesFromNode(prop.initializer);
656
- props.push(...nestedProps);
657
- }
658
- }
659
- else if (ts__namespace.isShorthandPropertyAssignment(prop)) {
660
- // { property }
661
- props.push(prop.getText());
662
- }
663
- }
664
- }
665
- else if (ts__namespace.isStringLiteral(node)) {
666
- // If this is a string literal then there are no properties
667
- // so we need to look in the surrounding context
668
- const parent = node.parent;
669
- if (ts__namespace.isCallExpression(parent) || ts__namespace.isNewExpression(parent)) {
670
- // Let's see if they are the next argument in the call
671
- const args = parent.arguments;
672
- if (args && args.length > 0) {
673
- const index = args.findIndex(a => a === node);
674
- if (index !== -1 && index + 1 < args.length) {
675
- return getPropertiesFromNode(args[index + 1]);
676
- }
677
- }
678
- }
679
- else if (ts__namespace.isPropertyAssignment(parent)) {
680
- // This is part of a property assignment in an object
681
- // so we can check the parent object for other properties
682
- if (ts__namespace.isObjectLiteralExpression(parent.parent)) {
683
- return getPropertiesFromNode(parent.parent);
684
- }
685
- }
686
- }
687
- return props;
688
- }
689
- /**
690
- * Check if a value is a file operation.
691
- * @param value The value to check.
692
- * @returns True if the value is a file operation.
693
- */
694
- function isSkipLiteral(value) {
695
- return SKIP_LITERALS.some(regex => regex.test(value));
696
- }
697
- /**
698
- * Get the locale from the class and message nodes.
699
- * @param classNode The class node.
700
- * @param messageNode The message node.
701
- * @param prefix The prefix for the locale key.
702
- * @returns The locale entry or undefined.
703
- */
704
- function localeFromClassAndMessage(classNode, messageNode, prefix) {
705
- if (!classNode || !messageNode) {
706
- return undefined;
707
- }
708
- const classNameParam = classNode.getText();
709
- const classNameParamParts = classNameParam?.split(".");
710
- if (core.Is.array(classNameParamParts)) {
711
- const localeKey = getExpandedText(messageNode);
712
- if (core.Is.stringValue(localeKey)) {
713
- const localeKeyParts = [];
714
- if (core.Is.stringValue(prefix)) {
715
- localeKeyParts.push(prefix);
716
- }
717
- // If the key is not fully qualified then add the class name
718
- if (!localeKey.includes(".")) {
719
- localeKeyParts.push(expandTemplatePart(classNameParam));
720
- }
721
- localeKeyParts.push(localeKey);
722
- return localeKeyParts.join(".");
723
- }
724
- }
725
- return undefined;
726
- }
727
-
728
- // Copyright 2024 IOTA Stiftung.
729
- // SPDX-License-Identifier: Apache-2.0.
730
- /**
731
- * The main entry point for the CLI.
732
- */
733
- class CLI extends cliCore.CLIBase {
734
- /**
735
- * Run the app.
736
- * @param argv The process arguments.
737
- * @param localesDirectory The directory for the locales, default to relative to the script.
738
- * @param options Additional options.
739
- * @param options.overrideOutputWidth Override the output width.
740
- * @returns The exit code.
741
- */
742
- async run(argv, localesDirectory, options) {
743
- return this.execute({
744
- title: "TWIN Validate Locales",
745
- appName: "validate-locales",
746
- version: "0.0.2-next.21", // x-release-please-version
747
- icon: "⚙️ ",
748
- supportsEnvFiles: false,
749
- overrideOutputWidth: options?.overrideOutputWidth
750
- }, localesDirectory ?? path.join(path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)))), "../locales"), argv);
751
- }
752
- /**
753
- * Configure any options or actions at the root program level.
754
- * @param program The root program command.
755
- */
756
- configureRoot(program) {
757
- buildCommandValidateLocales(program);
758
- }
759
- }
760
-
761
- exports.CLI = CLI;
762
- exports.actionCommandValidateLocales = actionCommandValidateLocales;
763
- exports.buildCommandValidateLocales = buildCommandValidateLocales;