@gtkx/cli 2.0.0-beta.1 → 2.0.0-beta.2
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.
- package/dist/builder.d.ts +1 -0
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +17 -5
- package/dist/builder.js.map +1 -1
- package/dist/cli.js +2 -0
- package/dist/cli.js.map +1 -1
- package/dist/codegen/run-codegen.d.ts +4 -2
- package/dist/codegen/run-codegen.d.ts.map +1 -1
- package/dist/codegen/run-codegen.js +13 -8
- package/dist/codegen/run-codegen.js.map +1 -1
- package/dist/codegen/store-resolver.d.ts +1 -1
- package/dist/codegen/store-resolver.d.ts.map +1 -1
- package/dist/codegen/store-resolver.js +2 -2
- package/dist/codegen/store-resolver.js.map +1 -1
- package/dist/commands/build.d.ts +5 -0
- package/dist/commands/build.d.ts.map +1 -1
- package/dist/commands/build.js +83 -10
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/codegen.d.ts +1 -0
- package/dist/commands/codegen.d.ts.map +1 -1
- package/dist/commands/codegen.js +6 -5
- package/dist/commands/codegen.js.map +1 -1
- package/dist/commands/deploy.d.ts +1 -0
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/deploy.js +4 -2
- package/dist/commands/deploy.js.map +1 -1
- package/dist/commands/dev.d.ts +8 -0
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +19 -1
- package/dist/commands/dev.js.map +1 -1
- package/dist/deploy/freedesktop/metainfo.d.ts.map +1 -1
- package/dist/deploy/freedesktop/metainfo.js +41 -2
- package/dist/deploy/freedesktop/metainfo.js.map +1 -1
- package/dist/deploy/nfpm/config.d.ts.map +1 -1
- package/dist/deploy/nfpm/config.js +5 -2
- package/dist/deploy/nfpm/config.js.map +1 -1
- package/dist/deploy/nfpm/contents.d.ts.map +1 -1
- package/dist/deploy/nfpm/contents.js +8 -0
- package/dist/deploy/nfpm/contents.js.map +1 -1
- package/dist/deploy/node-runtime/elf.d.ts +2 -1
- package/dist/deploy/node-runtime/elf.d.ts.map +1 -1
- package/dist/deploy/node-runtime/elf.js +32 -5
- package/dist/deploy/node-runtime/elf.js.map +1 -1
- package/dist/deploy/node-runtime/index.d.ts +2 -1
- package/dist/deploy/node-runtime/index.d.ts.map +1 -1
- package/dist/deploy/node-runtime/index.js +73 -4
- package/dist/deploy/node-runtime/index.js.map +1 -1
- package/dist/deploy/notices/node-runtime.js +2 -2
- package/dist/deploy/notices/node-runtime.js.map +1 -1
- package/dist/deploy/payload/launcher.d.ts.map +1 -1
- package/dist/deploy/payload/launcher.js +14 -1
- package/dist/deploy/payload/launcher.js.map +1 -1
- package/dist/deploy/payload/stage.js +1 -1
- package/dist/deploy/payload/stage.js.map +1 -1
- package/dist/deploy/run-deploy.d.ts +1 -0
- package/dist/deploy/run-deploy.d.ts.map +1 -1
- package/dist/deploy/run-deploy.js +100 -14
- package/dist/deploy/run-deploy.js.map +1 -1
- package/dist/deploy/settings/build-manifest.d.ts +6 -1
- package/dist/deploy/settings/build-manifest.d.ts.map +1 -1
- package/dist/deploy/settings/build-manifest.js +15 -4
- package/dist/deploy/settings/build-manifest.js.map +1 -1
- package/dist/deploy/settings/extra-files.d.ts +1 -1
- package/dist/deploy/settings/extra-files.d.ts.map +1 -1
- package/dist/deploy/settings/extra-files.js +22 -1
- package/dist/deploy/settings/extra-files.js.map +1 -1
- package/dist/deploy/settings/index.d.ts +1 -0
- package/dist/deploy/settings/index.d.ts.map +1 -1
- package/dist/deploy/settings/index.js +2 -1
- package/dist/deploy/settings/index.js.map +1 -1
- package/dist/deploy/settings/paths.d.ts +2 -1
- package/dist/deploy/settings/paths.d.ts.map +1 -1
- package/dist/deploy/settings/paths.js +113 -5
- package/dist/deploy/settings/paths.js.map +1 -1
- package/dist/deploy/targets/flatpak-source.d.ts.map +1 -1
- package/dist/deploy/targets/flatpak-source.js +11 -2
- package/dist/deploy/targets/flatpak-source.js.map +1 -1
- package/dist/deploy/types.d.ts +1 -0
- package/dist/deploy/types.d.ts.map +1 -1
- package/dist/deploy/types.js.map +1 -1
- package/dist/dev/headless.d.ts +3 -0
- package/dist/dev/headless.d.ts.map +1 -0
- package/dist/dev/headless.js +35 -0
- package/dist/dev/headless.js.map +1 -0
- package/dist/dev/runner-deps.js +1 -1
- package/dist/dev/runner-deps.js.map +1 -1
- package/dist/i18n/source-messages.d.ts.map +1 -1
- package/dist/i18n/source-messages.js +835 -1
- package/dist/i18n/source-messages.js.map +1 -1
- package/dist/internal/build-manifest.d.ts +5 -2
- package/dist/internal/build-manifest.d.ts.map +1 -1
- package/dist/internal/build-manifest.js +15 -2
- package/dist/internal/build-manifest.js.map +1 -1
- package/dist/internal/build-output.d.ts +8 -0
- package/dist/internal/build-output.d.ts.map +1 -0
- package/dist/internal/build-output.js +69 -0
- package/dist/internal/build-output.js.map +1 -0
- package/dist/internal/entry-arg.d.ts +4 -1
- package/dist/internal/entry-arg.d.ts.map +1 -1
- package/dist/internal/entry-arg.js +7 -1
- package/dist/internal/entry-arg.js.map +1 -1
- package/dist/internal/output-directory.d.ts +15 -0
- package/dist/internal/output-directory.d.ts.map +1 -0
- package/dist/internal/output-directory.js +490 -0
- package/dist/internal/output-directory.js.map +1 -0
- package/dist/internal/prepare-project.d.ts +2 -0
- package/dist/internal/prepare-project.d.ts.map +1 -1
- package/dist/internal/prepare-project.js +3 -3
- package/dist/internal/prepare-project.js.map +1 -1
- package/dist/internal/source-imports.js +1 -1
- package/dist/internal/source-imports.js.map +1 -1
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/client.js.map +1 -1
- package/dist/vite-plugins/build-manifest.d.ts +5 -1
- package/dist/vite-plugins/build-manifest.d.ts.map +1 -1
- package/dist/vite-plugins/build-manifest.js +2 -1
- package/dist/vite-plugins/build-manifest.js.map +1 -1
- package/dist/vite-plugins/i18n.d.ts.map +1 -1
- package/dist/vite-plugins/i18n.js +58 -6
- package/dist/vite-plugins/i18n.js.map +1 -1
- package/dist/vite-plugins/index.d.ts +8 -1
- package/dist/vite-plugins/index.d.ts.map +1 -1
- package/dist/vite-plugins/index.js +6 -2
- package/dist/vite-plugins/index.js.map +1 -1
- package/dist/vite-plugins/self-contained.d.ts.map +1 -1
- package/dist/vite-plugins/self-contained.js +159 -158
- package/dist/vite-plugins/self-contained.js.map +1 -1
- package/package.json +9 -9
- package/src/builder.ts +18 -5
- package/src/cli.ts +3 -0
- package/src/codegen/run-codegen.ts +15 -7
- package/src/codegen/store-resolver.ts +2 -2
- package/src/commands/build.ts +13 -2
- package/src/commands/codegen.ts +6 -5
- package/src/commands/deploy.ts +4 -2
- package/src/commands/dev.ts +20 -1
- package/src/deploy/freedesktop/metainfo.ts +73 -21
- package/src/deploy/nfpm/config.ts +5 -2
- package/src/deploy/nfpm/contents.ts +8 -0
- package/src/deploy/node-runtime/elf.ts +43 -5
- package/src/deploy/node-runtime/index.ts +105 -5
- package/src/deploy/notices/node-runtime.ts +2 -2
- package/src/deploy/payload/launcher.ts +20 -1
- package/src/deploy/payload/stage.ts +1 -1
- package/src/deploy/run-deploy.ts +56 -16
- package/src/deploy/settings/build-manifest.ts +28 -3
- package/src/deploy/settings/extra-files.ts +26 -2
- package/src/deploy/settings/index.ts +3 -1
- package/src/deploy/settings/paths.ts +70 -5
- package/src/deploy/targets/flatpak-source.ts +13 -2
- package/src/deploy/types.ts +1 -0
- package/src/dev/headless.ts +40 -0
- package/src/dev/runner-deps.ts +1 -1
- package/src/i18n/source-messages.ts +1220 -2
- package/src/internal/build-manifest.ts +25 -1
- package/src/internal/build-output.ts +99 -0
- package/src/internal/entry-arg.ts +8 -1
- package/src/internal/output-directory.ts +699 -0
- package/src/internal/prepare-project.ts +5 -5
- package/src/internal/source-imports.ts +1 -1
- package/src/mcp/client.ts +2 -2
- package/src/vite-plugins/build-manifest.ts +7 -1
- package/src/vite-plugins/i18n.ts +86 -5
- package/src/vite-plugins/index.ts +14 -7
- package/src/vite-plugins/self-contained.ts +184 -185
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { parseSync as parseBabelSync, traverse, types } from "@babel/core";
|
|
1
2
|
import { isPathInside, isPathWithin, toPosixPath } from "@gtkx/utils";
|
|
2
3
|
import { runExtractor } from "i18next-cli";
|
|
3
4
|
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
4
5
|
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
6
|
+
import { parseSync, Visitor } from "vite";
|
|
5
7
|
import { runCliTool } from "../internal/run-cli-tool.js";
|
|
8
|
+
import { sourceLanguage } from "../internal/source-imports.js";
|
|
6
9
|
import { replaceCatalogTemplate } from "./catalog-template.js";
|
|
7
10
|
import { metadataTemplateFiles } from "./metadata-templates.js";
|
|
8
11
|
import { clearI18nResources, i18nToolkitConfig } from "./types.js";
|
|
@@ -10,6 +13,26 @@ const CONTEXT_SEPARATOR = "\u{4}";
|
|
|
10
13
|
const DEFAULT_NAMESPACE = "translation";
|
|
11
14
|
const POTFILES_FILENAME = "POTFILES.in";
|
|
12
15
|
const SYNTHETIC_FILENAME = "messages.js";
|
|
16
|
+
const UNSUPPORTED_PLURAL_OPTION = /^defaultValue_(few|many|two|zero)$/u;
|
|
17
|
+
const STATIC_SOURCE_OPTION_NAMES = new Set([
|
|
18
|
+
"context",
|
|
19
|
+
"defaultValue",
|
|
20
|
+
"defaultValue_one",
|
|
21
|
+
"defaultValue_other",
|
|
22
|
+
"keyPrefix",
|
|
23
|
+
]);
|
|
24
|
+
const NESTED_CONTEXT = /["']?context["']?\s*:\s*(?:"([^"]*)"|'([^']*)')/u;
|
|
25
|
+
const NESTED_COUNT = /["']?count["']?\s*:/u;
|
|
26
|
+
const NESTED_STRUCTURE_TOKEN = /"[^"]*"|'[^']*'|[()]/gu;
|
|
27
|
+
const TRANSLATION_MODULES = new Set(["@gtkx/i18n", "i18next"]);
|
|
28
|
+
const TRANS_ELEMENTS = new Set(["Trans", "TransWithoutContext"]);
|
|
29
|
+
const STATIC_KEY_WRAPPER_TYPES = new Set([
|
|
30
|
+
"ParenthesizedExpression",
|
|
31
|
+
"TSAsExpression",
|
|
32
|
+
"TSSatisfiesExpression",
|
|
33
|
+
"TSTypeAssertion",
|
|
34
|
+
"TSNonNullExpression",
|
|
35
|
+
]);
|
|
13
36
|
const quietLogger = () => {
|
|
14
37
|
const reports = [];
|
|
15
38
|
return {
|
|
@@ -185,16 +208,828 @@ const normalizedSourceFiles = (root, paths) => {
|
|
|
185
208
|
.filter((path) => isPathWithin(root, path));
|
|
186
209
|
return new Set(files).values().toArray().toSorted((left, right) => left.localeCompare(right));
|
|
187
210
|
};
|
|
211
|
+
const isStaticKeyWrapper = (key) => STATIC_KEY_WRAPPER_TYPES.has(key.type);
|
|
212
|
+
const unwrapStaticWrapper = (node) => isStaticKeyWrapper(node) ? unwrapStaticWrapper(node.expression) : node;
|
|
213
|
+
const isStaticTranslationKey = (key) => {
|
|
214
|
+
if (key === undefined || key === null) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
if (key.type === "Literal") {
|
|
218
|
+
return typeof key.value === "string";
|
|
219
|
+
}
|
|
220
|
+
if (key.type === "TemplateLiteral") {
|
|
221
|
+
return key.expressions.length === 0;
|
|
222
|
+
}
|
|
223
|
+
if (key.type === "ParenthesizedExpression") {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (isStaticKeyWrapper(key)) {
|
|
227
|
+
return isStaticTranslationKey(key.expression);
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
};
|
|
231
|
+
const assertStaticTranslationKey = (key) => {
|
|
232
|
+
if (!isStaticTranslationKey(key)) {
|
|
233
|
+
throw new Error("Translation keys must be string literals");
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
const propertyName = (property) => {
|
|
237
|
+
if (!property.computed && property.key.type === "Identifier") {
|
|
238
|
+
return property.key.name;
|
|
239
|
+
}
|
|
240
|
+
return property.key.type === "Literal" && typeof property.key.value === "string"
|
|
241
|
+
? property.key.value
|
|
242
|
+
: null;
|
|
243
|
+
};
|
|
244
|
+
const assertSupportedOptionProperty = (property) => {
|
|
245
|
+
if (property.type === "SpreadElement") {
|
|
246
|
+
throw new Error("Translation extraction requires explicit option properties");
|
|
247
|
+
}
|
|
248
|
+
const name = propertyName(property);
|
|
249
|
+
if (name === null) {
|
|
250
|
+
throw new Error("Translation extraction requires static option property names");
|
|
251
|
+
}
|
|
252
|
+
if (UNSUPPORTED_PLURAL_OPTION.test(name)) {
|
|
253
|
+
throw new Error("GNU gettext accepts one singular and one plural source string");
|
|
254
|
+
}
|
|
255
|
+
if (STATIC_SOURCE_OPTION_NAMES.has(name)) {
|
|
256
|
+
assertStaticTranslationKey(property.value);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
const assertSupportedOptionObject = (options) => {
|
|
260
|
+
if (options === null || options === undefined) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (isStaticKeyWrapper(options) && unwrapStaticWrapper(options).type === "ObjectExpression") {
|
|
264
|
+
throw new Error("Translation option objects cannot be wrapped in another expression");
|
|
265
|
+
}
|
|
266
|
+
if (options.type !== "ObjectExpression") {
|
|
267
|
+
throw new Error("Translation option objects must be inline object literals");
|
|
268
|
+
}
|
|
269
|
+
for (const property of options.properties) {
|
|
270
|
+
assertSupportedOptionProperty(property);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
const namedImportedBinding = (path, source) => {
|
|
274
|
+
if (!path.isImportSpecifier() || path.node.importKind === "type") {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const imported = path.node.imported;
|
|
278
|
+
return {
|
|
279
|
+
imported: types.isIdentifier(imported) ? imported.name : imported.value,
|
|
280
|
+
local: path.node.local.name,
|
|
281
|
+
source,
|
|
282
|
+
style: "named",
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
const defaultImportedBinding = (path, source) => path.isImportDefaultSpecifier()
|
|
286
|
+
? { imported: "default", local: path.node.local.name, source, style: "default" }
|
|
287
|
+
: null;
|
|
288
|
+
const namespaceImportedBinding = (path, source) => path.isImportNamespaceSpecifier()
|
|
289
|
+
? { imported: "*", local: path.node.local.name, source, style: "namespace" }
|
|
290
|
+
: null;
|
|
291
|
+
const importedBinding = (binding) => {
|
|
292
|
+
if (binding === undefined) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
const path = binding.path;
|
|
296
|
+
const declaration = path.parentPath;
|
|
297
|
+
if (!declaration.isImportDeclaration() || declaration.node.importKind === "type") {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const source = declaration.node.source.value;
|
|
301
|
+
return namedImportedBinding(path, source) ??
|
|
302
|
+
defaultImportedBinding(path, source) ??
|
|
303
|
+
namespaceImportedBinding(path, source);
|
|
304
|
+
};
|
|
305
|
+
const babelPropertyName = (property) => {
|
|
306
|
+
const key = property.key;
|
|
307
|
+
if (!property.computed && types.isIdentifier(key)) {
|
|
308
|
+
return key.name;
|
|
309
|
+
}
|
|
310
|
+
return types.isStringLiteral(key) ? key.value : null;
|
|
311
|
+
};
|
|
312
|
+
const isBoundIdentifier = (node, name) => {
|
|
313
|
+
if (types.isIdentifier(node)) {
|
|
314
|
+
return node.name === name;
|
|
315
|
+
}
|
|
316
|
+
if (types.isAssignmentPattern(node)) {
|
|
317
|
+
return types.isIdentifier(node.left) && node.left.name === name;
|
|
318
|
+
}
|
|
319
|
+
return false;
|
|
320
|
+
};
|
|
321
|
+
const isHookPatternBinding = (pattern, name) => {
|
|
322
|
+
if (types.isObjectPattern(pattern)) {
|
|
323
|
+
return pattern.properties.some((property) => types.isObjectProperty(property) &&
|
|
324
|
+
babelPropertyName(property) === "t" &&
|
|
325
|
+
isBoundIdentifier(property.value, name));
|
|
326
|
+
}
|
|
327
|
+
if (types.isArrayPattern(pattern)) {
|
|
328
|
+
return isBoundIdentifier(pattern.elements[0] ?? null, name);
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
};
|
|
332
|
+
const importedTranslationHook = (scope, call) => {
|
|
333
|
+
if (!types.isIdentifier(call.callee)) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
const imported = importedBinding(scope.getBinding(call.callee.name));
|
|
337
|
+
return imported?.imported === "useTranslation" && TRANSLATION_MODULES.has(imported.source)
|
|
338
|
+
? imported
|
|
339
|
+
: null;
|
|
340
|
+
};
|
|
341
|
+
const translationHookBinding = (binding) => {
|
|
342
|
+
const path = binding.path;
|
|
343
|
+
if (!path.isVariableDeclarator()) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
const pattern = path.node.id;
|
|
347
|
+
const initializer = path.node.init;
|
|
348
|
+
if (!isHookPatternBinding(pattern, binding.identifier.name) ||
|
|
349
|
+
!types.isCallExpression(initializer)) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const imported = importedTranslationHook(path.scope, initializer);
|
|
353
|
+
if (imported === null) {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
return { call: initializer, isCanonical: imported.local === "useTranslation" };
|
|
357
|
+
};
|
|
358
|
+
const importedTranslationFunctionKind = (imported) => {
|
|
359
|
+
if (imported?.imported !== "t" || !TRANSLATION_MODULES.has(imported.source)) {
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
return imported.local === "t" ? "canonical" : "alias";
|
|
363
|
+
};
|
|
364
|
+
const hookTranslationFunctionKind = (binding) => {
|
|
365
|
+
if (binding === undefined) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
const hook = translationHookBinding(binding);
|
|
369
|
+
if (hook === null) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
return binding.identifier.name === "t" && hook.isCanonical ? "canonical" : "alias";
|
|
373
|
+
};
|
|
374
|
+
const directTranslationFunctionKind = (scope, name) => {
|
|
375
|
+
const binding = scope.getBinding(name);
|
|
376
|
+
return importedTranslationFunctionKind(importedBinding(binding)) ?? hookTranslationFunctionKind(binding);
|
|
377
|
+
};
|
|
378
|
+
const babelMemberName = (member) => {
|
|
379
|
+
const property = member.property;
|
|
380
|
+
if (member.computed) {
|
|
381
|
+
return types.isStringLiteral(property) ? property.value : null;
|
|
382
|
+
}
|
|
383
|
+
return types.isIdentifier(property) ? property.name : null;
|
|
384
|
+
};
|
|
385
|
+
const isImportedTranslationObject = (scope, object) => {
|
|
386
|
+
if (!types.isIdentifier(object)) {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
const imported = importedBinding(scope.getBinding(object.name));
|
|
390
|
+
return imported !== null &&
|
|
391
|
+
TRANSLATION_MODULES.has(imported.source) &&
|
|
392
|
+
(imported.style === "namespace" || (imported.source === "i18next" && imported.imported === "default"));
|
|
393
|
+
};
|
|
394
|
+
const constantVariableDeclarator = (binding) => {
|
|
395
|
+
const path = binding.path;
|
|
396
|
+
const declaration = path.parentPath;
|
|
397
|
+
if (!path.isVariableDeclarator() ||
|
|
398
|
+
!declaration.isVariableDeclaration() ||
|
|
399
|
+
declaration.node.kind !== "const") {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
return path;
|
|
403
|
+
};
|
|
404
|
+
const isTranslationHookResultBinding = (binding) => {
|
|
405
|
+
if (binding === undefined) {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
const path = constantVariableDeclarator(binding);
|
|
409
|
+
const initializer = path?.node.init;
|
|
410
|
+
return path !== null &&
|
|
411
|
+
types.isIdentifier(path.node.id) &&
|
|
412
|
+
types.isCallExpression(initializer) &&
|
|
413
|
+
importedTranslationHook(path.scope, initializer) !== null;
|
|
414
|
+
};
|
|
415
|
+
const isHookTranslationMember = (scope, member) => {
|
|
416
|
+
if (babelMemberName(member) !== "t") {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
const object = member.object;
|
|
420
|
+
if (types.isCallExpression(object)) {
|
|
421
|
+
return importedTranslationHook(scope, object) !== null;
|
|
422
|
+
}
|
|
423
|
+
return types.isIdentifier(object) && isTranslationHookResultBinding(scope.getBinding(object.name));
|
|
424
|
+
};
|
|
425
|
+
const isTranslationMember = (scope, member) => babelMemberName(member) === "t" &&
|
|
426
|
+
(isImportedTranslationObject(scope, member.object) || isHookTranslationMember(scope, member));
|
|
427
|
+
const isDirectTranslationFunction = (scope, node) => types.isIdentifier(node) && directTranslationFunctionKind(scope, node.name) !== null;
|
|
428
|
+
const isTranslationFunctionValue = (scope, node) => {
|
|
429
|
+
if (isDirectTranslationFunction(scope, node)) {
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
|
|
433
|
+
isTranslationMember(scope, node);
|
|
434
|
+
};
|
|
435
|
+
const isTranslationBindCall = (scope, node) => {
|
|
436
|
+
if (!types.isCallExpression(node) || !types.isMemberExpression(node.callee)) {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
return babelMemberName(node.callee) === "bind" &&
|
|
440
|
+
isTranslationFunctionValue(scope, node.callee.object);
|
|
441
|
+
};
|
|
442
|
+
const isTranslationObjectPatternAlias = (path, binding) => {
|
|
443
|
+
const pattern = path.node.id;
|
|
444
|
+
const initializer = path.node.init;
|
|
445
|
+
if (!types.isObjectPattern(pattern) ||
|
|
446
|
+
!isHookPatternBinding(pattern, binding.identifier.name) ||
|
|
447
|
+
!types.isIdentifier(initializer)) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
return isImportedTranslationObject(path.scope, initializer) ||
|
|
451
|
+
isTranslationHookResultBinding(path.scope.getBinding(initializer.name));
|
|
452
|
+
};
|
|
453
|
+
const isTranslationAliasBinding = (binding) => {
|
|
454
|
+
if (binding === undefined) {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
const path = constantVariableDeclarator(binding);
|
|
458
|
+
if (path === null) {
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
if (isTranslationObjectPatternAlias(path, binding)) {
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
const initializer = path.node.init;
|
|
465
|
+
return types.isIdentifier(path.node.id) &&
|
|
466
|
+
initializer !== null &&
|
|
467
|
+
initializer !== undefined &&
|
|
468
|
+
(isTranslationFunctionValue(path.scope, initializer) || isTranslationBindCall(path.scope, initializer));
|
|
469
|
+
};
|
|
470
|
+
const translationFunctionKind = (scope, name) => {
|
|
471
|
+
const direct = directTranslationFunctionKind(scope, name);
|
|
472
|
+
if (direct !== null) {
|
|
473
|
+
return direct;
|
|
474
|
+
}
|
|
475
|
+
return isTranslationAliasBinding(scope.getBinding(name)) ? "alias" : null;
|
|
476
|
+
};
|
|
477
|
+
const translationElementKind = (path, name) => {
|
|
478
|
+
const imported = importedBinding(path.scope.getBinding(name));
|
|
479
|
+
if (imported?.source !== "@gtkx/i18n" || !TRANS_ELEMENTS.has(imported.imported)) {
|
|
480
|
+
return null;
|
|
481
|
+
}
|
|
482
|
+
return imported.local === imported.imported ? "canonical" : "alias";
|
|
483
|
+
};
|
|
484
|
+
const nodeStart = (path) => {
|
|
485
|
+
const start = path.node.start;
|
|
486
|
+
if (start === null || start === undefined) {
|
|
487
|
+
throw new Error("Translation extraction could not locate a source expression");
|
|
488
|
+
}
|
|
489
|
+
return start;
|
|
490
|
+
};
|
|
491
|
+
const registerSourceMask = (node, references) => {
|
|
492
|
+
const { start, end } = node;
|
|
493
|
+
if (start === null || start === undefined || end === null || end === undefined) {
|
|
494
|
+
throw new Error("Translation extraction could not locate a source expression");
|
|
495
|
+
}
|
|
496
|
+
references.masks.push({ start, end });
|
|
497
|
+
};
|
|
498
|
+
const sourceLocationKey = (line, column) => `${String(line)}:${String(column)}`;
|
|
499
|
+
const sourcePoint = (node) => {
|
|
500
|
+
const location = node.loc?.start;
|
|
501
|
+
return location === undefined ? null : { column: location.column, line: location.line };
|
|
502
|
+
};
|
|
503
|
+
const registerSourceLocation = (node, references) => {
|
|
504
|
+
const point = sourcePoint(node);
|
|
505
|
+
if (point !== null) {
|
|
506
|
+
references.locations.add(sourceLocationKey(point.line, point.column));
|
|
507
|
+
}
|
|
508
|
+
return point;
|
|
509
|
+
};
|
|
510
|
+
const babelStaticKey = (node) => {
|
|
511
|
+
if (node === null || node === undefined) {
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
if (types.isStringLiteral(node)) {
|
|
515
|
+
return node.value;
|
|
516
|
+
}
|
|
517
|
+
if (types.isTemplateLiteral(node) && node.expressions.length === 0) {
|
|
518
|
+
return node.quasis[0]?.value.cooked ?? null;
|
|
519
|
+
}
|
|
520
|
+
if (types.isParenthesizedExpression(node) ||
|
|
521
|
+
types.isTSAsExpression(node) ||
|
|
522
|
+
types.isTSSatisfiesExpression(node) ||
|
|
523
|
+
types.isTSTypeAssertion(node) ||
|
|
524
|
+
types.isTSNonNullExpression(node)) {
|
|
525
|
+
return babelStaticKey(node.expression);
|
|
526
|
+
}
|
|
527
|
+
return null;
|
|
528
|
+
};
|
|
529
|
+
const babelObjectExpression = (node) => {
|
|
530
|
+
if (node === null || node === undefined) {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
if (types.isObjectExpression(node)) {
|
|
534
|
+
return node;
|
|
535
|
+
}
|
|
536
|
+
if (types.isParenthesizedExpression(node) ||
|
|
537
|
+
types.isTSAsExpression(node) ||
|
|
538
|
+
types.isTSSatisfiesExpression(node) ||
|
|
539
|
+
types.isTSTypeAssertion(node) ||
|
|
540
|
+
types.isTSNonNullExpression(node)) {
|
|
541
|
+
return babelObjectExpression(node.expression);
|
|
542
|
+
}
|
|
543
|
+
return null;
|
|
544
|
+
};
|
|
545
|
+
const babelNamedProperty = (object, name) => {
|
|
546
|
+
if (object === null) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
for (const property of object.properties) {
|
|
550
|
+
if (types.isObjectProperty(property) && babelPropertyName(property) === name) {
|
|
551
|
+
return property;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return null;
|
|
555
|
+
};
|
|
556
|
+
const babelStringProperty = (object, name) => {
|
|
557
|
+
const property = babelNamedProperty(object, name);
|
|
558
|
+
if (property === null) {
|
|
559
|
+
return null;
|
|
560
|
+
}
|
|
561
|
+
const value = babelStaticKey(property.value);
|
|
562
|
+
if (value === null) {
|
|
563
|
+
throw new Error(`Translation ${name} values must be string literals`);
|
|
564
|
+
}
|
|
565
|
+
return value;
|
|
566
|
+
};
|
|
567
|
+
const callOptions = (call) => babelObjectExpression(call.arguments[1]) ?? babelObjectExpression(call.arguments[2]);
|
|
568
|
+
const hookKeyPrefix = (callee) => {
|
|
569
|
+
const binding = callee.scope.getBinding(callee.node.name);
|
|
570
|
+
const hook = binding === undefined ? null : translationHookBinding(binding);
|
|
571
|
+
return babelStringProperty(hook === null ? null : babelObjectExpression(hook.call.arguments[1]), "keyPrefix") ?? "";
|
|
572
|
+
};
|
|
573
|
+
const prefixedTranslationKey = (prefix, key) => {
|
|
574
|
+
if (prefix.length === 0) {
|
|
575
|
+
return key;
|
|
576
|
+
}
|
|
577
|
+
return prefix.endsWith(".") ? `${prefix}${key}` : `${prefix}.${key}`;
|
|
578
|
+
};
|
|
579
|
+
const extractedEntryKey = (call, keyPrefix) => {
|
|
580
|
+
const key = babelStaticKey(call.arguments[0]);
|
|
581
|
+
if (key === null) {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
const options = callOptions(call);
|
|
585
|
+
const callPrefix = babelStringProperty(options, "keyPrefix");
|
|
586
|
+
if (callPrefix === "" && keyPrefix.length > 0) {
|
|
587
|
+
throw new Error("An empty call keyPrefix cannot override a hook keyPrefix during extraction");
|
|
588
|
+
}
|
|
589
|
+
const selectedPrefix = callPrefix ?? keyPrefix;
|
|
590
|
+
const prefixed = prefixedTranslationKey(selectedPrefix, key);
|
|
591
|
+
const context = babelStringProperty(options, "context");
|
|
592
|
+
return context === null ? prefixed : `${prefixed}${CONTEXT_SEPARATOR}${context}`;
|
|
593
|
+
};
|
|
594
|
+
const registerEntryLocation = (key, point, references) => {
|
|
595
|
+
if (key === null || point === null) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
const locations = references.entryLocations.get(key) ?? [];
|
|
599
|
+
locations.push(point);
|
|
600
|
+
references.entryLocations.set(key, locations);
|
|
601
|
+
};
|
|
602
|
+
const registerCallReference = (callPath, references, keyPrefix) => {
|
|
603
|
+
references.calls.add(nodeStart(callPath));
|
|
604
|
+
const firstArgument = callPath.node.arguments[0];
|
|
605
|
+
const point = registerSourceLocation(firstArgument ?? callPath.node, references);
|
|
606
|
+
registerEntryLocation(extractedEntryKey(callPath.node, keyPrefix), point, references);
|
|
607
|
+
};
|
|
608
|
+
const parserPlugins = (lang) => [
|
|
609
|
+
"decorators",
|
|
610
|
+
...(lang === "ts" || lang === "tsx" ? ["typescript"] : []),
|
|
611
|
+
...(lang === "jsx" || lang === "tsx" ? ["jsx"] : []),
|
|
612
|
+
];
|
|
613
|
+
const registerIdentifierTranslationCall = (callPath, callee, references) => {
|
|
614
|
+
if (!callee.isIdentifier()) {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const kind = translationFunctionKind(callee.scope, callee.node.name);
|
|
618
|
+
if (kind === "alias") {
|
|
619
|
+
throw new Error("Translation calls must use the imported name t");
|
|
620
|
+
}
|
|
621
|
+
if (kind === "canonical") {
|
|
622
|
+
registerCallReference(callPath, references, hookKeyPrefix(callee));
|
|
623
|
+
}
|
|
624
|
+
else if (callee.node.name === "t") {
|
|
625
|
+
registerSourceMask(callee.node, references);
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
const registerTranslationCall = (callPath, references) => {
|
|
629
|
+
assertSupportedWrappedCallee(callPath);
|
|
630
|
+
const callee = callPath.get("callee");
|
|
631
|
+
if (callee.isIdentifier()) {
|
|
632
|
+
registerIdentifierTranslationCall(callPath, callee, references);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
if (callee.isMemberExpression() && isTranslationMember(callee.scope, callee.node)) {
|
|
636
|
+
throw new Error("Translation calls must use the named t export");
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
const unwrapTranslationCallee = (node) => {
|
|
640
|
+
if (types.isParenthesizedExpression(node) ||
|
|
641
|
+
types.isTSAsExpression(node) ||
|
|
642
|
+
types.isTSSatisfiesExpression(node) ||
|
|
643
|
+
types.isTSTypeAssertion(node) ||
|
|
644
|
+
types.isTSNonNullExpression(node)) {
|
|
645
|
+
return unwrapTranslationCallee(node.expression);
|
|
646
|
+
}
|
|
647
|
+
return node;
|
|
648
|
+
};
|
|
649
|
+
const isTranslationCallee = (scope, node) => {
|
|
650
|
+
if (types.isIdentifier(node)) {
|
|
651
|
+
return translationFunctionKind(scope, node.name) !== null;
|
|
652
|
+
}
|
|
653
|
+
return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
|
|
654
|
+
isTranslationMember(scope, node);
|
|
655
|
+
};
|
|
656
|
+
const assertSupportedWrappedCallee = (callPath) => {
|
|
657
|
+
const callee = callPath.node.callee;
|
|
658
|
+
const unwrapped = unwrapTranslationCallee(callee);
|
|
659
|
+
if (unwrapped !== callee && isTranslationCallee(callPath.scope, unwrapped)) {
|
|
660
|
+
throw new Error("Translation call expressions cannot wrap their callee");
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
const assertSupportedOptionalCall = (callPath) => {
|
|
664
|
+
const callee = unwrapTranslationCallee(callPath.node.callee);
|
|
665
|
+
if (isTranslationCallee(callPath.scope, callee)) {
|
|
666
|
+
throw new Error("Translation calls cannot use optional chaining");
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
const assertUnsupportedTranslationTag = (tagPath) => {
|
|
670
|
+
const tag = unwrapTranslationCallee(tagPath.node.tag);
|
|
671
|
+
if (isTranslationCallee(tagPath.scope, tag)) {
|
|
672
|
+
throw new Error("Translation calls cannot use tagged-template syntax");
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
const registerUnrecognizedElementMasks = (elementPath, name, references) => {
|
|
676
|
+
registerSourceMask(name.node, references);
|
|
677
|
+
const parent = elementPath.parentPath.node;
|
|
678
|
+
const closingName = types.isJSXElement(parent) ? parent.closingElement?.name : null;
|
|
679
|
+
if (types.isJSXIdentifier(closingName)) {
|
|
680
|
+
registerSourceMask(closingName, references);
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
const registerTranslationElement = (elementPath, references) => {
|
|
684
|
+
const name = elementPath.get("name");
|
|
685
|
+
if (!name.isJSXIdentifier()) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const kind = translationElementKind(name, name.node.name);
|
|
689
|
+
if (kind === "alias") {
|
|
690
|
+
throw new Error("Translation components must use their imported names");
|
|
691
|
+
}
|
|
692
|
+
if (kind === "canonical") {
|
|
693
|
+
references.elements.add(nodeStart(elementPath));
|
|
694
|
+
registerSourceLocation(elementPath.node, references);
|
|
695
|
+
}
|
|
696
|
+
else if (TRANS_ELEMENTS.has(name.node.name)) {
|
|
697
|
+
registerUnrecognizedElementMasks(elementPath, name, references);
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
const maskUnrecognizedTranslations = (source, references) => {
|
|
701
|
+
let masked = source;
|
|
702
|
+
const masks = references.masks.toSorted((left, right) => right.start - left.start);
|
|
703
|
+
for (const { start, end } of masks) {
|
|
704
|
+
masked = `${masked.slice(0, start)}${"_".repeat(end - start)}${masked.slice(end)}`;
|
|
705
|
+
}
|
|
706
|
+
return masked;
|
|
707
|
+
};
|
|
708
|
+
const translationReferences = (path, source, lang) => {
|
|
709
|
+
const parsed = parseBabelSync(source, {
|
|
710
|
+
ast: true,
|
|
711
|
+
babelrc: false,
|
|
712
|
+
configFile: false,
|
|
713
|
+
filename: path,
|
|
714
|
+
parserOpts: { createParenthesizedExpressions: true, plugins: parserPlugins(lang) },
|
|
715
|
+
sourceType: "module",
|
|
716
|
+
});
|
|
717
|
+
if (parsed === null) {
|
|
718
|
+
throw new Error(`Translation extraction could not parse ${path}`);
|
|
719
|
+
}
|
|
720
|
+
const references = {
|
|
721
|
+
calls: new Set(),
|
|
722
|
+
elements: new Set(),
|
|
723
|
+
entryLocations: new Map(),
|
|
724
|
+
locations: new Set(),
|
|
725
|
+
masks: [],
|
|
726
|
+
};
|
|
727
|
+
traverse(parsed, {
|
|
728
|
+
CallExpression(callPath) {
|
|
729
|
+
registerTranslationCall(callPath, references);
|
|
730
|
+
},
|
|
731
|
+
JSXOpeningElement(elementPath) {
|
|
732
|
+
registerTranslationElement(elementPath, references);
|
|
733
|
+
},
|
|
734
|
+
OptionalCallExpression(callPath) {
|
|
735
|
+
assertSupportedOptionalCall(callPath);
|
|
736
|
+
},
|
|
737
|
+
TaggedTemplateExpression(tagPath) {
|
|
738
|
+
assertUnsupportedTranslationTag(tagPath);
|
|
739
|
+
},
|
|
740
|
+
});
|
|
741
|
+
return references;
|
|
742
|
+
};
|
|
743
|
+
const assertSupportedTranslationCall = (node, references) => {
|
|
744
|
+
if (!references.calls.has(node.start)) {
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
assertStaticTranslationKey(node.arguments[0]);
|
|
748
|
+
assertSupportedTranslationOptions(node.arguments[1], node.arguments[2]);
|
|
749
|
+
};
|
|
750
|
+
const assertSupportedTranslationOptions = (second, third) => {
|
|
751
|
+
if (third !== undefined && third !== null) {
|
|
752
|
+
assertStaticTranslationKey(second);
|
|
753
|
+
assertSupportedOptionObject(third);
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
if (second === undefined || second === null || isStaticTranslationKey(second)) {
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
assertSupportedOptionObject(second);
|
|
760
|
+
};
|
|
761
|
+
const isNamedJsxAttribute = (attribute, name) => attribute.name.type === "JSXIdentifier" && attribute.name.name === name;
|
|
762
|
+
const assertSupportedTransKey = (attribute) => {
|
|
763
|
+
if (!isNamedJsxAttribute(attribute, "i18nKey")) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
const key = attribute.value?.type === "JSXExpressionContainer"
|
|
767
|
+
? attribute.value.expression
|
|
768
|
+
: attribute.value;
|
|
769
|
+
assertStaticTranslationKey(key);
|
|
770
|
+
};
|
|
771
|
+
const assertSupportedTransStringAttribute = (attribute, name) => {
|
|
772
|
+
if (!isNamedJsxAttribute(attribute, name)) {
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
const value = attribute.value?.type === "JSXExpressionContainer"
|
|
776
|
+
? attribute.value.expression
|
|
777
|
+
: attribute.value;
|
|
778
|
+
assertStaticTranslationKey(value);
|
|
779
|
+
};
|
|
780
|
+
const assertSupportedTransOptions = (attribute) => {
|
|
781
|
+
if (isNamedJsxAttribute(attribute, "tOptions") && attribute.value?.type === "JSXExpressionContainer") {
|
|
782
|
+
assertSupportedOptionObject(attribute.value.expression);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
const assertSupportedTransAttribute = (attribute) => {
|
|
786
|
+
if (attribute.type === "JSXSpreadAttribute") {
|
|
787
|
+
throw new Error("Translation extraction requires explicit component properties");
|
|
788
|
+
}
|
|
789
|
+
assertSupportedTransKey(attribute);
|
|
790
|
+
assertSupportedTransStringAttribute(attribute, "context");
|
|
791
|
+
assertSupportedTransStringAttribute(attribute, "defaults");
|
|
792
|
+
assertSupportedTransOptions(attribute);
|
|
793
|
+
};
|
|
794
|
+
const assertSupportedTransElement = (node, references) => {
|
|
795
|
+
if (!references.elements.has(node.start)) {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
for (const attribute of node.attributes) {
|
|
799
|
+
assertSupportedTransAttribute(attribute);
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
const assertSupportedTranslationSyntax = (path, source) => {
|
|
803
|
+
const lang = sourceLanguage(path);
|
|
804
|
+
if (lang === undefined) {
|
|
805
|
+
return { calls: new Set(), elements: new Set(), entryLocations: new Map(), locations: new Set(), masks: [] };
|
|
806
|
+
}
|
|
807
|
+
const parsed = parseSync(path, source, { lang });
|
|
808
|
+
const references = translationReferences(path, source, lang);
|
|
809
|
+
new Visitor({
|
|
810
|
+
CallExpression(node) {
|
|
811
|
+
assertSupportedTranslationCall(node, references);
|
|
812
|
+
},
|
|
813
|
+
JSXOpeningElement(node) {
|
|
814
|
+
assertSupportedTransElement(node, references);
|
|
815
|
+
},
|
|
816
|
+
}).visit(parsed.program);
|
|
817
|
+
return references;
|
|
818
|
+
};
|
|
819
|
+
const extractedLocationFile = (root, file) => resolve(root, file);
|
|
820
|
+
const isRecognizedLocation = (root, references, location) => {
|
|
821
|
+
if (location.line === undefined || location.column === undefined) {
|
|
822
|
+
return false;
|
|
823
|
+
}
|
|
824
|
+
const sourceReferences = references.get(extractedLocationFile(root, location.file));
|
|
825
|
+
return sourceReferences?.locations.has(sourceLocationKey(location.line, location.column)) === true;
|
|
826
|
+
};
|
|
827
|
+
const recognizedKeyLocations = (references, extractedKey) => {
|
|
828
|
+
const locations = [];
|
|
829
|
+
for (const [file, sourceReferences] of references) {
|
|
830
|
+
const points = sourceReferences.entryLocations.get(extractedKey) ?? [];
|
|
831
|
+
for (const point of points) {
|
|
832
|
+
locations.push({ file, column: point.column, line: point.line });
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return locations;
|
|
836
|
+
};
|
|
837
|
+
const recognizedEntry = (root, references, entry) => {
|
|
838
|
+
const extractedLocations = entry.locations ?? [];
|
|
839
|
+
const locations = extractedLocations.length === 0
|
|
840
|
+
? recognizedKeyLocations(references, entry.key)
|
|
841
|
+
: extractedLocations.filter((location) => isRecognizedLocation(root, references, location));
|
|
842
|
+
return locations.length === 0 ? null : { ...entry, locations };
|
|
843
|
+
};
|
|
844
|
+
const quotedNestedReferenceParts = (content, quote) => {
|
|
845
|
+
const end = content.indexOf(quote, 1);
|
|
846
|
+
return end === -1
|
|
847
|
+
? null
|
|
848
|
+
: { base: content.slice(1, end), options: content.slice(end + 1) };
|
|
849
|
+
};
|
|
850
|
+
const nestedReferenceParts = (value) => {
|
|
851
|
+
const content = value.trim();
|
|
852
|
+
const quote = content[0];
|
|
853
|
+
if (quote === "\"" || quote === "'") {
|
|
854
|
+
return quotedNestedReferenceParts(content, quote);
|
|
855
|
+
}
|
|
856
|
+
const separator = content.indexOf(",");
|
|
857
|
+
const base = (separator === -1 ? content : content.slice(0, separator)).trim();
|
|
858
|
+
return base.length === 0
|
|
859
|
+
? null
|
|
860
|
+
: { base, options: separator === -1 ? "" : content.slice(separator + 1) };
|
|
861
|
+
};
|
|
862
|
+
const nestedReferenceContext = (options) => {
|
|
863
|
+
const match = NESTED_CONTEXT.exec(options);
|
|
864
|
+
const context = match?.[1] ?? match?.[2] ?? null;
|
|
865
|
+
return context === null || context.length === 0 ? null : context;
|
|
866
|
+
};
|
|
867
|
+
const adjustedParenthesisDepth = (character, depth) => {
|
|
868
|
+
if (character === "(") {
|
|
869
|
+
return depth + 1;
|
|
870
|
+
}
|
|
871
|
+
return character === ")" ? depth - 1 : depth;
|
|
872
|
+
};
|
|
873
|
+
const nestedReferenceEnd = (text, start) => {
|
|
874
|
+
let depth = 0;
|
|
875
|
+
const tokens = text.slice(start).matchAll(NESTED_STRUCTURE_TOKEN);
|
|
876
|
+
for (const token of tokens) {
|
|
877
|
+
const character = token[0];
|
|
878
|
+
if (character === ")" && depth === 0) {
|
|
879
|
+
return start + token.index;
|
|
880
|
+
}
|
|
881
|
+
depth = adjustedParenthesisDepth(character, depth);
|
|
882
|
+
}
|
|
883
|
+
return -1;
|
|
884
|
+
};
|
|
885
|
+
const nestedReferenceContents = (text) => {
|
|
886
|
+
const contents = [];
|
|
887
|
+
let offset = 0;
|
|
888
|
+
while (offset < text.length) {
|
|
889
|
+
const prefix = text.indexOf("$t(", offset);
|
|
890
|
+
if (prefix === -1) {
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
const start = prefix + 3;
|
|
894
|
+
const end = nestedReferenceEnd(text, start);
|
|
895
|
+
if (end === -1) {
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
898
|
+
contents.push(text.slice(start, end));
|
|
899
|
+
offset = end + 1;
|
|
900
|
+
}
|
|
901
|
+
return contents;
|
|
902
|
+
};
|
|
903
|
+
const nestedPluralKeys = (base, context) => {
|
|
904
|
+
const pluralBase = context === null ? base : `${base}${CONTEXT_SEPARATOR}${context}`;
|
|
905
|
+
return [`${pluralBase}_one`, `${pluralBase}_other`];
|
|
906
|
+
};
|
|
907
|
+
const nestedReferenceKeys = (content) => {
|
|
908
|
+
const parts = nestedReferenceParts(content);
|
|
909
|
+
if (parts === null) {
|
|
910
|
+
return [];
|
|
911
|
+
}
|
|
912
|
+
const context = nestedReferenceContext(parts.options);
|
|
913
|
+
const contextual = context === null
|
|
914
|
+
? []
|
|
915
|
+
: [`${parts.base}_${context}`, `${parts.base}${CONTEXT_SEPARATOR}${context}`];
|
|
916
|
+
return NESTED_COUNT.test(parts.options)
|
|
917
|
+
? [parts.base, ...contextual, ...nestedPluralKeys(parts.base, context)]
|
|
918
|
+
: [parts.base, ...contextual];
|
|
919
|
+
};
|
|
920
|
+
const nestedEntryKeys = (text) => {
|
|
921
|
+
const keys = new Set();
|
|
922
|
+
for (const content of nestedReferenceContents(text)) {
|
|
923
|
+
for (const key of nestedReferenceKeys(content)) {
|
|
924
|
+
keys.add(key);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
return keys;
|
|
928
|
+
};
|
|
929
|
+
const registerNestedKeyLocations = (root, references, key, locations) => {
|
|
930
|
+
for (const location of locations) {
|
|
931
|
+
if (location.line === undefined || location.column === undefined) {
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
const sourceReferences = references.get(resolve(root, location.file));
|
|
935
|
+
if (sourceReferences !== undefined) {
|
|
936
|
+
registerEntryLocation(key, { column: location.column, line: location.line }, sourceReferences);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
const registerNestedTextLocations = (root, references, text, locations) => {
|
|
941
|
+
for (const key of nestedEntryKeys(text)) {
|
|
942
|
+
registerNestedKeyLocations(root, references, key, locations);
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
const registerNestedEntryLocations = (root, references, entry, locations) => {
|
|
946
|
+
registerNestedTextLocations(root, references, entry.key, locations);
|
|
947
|
+
if (typeof entry.defaultValue === "string") {
|
|
948
|
+
registerNestedTextLocations(root, references, entry.defaultValue, locations);
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
const didRegisterNestedLocationPass = (root, references, entries, processed) => {
|
|
952
|
+
let didRegister = false;
|
|
953
|
+
for (const [mapKey, entry] of entries) {
|
|
954
|
+
if (processed.has(mapKey)) {
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
const recognized = recognizedEntry(root, references, entry);
|
|
958
|
+
if (recognized !== null) {
|
|
959
|
+
processed.add(mapKey);
|
|
960
|
+
registerNestedEntryLocations(root, references, entry, recognized.locations ?? []);
|
|
961
|
+
didRegister = true;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return didRegister;
|
|
965
|
+
};
|
|
966
|
+
const registerNestedLocations = (root, references, entries) => {
|
|
967
|
+
const processed = new Set();
|
|
968
|
+
let hasRegisteredLocations = true;
|
|
969
|
+
while (hasRegisteredLocations) {
|
|
970
|
+
hasRegisteredLocations = didRegisterNestedLocationPass(root, references, entries, processed);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
const retainRecognizedEntries = (root, references, entries) => {
|
|
974
|
+
for (const [mapKey, entry] of entries) {
|
|
975
|
+
const recognized = recognizedEntry(root, references, entry);
|
|
976
|
+
if (recognized === null) {
|
|
977
|
+
entries.delete(mapKey);
|
|
978
|
+
}
|
|
979
|
+
else {
|
|
980
|
+
entries.set(mapKey, recognized);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
const fileSourcePointKey = (file, line, column) => `${file}\0${sourceLocationKey(line, column)}`;
|
|
985
|
+
const registerExtractedSourcePoint = (root, points, location) => {
|
|
986
|
+
if (location.line !== undefined && location.column !== undefined) {
|
|
987
|
+
points.add(fileSourcePointKey(resolve(root, location.file), location.line, location.column));
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
const extractedSourcePoints = (root, entries) => {
|
|
991
|
+
const points = new Set();
|
|
992
|
+
for (const entry of entries.values()) {
|
|
993
|
+
const locations = entry.locations ?? [];
|
|
994
|
+
for (const location of locations) {
|
|
995
|
+
registerExtractedSourcePoint(root, points, location);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
return points;
|
|
999
|
+
};
|
|
1000
|
+
const hasMissingSourcePoint = (file, references, extracted) => references.locations.values().some((location) => !extracted.has(`${file}\0${location}`));
|
|
1001
|
+
const assertAllReferencesExtracted = (root, references, entries) => {
|
|
1002
|
+
const extracted = extractedSourcePoints(root, entries);
|
|
1003
|
+
for (const [file, sourceReferences] of references) {
|
|
1004
|
+
if (hasMissingSourcePoint(file, sourceReferences, extracted)) {
|
|
1005
|
+
throw new Error("A translation expression could not be extracted safely");
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
188
1009
|
const findSourceMessages = async (root, sourceFiles) => {
|
|
189
1010
|
if (sourceFiles.length === 0) {
|
|
190
1011
|
clearI18nResources(root);
|
|
191
1012
|
return [];
|
|
192
1013
|
}
|
|
1014
|
+
const sources = new Map(sourceFiles.map((file) => [file, readFileSync(file, "utf8")]));
|
|
1015
|
+
const references = new Map();
|
|
1016
|
+
for (const [file, source] of sources) {
|
|
1017
|
+
references.set(file, assertSupportedTranslationSyntax(file, source));
|
|
1018
|
+
}
|
|
193
1019
|
let extracted;
|
|
194
1020
|
const { logger, reports } = quietLogger();
|
|
195
1021
|
const capture = {
|
|
196
1022
|
name: "gtkx-gettext",
|
|
1023
|
+
onLoad(code, path) {
|
|
1024
|
+
const sourceReferences = references.get(resolve(root, path));
|
|
1025
|
+
return sourceReferences === undefined
|
|
1026
|
+
? code
|
|
1027
|
+
: maskUnrecognizedTranslations(code, sourceReferences);
|
|
1028
|
+
},
|
|
197
1029
|
onEnd(keys) {
|
|
1030
|
+
registerNestedLocations(root, references, keys);
|
|
1031
|
+
retainRecognizedEntries(root, references, keys);
|
|
1032
|
+
assertAllReferencesExtracted(root, references, keys);
|
|
198
1033
|
extracted = keys;
|
|
199
1034
|
},
|
|
200
1035
|
};
|
|
@@ -207,7 +1042,6 @@ const findSourceMessages = async (root, sourceFiles) => {
|
|
|
207
1042
|
if (extracted === undefined || reports.length > 0 || result.hasErrors) {
|
|
208
1043
|
throw new Error(reports.join("\n") || "i18next extraction failed");
|
|
209
1044
|
}
|
|
210
|
-
const sources = new Map(sourceFiles.map((file) => [file, readFileSync(file, "utf8")]));
|
|
211
1045
|
return sourceMessages(extracted.values().toArray())
|
|
212
1046
|
.map((message) => inferLocations(message, sources))
|
|
213
1047
|
.toSorted(compareMessages);
|