@gtkx/cli 2.0.0-beta.1 → 2.0.0-beta.3
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.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +9 -1
- package/dist/cli.js.map +1 -1
- package/dist/codegen/reference.d.ts.map +1 -1
- package/dist/codegen/reference.js +1 -0
- package/dist/codegen/reference.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 +15 -16
- package/dist/codegen/run-codegen.js.map +1 -1
- package/dist/codegen/store-resolver.d.ts +2 -1
- package/dist/codegen/store-resolver.d.ts.map +1 -1
- package/dist/codegen/store-resolver.js +4 -3
- 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 +9 -0
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +29 -4
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/docs.d.ts.map +1 -1
- package/dist/commands/docs.js +1 -0
- package/dist/commands/docs.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/freedesktop/validate.d.ts.map +1 -1
- package/dist/deploy/freedesktop/validate.js +10 -4
- package/dist/deploy/freedesktop/validate.js.map +1 -1
- package/dist/deploy/nfpm/config.d.ts +3 -2
- package/dist/deploy/nfpm/config.d.ts.map +1 -1
- package/dist/deploy/nfpm/config.js +7 -4
- 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 +75 -5
- 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/targets/nfpm-target.d.ts.map +1 -1
- package/dist/deploy/targets/nfpm-target.js +12 -7
- package/dist/deploy/targets/nfpm-target.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/entry-env.d.ts +2 -1
- package/dist/dev/entry-env.d.ts.map +1 -1
- package/dist/dev/entry-env.js +2 -1
- package/dist/dev/entry-env.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.d.ts +1 -1
- package/dist/dev/runner-deps.d.ts.map +1 -1
- package/dist/dev/runner-deps.js +3 -3
- package/dist/dev/runner-deps.js.map +1 -1
- package/dist/dev/runner-main.d.ts.map +1 -1
- package/dist/dev/runner-main.js +8 -3
- package/dist/dev/runner-main.js.map +1 -1
- package/dist/dev/supervisor.d.ts +1 -0
- package/dist/dev/supervisor.d.ts.map +1 -1
- package/dist/dev/supervisor.js +4 -3
- package/dist/dev/supervisor.js.map +1 -1
- package/dist/i18n/catalogs.d.ts.map +1 -1
- package/dist/i18n/catalogs.js +1 -0
- package/dist/i18n/catalogs.js.map +1 -1
- package/dist/i18n/source-messages.d.ts.map +1 -1
- package/dist/i18n/source-messages.js +960 -8
- 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 +79 -0
- package/dist/internal/build-output.js.map +1 -0
- package/dist/internal/docs-elements.d.ts +1 -0
- package/dist/internal/docs-elements.d.ts.map +1 -1
- package/dist/internal/docs-elements.js +5 -4
- package/dist/internal/docs-elements.js.map +1 -1
- 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/errors.d.ts +2 -1
- package/dist/internal/errors.d.ts.map +1 -1
- package/dist/internal/errors.js +1 -1
- package/dist/internal/errors.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/parent-process.d.ts +12 -0
- package/dist/internal/parent-process.d.ts.map +1 -0
- package/dist/internal/parent-process.js +74 -0
- package/dist/internal/parent-process.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/ensure-store.d.ts +1 -1
- package/dist/vite-plugins/ensure-store.d.ts.map +1 -1
- package/dist/vite-plugins/ensure-store.js +2 -2
- package/dist/vite-plugins/ensure-store.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/native.d.ts.map +1 -1
- package/dist/vite-plugins/native.js +0 -4
- package/dist/vite-plugins/native.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/dist/vite-plugins/settings-worker-env.d.ts +1 -1
- package/dist/vite-plugins/settings-worker-env.d.ts.map +1 -1
- package/dist/vite-plugins/settings-worker-env.js +2 -2
- package/dist/vite-plugins/settings-worker-env.js.map +1 -1
- package/dist/vitest-plugin.d.ts +2 -1
- package/dist/vitest-plugin.d.ts.map +1 -1
- package/dist/vitest-plugin.js +6 -6
- package/dist/vitest-plugin.js.map +1 -1
- package/package.json +10 -9
- package/src/builder.ts +18 -5
- package/src/cli.ts +9 -1
- package/src/codegen/reference.ts +1 -0
- package/src/codegen/run-codegen.ts +17 -19
- package/src/codegen/store-resolver.ts +5 -3
- 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 +38 -4
- package/src/commands/docs.ts +1 -0
- package/src/deploy/freedesktop/metainfo.ts +73 -21
- package/src/deploy/freedesktop/validate.ts +12 -4
- package/src/deploy/nfpm/config.ts +13 -4
- package/src/deploy/nfpm/contents.ts +8 -0
- package/src/deploy/node-runtime/elf.ts +43 -5
- package/src/deploy/node-runtime/index.ts +107 -6
- 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/targets/nfpm-target.ts +13 -6
- package/src/deploy/types.ts +1 -0
- package/src/dev/entry-env.ts +2 -1
- package/src/dev/headless.ts +40 -0
- package/src/dev/runner-deps.ts +3 -3
- package/src/dev/runner-main.ts +10 -3
- package/src/dev/supervisor.ts +6 -3
- package/src/i18n/catalogs.ts +1 -0
- package/src/i18n/source-messages.ts +1429 -28
- package/src/internal/build-manifest.ts +25 -1
- package/src/internal/build-output.ts +113 -0
- package/src/internal/docs-elements.ts +10 -4
- package/src/internal/entry-arg.ts +8 -1
- package/src/internal/errors.ts +1 -1
- package/src/internal/output-directory.ts +699 -0
- package/src/internal/parent-process.ts +110 -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/ensure-store.ts +2 -2
- package/src/vite-plugins/i18n.ts +83 -5
- package/src/vite-plugins/index.ts +14 -7
- package/src/vite-plugins/native.ts +0 -5
- package/src/vite-plugins/self-contained.ts +184 -185
- package/src/vite-plugins/settings-worker-env.ts +2 -2
- package/src/vitest-plugin.ts +6 -6
|
@@ -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,82 @@ 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
|
+
]);
|
|
36
|
+
class SourceExtractionError extends Error {
|
|
37
|
+
}
|
|
38
|
+
const sourceErrorMessage = (error) => error instanceof Error ? error.message : String(error);
|
|
39
|
+
const projectSourcePath = (root, path) => {
|
|
40
|
+
const absolute = isAbsolute(path) ? resolve(path) : resolve(root, path);
|
|
41
|
+
return toPosixPath(isPathInside(root, absolute) ? relative(root, absolute) : path);
|
|
42
|
+
};
|
|
43
|
+
const sourceExtractionError = (root, path, point, error) => {
|
|
44
|
+
if (error instanceof SourceExtractionError) {
|
|
45
|
+
return error;
|
|
46
|
+
}
|
|
47
|
+
const file = projectSourcePath(root, path);
|
|
48
|
+
const location = point === null
|
|
49
|
+
? file
|
|
50
|
+
: `${file}:${String(point.line)}:${String(point.column + 1)}`;
|
|
51
|
+
return new SourceExtractionError(`${location}: ${sourceErrorMessage(error)}`, { cause: error });
|
|
52
|
+
};
|
|
53
|
+
const locatablePoint = (node) => {
|
|
54
|
+
const location = node.loc?.start;
|
|
55
|
+
return location === undefined ? null : { column: location.column, line: location.line };
|
|
56
|
+
};
|
|
57
|
+
const isSourcePoint = (value) => typeof value === "object" &&
|
|
58
|
+
value !== null &&
|
|
59
|
+
"column" in value &&
|
|
60
|
+
typeof value.column === "number" &&
|
|
61
|
+
"line" in value &&
|
|
62
|
+
typeof value.line === "number";
|
|
63
|
+
const errorSourcePoint = (error) => {
|
|
64
|
+
if (typeof error !== "object" || error === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
if ("loc" in error && isSourcePoint(error.loc)) {
|
|
68
|
+
return error.loc;
|
|
69
|
+
}
|
|
70
|
+
return "location" in error && isSourcePoint(error.location) ? error.location : null;
|
|
71
|
+
};
|
|
72
|
+
const atSourceNode = (root, path, node, operation) => {
|
|
73
|
+
try {
|
|
74
|
+
return operation();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw sourceExtractionError(root, path, locatablePoint(node), error);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const atExtractedLocation = (root, locations, operation) => {
|
|
81
|
+
const location = locations.find((candidate) => candidate.line !== undefined && candidate.column !== undefined);
|
|
82
|
+
if (location?.line === undefined || location.column === undefined) {
|
|
83
|
+
return operation();
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
return operation();
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw sourceExtractionError(root, location.file, { column: location.column, line: location.line }, error);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
13
92
|
const quietLogger = () => {
|
|
14
93
|
const reports = [];
|
|
15
94
|
return {
|
|
@@ -98,10 +177,10 @@ const expandedPlural = (entry) => {
|
|
|
98
177
|
},
|
|
99
178
|
};
|
|
100
179
|
};
|
|
101
|
-
const groupedPlurals = (entries) => {
|
|
180
|
+
const groupedPlurals = (root, entries) => {
|
|
102
181
|
const groups = new Map();
|
|
103
182
|
for (const entry of entries) {
|
|
104
|
-
const { baseKey, variant } = expandedPlural(entry);
|
|
183
|
+
const { baseKey, variant } = atExtractedLocation(root, entry.locations ?? [], () => expandedPlural(entry));
|
|
105
184
|
const namespace = namespaceFor(entry);
|
|
106
185
|
const key = `${namespace}\0${baseKey}`;
|
|
107
186
|
const group = groups.get(key) ?? { baseKey, namespace, variants: [] };
|
|
@@ -140,17 +219,19 @@ const pluralMessage = ({ baseKey, namespace, variants }) => {
|
|
|
140
219
|
sourceKey: msgid,
|
|
141
220
|
};
|
|
142
221
|
};
|
|
143
|
-
const pluralMessages = (entries) => groupedPlurals(entries).map((group) => pluralMessage(group));
|
|
222
|
+
const pluralMessages = (root, entries) => groupedPlurals(root, entries).map((group) => atExtractedLocation(root, group.variants.flatMap((variant) => variant.locations), () => pluralMessage(group)));
|
|
144
223
|
const assertCardinal = (entry) => {
|
|
145
224
|
if (entry.isOrdinal === true) {
|
|
146
225
|
throw new Error("GNU gettext catalogs do not support i18next ordinal plurals");
|
|
147
226
|
}
|
|
148
227
|
};
|
|
149
|
-
const sourceMessages = (entries) => {
|
|
228
|
+
const sourceMessages = (root, entries) => {
|
|
150
229
|
const points = [];
|
|
151
230
|
const plurals = [];
|
|
152
231
|
for (const entry of entries) {
|
|
153
|
-
|
|
232
|
+
atExtractedLocation(root, entry.locations ?? [], () => {
|
|
233
|
+
assertCardinal(entry);
|
|
234
|
+
});
|
|
154
235
|
if (entry.isExpandedPlural === true) {
|
|
155
236
|
plurals.push(entry);
|
|
156
237
|
continue;
|
|
@@ -160,7 +241,10 @@ const sourceMessages = (entries) => {
|
|
|
160
241
|
}
|
|
161
242
|
points.push(entry);
|
|
162
243
|
}
|
|
163
|
-
return [
|
|
244
|
+
return [
|
|
245
|
+
...points.map((entry) => atExtractedLocation(root, entry.locations ?? [], () => pointMessage(entry))),
|
|
246
|
+
...pluralMessages(root, plurals),
|
|
247
|
+
];
|
|
164
248
|
};
|
|
165
249
|
const compareMessages = (left, right) => left.namespace.localeCompare(right.namespace) ||
|
|
166
250
|
(left.context ?? "").localeCompare(right.context ?? "") ||
|
|
@@ -185,16 +269,885 @@ const normalizedSourceFiles = (root, paths) => {
|
|
|
185
269
|
.filter((path) => isPathWithin(root, path));
|
|
186
270
|
return new Set(files).values().toArray().toSorted((left, right) => left.localeCompare(right));
|
|
187
271
|
};
|
|
272
|
+
const isStaticKeyWrapper = (key) => STATIC_KEY_WRAPPER_TYPES.has(key.type);
|
|
273
|
+
const unwrapStaticWrapper = (node) => isStaticKeyWrapper(node) ? unwrapStaticWrapper(node.expression) : node;
|
|
274
|
+
const isStaticTranslationKey = (key) => {
|
|
275
|
+
if (key === undefined || key === null) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
if (key.type === "Literal") {
|
|
279
|
+
return typeof key.value === "string";
|
|
280
|
+
}
|
|
281
|
+
if (key.type === "TemplateLiteral") {
|
|
282
|
+
return key.expressions.length === 0;
|
|
283
|
+
}
|
|
284
|
+
if (key.type === "ParenthesizedExpression") {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (isStaticKeyWrapper(key)) {
|
|
288
|
+
return isStaticTranslationKey(key.expression);
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
};
|
|
292
|
+
const assertStaticTranslationKey = (key) => {
|
|
293
|
+
if (!isStaticTranslationKey(key)) {
|
|
294
|
+
throw new Error("Translation keys must be string literals");
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const propertyName = (property) => {
|
|
298
|
+
if (!property.computed && property.key.type === "Identifier") {
|
|
299
|
+
return property.key.name;
|
|
300
|
+
}
|
|
301
|
+
return property.key.type === "Literal" && typeof property.key.value === "string"
|
|
302
|
+
? property.key.value
|
|
303
|
+
: null;
|
|
304
|
+
};
|
|
305
|
+
const assertSupportedOptionProperty = (property) => {
|
|
306
|
+
if (property.type === "SpreadElement") {
|
|
307
|
+
throw new Error("Translation extraction requires explicit option properties");
|
|
308
|
+
}
|
|
309
|
+
const name = propertyName(property);
|
|
310
|
+
if (name === null) {
|
|
311
|
+
throw new Error("Translation extraction requires static option property names");
|
|
312
|
+
}
|
|
313
|
+
if (UNSUPPORTED_PLURAL_OPTION.test(name)) {
|
|
314
|
+
throw new Error("GNU gettext accepts one singular and one plural source string");
|
|
315
|
+
}
|
|
316
|
+
if (STATIC_SOURCE_OPTION_NAMES.has(name)) {
|
|
317
|
+
assertStaticTranslationKey(property.value);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
const assertSupportedOptionObject = (options) => {
|
|
321
|
+
if (options === null || options === undefined) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (isStaticKeyWrapper(options) && unwrapStaticWrapper(options).type === "ObjectExpression") {
|
|
325
|
+
throw new Error("Translation option objects cannot be wrapped in another expression");
|
|
326
|
+
}
|
|
327
|
+
if (options.type !== "ObjectExpression") {
|
|
328
|
+
throw new Error("Translation option objects must be inline object literals");
|
|
329
|
+
}
|
|
330
|
+
for (const property of options.properties) {
|
|
331
|
+
assertSupportedOptionProperty(property);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const namedImportedBinding = (path, source) => {
|
|
335
|
+
if (!path.isImportSpecifier() || path.node.importKind === "type") {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
const imported = path.node.imported;
|
|
339
|
+
return {
|
|
340
|
+
imported: types.isIdentifier(imported) ? imported.name : imported.value,
|
|
341
|
+
local: path.node.local.name,
|
|
342
|
+
source,
|
|
343
|
+
style: "named",
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
const defaultImportedBinding = (path, source) => path.isImportDefaultSpecifier()
|
|
347
|
+
? { imported: "default", local: path.node.local.name, source, style: "default" }
|
|
348
|
+
: null;
|
|
349
|
+
const namespaceImportedBinding = (path, source) => path.isImportNamespaceSpecifier()
|
|
350
|
+
? { imported: "*", local: path.node.local.name, source, style: "namespace" }
|
|
351
|
+
: null;
|
|
352
|
+
const importedBinding = (binding) => {
|
|
353
|
+
if (binding === undefined) {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
const path = binding.path;
|
|
357
|
+
const declaration = path.parentPath;
|
|
358
|
+
if (!declaration.isImportDeclaration() || declaration.node.importKind === "type") {
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
const source = declaration.node.source.value;
|
|
362
|
+
return namedImportedBinding(path, source) ??
|
|
363
|
+
defaultImportedBinding(path, source) ??
|
|
364
|
+
namespaceImportedBinding(path, source);
|
|
365
|
+
};
|
|
366
|
+
const babelPropertyName = (property) => {
|
|
367
|
+
const key = property.key;
|
|
368
|
+
if (!property.computed && types.isIdentifier(key)) {
|
|
369
|
+
return key.name;
|
|
370
|
+
}
|
|
371
|
+
return types.isStringLiteral(key) ? key.value : null;
|
|
372
|
+
};
|
|
373
|
+
const isBoundIdentifier = (node, name) => {
|
|
374
|
+
if (types.isIdentifier(node)) {
|
|
375
|
+
return node.name === name;
|
|
376
|
+
}
|
|
377
|
+
if (types.isAssignmentPattern(node)) {
|
|
378
|
+
return types.isIdentifier(node.left) && node.left.name === name;
|
|
379
|
+
}
|
|
380
|
+
return false;
|
|
381
|
+
};
|
|
382
|
+
const isHookPatternBinding = (pattern, name) => {
|
|
383
|
+
if (types.isObjectPattern(pattern)) {
|
|
384
|
+
return pattern.properties.some((property) => types.isObjectProperty(property) &&
|
|
385
|
+
babelPropertyName(property) === "t" &&
|
|
386
|
+
isBoundIdentifier(property.value, name));
|
|
387
|
+
}
|
|
388
|
+
if (types.isArrayPattern(pattern)) {
|
|
389
|
+
return isBoundIdentifier(pattern.elements[0] ?? null, name);
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
};
|
|
393
|
+
const importedTranslationHook = (scope, call) => {
|
|
394
|
+
if (!types.isIdentifier(call.callee)) {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
const imported = importedBinding(scope.getBinding(call.callee.name));
|
|
398
|
+
return imported?.imported === "useTranslation" && TRANSLATION_MODULES.has(imported.source)
|
|
399
|
+
? imported
|
|
400
|
+
: null;
|
|
401
|
+
};
|
|
402
|
+
const translationHookBinding = (binding) => {
|
|
403
|
+
const path = binding.path;
|
|
404
|
+
if (!path.isVariableDeclarator()) {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const pattern = path.node.id;
|
|
408
|
+
const initializer = path.node.init;
|
|
409
|
+
if (!isHookPatternBinding(pattern, binding.identifier.name) ||
|
|
410
|
+
!types.isCallExpression(initializer)) {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
const imported = importedTranslationHook(path.scope, initializer);
|
|
414
|
+
if (imported === null) {
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
return { call: initializer, isCanonical: imported.local === "useTranslation" };
|
|
418
|
+
};
|
|
419
|
+
const importedTranslationFunctionKind = (imported) => {
|
|
420
|
+
if (imported?.imported !== "t" || !TRANSLATION_MODULES.has(imported.source)) {
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
return imported.local === "t" ? "canonical" : "alias";
|
|
424
|
+
};
|
|
425
|
+
const hookTranslationFunctionKind = (binding) => {
|
|
426
|
+
if (binding === undefined) {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
const hook = translationHookBinding(binding);
|
|
430
|
+
if (hook === null) {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
return binding.identifier.name === "t" && hook.isCanonical ? "canonical" : "alias";
|
|
434
|
+
};
|
|
435
|
+
const directTranslationFunctionKind = (scope, name) => {
|
|
436
|
+
const binding = scope.getBinding(name);
|
|
437
|
+
return importedTranslationFunctionKind(importedBinding(binding)) ?? hookTranslationFunctionKind(binding);
|
|
438
|
+
};
|
|
439
|
+
const babelMemberName = (member) => {
|
|
440
|
+
const property = member.property;
|
|
441
|
+
if (member.computed) {
|
|
442
|
+
return types.isStringLiteral(property) ? property.value : null;
|
|
443
|
+
}
|
|
444
|
+
return types.isIdentifier(property) ? property.name : null;
|
|
445
|
+
};
|
|
446
|
+
const isImportedTranslationObject = (scope, object) => {
|
|
447
|
+
if (!types.isIdentifier(object)) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
const imported = importedBinding(scope.getBinding(object.name));
|
|
451
|
+
return imported !== null &&
|
|
452
|
+
TRANSLATION_MODULES.has(imported.source) &&
|
|
453
|
+
(imported.style === "namespace" || (imported.source === "i18next" && imported.imported === "default"));
|
|
454
|
+
};
|
|
455
|
+
const constantVariableDeclarator = (binding) => {
|
|
456
|
+
const path = binding.path;
|
|
457
|
+
const declaration = path.parentPath;
|
|
458
|
+
if (!path.isVariableDeclarator() ||
|
|
459
|
+
!declaration.isVariableDeclaration() ||
|
|
460
|
+
declaration.node.kind !== "const") {
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
return path;
|
|
464
|
+
};
|
|
465
|
+
const isTranslationHookResultBinding = (binding) => {
|
|
466
|
+
if (binding === undefined) {
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
const path = constantVariableDeclarator(binding);
|
|
470
|
+
const initializer = path?.node.init;
|
|
471
|
+
return path !== null &&
|
|
472
|
+
types.isIdentifier(path.node.id) &&
|
|
473
|
+
types.isCallExpression(initializer) &&
|
|
474
|
+
importedTranslationHook(path.scope, initializer) !== null;
|
|
475
|
+
};
|
|
476
|
+
const isHookTranslationMember = (scope, member) => {
|
|
477
|
+
if (babelMemberName(member) !== "t") {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
const object = member.object;
|
|
481
|
+
if (types.isCallExpression(object)) {
|
|
482
|
+
return importedTranslationHook(scope, object) !== null;
|
|
483
|
+
}
|
|
484
|
+
return types.isIdentifier(object) && isTranslationHookResultBinding(scope.getBinding(object.name));
|
|
485
|
+
};
|
|
486
|
+
const isTranslationMember = (scope, member) => babelMemberName(member) === "t" &&
|
|
487
|
+
(isImportedTranslationObject(scope, member.object) || isHookTranslationMember(scope, member));
|
|
488
|
+
const isDirectTranslationFunction = (scope, node) => types.isIdentifier(node) && directTranslationFunctionKind(scope, node.name) !== null;
|
|
489
|
+
const isTranslationFunctionValue = (scope, node) => {
|
|
490
|
+
if (isDirectTranslationFunction(scope, node)) {
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
|
|
494
|
+
isTranslationMember(scope, node);
|
|
495
|
+
};
|
|
496
|
+
const isTranslationBindCall = (scope, node) => {
|
|
497
|
+
if (!types.isCallExpression(node) || !types.isMemberExpression(node.callee)) {
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
500
|
+
return babelMemberName(node.callee) === "bind" &&
|
|
501
|
+
isTranslationFunctionValue(scope, node.callee.object);
|
|
502
|
+
};
|
|
503
|
+
const isTranslationObjectPatternAlias = (path, binding) => {
|
|
504
|
+
const pattern = path.node.id;
|
|
505
|
+
const initializer = path.node.init;
|
|
506
|
+
if (!types.isObjectPattern(pattern) ||
|
|
507
|
+
!isHookPatternBinding(pattern, binding.identifier.name) ||
|
|
508
|
+
!types.isIdentifier(initializer)) {
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
return isImportedTranslationObject(path.scope, initializer) ||
|
|
512
|
+
isTranslationHookResultBinding(path.scope.getBinding(initializer.name));
|
|
513
|
+
};
|
|
514
|
+
const isTranslationAliasBinding = (binding) => {
|
|
515
|
+
if (binding === undefined) {
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
const path = constantVariableDeclarator(binding);
|
|
519
|
+
if (path === null) {
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
if (isTranslationObjectPatternAlias(path, binding)) {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
const initializer = path.node.init;
|
|
526
|
+
return types.isIdentifier(path.node.id) &&
|
|
527
|
+
initializer !== null &&
|
|
528
|
+
initializer !== undefined &&
|
|
529
|
+
(isTranslationFunctionValue(path.scope, initializer) || isTranslationBindCall(path.scope, initializer));
|
|
530
|
+
};
|
|
531
|
+
const translationFunctionKind = (scope, name) => {
|
|
532
|
+
const direct = directTranslationFunctionKind(scope, name);
|
|
533
|
+
if (direct !== null) {
|
|
534
|
+
return direct;
|
|
535
|
+
}
|
|
536
|
+
return isTranslationAliasBinding(scope.getBinding(name)) ? "alias" : null;
|
|
537
|
+
};
|
|
538
|
+
const translationElementKind = (path, name) => {
|
|
539
|
+
const imported = importedBinding(path.scope.getBinding(name));
|
|
540
|
+
if (imported?.source !== "@gtkx/i18n" || !TRANS_ELEMENTS.has(imported.imported)) {
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
return imported.local === imported.imported ? "canonical" : "alias";
|
|
544
|
+
};
|
|
545
|
+
const nodeStart = (path) => {
|
|
546
|
+
const start = path.node.start;
|
|
547
|
+
if (start === null || start === undefined) {
|
|
548
|
+
throw new Error("Translation extraction could not locate a source expression");
|
|
549
|
+
}
|
|
550
|
+
return start;
|
|
551
|
+
};
|
|
552
|
+
const registerSourceMask = (node, references) => {
|
|
553
|
+
const { start, end } = node;
|
|
554
|
+
if (start === null || start === undefined || end === null || end === undefined) {
|
|
555
|
+
throw new Error("Translation extraction could not locate a source expression");
|
|
556
|
+
}
|
|
557
|
+
references.masks.push({ start, end });
|
|
558
|
+
};
|
|
559
|
+
const sourceLocationKey = (line, column) => `${String(line)}:${String(column)}`;
|
|
560
|
+
const sourcePoint = (node) => {
|
|
561
|
+
return locatablePoint(node);
|
|
562
|
+
};
|
|
563
|
+
const registerSourceLocation = (node, references) => {
|
|
564
|
+
const point = sourcePoint(node);
|
|
565
|
+
if (point !== null) {
|
|
566
|
+
references.locations.set(sourceLocationKey(point.line, point.column), point);
|
|
567
|
+
}
|
|
568
|
+
return point;
|
|
569
|
+
};
|
|
570
|
+
const babelStaticKey = (node) => {
|
|
571
|
+
if (node === null || node === undefined) {
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
if (types.isStringLiteral(node)) {
|
|
575
|
+
return node.value;
|
|
576
|
+
}
|
|
577
|
+
if (types.isTemplateLiteral(node) && node.expressions.length === 0) {
|
|
578
|
+
return node.quasis[0]?.value.cooked ?? null;
|
|
579
|
+
}
|
|
580
|
+
if (types.isParenthesizedExpression(node) ||
|
|
581
|
+
types.isTSAsExpression(node) ||
|
|
582
|
+
types.isTSSatisfiesExpression(node) ||
|
|
583
|
+
types.isTSTypeAssertion(node) ||
|
|
584
|
+
types.isTSNonNullExpression(node)) {
|
|
585
|
+
return babelStaticKey(node.expression);
|
|
586
|
+
}
|
|
587
|
+
return null;
|
|
588
|
+
};
|
|
589
|
+
const babelObjectExpression = (node) => {
|
|
590
|
+
if (node === null || node === undefined) {
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
if (types.isObjectExpression(node)) {
|
|
594
|
+
return node;
|
|
595
|
+
}
|
|
596
|
+
if (types.isParenthesizedExpression(node) ||
|
|
597
|
+
types.isTSAsExpression(node) ||
|
|
598
|
+
types.isTSSatisfiesExpression(node) ||
|
|
599
|
+
types.isTSTypeAssertion(node) ||
|
|
600
|
+
types.isTSNonNullExpression(node)) {
|
|
601
|
+
return babelObjectExpression(node.expression);
|
|
602
|
+
}
|
|
603
|
+
return null;
|
|
604
|
+
};
|
|
605
|
+
const babelNamedProperty = (object, name) => {
|
|
606
|
+
if (object === null) {
|
|
607
|
+
return null;
|
|
608
|
+
}
|
|
609
|
+
for (const property of object.properties) {
|
|
610
|
+
if (types.isObjectProperty(property) && babelPropertyName(property) === name) {
|
|
611
|
+
return property;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return null;
|
|
615
|
+
};
|
|
616
|
+
const babelStringProperty = (object, name) => {
|
|
617
|
+
const property = babelNamedProperty(object, name);
|
|
618
|
+
if (property === null) {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
const value = babelStaticKey(property.value);
|
|
622
|
+
if (value === null) {
|
|
623
|
+
throw new Error(`Translation ${name} values must be string literals`);
|
|
624
|
+
}
|
|
625
|
+
return value;
|
|
626
|
+
};
|
|
627
|
+
const callOptions = (call) => babelObjectExpression(call.arguments[1]) ?? babelObjectExpression(call.arguments[2]);
|
|
628
|
+
const hookKeyPrefix = (callee) => {
|
|
629
|
+
const binding = callee.scope.getBinding(callee.node.name);
|
|
630
|
+
const hook = binding === undefined ? null : translationHookBinding(binding);
|
|
631
|
+
return babelStringProperty(hook === null ? null : babelObjectExpression(hook.call.arguments[1]), "keyPrefix") ?? "";
|
|
632
|
+
};
|
|
633
|
+
const prefixedTranslationKey = (prefix, key) => {
|
|
634
|
+
if (prefix.length === 0) {
|
|
635
|
+
return key;
|
|
636
|
+
}
|
|
637
|
+
return prefix.endsWith(".") ? `${prefix}${key}` : `${prefix}.${key}`;
|
|
638
|
+
};
|
|
639
|
+
const extractedEntryKey = (call, keyPrefix) => {
|
|
640
|
+
const key = babelStaticKey(call.arguments[0]);
|
|
641
|
+
if (key === null) {
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
const options = callOptions(call);
|
|
645
|
+
const callPrefix = babelStringProperty(options, "keyPrefix");
|
|
646
|
+
if (callPrefix === "" && keyPrefix.length > 0) {
|
|
647
|
+
throw new Error("An empty call keyPrefix cannot override a hook keyPrefix during extraction");
|
|
648
|
+
}
|
|
649
|
+
const selectedPrefix = callPrefix ?? keyPrefix;
|
|
650
|
+
const prefixed = prefixedTranslationKey(selectedPrefix, key);
|
|
651
|
+
const context = babelStringProperty(options, "context");
|
|
652
|
+
return context === null ? prefixed : `${prefixed}${CONTEXT_SEPARATOR}${context}`;
|
|
653
|
+
};
|
|
654
|
+
const registerEntryLocation = (key, point, references) => {
|
|
655
|
+
if (key === null || point === null) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const locations = references.entryLocations.get(key) ?? [];
|
|
659
|
+
locations.push(point);
|
|
660
|
+
references.entryLocations.set(key, locations);
|
|
661
|
+
};
|
|
662
|
+
const registerCallReference = (callPath, references, keyPrefix) => {
|
|
663
|
+
references.calls.add(nodeStart(callPath));
|
|
664
|
+
const firstArgument = callPath.node.arguments[0];
|
|
665
|
+
const point = registerSourceLocation(firstArgument ?? callPath.node, references);
|
|
666
|
+
if (point !== null) {
|
|
667
|
+
references.sourcePoints.set(nodeStart(callPath), point);
|
|
668
|
+
}
|
|
669
|
+
registerEntryLocation(extractedEntryKey(callPath.node, keyPrefix), point, references);
|
|
670
|
+
};
|
|
671
|
+
const parserPlugins = (lang) => [
|
|
672
|
+
"decorators",
|
|
673
|
+
...(lang === "ts" || lang === "tsx" ? ["typescript"] : []),
|
|
674
|
+
...(lang === "jsx" || lang === "tsx" ? ["jsx"] : []),
|
|
675
|
+
];
|
|
676
|
+
const parseTranslationReferences = (root, path, source, lang) => {
|
|
677
|
+
try {
|
|
678
|
+
return parseBabelSync(source, {
|
|
679
|
+
ast: true,
|
|
680
|
+
babelrc: false,
|
|
681
|
+
configFile: false,
|
|
682
|
+
filename: projectSourcePath(root, path),
|
|
683
|
+
parserOpts: { createParenthesizedExpressions: true, plugins: parserPlugins(lang) },
|
|
684
|
+
sourceType: "module",
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
catch (error) {
|
|
688
|
+
throw sourceExtractionError(root, path, errorSourcePoint(error), error);
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
const registerIdentifierTranslationCall = (callPath, callee, references) => {
|
|
692
|
+
if (!callee.isIdentifier()) {
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const kind = translationFunctionKind(callee.scope, callee.node.name);
|
|
696
|
+
if (kind === "alias") {
|
|
697
|
+
throw new Error("Translation calls must use the imported name t");
|
|
698
|
+
}
|
|
699
|
+
if (kind === "canonical") {
|
|
700
|
+
registerCallReference(callPath, references, hookKeyPrefix(callee));
|
|
701
|
+
}
|
|
702
|
+
else if (callee.node.name === "t") {
|
|
703
|
+
registerSourceMask(callee.node, references);
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
const registerTranslationCall = (callPath, references) => {
|
|
707
|
+
assertSupportedWrappedCallee(callPath);
|
|
708
|
+
const callee = callPath.get("callee");
|
|
709
|
+
if (callee.isIdentifier()) {
|
|
710
|
+
registerIdentifierTranslationCall(callPath, callee, references);
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
if (callee.isMemberExpression() && isTranslationMember(callee.scope, callee.node)) {
|
|
714
|
+
throw new Error("Translation calls must use the named t export");
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
const unwrapTranslationCallee = (node) => {
|
|
718
|
+
if (types.isParenthesizedExpression(node) ||
|
|
719
|
+
types.isTSAsExpression(node) ||
|
|
720
|
+
types.isTSSatisfiesExpression(node) ||
|
|
721
|
+
types.isTSTypeAssertion(node) ||
|
|
722
|
+
types.isTSNonNullExpression(node)) {
|
|
723
|
+
return unwrapTranslationCallee(node.expression);
|
|
724
|
+
}
|
|
725
|
+
return node;
|
|
726
|
+
};
|
|
727
|
+
const isTranslationCallee = (scope, node) => {
|
|
728
|
+
if (types.isIdentifier(node)) {
|
|
729
|
+
return translationFunctionKind(scope, node.name) !== null;
|
|
730
|
+
}
|
|
731
|
+
return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
|
|
732
|
+
isTranslationMember(scope, node);
|
|
733
|
+
};
|
|
734
|
+
const assertSupportedWrappedCallee = (callPath) => {
|
|
735
|
+
const callee = callPath.node.callee;
|
|
736
|
+
const unwrapped = unwrapTranslationCallee(callee);
|
|
737
|
+
if (unwrapped !== callee && isTranslationCallee(callPath.scope, unwrapped)) {
|
|
738
|
+
throw new Error("Translation call expressions cannot wrap their callee");
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
const assertSupportedOptionalCall = (callPath) => {
|
|
742
|
+
const callee = unwrapTranslationCallee(callPath.node.callee);
|
|
743
|
+
if (isTranslationCallee(callPath.scope, callee)) {
|
|
744
|
+
throw new Error("Translation calls cannot use optional chaining");
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
const assertUnsupportedTranslationTag = (tagPath) => {
|
|
748
|
+
const tag = unwrapTranslationCallee(tagPath.node.tag);
|
|
749
|
+
if (isTranslationCallee(tagPath.scope, tag)) {
|
|
750
|
+
throw new Error("Translation calls cannot use tagged-template syntax");
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
const registerUnrecognizedElementMasks = (elementPath, name, references) => {
|
|
754
|
+
registerSourceMask(name.node, references);
|
|
755
|
+
const parent = elementPath.parentPath.node;
|
|
756
|
+
const closingName = types.isJSXElement(parent) ? parent.closingElement?.name : null;
|
|
757
|
+
if (types.isJSXIdentifier(closingName)) {
|
|
758
|
+
registerSourceMask(closingName, references);
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
const registerElementReference = (elementPath, references) => {
|
|
762
|
+
const start = nodeStart(elementPath);
|
|
763
|
+
const point = registerSourceLocation(elementPath.node, references);
|
|
764
|
+
references.elements.add(start);
|
|
765
|
+
if (point !== null) {
|
|
766
|
+
references.sourcePoints.set(start, point);
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
const registerTranslationElement = (elementPath, references) => {
|
|
770
|
+
const name = elementPath.get("name");
|
|
771
|
+
if (!name.isJSXIdentifier()) {
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
const kind = translationElementKind(name, name.node.name);
|
|
775
|
+
if (kind === "alias") {
|
|
776
|
+
throw new Error("Translation components must use their imported names");
|
|
777
|
+
}
|
|
778
|
+
if (kind === "canonical") {
|
|
779
|
+
registerElementReference(elementPath, references);
|
|
780
|
+
}
|
|
781
|
+
else if (TRANS_ELEMENTS.has(name.node.name)) {
|
|
782
|
+
registerUnrecognizedElementMasks(elementPath, name, references);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
const maskUnrecognizedTranslations = (source, references) => {
|
|
786
|
+
let masked = source;
|
|
787
|
+
const masks = references.masks.toSorted((left, right) => right.start - left.start);
|
|
788
|
+
for (const { start, end } of masks) {
|
|
789
|
+
masked = `${masked.slice(0, start)}${"_".repeat(end - start)}${masked.slice(end)}`;
|
|
790
|
+
}
|
|
791
|
+
return masked;
|
|
792
|
+
};
|
|
793
|
+
const translationReferences = (root, path, source, lang) => {
|
|
794
|
+
const parsed = parseTranslationReferences(root, path, source, lang);
|
|
795
|
+
if (parsed === null) {
|
|
796
|
+
throw new Error(`Translation extraction could not parse ${path}`);
|
|
797
|
+
}
|
|
798
|
+
const references = {
|
|
799
|
+
calls: new Set(),
|
|
800
|
+
elements: new Set(),
|
|
801
|
+
entryLocations: new Map(),
|
|
802
|
+
locations: new Map(),
|
|
803
|
+
masks: [],
|
|
804
|
+
sourcePoints: new Map(),
|
|
805
|
+
};
|
|
806
|
+
traverse(parsed, {
|
|
807
|
+
CallExpression(callPath) {
|
|
808
|
+
atSourceNode(root, path, callPath.node, () => {
|
|
809
|
+
registerTranslationCall(callPath, references);
|
|
810
|
+
});
|
|
811
|
+
},
|
|
812
|
+
JSXOpeningElement(elementPath) {
|
|
813
|
+
atSourceNode(root, path, elementPath.node, () => {
|
|
814
|
+
registerTranslationElement(elementPath, references);
|
|
815
|
+
});
|
|
816
|
+
},
|
|
817
|
+
OptionalCallExpression(callPath) {
|
|
818
|
+
atSourceNode(root, path, callPath.node, () => {
|
|
819
|
+
assertSupportedOptionalCall(callPath);
|
|
820
|
+
});
|
|
821
|
+
},
|
|
822
|
+
TaggedTemplateExpression(tagPath) {
|
|
823
|
+
atSourceNode(root, path, tagPath.node, () => {
|
|
824
|
+
assertUnsupportedTranslationTag(tagPath);
|
|
825
|
+
});
|
|
826
|
+
},
|
|
827
|
+
});
|
|
828
|
+
return references;
|
|
829
|
+
};
|
|
830
|
+
const assertSupportedTranslationCall = (node, references) => {
|
|
831
|
+
if (!references.calls.has(node.start)) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
assertStaticTranslationKey(node.arguments[0]);
|
|
835
|
+
assertSupportedTranslationOptions(node.arguments[1], node.arguments[2]);
|
|
836
|
+
};
|
|
837
|
+
const assertSupportedTranslationOptions = (second, third) => {
|
|
838
|
+
if (third !== undefined && third !== null) {
|
|
839
|
+
assertStaticTranslationKey(second);
|
|
840
|
+
assertSupportedOptionObject(third);
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
if (second === undefined || second === null || isStaticTranslationKey(second)) {
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
assertSupportedOptionObject(second);
|
|
847
|
+
};
|
|
848
|
+
const isNamedJsxAttribute = (attribute, name) => attribute.name.type === "JSXIdentifier" && attribute.name.name === name;
|
|
849
|
+
const assertSupportedTransKey = (attribute) => {
|
|
850
|
+
if (!isNamedJsxAttribute(attribute, "i18nKey")) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
const key = attribute.value?.type === "JSXExpressionContainer"
|
|
854
|
+
? attribute.value.expression
|
|
855
|
+
: attribute.value;
|
|
856
|
+
assertStaticTranslationKey(key);
|
|
857
|
+
};
|
|
858
|
+
const assertSupportedTransStringAttribute = (attribute, name) => {
|
|
859
|
+
if (!isNamedJsxAttribute(attribute, name)) {
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
const value = attribute.value?.type === "JSXExpressionContainer"
|
|
863
|
+
? attribute.value.expression
|
|
864
|
+
: attribute.value;
|
|
865
|
+
assertStaticTranslationKey(value);
|
|
866
|
+
};
|
|
867
|
+
const assertSupportedTransOptions = (attribute) => {
|
|
868
|
+
if (isNamedJsxAttribute(attribute, "tOptions") && attribute.value?.type === "JSXExpressionContainer") {
|
|
869
|
+
assertSupportedOptionObject(attribute.value.expression);
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
const assertSupportedTransAttribute = (attribute) => {
|
|
873
|
+
if (attribute.type === "JSXSpreadAttribute") {
|
|
874
|
+
throw new Error("Translation extraction requires explicit component properties");
|
|
875
|
+
}
|
|
876
|
+
assertSupportedTransKey(attribute);
|
|
877
|
+
assertSupportedTransStringAttribute(attribute, "context");
|
|
878
|
+
assertSupportedTransStringAttribute(attribute, "defaults");
|
|
879
|
+
assertSupportedTransOptions(attribute);
|
|
880
|
+
};
|
|
881
|
+
const assertSupportedTransElement = (node, references) => {
|
|
882
|
+
if (!references.elements.has(node.start)) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
for (const attribute of node.attributes) {
|
|
886
|
+
assertSupportedTransAttribute(attribute);
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
const assertSupportedTranslationSyntax = (root, path, source) => {
|
|
890
|
+
const lang = sourceLanguage(path);
|
|
891
|
+
if (lang === undefined) {
|
|
892
|
+
return {
|
|
893
|
+
calls: new Set(),
|
|
894
|
+
elements: new Set(),
|
|
895
|
+
entryLocations: new Map(),
|
|
896
|
+
locations: new Map(),
|
|
897
|
+
masks: [],
|
|
898
|
+
sourcePoints: new Map(),
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
let parsed;
|
|
902
|
+
try {
|
|
903
|
+
parsed = parseSync(projectSourcePath(root, path), source, { lang });
|
|
904
|
+
}
|
|
905
|
+
catch (error) {
|
|
906
|
+
throw sourceExtractionError(root, path, errorSourcePoint(error), error);
|
|
907
|
+
}
|
|
908
|
+
const references = translationReferences(root, path, source, lang);
|
|
909
|
+
new Visitor({
|
|
910
|
+
CallExpression(node) {
|
|
911
|
+
try {
|
|
912
|
+
assertSupportedTranslationCall(node, references);
|
|
913
|
+
}
|
|
914
|
+
catch (error) {
|
|
915
|
+
throw sourceExtractionError(root, path, references.sourcePoints.get(node.start) ?? null, error);
|
|
916
|
+
}
|
|
917
|
+
},
|
|
918
|
+
JSXOpeningElement(node) {
|
|
919
|
+
try {
|
|
920
|
+
assertSupportedTransElement(node, references);
|
|
921
|
+
}
|
|
922
|
+
catch (error) {
|
|
923
|
+
throw sourceExtractionError(root, path, references.sourcePoints.get(node.start) ?? null, error);
|
|
924
|
+
}
|
|
925
|
+
},
|
|
926
|
+
}).visit(parsed.program);
|
|
927
|
+
return references;
|
|
928
|
+
};
|
|
929
|
+
const extractedLocationFile = (root, file) => resolve(root, file);
|
|
930
|
+
const isRecognizedLocation = (root, references, location) => {
|
|
931
|
+
if (location.line === undefined || location.column === undefined) {
|
|
932
|
+
return false;
|
|
933
|
+
}
|
|
934
|
+
const sourceReferences = references.get(extractedLocationFile(root, location.file));
|
|
935
|
+
return sourceReferences?.locations.has(sourceLocationKey(location.line, location.column)) === true;
|
|
936
|
+
};
|
|
937
|
+
const recognizedKeyLocations = (references, extractedKey) => {
|
|
938
|
+
const locations = [];
|
|
939
|
+
for (const [file, sourceReferences] of references) {
|
|
940
|
+
const points = sourceReferences.entryLocations.get(extractedKey) ?? [];
|
|
941
|
+
for (const point of points) {
|
|
942
|
+
locations.push({ file, column: point.column, line: point.line });
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
return locations;
|
|
946
|
+
};
|
|
947
|
+
const recognizedEntry = (root, references, entry) => {
|
|
948
|
+
const extractedLocations = entry.locations ?? [];
|
|
949
|
+
const locations = extractedLocations.length === 0
|
|
950
|
+
? recognizedKeyLocations(references, entry.key)
|
|
951
|
+
: extractedLocations.filter((location) => isRecognizedLocation(root, references, location));
|
|
952
|
+
return locations.length === 0 ? null : { ...entry, locations };
|
|
953
|
+
};
|
|
954
|
+
const quotedNestedReferenceParts = (content, quote) => {
|
|
955
|
+
const end = content.indexOf(quote, 1);
|
|
956
|
+
return end === -1
|
|
957
|
+
? null
|
|
958
|
+
: { base: content.slice(1, end), options: content.slice(end + 1) };
|
|
959
|
+
};
|
|
960
|
+
const nestedReferenceParts = (value) => {
|
|
961
|
+
const content = value.trim();
|
|
962
|
+
const quote = content[0];
|
|
963
|
+
if (quote === "\"" || quote === "'") {
|
|
964
|
+
return quotedNestedReferenceParts(content, quote);
|
|
965
|
+
}
|
|
966
|
+
const separator = content.indexOf(",");
|
|
967
|
+
const base = (separator === -1 ? content : content.slice(0, separator)).trim();
|
|
968
|
+
return base.length === 0
|
|
969
|
+
? null
|
|
970
|
+
: { base, options: separator === -1 ? "" : content.slice(separator + 1) };
|
|
971
|
+
};
|
|
972
|
+
const nestedReferenceContext = (options) => {
|
|
973
|
+
const match = NESTED_CONTEXT.exec(options);
|
|
974
|
+
const context = match?.[1] ?? match?.[2] ?? null;
|
|
975
|
+
return context === null || context.length === 0 ? null : context;
|
|
976
|
+
};
|
|
977
|
+
const adjustedParenthesisDepth = (character, depth) => {
|
|
978
|
+
if (character === "(") {
|
|
979
|
+
return depth + 1;
|
|
980
|
+
}
|
|
981
|
+
return character === ")" ? depth - 1 : depth;
|
|
982
|
+
};
|
|
983
|
+
const nestedReferenceEnd = (text, start) => {
|
|
984
|
+
let depth = 0;
|
|
985
|
+
const tokens = text.slice(start).matchAll(NESTED_STRUCTURE_TOKEN);
|
|
986
|
+
for (const token of tokens) {
|
|
987
|
+
const character = token[0];
|
|
988
|
+
if (character === ")" && depth === 0) {
|
|
989
|
+
return start + token.index;
|
|
990
|
+
}
|
|
991
|
+
depth = adjustedParenthesisDepth(character, depth);
|
|
992
|
+
}
|
|
993
|
+
return -1;
|
|
994
|
+
};
|
|
995
|
+
const nestedReferenceContents = (text) => {
|
|
996
|
+
const contents = [];
|
|
997
|
+
let offset = 0;
|
|
998
|
+
while (offset < text.length) {
|
|
999
|
+
const prefix = text.indexOf("$t(", offset);
|
|
1000
|
+
if (prefix === -1) {
|
|
1001
|
+
break;
|
|
1002
|
+
}
|
|
1003
|
+
const start = prefix + 3;
|
|
1004
|
+
const end = nestedReferenceEnd(text, start);
|
|
1005
|
+
if (end === -1) {
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
contents.push(text.slice(start, end));
|
|
1009
|
+
offset = end + 1;
|
|
1010
|
+
}
|
|
1011
|
+
return contents;
|
|
1012
|
+
};
|
|
1013
|
+
const nestedPluralKeys = (base, context) => {
|
|
1014
|
+
const pluralBase = context === null ? base : `${base}${CONTEXT_SEPARATOR}${context}`;
|
|
1015
|
+
return [`${pluralBase}_one`, `${pluralBase}_other`];
|
|
1016
|
+
};
|
|
1017
|
+
const nestedReferenceKeys = (content) => {
|
|
1018
|
+
const parts = nestedReferenceParts(content);
|
|
1019
|
+
if (parts === null) {
|
|
1020
|
+
return [];
|
|
1021
|
+
}
|
|
1022
|
+
const context = nestedReferenceContext(parts.options);
|
|
1023
|
+
const contextual = context === null
|
|
1024
|
+
? []
|
|
1025
|
+
: [`${parts.base}_${context}`, `${parts.base}${CONTEXT_SEPARATOR}${context}`];
|
|
1026
|
+
return NESTED_COUNT.test(parts.options)
|
|
1027
|
+
? [parts.base, ...contextual, ...nestedPluralKeys(parts.base, context)]
|
|
1028
|
+
: [parts.base, ...contextual];
|
|
1029
|
+
};
|
|
1030
|
+
const nestedEntryKeys = (text) => {
|
|
1031
|
+
const keys = new Set();
|
|
1032
|
+
for (const content of nestedReferenceContents(text)) {
|
|
1033
|
+
for (const key of nestedReferenceKeys(content)) {
|
|
1034
|
+
keys.add(key);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
return keys;
|
|
1038
|
+
};
|
|
1039
|
+
const registerNestedKeyLocations = (root, references, key, locations) => {
|
|
1040
|
+
for (const location of locations) {
|
|
1041
|
+
if (location.line === undefined || location.column === undefined) {
|
|
1042
|
+
continue;
|
|
1043
|
+
}
|
|
1044
|
+
const sourceReferences = references.get(resolve(root, location.file));
|
|
1045
|
+
if (sourceReferences !== undefined) {
|
|
1046
|
+
registerEntryLocation(key, { column: location.column, line: location.line }, sourceReferences);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
const registerNestedTextLocations = (root, references, text, locations) => {
|
|
1051
|
+
for (const key of nestedEntryKeys(text)) {
|
|
1052
|
+
registerNestedKeyLocations(root, references, key, locations);
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
const registerNestedEntryLocations = (root, references, entry, locations) => {
|
|
1056
|
+
registerNestedTextLocations(root, references, entry.key, locations);
|
|
1057
|
+
if (typeof entry.defaultValue === "string") {
|
|
1058
|
+
registerNestedTextLocations(root, references, entry.defaultValue, locations);
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
const didRegisterNestedLocationPass = (root, references, entries, processed) => {
|
|
1062
|
+
let didRegister = false;
|
|
1063
|
+
for (const [mapKey, entry] of entries) {
|
|
1064
|
+
if (processed.has(mapKey)) {
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
const recognized = recognizedEntry(root, references, entry);
|
|
1068
|
+
if (recognized !== null) {
|
|
1069
|
+
processed.add(mapKey);
|
|
1070
|
+
registerNestedEntryLocations(root, references, entry, recognized.locations ?? []);
|
|
1071
|
+
didRegister = true;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
return didRegister;
|
|
1075
|
+
};
|
|
1076
|
+
const registerNestedLocations = (root, references, entries) => {
|
|
1077
|
+
const processed = new Set();
|
|
1078
|
+
let hasRegisteredLocations = true;
|
|
1079
|
+
while (hasRegisteredLocations) {
|
|
1080
|
+
hasRegisteredLocations = didRegisterNestedLocationPass(root, references, entries, processed);
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
const retainRecognizedEntries = (root, references, entries) => {
|
|
1084
|
+
for (const [mapKey, entry] of entries) {
|
|
1085
|
+
const recognized = recognizedEntry(root, references, entry);
|
|
1086
|
+
if (recognized === null) {
|
|
1087
|
+
entries.delete(mapKey);
|
|
1088
|
+
}
|
|
1089
|
+
else {
|
|
1090
|
+
entries.set(mapKey, recognized);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
const fileSourcePointKey = (file, line, column) => `${file}\0${sourceLocationKey(line, column)}`;
|
|
1095
|
+
const registerExtractedSourcePoint = (root, points, location) => {
|
|
1096
|
+
if (location.line !== undefined && location.column !== undefined) {
|
|
1097
|
+
points.add(fileSourcePointKey(resolve(root, location.file), location.line, location.column));
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
const extractedSourcePoints = (root, entries) => {
|
|
1101
|
+
const points = new Set();
|
|
1102
|
+
for (const entry of entries.values()) {
|
|
1103
|
+
const locations = entry.locations ?? [];
|
|
1104
|
+
for (const location of locations) {
|
|
1105
|
+
registerExtractedSourcePoint(root, points, location);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
return points;
|
|
1109
|
+
};
|
|
1110
|
+
const missingSourcePoint = (file, references, extracted) => {
|
|
1111
|
+
for (const [location, point] of references.locations) {
|
|
1112
|
+
if (!extracted.has(`${file}\0${location}`)) {
|
|
1113
|
+
return point;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
return null;
|
|
1117
|
+
};
|
|
1118
|
+
const assertAllReferencesExtracted = (root, references, entries) => {
|
|
1119
|
+
const extracted = extractedSourcePoints(root, entries);
|
|
1120
|
+
for (const [file, sourceReferences] of references) {
|
|
1121
|
+
const point = missingSourcePoint(file, sourceReferences, extracted);
|
|
1122
|
+
if (point !== null) {
|
|
1123
|
+
throw sourceExtractionError(root, file, point, new Error("A translation expression could not be extracted safely"));
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
188
1127
|
const findSourceMessages = async (root, sourceFiles) => {
|
|
189
1128
|
if (sourceFiles.length === 0) {
|
|
190
1129
|
clearI18nResources(root);
|
|
191
1130
|
return [];
|
|
192
1131
|
}
|
|
1132
|
+
const sources = new Map(sourceFiles.map((file) => [file, readFileSync(file, "utf8")]));
|
|
1133
|
+
const references = new Map();
|
|
1134
|
+
for (const [file, source] of sources) {
|
|
1135
|
+
references.set(file, assertSupportedTranslationSyntax(root, file, source));
|
|
1136
|
+
}
|
|
193
1137
|
let extracted;
|
|
194
1138
|
const { logger, reports } = quietLogger();
|
|
195
1139
|
const capture = {
|
|
196
1140
|
name: "gtkx-gettext",
|
|
1141
|
+
onLoad(code, path) {
|
|
1142
|
+
const sourceReferences = references.get(resolve(root, path));
|
|
1143
|
+
return sourceReferences === undefined
|
|
1144
|
+
? code
|
|
1145
|
+
: maskUnrecognizedTranslations(code, sourceReferences);
|
|
1146
|
+
},
|
|
197
1147
|
onEnd(keys) {
|
|
1148
|
+
registerNestedLocations(root, references, keys);
|
|
1149
|
+
retainRecognizedEntries(root, references, keys);
|
|
1150
|
+
assertAllReferencesExtracted(root, references, keys);
|
|
198
1151
|
extracted = keys;
|
|
199
1152
|
},
|
|
200
1153
|
};
|
|
@@ -207,8 +1160,7 @@ const findSourceMessages = async (root, sourceFiles) => {
|
|
|
207
1160
|
if (extracted === undefined || reports.length > 0 || result.hasErrors) {
|
|
208
1161
|
throw new Error(reports.join("\n") || "i18next extraction failed");
|
|
209
1162
|
}
|
|
210
|
-
|
|
211
|
-
return sourceMessages(extracted.values().toArray())
|
|
1163
|
+
return sourceMessages(root, extracted.values().toArray())
|
|
212
1164
|
.map((message) => inferLocations(message, sources))
|
|
213
1165
|
.toSorted(compareMessages);
|
|
214
1166
|
};
|