@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.
Files changed (238) hide show
  1. package/dist/builder.d.ts +1 -0
  2. package/dist/builder.d.ts.map +1 -1
  3. package/dist/builder.js +17 -5
  4. package/dist/builder.js.map +1 -1
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.d.ts.map +1 -1
  7. package/dist/cli.js +9 -1
  8. package/dist/cli.js.map +1 -1
  9. package/dist/codegen/reference.d.ts.map +1 -1
  10. package/dist/codegen/reference.js +1 -0
  11. package/dist/codegen/reference.js.map +1 -1
  12. package/dist/codegen/run-codegen.d.ts +4 -2
  13. package/dist/codegen/run-codegen.d.ts.map +1 -1
  14. package/dist/codegen/run-codegen.js +15 -16
  15. package/dist/codegen/run-codegen.js.map +1 -1
  16. package/dist/codegen/store-resolver.d.ts +2 -1
  17. package/dist/codegen/store-resolver.d.ts.map +1 -1
  18. package/dist/codegen/store-resolver.js +4 -3
  19. package/dist/codegen/store-resolver.js.map +1 -1
  20. package/dist/commands/build.d.ts +5 -0
  21. package/dist/commands/build.d.ts.map +1 -1
  22. package/dist/commands/build.js +83 -10
  23. package/dist/commands/build.js.map +1 -1
  24. package/dist/commands/codegen.d.ts +1 -0
  25. package/dist/commands/codegen.d.ts.map +1 -1
  26. package/dist/commands/codegen.js +6 -5
  27. package/dist/commands/codegen.js.map +1 -1
  28. package/dist/commands/deploy.d.ts +1 -0
  29. package/dist/commands/deploy.d.ts.map +1 -1
  30. package/dist/commands/deploy.js +4 -2
  31. package/dist/commands/deploy.js.map +1 -1
  32. package/dist/commands/dev.d.ts +9 -0
  33. package/dist/commands/dev.d.ts.map +1 -1
  34. package/dist/commands/dev.js +29 -4
  35. package/dist/commands/dev.js.map +1 -1
  36. package/dist/commands/docs.d.ts.map +1 -1
  37. package/dist/commands/docs.js +1 -0
  38. package/dist/commands/docs.js.map +1 -1
  39. package/dist/deploy/freedesktop/metainfo.d.ts.map +1 -1
  40. package/dist/deploy/freedesktop/metainfo.js +41 -2
  41. package/dist/deploy/freedesktop/metainfo.js.map +1 -1
  42. package/dist/deploy/freedesktop/validate.d.ts.map +1 -1
  43. package/dist/deploy/freedesktop/validate.js +10 -4
  44. package/dist/deploy/freedesktop/validate.js.map +1 -1
  45. package/dist/deploy/nfpm/config.d.ts +3 -2
  46. package/dist/deploy/nfpm/config.d.ts.map +1 -1
  47. package/dist/deploy/nfpm/config.js +7 -4
  48. package/dist/deploy/nfpm/config.js.map +1 -1
  49. package/dist/deploy/nfpm/contents.d.ts.map +1 -1
  50. package/dist/deploy/nfpm/contents.js +8 -0
  51. package/dist/deploy/nfpm/contents.js.map +1 -1
  52. package/dist/deploy/node-runtime/elf.d.ts +2 -1
  53. package/dist/deploy/node-runtime/elf.d.ts.map +1 -1
  54. package/dist/deploy/node-runtime/elf.js +32 -5
  55. package/dist/deploy/node-runtime/elf.js.map +1 -1
  56. package/dist/deploy/node-runtime/index.d.ts +2 -1
  57. package/dist/deploy/node-runtime/index.d.ts.map +1 -1
  58. package/dist/deploy/node-runtime/index.js +75 -5
  59. package/dist/deploy/node-runtime/index.js.map +1 -1
  60. package/dist/deploy/notices/node-runtime.js +2 -2
  61. package/dist/deploy/notices/node-runtime.js.map +1 -1
  62. package/dist/deploy/payload/launcher.d.ts.map +1 -1
  63. package/dist/deploy/payload/launcher.js +14 -1
  64. package/dist/deploy/payload/launcher.js.map +1 -1
  65. package/dist/deploy/payload/stage.js +1 -1
  66. package/dist/deploy/payload/stage.js.map +1 -1
  67. package/dist/deploy/run-deploy.d.ts +1 -0
  68. package/dist/deploy/run-deploy.d.ts.map +1 -1
  69. package/dist/deploy/run-deploy.js +100 -14
  70. package/dist/deploy/run-deploy.js.map +1 -1
  71. package/dist/deploy/settings/build-manifest.d.ts +6 -1
  72. package/dist/deploy/settings/build-manifest.d.ts.map +1 -1
  73. package/dist/deploy/settings/build-manifest.js +15 -4
  74. package/dist/deploy/settings/build-manifest.js.map +1 -1
  75. package/dist/deploy/settings/extra-files.d.ts +1 -1
  76. package/dist/deploy/settings/extra-files.d.ts.map +1 -1
  77. package/dist/deploy/settings/extra-files.js +22 -1
  78. package/dist/deploy/settings/extra-files.js.map +1 -1
  79. package/dist/deploy/settings/index.d.ts +1 -0
  80. package/dist/deploy/settings/index.d.ts.map +1 -1
  81. package/dist/deploy/settings/index.js +2 -1
  82. package/dist/deploy/settings/index.js.map +1 -1
  83. package/dist/deploy/settings/paths.d.ts +2 -1
  84. package/dist/deploy/settings/paths.d.ts.map +1 -1
  85. package/dist/deploy/settings/paths.js +113 -5
  86. package/dist/deploy/settings/paths.js.map +1 -1
  87. package/dist/deploy/targets/flatpak-source.d.ts.map +1 -1
  88. package/dist/deploy/targets/flatpak-source.js +11 -2
  89. package/dist/deploy/targets/flatpak-source.js.map +1 -1
  90. package/dist/deploy/targets/nfpm-target.d.ts.map +1 -1
  91. package/dist/deploy/targets/nfpm-target.js +12 -7
  92. package/dist/deploy/targets/nfpm-target.js.map +1 -1
  93. package/dist/deploy/types.d.ts +1 -0
  94. package/dist/deploy/types.d.ts.map +1 -1
  95. package/dist/deploy/types.js.map +1 -1
  96. package/dist/dev/entry-env.d.ts +2 -1
  97. package/dist/dev/entry-env.d.ts.map +1 -1
  98. package/dist/dev/entry-env.js +2 -1
  99. package/dist/dev/entry-env.js.map +1 -1
  100. package/dist/dev/headless.d.ts +3 -0
  101. package/dist/dev/headless.d.ts.map +1 -0
  102. package/dist/dev/headless.js +35 -0
  103. package/dist/dev/headless.js.map +1 -0
  104. package/dist/dev/runner-deps.d.ts +1 -1
  105. package/dist/dev/runner-deps.d.ts.map +1 -1
  106. package/dist/dev/runner-deps.js +3 -3
  107. package/dist/dev/runner-deps.js.map +1 -1
  108. package/dist/dev/runner-main.d.ts.map +1 -1
  109. package/dist/dev/runner-main.js +8 -3
  110. package/dist/dev/runner-main.js.map +1 -1
  111. package/dist/dev/supervisor.d.ts +1 -0
  112. package/dist/dev/supervisor.d.ts.map +1 -1
  113. package/dist/dev/supervisor.js +4 -3
  114. package/dist/dev/supervisor.js.map +1 -1
  115. package/dist/i18n/catalogs.d.ts.map +1 -1
  116. package/dist/i18n/catalogs.js +1 -0
  117. package/dist/i18n/catalogs.js.map +1 -1
  118. package/dist/i18n/source-messages.d.ts.map +1 -1
  119. package/dist/i18n/source-messages.js +960 -8
  120. package/dist/i18n/source-messages.js.map +1 -1
  121. package/dist/internal/build-manifest.d.ts +5 -2
  122. package/dist/internal/build-manifest.d.ts.map +1 -1
  123. package/dist/internal/build-manifest.js +15 -2
  124. package/dist/internal/build-manifest.js.map +1 -1
  125. package/dist/internal/build-output.d.ts +8 -0
  126. package/dist/internal/build-output.d.ts.map +1 -0
  127. package/dist/internal/build-output.js +79 -0
  128. package/dist/internal/build-output.js.map +1 -0
  129. package/dist/internal/docs-elements.d.ts +1 -0
  130. package/dist/internal/docs-elements.d.ts.map +1 -1
  131. package/dist/internal/docs-elements.js +5 -4
  132. package/dist/internal/docs-elements.js.map +1 -1
  133. package/dist/internal/entry-arg.d.ts +4 -1
  134. package/dist/internal/entry-arg.d.ts.map +1 -1
  135. package/dist/internal/entry-arg.js +7 -1
  136. package/dist/internal/entry-arg.js.map +1 -1
  137. package/dist/internal/errors.d.ts +2 -1
  138. package/dist/internal/errors.d.ts.map +1 -1
  139. package/dist/internal/errors.js +1 -1
  140. package/dist/internal/errors.js.map +1 -1
  141. package/dist/internal/output-directory.d.ts +15 -0
  142. package/dist/internal/output-directory.d.ts.map +1 -0
  143. package/dist/internal/output-directory.js +490 -0
  144. package/dist/internal/output-directory.js.map +1 -0
  145. package/dist/internal/parent-process.d.ts +12 -0
  146. package/dist/internal/parent-process.d.ts.map +1 -0
  147. package/dist/internal/parent-process.js +74 -0
  148. package/dist/internal/parent-process.js.map +1 -0
  149. package/dist/internal/prepare-project.d.ts +2 -0
  150. package/dist/internal/prepare-project.d.ts.map +1 -1
  151. package/dist/internal/prepare-project.js +3 -3
  152. package/dist/internal/prepare-project.js.map +1 -1
  153. package/dist/internal/source-imports.js +1 -1
  154. package/dist/internal/source-imports.js.map +1 -1
  155. package/dist/mcp/client.js +2 -2
  156. package/dist/mcp/client.js.map +1 -1
  157. package/dist/vite-plugins/build-manifest.d.ts +5 -1
  158. package/dist/vite-plugins/build-manifest.d.ts.map +1 -1
  159. package/dist/vite-plugins/build-manifest.js +2 -1
  160. package/dist/vite-plugins/build-manifest.js.map +1 -1
  161. package/dist/vite-plugins/ensure-store.d.ts +1 -1
  162. package/dist/vite-plugins/ensure-store.d.ts.map +1 -1
  163. package/dist/vite-plugins/ensure-store.js +2 -2
  164. package/dist/vite-plugins/ensure-store.js.map +1 -1
  165. package/dist/vite-plugins/i18n.d.ts.map +1 -1
  166. package/dist/vite-plugins/i18n.js +58 -6
  167. package/dist/vite-plugins/i18n.js.map +1 -1
  168. package/dist/vite-plugins/index.d.ts +8 -1
  169. package/dist/vite-plugins/index.d.ts.map +1 -1
  170. package/dist/vite-plugins/index.js +6 -2
  171. package/dist/vite-plugins/index.js.map +1 -1
  172. package/dist/vite-plugins/native.d.ts.map +1 -1
  173. package/dist/vite-plugins/native.js +0 -4
  174. package/dist/vite-plugins/native.js.map +1 -1
  175. package/dist/vite-plugins/self-contained.d.ts.map +1 -1
  176. package/dist/vite-plugins/self-contained.js +159 -158
  177. package/dist/vite-plugins/self-contained.js.map +1 -1
  178. package/dist/vite-plugins/settings-worker-env.d.ts +1 -1
  179. package/dist/vite-plugins/settings-worker-env.d.ts.map +1 -1
  180. package/dist/vite-plugins/settings-worker-env.js +2 -2
  181. package/dist/vite-plugins/settings-worker-env.js.map +1 -1
  182. package/dist/vitest-plugin.d.ts +2 -1
  183. package/dist/vitest-plugin.d.ts.map +1 -1
  184. package/dist/vitest-plugin.js +6 -6
  185. package/dist/vitest-plugin.js.map +1 -1
  186. package/package.json +10 -9
  187. package/src/builder.ts +18 -5
  188. package/src/cli.ts +9 -1
  189. package/src/codegen/reference.ts +1 -0
  190. package/src/codegen/run-codegen.ts +17 -19
  191. package/src/codegen/store-resolver.ts +5 -3
  192. package/src/commands/build.ts +13 -2
  193. package/src/commands/codegen.ts +6 -5
  194. package/src/commands/deploy.ts +4 -2
  195. package/src/commands/dev.ts +38 -4
  196. package/src/commands/docs.ts +1 -0
  197. package/src/deploy/freedesktop/metainfo.ts +73 -21
  198. package/src/deploy/freedesktop/validate.ts +12 -4
  199. package/src/deploy/nfpm/config.ts +13 -4
  200. package/src/deploy/nfpm/contents.ts +8 -0
  201. package/src/deploy/node-runtime/elf.ts +43 -5
  202. package/src/deploy/node-runtime/index.ts +107 -6
  203. package/src/deploy/notices/node-runtime.ts +2 -2
  204. package/src/deploy/payload/launcher.ts +20 -1
  205. package/src/deploy/payload/stage.ts +1 -1
  206. package/src/deploy/run-deploy.ts +56 -16
  207. package/src/deploy/settings/build-manifest.ts +28 -3
  208. package/src/deploy/settings/extra-files.ts +26 -2
  209. package/src/deploy/settings/index.ts +3 -1
  210. package/src/deploy/settings/paths.ts +70 -5
  211. package/src/deploy/targets/flatpak-source.ts +13 -2
  212. package/src/deploy/targets/nfpm-target.ts +13 -6
  213. package/src/deploy/types.ts +1 -0
  214. package/src/dev/entry-env.ts +2 -1
  215. package/src/dev/headless.ts +40 -0
  216. package/src/dev/runner-deps.ts +3 -3
  217. package/src/dev/runner-main.ts +10 -3
  218. package/src/dev/supervisor.ts +6 -3
  219. package/src/i18n/catalogs.ts +1 -0
  220. package/src/i18n/source-messages.ts +1429 -28
  221. package/src/internal/build-manifest.ts +25 -1
  222. package/src/internal/build-output.ts +113 -0
  223. package/src/internal/docs-elements.ts +10 -4
  224. package/src/internal/entry-arg.ts +8 -1
  225. package/src/internal/errors.ts +1 -1
  226. package/src/internal/output-directory.ts +699 -0
  227. package/src/internal/parent-process.ts +110 -0
  228. package/src/internal/prepare-project.ts +5 -5
  229. package/src/internal/source-imports.ts +1 -1
  230. package/src/mcp/client.ts +2 -2
  231. package/src/vite-plugins/build-manifest.ts +7 -1
  232. package/src/vite-plugins/ensure-store.ts +2 -2
  233. package/src/vite-plugins/i18n.ts +83 -5
  234. package/src/vite-plugins/index.ts +14 -7
  235. package/src/vite-plugins/native.ts +0 -5
  236. package/src/vite-plugins/self-contained.ts +184 -185
  237. package/src/vite-plugins/settings-worker-env.ts +2 -2
  238. package/src/vitest-plugin.ts +6 -6
@@ -1,4 +1,6 @@
1
1
  import type { ExtractedKey, ExtractedKeysMap, Logger, Plugin } from "i18next-cli";
2
+ import type { ESTree } from "vite";
3
+ import { type NodePath, parseSync as parseBabelSync, type Scope, traverse, types } from "@babel/core";
2
4
  import { isPathInside, isPathWithin, toPosixPath } from "@gtkx/utils";
3
5
  import { runExtractor } from "i18next-cli";
4
6
  import {
@@ -10,8 +12,10 @@ import {
10
12
  writeFileSync,
11
13
  } from "node:fs";
12
14
  import { dirname, isAbsolute, join, relative, resolve } from "node:path";
15
+ import { parseSync, Visitor } from "vite";
13
16
  import type { CatalogProject } from "./catalogs.js";
14
17
  import { runCliTool } from "../internal/run-cli-tool.js";
18
+ import { sourceLanguage } from "../internal/source-imports.js";
15
19
  import { replaceCatalogTemplate } from "./catalog-template.js";
16
20
  import { metadataTemplateFiles } from "./metadata-templates.js";
17
21
  import { clearI18nResources, i18nToolkitConfig } from "./types.js";
@@ -21,6 +25,7 @@ type SourceLocation = {
21
25
  line?: number | undefined;
22
26
  column?: number | undefined;
23
27
  };
28
+ type ExtractedLocation = NonNullable<ExtractedKey["locations"]>[number];
24
29
 
25
30
  type SourceMessage = {
26
31
  context: string | null;
@@ -62,10 +67,144 @@ type PluralGroup = {
62
67
  variants: PluralVariant[];
63
68
  };
64
69
 
70
+ type TranslationReferences = {
71
+ calls: Set<number>;
72
+ elements: Set<number>;
73
+ entryLocations: Map<string, SourcePoint[]>;
74
+ locations: Map<string, SourcePoint>;
75
+ masks: SourceMask[];
76
+ sourcePoints: Map<number, SourcePoint>;
77
+ };
78
+ type SourceMask = { end: number; start: number };
79
+ type SourcePoint = { column: number; line: number };
80
+ type TranslationCallKind = "alias" | "canonical";
81
+ type ImportedBinding = {
82
+ imported: string;
83
+ local: string;
84
+ source: string;
85
+ style: "default" | "named" | "namespace";
86
+ };
87
+ type TranslationHookBinding = { call: types.CallExpression; isCanonical: boolean };
88
+ type TranslationMemberExpression = types.MemberExpression | types.OptionalMemberExpression;
89
+ type ResolvedBinding = NonNullable<ReturnType<Scope["getBinding"]>>;
90
+ type LocatableNode = { loc?: { start: SourcePoint } | null | undefined };
91
+
65
92
  const CONTEXT_SEPARATOR = "\u{4}";
66
93
  const DEFAULT_NAMESPACE = "translation";
67
94
  const POTFILES_FILENAME = "POTFILES.in";
68
95
  const SYNTHETIC_FILENAME = "messages.js";
96
+ const UNSUPPORTED_PLURAL_OPTION = /^defaultValue_(few|many|two|zero)$/u;
97
+ const STATIC_SOURCE_OPTION_NAMES: ReadonlySet<string> = new Set([
98
+ "context",
99
+ "defaultValue",
100
+ "defaultValue_one",
101
+ "defaultValue_other",
102
+ "keyPrefix",
103
+ ]);
104
+ const NESTED_CONTEXT = /["']?context["']?\s*:\s*(?:"([^"]*)"|'([^']*)')/u;
105
+ const NESTED_COUNT = /["']?count["']?\s*:/u;
106
+ const NESTED_STRUCTURE_TOKEN = /"[^"]*"|'[^']*'|[()]/gu;
107
+ const TRANSLATION_MODULES = new Set(["@gtkx/i18n", "i18next"]);
108
+ const TRANS_ELEMENTS: ReadonlySet<string> = new Set(["Trans", "TransWithoutContext"]);
109
+ const STATIC_KEY_WRAPPER_TYPES: ReadonlySet<string> = new Set([
110
+ "ParenthesizedExpression",
111
+ "TSAsExpression",
112
+ "TSSatisfiesExpression",
113
+ "TSTypeAssertion",
114
+ "TSNonNullExpression",
115
+ ]);
116
+
117
+ class SourceExtractionError extends Error {}
118
+
119
+ const sourceErrorMessage = (error: unknown): string =>
120
+ error instanceof Error ? error.message : String(error);
121
+
122
+ const projectSourcePath = (root: string, path: string): string => {
123
+ const absolute = isAbsolute(path) ? resolve(path) : resolve(root, path);
124
+
125
+ return toPosixPath(isPathInside(root, absolute) ? relative(root, absolute) : path);
126
+ };
127
+
128
+ const sourceExtractionError = (
129
+ root: string,
130
+ path: string,
131
+ point: SourcePoint | null,
132
+ error: unknown,
133
+ ): SourceExtractionError => {
134
+ if (error instanceof SourceExtractionError) {
135
+ return error;
136
+ }
137
+
138
+ const file = projectSourcePath(root, path);
139
+ const location = point === null
140
+ ? file
141
+ : `${file}:${String(point.line)}:${String(point.column + 1)}`;
142
+
143
+ return new SourceExtractionError(`${location}: ${sourceErrorMessage(error)}`, { cause: error });
144
+ };
145
+
146
+ const locatablePoint = (node: LocatableNode): SourcePoint | null => {
147
+ const location = node.loc?.start;
148
+
149
+ return location === undefined ? null : { column: location.column, line: location.line };
150
+ };
151
+
152
+ const isSourcePoint = (value: unknown): value is SourcePoint =>
153
+ typeof value === "object" &&
154
+ value !== null &&
155
+ "column" in value &&
156
+ typeof value.column === "number" &&
157
+ "line" in value &&
158
+ typeof value.line === "number";
159
+
160
+ const errorSourcePoint = (error: unknown): SourcePoint | null => {
161
+ if (typeof error !== "object" || error === null) {
162
+ return null;
163
+ }
164
+
165
+ if ("loc" in error && isSourcePoint(error.loc)) {
166
+ return error.loc;
167
+ }
168
+
169
+ return "location" in error && isSourcePoint(error.location) ? error.location : null;
170
+ };
171
+
172
+ const atSourceNode = <T>(
173
+ root: string,
174
+ path: string,
175
+ node: LocatableNode,
176
+ operation: () => T,
177
+ ): T => {
178
+ try {
179
+ return operation();
180
+ } catch (error) {
181
+ throw sourceExtractionError(root, path, locatablePoint(node), error);
182
+ }
183
+ };
184
+
185
+ const atExtractedLocation = <T>(
186
+ root: string,
187
+ locations: SourceLocation[],
188
+ operation: () => T,
189
+ ): T => {
190
+ const location = locations.find((candidate) =>
191
+ candidate.line !== undefined && candidate.column !== undefined);
192
+
193
+ if (location?.line === undefined || location.column === undefined) {
194
+ return operation();
195
+ }
196
+
197
+ try {
198
+ return operation();
199
+ } catch (error) {
200
+ throw sourceExtractionError(
201
+ root,
202
+ location.file,
203
+ { column: location.column, line: location.line },
204
+ error,
205
+ );
206
+ }
207
+ };
69
208
 
70
209
  const quietLogger = (): { logger: Logger; reports: string[] } => {
71
210
  const reports: string[] = [];
@@ -178,11 +317,15 @@ const expandedPlural = (entry: ExtractedKey): { baseKey: string; variant: Plural
178
317
  };
179
318
  };
180
319
 
181
- const groupedPlurals = (entries: ExtractedKey[]): PluralGroup[] => {
320
+ const groupedPlurals = (root: string, entries: ExtractedKey[]): PluralGroup[] => {
182
321
  const groups: Map<string, PluralGroup> = new Map();
183
322
 
184
323
  for (const entry of entries) {
185
- const { baseKey, variant } = expandedPlural(entry);
324
+ const { baseKey, variant } = atExtractedLocation(
325
+ root,
326
+ entry.locations ?? [],
327
+ () => expandedPlural(entry),
328
+ );
186
329
  const namespace = namespaceFor(entry);
187
330
  const key = `${namespace}\0${baseKey}`;
188
331
  const group = groups.get(key) ?? { baseKey, namespace, variants: [] };
@@ -231,8 +374,13 @@ const pluralMessage = ({ baseKey, namespace, variants }: PluralGroup): SourceMes
231
374
  };
232
375
  };
233
376
 
234
- const pluralMessages = (entries: ExtractedKey[]): SourceMessage[] =>
235
- groupedPlurals(entries).map((group) => pluralMessage(group));
377
+ const pluralMessages = (root: string, entries: ExtractedKey[]): SourceMessage[] =>
378
+ groupedPlurals(root, entries).map((group) =>
379
+ atExtractedLocation(
380
+ root,
381
+ group.variants.flatMap((variant) => variant.locations),
382
+ () => pluralMessage(group),
383
+ ));
236
384
 
237
385
  const assertCardinal = (entry: ExtractedKey): void => {
238
386
  if (entry.isOrdinal === true) {
@@ -240,12 +388,14 @@ const assertCardinal = (entry: ExtractedKey): void => {
240
388
  }
241
389
  };
242
390
 
243
- const sourceMessages = (entries: ExtractedKey[]): SourceMessage[] => {
391
+ const sourceMessages = (root: string, entries: ExtractedKey[]): SourceMessage[] => {
244
392
  const points: ExtractedKey[] = [];
245
393
  const plurals: ExtractedKey[] = [];
246
394
 
247
395
  for (const entry of entries) {
248
- assertCardinal(entry);
396
+ atExtractedLocation(root, entry.locations ?? [], () => {
397
+ assertCardinal(entry);
398
+ });
249
399
 
250
400
  if (entry.isExpandedPlural === true) {
251
401
  plurals.push(entry);
@@ -259,7 +409,11 @@ const sourceMessages = (entries: ExtractedKey[]): SourceMessage[] => {
259
409
  points.push(entry);
260
410
  }
261
411
 
262
- return [...points.map((entry) => pointMessage(entry)), ...pluralMessages(plurals)];
412
+ return [
413
+ ...points.map((entry) =>
414
+ atExtractedLocation(root, entry.locations ?? [], () => pointMessage(entry))),
415
+ ...pluralMessages(root, plurals),
416
+ ];
263
417
  };
264
418
 
265
419
  const compareMessages = (left: SourceMessage, right: SourceMessage): number =>
@@ -294,37 +448,1284 @@ const normalizedSourceFiles = (root: string, paths: string[]): string[] => {
294
448
  return new Set(files).values().toArray().toSorted((left, right) => left.localeCompare(right));
295
449
  };
296
450
 
297
- const findSourceMessages = async (root: string, sourceFiles: string[]): Promise<SourceMessage[]> => {
298
- if (sourceFiles.length === 0) {
299
- clearI18nResources(root);
451
+ type StaticKeyWrapper = ESTree.ParenthesizedExpression |
452
+ ESTree.TSAsExpression |
453
+ ESTree.TSSatisfiesExpression |
454
+ ESTree.TSTypeAssertion |
455
+ ESTree.TSNonNullExpression;
300
456
 
301
- return [];
457
+ const isStaticKeyWrapper = (key: ESTree.Node): key is StaticKeyWrapper =>
458
+ STATIC_KEY_WRAPPER_TYPES.has(key.type);
459
+
460
+ const unwrapStaticWrapper = (node: ESTree.Node): ESTree.Node =>
461
+ isStaticKeyWrapper(node) ? unwrapStaticWrapper(node.expression) : node;
462
+
463
+ const isStaticTranslationKey = (key: ESTree.Node | null | undefined): boolean => {
464
+ if (key === undefined || key === null) {
465
+ return false;
302
466
  }
303
467
 
304
- let extracted: ExtractedKeysMap | undefined;
305
- const { logger, reports } = quietLogger();
468
+ if (key.type === "Literal") {
469
+ return typeof key.value === "string";
470
+ }
306
471
 
307
- const capture: Plugin = {
308
- name: "gtkx-gettext",
309
- onEnd(keys) {
310
- extracted = keys;
311
- },
472
+ if (key.type === "TemplateLiteral") {
473
+ return key.expressions.length === 0;
474
+ }
475
+
476
+ if (key.type === "ParenthesizedExpression") {
477
+ return false;
478
+ }
479
+
480
+ if (isStaticKeyWrapper(key)) {
481
+ return isStaticTranslationKey(key.expression);
482
+ }
483
+
484
+ return false;
485
+ };
486
+
487
+ const assertStaticTranslationKey = (key: ESTree.Node | null | undefined): void => {
488
+ if (!isStaticTranslationKey(key)) {
489
+ throw new Error("Translation keys must be string literals");
490
+ }
491
+ };
492
+
493
+ const propertyName = (property: ESTree.ObjectProperty): string | null => {
494
+ if (!property.computed && property.key.type === "Identifier") {
495
+ return property.key.name;
496
+ }
497
+
498
+ return property.key.type === "Literal" && typeof property.key.value === "string"
499
+ ? property.key.value
500
+ : null;
501
+ };
502
+
503
+ const assertSupportedOptionProperty = (property: ESTree.ObjectPropertyKind): void => {
504
+ if (property.type === "SpreadElement") {
505
+ throw new Error("Translation extraction requires explicit option properties");
506
+ }
507
+
508
+ const name = propertyName(property);
509
+
510
+ if (name === null) {
511
+ throw new Error("Translation extraction requires static option property names");
512
+ }
513
+
514
+ if (UNSUPPORTED_PLURAL_OPTION.test(name)) {
515
+ throw new Error("GNU gettext accepts one singular and one plural source string");
516
+ }
517
+
518
+ if (STATIC_SOURCE_OPTION_NAMES.has(name)) {
519
+ assertStaticTranslationKey(property.value);
520
+ }
521
+ };
522
+
523
+ const assertSupportedOptionObject = (options: ESTree.Node | null | undefined): void => {
524
+ if (options === null || options === undefined) {
525
+ return;
526
+ }
527
+
528
+ if (isStaticKeyWrapper(options) && unwrapStaticWrapper(options).type === "ObjectExpression") {
529
+ throw new Error("Translation option objects cannot be wrapped in another expression");
530
+ }
531
+
532
+ if (options.type !== "ObjectExpression") {
533
+ throw new Error("Translation option objects must be inline object literals");
534
+ }
535
+
536
+ for (const property of options.properties) {
537
+ assertSupportedOptionProperty(property);
538
+ }
539
+ };
540
+
541
+ const namedImportedBinding = (path: NodePath, source: string): ImportedBinding | null => {
542
+ if (!path.isImportSpecifier() || path.node.importKind === "type") {
543
+ return null;
544
+ }
545
+
546
+ const imported = path.node.imported;
547
+
548
+ return {
549
+ imported: types.isIdentifier(imported) ? imported.name : imported.value,
550
+ local: path.node.local.name,
551
+ source,
552
+ style: "named",
312
553
  };
554
+ };
313
555
 
314
- const result = await runExtractor(i18nToolkitConfig(root, sourceFiles, [capture]), {
315
- logger,
316
- quiet: true,
317
- syncPrimaryWithDefaults: true,
318
- trustDerivedDefaults: true,
319
- });
556
+ const defaultImportedBinding = (path: NodePath, source: string): ImportedBinding | null =>
557
+ path.isImportDefaultSpecifier()
558
+ ? { imported: "default", local: path.node.local.name, source, style: "default" }
559
+ : null;
320
560
 
321
- if (extracted === undefined || reports.length > 0 || result.hasErrors) {
322
- throw new Error(reports.join("\n") || "i18next extraction failed");
561
+ const namespaceImportedBinding = (path: NodePath, source: string): ImportedBinding | null =>
562
+ path.isImportNamespaceSpecifier()
563
+ ? { imported: "*", local: path.node.local.name, source, style: "namespace" }
564
+ : null;
565
+
566
+ const importedBinding = (binding: ResolvedBinding | undefined): ImportedBinding | null => {
567
+ if (binding === undefined) {
568
+ return null;
323
569
  }
324
570
 
325
- const sources = new Map(sourceFiles.map((file) => [file, readFileSync(file, "utf8")]));
571
+ const path = binding.path;
572
+ const declaration = path.parentPath;
573
+
574
+ if (!declaration.isImportDeclaration() || declaration.node.importKind === "type") {
575
+ return null;
576
+ }
577
+
578
+ const source = declaration.node.source.value;
579
+
580
+ return namedImportedBinding(path, source) ??
581
+ defaultImportedBinding(path, source) ??
582
+ namespaceImportedBinding(path, source);
583
+ };
584
+
585
+ const babelPropertyName = (property: types.ObjectProperty): string | null => {
586
+ const key = property.key;
587
+
588
+ if (!property.computed && types.isIdentifier(key)) {
589
+ return key.name;
590
+ }
591
+
592
+ return types.isStringLiteral(key) ? key.value : null;
593
+ };
594
+
595
+ const isBoundIdentifier = (node: types.Node | null, name: string): boolean => {
596
+ if (types.isIdentifier(node)) {
597
+ return node.name === name;
598
+ }
599
+
600
+ if (types.isAssignmentPattern(node)) {
601
+ return types.isIdentifier(node.left) && node.left.name === name;
602
+ }
603
+
604
+ return false;
605
+ };
606
+
607
+ const isHookPatternBinding = (pattern: types.VariableDeclarator["id"], name: string): boolean => {
608
+ if (types.isObjectPattern(pattern)) {
609
+ return pattern.properties.some((property) =>
610
+ types.isObjectProperty(property) &&
611
+ babelPropertyName(property) === "t" &&
612
+ isBoundIdentifier(property.value, name));
613
+ }
614
+
615
+ if (types.isArrayPattern(pattern)) {
616
+ return isBoundIdentifier(pattern.elements[0] ?? null, name);
617
+ }
618
+
619
+ return false;
620
+ };
621
+
622
+ const importedTranslationHook = (scope: Scope, call: types.CallExpression): ImportedBinding | null => {
623
+ if (!types.isIdentifier(call.callee)) {
624
+ return null;
625
+ }
626
+
627
+ const imported = importedBinding(scope.getBinding(call.callee.name));
628
+
629
+ return imported?.imported === "useTranslation" && TRANSLATION_MODULES.has(imported.source)
630
+ ? imported
631
+ : null;
632
+ };
633
+
634
+ const translationHookBinding = (binding: ResolvedBinding): TranslationHookBinding | null => {
635
+ const path = binding.path;
636
+
637
+ if (!path.isVariableDeclarator()) {
638
+ return null;
639
+ }
640
+
641
+ const pattern = path.node.id;
642
+ const initializer = path.node.init;
643
+
644
+ if (!isHookPatternBinding(pattern, binding.identifier.name) ||
645
+ !types.isCallExpression(initializer)) {
646
+ return null;
647
+ }
648
+
649
+ const imported = importedTranslationHook(path.scope, initializer);
650
+
651
+ if (imported === null) {
652
+ return null;
653
+ }
654
+
655
+ return { call: initializer, isCanonical: imported.local === "useTranslation" };
656
+ };
657
+
658
+ const importedTranslationFunctionKind = (imported: ImportedBinding | null): TranslationCallKind | null => {
659
+ if (imported?.imported !== "t" || !TRANSLATION_MODULES.has(imported.source)) {
660
+ return null;
661
+ }
662
+
663
+ return imported.local === "t" ? "canonical" : "alias";
664
+ };
665
+
666
+ const hookTranslationFunctionKind = (binding: ResolvedBinding | undefined): TranslationCallKind | null => {
667
+ if (binding === undefined) {
668
+ return null;
669
+ }
670
+
671
+ const hook = translationHookBinding(binding);
672
+
673
+ if (hook === null) {
674
+ return null;
675
+ }
676
+
677
+ return binding.identifier.name === "t" && hook.isCanonical ? "canonical" : "alias";
678
+ };
679
+
680
+ const directTranslationFunctionKind = (scope: Scope, name: string): TranslationCallKind | null => {
681
+ const binding = scope.getBinding(name);
682
+
683
+ return importedTranslationFunctionKind(importedBinding(binding)) ?? hookTranslationFunctionKind(binding);
684
+ };
685
+
686
+ const babelMemberName = (member: TranslationMemberExpression): string | null => {
687
+ const property = member.property;
688
+
689
+ if (member.computed) {
690
+ return types.isStringLiteral(property) ? property.value : null;
691
+ }
692
+
693
+ return types.isIdentifier(property) ? property.name : null;
694
+ };
695
+
696
+ const isImportedTranslationObject = (scope: Scope, object: types.Node): boolean => {
697
+ if (!types.isIdentifier(object)) {
698
+ return false;
699
+ }
700
+
701
+ const imported = importedBinding(scope.getBinding(object.name));
702
+
703
+ return imported !== null &&
704
+ TRANSLATION_MODULES.has(imported.source) &&
705
+ (imported.style === "namespace" || (imported.source === "i18next" && imported.imported === "default"));
706
+ };
707
+
708
+ const constantVariableDeclarator = (binding: ResolvedBinding): NodePath<types.VariableDeclarator> | null => {
709
+ const path = binding.path;
710
+ const declaration = path.parentPath;
711
+
712
+ if (!path.isVariableDeclarator() ||
713
+ !declaration.isVariableDeclaration() ||
714
+ declaration.node.kind !== "const") {
715
+ return null;
716
+ }
717
+
718
+ return path;
719
+ };
720
+
721
+ const isTranslationHookResultBinding = (binding: ResolvedBinding | undefined): boolean => {
722
+ if (binding === undefined) {
723
+ return false;
724
+ }
725
+
726
+ const path = constantVariableDeclarator(binding);
727
+ const initializer = path?.node.init;
728
+
729
+ return path !== null &&
730
+ types.isIdentifier(path.node.id) &&
731
+ types.isCallExpression(initializer) &&
732
+ importedTranslationHook(path.scope, initializer) !== null;
733
+ };
734
+
735
+ const isHookTranslationMember = (scope: Scope, member: TranslationMemberExpression): boolean => {
736
+ if (babelMemberName(member) !== "t") {
737
+ return false;
738
+ }
739
+
740
+ const object = member.object;
741
+
742
+ if (types.isCallExpression(object)) {
743
+ return importedTranslationHook(scope, object) !== null;
744
+ }
745
+
746
+ return types.isIdentifier(object) && isTranslationHookResultBinding(scope.getBinding(object.name));
747
+ };
748
+
749
+ const isTranslationMember = (scope: Scope, member: TranslationMemberExpression): boolean =>
750
+ babelMemberName(member) === "t" &&
751
+ (isImportedTranslationObject(scope, member.object) || isHookTranslationMember(scope, member));
752
+
753
+ const isDirectTranslationFunction = (scope: Scope, node: types.Node): boolean =>
754
+ types.isIdentifier(node) && directTranslationFunctionKind(scope, node.name) !== null;
755
+
756
+ const isTranslationFunctionValue = (scope: Scope, node: types.Node): boolean => {
757
+ if (isDirectTranslationFunction(scope, node)) {
758
+ return true;
759
+ }
760
+
761
+ return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
762
+ isTranslationMember(scope, node);
763
+ };
764
+
765
+ const isTranslationBindCall = (scope: Scope, node: types.Node): boolean => {
766
+ if (!types.isCallExpression(node) || !types.isMemberExpression(node.callee)) {
767
+ return false;
768
+ }
769
+
770
+ return babelMemberName(node.callee) === "bind" &&
771
+ isTranslationFunctionValue(scope, node.callee.object);
772
+ };
773
+
774
+ const isTranslationObjectPatternAlias = (
775
+ path: NodePath<types.VariableDeclarator>,
776
+ binding: ResolvedBinding,
777
+ ): boolean => {
778
+ const pattern = path.node.id;
779
+ const initializer = path.node.init;
780
+
781
+ if (!types.isObjectPattern(pattern) ||
782
+ !isHookPatternBinding(pattern, binding.identifier.name) ||
783
+ !types.isIdentifier(initializer)) {
784
+ return false;
785
+ }
786
+
787
+ return isImportedTranslationObject(path.scope, initializer) ||
788
+ isTranslationHookResultBinding(path.scope.getBinding(initializer.name));
789
+ };
790
+
791
+ const isTranslationAliasBinding = (binding: ResolvedBinding | undefined): boolean => {
792
+ if (binding === undefined) {
793
+ return false;
794
+ }
795
+
796
+ const path = constantVariableDeclarator(binding);
797
+
798
+ if (path === null) {
799
+ return false;
800
+ }
801
+
802
+ if (isTranslationObjectPatternAlias(path, binding)) {
803
+ return true;
804
+ }
805
+
806
+ const initializer = path.node.init;
807
+
808
+ return types.isIdentifier(path.node.id) &&
809
+ initializer !== null &&
810
+ initializer !== undefined &&
811
+ (isTranslationFunctionValue(path.scope, initializer) || isTranslationBindCall(path.scope, initializer));
812
+ };
813
+
814
+ const translationFunctionKind = (scope: Scope, name: string): TranslationCallKind | null => {
815
+ const direct = directTranslationFunctionKind(scope, name);
816
+
817
+ if (direct !== null) {
818
+ return direct;
819
+ }
820
+
821
+ return isTranslationAliasBinding(scope.getBinding(name)) ? "alias" : null;
822
+ };
823
+
824
+ const translationElementKind = (path: NodePath, name: string): TranslationCallKind | null => {
825
+ const imported = importedBinding(path.scope.getBinding(name));
826
+
827
+ if (imported?.source !== "@gtkx/i18n" || !TRANS_ELEMENTS.has(imported.imported)) {
828
+ return null;
829
+ }
830
+
831
+ return imported.local === imported.imported ? "canonical" : "alias";
832
+ };
833
+
834
+ const nodeStart = (path: NodePath): number => {
835
+ const start = path.node.start;
836
+
837
+ if (start === null || start === undefined) {
838
+ throw new Error("Translation extraction could not locate a source expression");
839
+ }
840
+
841
+ return start;
842
+ };
843
+
844
+ const registerSourceMask = (node: types.Node, references: TranslationReferences): void => {
845
+ const { start, end } = node;
846
+
847
+ if (start === null || start === undefined || end === null || end === undefined) {
848
+ throw new Error("Translation extraction could not locate a source expression");
849
+ }
850
+
851
+ references.masks.push({ start, end });
852
+ };
853
+
854
+ const sourceLocationKey = (line: number, column: number): string => `${String(line)}:${String(column)}`;
855
+
856
+ const sourcePoint = (node: types.Node): SourcePoint | null => {
857
+ return locatablePoint(node);
858
+ };
859
+
860
+ const registerSourceLocation = (node: types.Node, references: TranslationReferences): SourcePoint | null => {
861
+ const point = sourcePoint(node);
862
+
863
+ if (point !== null) {
864
+ references.locations.set(sourceLocationKey(point.line, point.column), point);
865
+ }
866
+
867
+ return point;
868
+ };
869
+
870
+ const babelStaticKey = (node: types.Node | null | undefined): string | null => {
871
+ if (node === null || node === undefined) {
872
+ return null;
873
+ }
874
+
875
+ if (types.isStringLiteral(node)) {
876
+ return node.value;
877
+ }
878
+
879
+ if (types.isTemplateLiteral(node) && node.expressions.length === 0) {
880
+ return node.quasis[0]?.value.cooked ?? null;
881
+ }
882
+
883
+ if (types.isParenthesizedExpression(node) ||
884
+ types.isTSAsExpression(node) ||
885
+ types.isTSSatisfiesExpression(node) ||
886
+ types.isTSTypeAssertion(node) ||
887
+ types.isTSNonNullExpression(node)) {
888
+ return babelStaticKey(node.expression);
889
+ }
890
+
891
+ return null;
892
+ };
893
+
894
+ const babelObjectExpression = (node: types.Node | null | undefined): types.ObjectExpression | null => {
895
+ if (node === null || node === undefined) {
896
+ return null;
897
+ }
898
+
899
+ if (types.isObjectExpression(node)) {
900
+ return node;
901
+ }
902
+
903
+ if (types.isParenthesizedExpression(node) ||
904
+ types.isTSAsExpression(node) ||
905
+ types.isTSSatisfiesExpression(node) ||
906
+ types.isTSTypeAssertion(node) ||
907
+ types.isTSNonNullExpression(node)) {
908
+ return babelObjectExpression(node.expression);
909
+ }
910
+
911
+ return null;
912
+ };
913
+
914
+ const babelNamedProperty = (
915
+ object: types.ObjectExpression | null,
916
+ name: string,
917
+ ): types.ObjectProperty | null => {
918
+ if (object === null) {
919
+ return null;
920
+ }
921
+
922
+ for (const property of object.properties) {
923
+ if (types.isObjectProperty(property) && babelPropertyName(property) === name) {
924
+ return property;
925
+ }
926
+ }
927
+
928
+ return null;
929
+ };
930
+
931
+ const babelStringProperty = (object: types.ObjectExpression | null, name: string): string | null => {
932
+ const property = babelNamedProperty(object, name);
933
+
934
+ if (property === null) {
935
+ return null;
936
+ }
937
+
938
+ const value = babelStaticKey(property.value);
939
+
940
+ if (value === null) {
941
+ throw new Error(`Translation ${name} values must be string literals`);
942
+ }
943
+
944
+ return value;
945
+ };
946
+
947
+ const callOptions = (call: types.CallExpression): types.ObjectExpression | null =>
948
+ babelObjectExpression(call.arguments[1]) ?? babelObjectExpression(call.arguments[2]);
949
+
950
+ const hookKeyPrefix = (callee: NodePath<types.Identifier>): string => {
951
+ const binding = callee.scope.getBinding(callee.node.name);
952
+ const hook = binding === undefined ? null : translationHookBinding(binding);
953
+
954
+ return babelStringProperty(
955
+ hook === null ? null : babelObjectExpression(hook.call.arguments[1]),
956
+ "keyPrefix",
957
+ ) ?? "";
958
+ };
959
+
960
+ const prefixedTranslationKey = (prefix: string, key: string): string => {
961
+ if (prefix.length === 0) {
962
+ return key;
963
+ }
964
+
965
+ return prefix.endsWith(".") ? `${prefix}${key}` : `${prefix}.${key}`;
966
+ };
967
+
968
+ const extractedEntryKey = (call: types.CallExpression, keyPrefix: string): string | null => {
969
+ const key = babelStaticKey(call.arguments[0]);
970
+
971
+ if (key === null) {
972
+ return null;
973
+ }
974
+
975
+ const options = callOptions(call);
976
+ const callPrefix = babelStringProperty(options, "keyPrefix");
977
+
978
+ if (callPrefix === "" && keyPrefix.length > 0) {
979
+ throw new Error("An empty call keyPrefix cannot override a hook keyPrefix during extraction");
980
+ }
981
+
982
+ const selectedPrefix = callPrefix ?? keyPrefix;
983
+ const prefixed = prefixedTranslationKey(selectedPrefix, key);
984
+ const context = babelStringProperty(options, "context");
985
+
986
+ return context === null ? prefixed : `${prefixed}${CONTEXT_SEPARATOR}${context}`;
987
+ };
988
+
989
+ const registerEntryLocation = (
990
+ key: string | null,
991
+ point: SourcePoint | null,
992
+ references: TranslationReferences,
993
+ ): void => {
994
+ if (key === null || point === null) {
995
+ return;
996
+ }
997
+
998
+ const locations = references.entryLocations.get(key) ?? [];
999
+ locations.push(point);
1000
+ references.entryLocations.set(key, locations);
1001
+ };
1002
+
1003
+ const registerCallReference = (
1004
+ callPath: NodePath<types.CallExpression>,
1005
+ references: TranslationReferences,
1006
+ keyPrefix: string,
1007
+ ): void => {
1008
+ references.calls.add(nodeStart(callPath));
1009
+ const firstArgument = callPath.node.arguments[0];
1010
+ const point = registerSourceLocation(firstArgument ?? callPath.node, references);
1011
+
1012
+ if (point !== null) {
1013
+ references.sourcePoints.set(nodeStart(callPath), point);
1014
+ }
1015
+
1016
+ registerEntryLocation(extractedEntryKey(callPath.node, keyPrefix), point, references);
1017
+ };
1018
+
1019
+ const parserPlugins = (lang: ReturnType<typeof sourceLanguage>): ("decorators" | "jsx" | "typescript")[] => [
1020
+ "decorators",
1021
+ ...(lang === "ts" || lang === "tsx" ? ["typescript" as const] : []),
1022
+ ...(lang === "jsx" || lang === "tsx" ? ["jsx" as const] : []),
1023
+ ];
1024
+
1025
+ const parseTranslationReferences = (
1026
+ root: string,
1027
+ path: string,
1028
+ source: string,
1029
+ lang: ReturnType<typeof sourceLanguage>,
1030
+ ) => {
1031
+ try {
1032
+ return parseBabelSync(source, {
1033
+ ast: true,
1034
+ babelrc: false,
1035
+ configFile: false,
1036
+ filename: projectSourcePath(root, path),
1037
+ parserOpts: { createParenthesizedExpressions: true, plugins: parserPlugins(lang) },
1038
+ sourceType: "module",
1039
+ });
1040
+ } catch (error) {
1041
+ throw sourceExtractionError(root, path, errorSourcePoint(error), error);
1042
+ }
1043
+ };
1044
+
1045
+ const registerIdentifierTranslationCall = (
1046
+ callPath: NodePath<types.CallExpression>,
1047
+ callee: NodePath,
1048
+ references: TranslationReferences,
1049
+ ): void => {
1050
+ if (!callee.isIdentifier()) {
1051
+ return;
1052
+ }
1053
+
1054
+ const kind = translationFunctionKind(callee.scope, callee.node.name);
1055
+
1056
+ if (kind === "alias") {
1057
+ throw new Error("Translation calls must use the imported name t");
1058
+ }
1059
+
1060
+ if (kind === "canonical") {
1061
+ registerCallReference(callPath, references, hookKeyPrefix(callee));
1062
+ } else if (callee.node.name === "t") {
1063
+ registerSourceMask(callee.node, references);
1064
+ }
1065
+ };
1066
+
1067
+ const registerTranslationCall = (
1068
+ callPath: NodePath<types.CallExpression>,
1069
+ references: TranslationReferences,
1070
+ ): void => {
1071
+ assertSupportedWrappedCallee(callPath);
1072
+ const callee = callPath.get("callee");
1073
+
1074
+ if (callee.isIdentifier()) {
1075
+ registerIdentifierTranslationCall(callPath, callee, references);
1076
+
1077
+ return;
1078
+ }
1079
+
1080
+ if (callee.isMemberExpression() && isTranslationMember(callee.scope, callee.node)) {
1081
+ throw new Error("Translation calls must use the named t export");
1082
+ }
1083
+ };
1084
+
1085
+ const unwrapTranslationCallee = (node: types.Node): types.Node => {
1086
+ if (types.isParenthesizedExpression(node) ||
1087
+ types.isTSAsExpression(node) ||
1088
+ types.isTSSatisfiesExpression(node) ||
1089
+ types.isTSTypeAssertion(node) ||
1090
+ types.isTSNonNullExpression(node)) {
1091
+ return unwrapTranslationCallee(node.expression);
1092
+ }
1093
+
1094
+ return node;
1095
+ };
1096
+
1097
+ const isTranslationCallee = (scope: Scope, node: types.Node): boolean => {
1098
+ if (types.isIdentifier(node)) {
1099
+ return translationFunctionKind(scope, node.name) !== null;
1100
+ }
1101
+
1102
+ return (types.isMemberExpression(node) || types.isOptionalMemberExpression(node)) &&
1103
+ isTranslationMember(scope, node);
1104
+ };
1105
+
1106
+ const assertSupportedWrappedCallee = (callPath: NodePath<types.CallExpression>): void => {
1107
+ const callee = callPath.node.callee;
1108
+ const unwrapped = unwrapTranslationCallee(callee);
1109
+
1110
+ if (unwrapped !== callee && isTranslationCallee(callPath.scope, unwrapped)) {
1111
+ throw new Error("Translation call expressions cannot wrap their callee");
1112
+ }
1113
+ };
1114
+
1115
+ const assertSupportedOptionalCall = (callPath: NodePath<types.OptionalCallExpression>): void => {
1116
+ const callee = unwrapTranslationCallee(callPath.node.callee);
1117
+
1118
+ if (isTranslationCallee(callPath.scope, callee)) {
1119
+ throw new Error("Translation calls cannot use optional chaining");
1120
+ }
1121
+ };
1122
+
1123
+ const assertUnsupportedTranslationTag = (tagPath: NodePath<types.TaggedTemplateExpression>): void => {
1124
+ const tag = unwrapTranslationCallee(tagPath.node.tag);
1125
+
1126
+ if (isTranslationCallee(tagPath.scope, tag)) {
1127
+ throw new Error("Translation calls cannot use tagged-template syntax");
1128
+ }
1129
+ };
1130
+
1131
+ const registerUnrecognizedElementMasks = (
1132
+ elementPath: NodePath,
1133
+ name: NodePath<types.JSXIdentifier>,
1134
+ references: TranslationReferences,
1135
+ ): void => {
1136
+ registerSourceMask(name.node, references);
1137
+ const parent = elementPath.parentPath.node;
1138
+ const closingName = types.isJSXElement(parent) ? parent.closingElement?.name : null;
1139
+
1140
+ if (types.isJSXIdentifier(closingName)) {
1141
+ registerSourceMask(closingName, references);
1142
+ }
1143
+ };
1144
+
1145
+ const registerElementReference = (
1146
+ elementPath: NodePath,
1147
+ references: TranslationReferences,
1148
+ ): void => {
1149
+ const start = nodeStart(elementPath);
1150
+ const point = registerSourceLocation(elementPath.node, references);
1151
+ references.elements.add(start);
1152
+
1153
+ if (point !== null) {
1154
+ references.sourcePoints.set(start, point);
1155
+ }
1156
+ };
1157
+
1158
+ const registerTranslationElement = (elementPath: NodePath, references: TranslationReferences): void => {
1159
+ const name = elementPath.get("name");
1160
+
1161
+ if (!name.isJSXIdentifier()) {
1162
+ return;
1163
+ }
1164
+
1165
+ const kind = translationElementKind(name, name.node.name);
1166
+
1167
+ if (kind === "alias") {
1168
+ throw new Error("Translation components must use their imported names");
1169
+ }
1170
+
1171
+ if (kind === "canonical") {
1172
+ registerElementReference(elementPath, references);
1173
+ } else if (TRANS_ELEMENTS.has(name.node.name)) {
1174
+ registerUnrecognizedElementMasks(elementPath, name, references);
1175
+ }
1176
+ };
1177
+
1178
+ const maskUnrecognizedTranslations = (source: string, references: TranslationReferences): string => {
1179
+ let masked = source;
1180
+ const masks = references.masks.toSorted((left, right) => right.start - left.start);
1181
+
1182
+ for (const { start, end } of masks) {
1183
+ masked = `${masked.slice(0, start)}${"_".repeat(end - start)}${masked.slice(end)}`;
1184
+ }
1185
+
1186
+ return masked;
1187
+ };
1188
+
1189
+ const translationReferences = (
1190
+ root: string,
1191
+ path: string,
1192
+ source: string,
1193
+ lang: ReturnType<typeof sourceLanguage>,
1194
+ ): TranslationReferences => {
1195
+ const parsed = parseTranslationReferences(root, path, source, lang);
1196
+
1197
+ if (parsed === null) {
1198
+ throw new Error(`Translation extraction could not parse ${path}`);
1199
+ }
1200
+
1201
+ const references: TranslationReferences = {
1202
+ calls: new Set(),
1203
+ elements: new Set(),
1204
+ entryLocations: new Map(),
1205
+ locations: new Map(),
1206
+ masks: [],
1207
+ sourcePoints: new Map(),
1208
+ };
1209
+
1210
+ traverse(parsed, {
1211
+ CallExpression(callPath) {
1212
+ atSourceNode(root, path, callPath.node, () => {
1213
+ registerTranslationCall(callPath, references);
1214
+ });
1215
+ },
1216
+ JSXOpeningElement(elementPath) {
1217
+ atSourceNode(root, path, elementPath.node, () => {
1218
+ registerTranslationElement(elementPath, references);
1219
+ });
1220
+ },
1221
+ OptionalCallExpression(callPath) {
1222
+ atSourceNode(root, path, callPath.node, () => {
1223
+ assertSupportedOptionalCall(callPath);
1224
+ });
1225
+ },
1226
+ TaggedTemplateExpression(tagPath) {
1227
+ atSourceNode(root, path, tagPath.node, () => {
1228
+ assertUnsupportedTranslationTag(tagPath);
1229
+ });
1230
+ },
1231
+ });
1232
+
1233
+ return references;
1234
+ };
1235
+
1236
+ const assertSupportedTranslationCall = (node: ESTree.CallExpression, references: TranslationReferences): void => {
1237
+ if (!references.calls.has(node.start)) {
1238
+ return;
1239
+ }
1240
+
1241
+ assertStaticTranslationKey(node.arguments[0]);
1242
+ assertSupportedTranslationOptions(node.arguments[1], node.arguments[2]);
1243
+ };
1244
+
1245
+ const assertSupportedTranslationOptions = (
1246
+ second: ESTree.Node | null | undefined,
1247
+ third: ESTree.Node | null | undefined,
1248
+ ): void => {
1249
+ if (third !== undefined && third !== null) {
1250
+ assertStaticTranslationKey(second);
1251
+ assertSupportedOptionObject(third);
1252
+
1253
+ return;
1254
+ }
1255
+
1256
+ if (second === undefined || second === null || isStaticTranslationKey(second)) {
1257
+ return;
1258
+ }
1259
+
1260
+ assertSupportedOptionObject(second);
1261
+ };
1262
+
1263
+ const isNamedJsxAttribute = (attribute: ESTree.JSXAttribute, name: string): boolean =>
1264
+ attribute.name.type === "JSXIdentifier" && attribute.name.name === name;
1265
+
1266
+ const assertSupportedTransKey = (attribute: ESTree.JSXAttribute): void => {
1267
+ if (!isNamedJsxAttribute(attribute, "i18nKey")) {
1268
+ return;
1269
+ }
1270
+
1271
+ const key = attribute.value?.type === "JSXExpressionContainer"
1272
+ ? attribute.value.expression
1273
+ : attribute.value;
1274
+ assertStaticTranslationKey(key);
1275
+ };
1276
+
1277
+ const assertSupportedTransStringAttribute = (attribute: ESTree.JSXAttribute, name: string): void => {
1278
+ if (!isNamedJsxAttribute(attribute, name)) {
1279
+ return;
1280
+ }
1281
+
1282
+ const value = attribute.value?.type === "JSXExpressionContainer"
1283
+ ? attribute.value.expression
1284
+ : attribute.value;
1285
+ assertStaticTranslationKey(value);
1286
+ };
1287
+
1288
+ const assertSupportedTransOptions = (attribute: ESTree.JSXAttribute): void => {
1289
+ if (isNamedJsxAttribute(attribute, "tOptions") && attribute.value?.type === "JSXExpressionContainer") {
1290
+ assertSupportedOptionObject(attribute.value.expression);
1291
+ }
1292
+ };
1293
+
1294
+ const assertSupportedTransAttribute = (attribute: ESTree.JSXAttributeItem): void => {
1295
+ if (attribute.type === "JSXSpreadAttribute") {
1296
+ throw new Error("Translation extraction requires explicit component properties");
1297
+ }
1298
+
1299
+ assertSupportedTransKey(attribute);
1300
+ assertSupportedTransStringAttribute(attribute, "context");
1301
+ assertSupportedTransStringAttribute(attribute, "defaults");
1302
+ assertSupportedTransOptions(attribute);
1303
+ };
1304
+
1305
+ const assertSupportedTransElement = (node: ESTree.JSXOpeningElement, references: TranslationReferences): void => {
1306
+ if (!references.elements.has(node.start)) {
1307
+ return;
1308
+ }
1309
+
1310
+ for (const attribute of node.attributes) {
1311
+ assertSupportedTransAttribute(attribute);
1312
+ }
1313
+ };
1314
+
1315
+ const assertSupportedTranslationSyntax = (root: string, path: string, source: string): TranslationReferences => {
1316
+ const lang = sourceLanguage(path);
1317
+
1318
+ if (lang === undefined) {
1319
+ return {
1320
+ calls: new Set(),
1321
+ elements: new Set(),
1322
+ entryLocations: new Map(),
1323
+ locations: new Map(),
1324
+ masks: [],
1325
+ sourcePoints: new Map(),
1326
+ };
1327
+ }
1328
+
1329
+ let parsed: ReturnType<typeof parseSync>;
1330
+
1331
+ try {
1332
+ parsed = parseSync(projectSourcePath(root, path), source, { lang });
1333
+ } catch (error) {
1334
+ throw sourceExtractionError(root, path, errorSourcePoint(error), error);
1335
+ }
1336
+
1337
+ const references = translationReferences(root, path, source, lang);
1338
+
1339
+ new Visitor({
1340
+ CallExpression(node) {
1341
+ try {
1342
+ assertSupportedTranslationCall(node, references);
1343
+ } catch (error) {
1344
+ throw sourceExtractionError(root, path, references.sourcePoints.get(node.start) ?? null, error);
1345
+ }
1346
+ },
1347
+ JSXOpeningElement(node) {
1348
+ try {
1349
+ assertSupportedTransElement(node, references);
1350
+ } catch (error) {
1351
+ throw sourceExtractionError(root, path, references.sourcePoints.get(node.start) ?? null, error);
1352
+ }
1353
+ },
1354
+ }).visit(parsed.program);
1355
+
1356
+ return references;
1357
+ };
1358
+
1359
+ const extractedLocationFile = (root: string, file: string): string =>
1360
+ resolve(root, file);
1361
+
1362
+ const isRecognizedLocation = (
1363
+ root: string,
1364
+ references: Map<string, TranslationReferences>,
1365
+ location: SourceLocation,
1366
+ ): boolean => {
1367
+ if (location.line === undefined || location.column === undefined) {
1368
+ return false;
1369
+ }
1370
+
1371
+ const sourceReferences = references.get(extractedLocationFile(root, location.file));
1372
+
1373
+ return sourceReferences?.locations.has(sourceLocationKey(location.line, location.column)) === true;
1374
+ };
1375
+
1376
+ const recognizedKeyLocations = (
1377
+ references: Map<string, TranslationReferences>,
1378
+ extractedKey: string,
1379
+ ): ExtractedLocation[] => {
1380
+ const locations: ExtractedLocation[] = [];
1381
+
1382
+ for (const [file, sourceReferences] of references) {
1383
+ const points = sourceReferences.entryLocations.get(extractedKey) ?? [];
1384
+
1385
+ for (const point of points) {
1386
+ locations.push({ file, column: point.column, line: point.line });
1387
+ }
1388
+ }
1389
+
1390
+ return locations;
1391
+ };
1392
+
1393
+ const recognizedEntry = (
1394
+ root: string,
1395
+ references: Map<string, TranslationReferences>,
1396
+ entry: ExtractedKey,
1397
+ ): ExtractedKey | null => {
1398
+ const extractedLocations = entry.locations ?? [];
1399
+ const locations = extractedLocations.length === 0
1400
+ ? recognizedKeyLocations(references, entry.key)
1401
+ : extractedLocations.filter((location) => isRecognizedLocation(root, references, location));
1402
+
1403
+ return locations.length === 0 ? null : { ...entry, locations };
1404
+ };
1405
+
1406
+ type NestedReferenceParts = { base: string; options: string };
1407
+
1408
+ const quotedNestedReferenceParts = (content: string, quote: string): NestedReferenceParts | null => {
1409
+ const end = content.indexOf(quote, 1);
1410
+
1411
+ return end === -1
1412
+ ? null
1413
+ : { base: content.slice(1, end), options: content.slice(end + 1) };
1414
+ };
1415
+
1416
+ const nestedReferenceParts = (value: string): NestedReferenceParts | null => {
1417
+ const content = value.trim();
1418
+ const quote = content[0];
1419
+
1420
+ if (quote === "\"" || quote === "'") {
1421
+ return quotedNestedReferenceParts(content, quote);
1422
+ }
1423
+
1424
+ const separator = content.indexOf(",");
1425
+ const base = (separator === -1 ? content : content.slice(0, separator)).trim();
1426
+
1427
+ return base.length === 0
1428
+ ? null
1429
+ : { base, options: separator === -1 ? "" : content.slice(separator + 1) };
1430
+ };
1431
+
1432
+ const nestedReferenceContext = (options: string): string | null => {
1433
+ const match = NESTED_CONTEXT.exec(options);
1434
+ const context = match?.[1] ?? match?.[2] ?? null;
1435
+
1436
+ return context === null || context.length === 0 ? null : context;
1437
+ };
1438
+
1439
+ const adjustedParenthesisDepth = (character: string | undefined, depth: number): number => {
1440
+ if (character === "(") {
1441
+ return depth + 1;
1442
+ }
1443
+
1444
+ return character === ")" ? depth - 1 : depth;
1445
+ };
1446
+
1447
+ const nestedReferenceEnd = (text: string, start: number): number => {
1448
+ let depth = 0;
1449
+ const tokens = text.slice(start).matchAll(NESTED_STRUCTURE_TOKEN);
1450
+
1451
+ for (const token of tokens) {
1452
+ const character = token[0];
1453
+
1454
+ if (character === ")" && depth === 0) {
1455
+ return start + token.index;
1456
+ }
1457
+
1458
+ depth = adjustedParenthesisDepth(character, depth);
1459
+ }
1460
+
1461
+ return -1;
1462
+ };
1463
+
1464
+ const nestedReferenceContents = (text: string): string[] => {
1465
+ const contents: string[] = [];
1466
+ let offset = 0;
1467
+
1468
+ while (offset < text.length) {
1469
+ const prefix = text.indexOf("$t(", offset);
1470
+
1471
+ if (prefix === -1) {
1472
+ break;
1473
+ }
1474
+
1475
+ const start = prefix + 3;
1476
+ const end = nestedReferenceEnd(text, start);
1477
+
1478
+ if (end === -1) {
1479
+ break;
1480
+ }
1481
+
1482
+ contents.push(text.slice(start, end));
1483
+ offset = end + 1;
1484
+ }
1485
+
1486
+ return contents;
1487
+ };
1488
+
1489
+ const nestedPluralKeys = (base: string, context: string | null): string[] => {
1490
+ const pluralBase = context === null ? base : `${base}${CONTEXT_SEPARATOR}${context}`;
1491
+
1492
+ return [`${pluralBase}_one`, `${pluralBase}_other`];
1493
+ };
1494
+
1495
+ const nestedReferenceKeys = (content: string): string[] => {
1496
+ const parts = nestedReferenceParts(content);
1497
+
1498
+ if (parts === null) {
1499
+ return [];
1500
+ }
1501
+
1502
+ const context = nestedReferenceContext(parts.options);
1503
+ const contextual = context === null
1504
+ ? []
1505
+ : [`${parts.base}_${context}`, `${parts.base}${CONTEXT_SEPARATOR}${context}`];
1506
+
1507
+ return NESTED_COUNT.test(parts.options)
1508
+ ? [parts.base, ...contextual, ...nestedPluralKeys(parts.base, context)]
1509
+ : [parts.base, ...contextual];
1510
+ };
1511
+
1512
+ const nestedEntryKeys = (text: string): Set<string> => {
1513
+ const keys: Set<string> = new Set();
1514
+
1515
+ for (const content of nestedReferenceContents(text)) {
1516
+ for (const key of nestedReferenceKeys(content)) {
1517
+ keys.add(key);
1518
+ }
1519
+ }
1520
+
1521
+ return keys;
1522
+ };
1523
+
1524
+ const registerNestedKeyLocations = (
1525
+ root: string,
1526
+ references: Map<string, TranslationReferences>,
1527
+ key: string,
1528
+ locations: ExtractedLocation[],
1529
+ ): void => {
1530
+ for (const location of locations) {
1531
+ if (location.line === undefined || location.column === undefined) {
1532
+ continue;
1533
+ }
1534
+
1535
+ const sourceReferences = references.get(resolve(root, location.file));
1536
+
1537
+ if (sourceReferences !== undefined) {
1538
+ registerEntryLocation(key, { column: location.column, line: location.line }, sourceReferences);
1539
+ }
1540
+ }
1541
+ };
1542
+
1543
+ const registerNestedTextLocations = (
1544
+ root: string,
1545
+ references: Map<string, TranslationReferences>,
1546
+ text: string,
1547
+ locations: ExtractedLocation[],
1548
+ ): void => {
1549
+ for (const key of nestedEntryKeys(text)) {
1550
+ registerNestedKeyLocations(root, references, key, locations);
1551
+ }
1552
+ };
1553
+
1554
+ const registerNestedEntryLocations = (
1555
+ root: string,
1556
+ references: Map<string, TranslationReferences>,
1557
+ entry: ExtractedKey,
1558
+ locations: ExtractedLocation[],
1559
+ ): void => {
1560
+ registerNestedTextLocations(root, references, entry.key, locations);
1561
+
1562
+ if (typeof entry.defaultValue === "string") {
1563
+ registerNestedTextLocations(root, references, entry.defaultValue, locations);
1564
+ }
1565
+ };
1566
+
1567
+ const didRegisterNestedLocationPass = (
1568
+ root: string,
1569
+ references: Map<string, TranslationReferences>,
1570
+ entries: ExtractedKeysMap,
1571
+ processed: Set<string>,
1572
+ ): boolean => {
1573
+ let didRegister = false;
1574
+
1575
+ for (const [mapKey, entry] of entries) {
1576
+ if (processed.has(mapKey)) {
1577
+ continue;
1578
+ }
1579
+
1580
+ const recognized = recognizedEntry(root, references, entry);
1581
+
1582
+ if (recognized !== null) {
1583
+ processed.add(mapKey);
1584
+ registerNestedEntryLocations(root, references, entry, recognized.locations ?? []);
1585
+ didRegister = true;
1586
+ }
1587
+ }
1588
+
1589
+ return didRegister;
1590
+ };
1591
+
1592
+ const registerNestedLocations = (
1593
+ root: string,
1594
+ references: Map<string, TranslationReferences>,
1595
+ entries: ExtractedKeysMap,
1596
+ ): void => {
1597
+ const processed: Set<string> = new Set();
1598
+ let hasRegisteredLocations = true;
1599
+
1600
+ while (hasRegisteredLocations) {
1601
+ hasRegisteredLocations = didRegisterNestedLocationPass(root, references, entries, processed);
1602
+ }
1603
+ };
1604
+
1605
+ const retainRecognizedEntries = (
1606
+ root: string,
1607
+ references: Map<string, TranslationReferences>,
1608
+ entries: ExtractedKeysMap,
1609
+ ): void => {
1610
+ for (const [mapKey, entry] of entries) {
1611
+ const recognized = recognizedEntry(root, references, entry);
1612
+
1613
+ if (recognized === null) {
1614
+ entries.delete(mapKey);
1615
+ } else {
1616
+ entries.set(mapKey, recognized);
1617
+ }
1618
+ }
1619
+ };
1620
+
1621
+ const fileSourcePointKey = (file: string, line: number, column: number): string =>
1622
+ `${file}\0${sourceLocationKey(line, column)}`;
1623
+
1624
+ const registerExtractedSourcePoint = (
1625
+ root: string,
1626
+ points: Set<string>,
1627
+ location: ExtractedLocation,
1628
+ ): void => {
1629
+ if (location.line !== undefined && location.column !== undefined) {
1630
+ points.add(fileSourcePointKey(resolve(root, location.file), location.line, location.column));
1631
+ }
1632
+ };
1633
+
1634
+ const extractedSourcePoints = (root: string, entries: ExtractedKeysMap): Set<string> => {
1635
+ const points: Set<string> = new Set();
1636
+
1637
+ for (const entry of entries.values()) {
1638
+ const locations = entry.locations ?? [];
1639
+
1640
+ for (const location of locations) {
1641
+ registerExtractedSourcePoint(root, points, location);
1642
+ }
1643
+ }
1644
+
1645
+ return points;
1646
+ };
1647
+
1648
+ const missingSourcePoint = (
1649
+ file: string,
1650
+ references: TranslationReferences,
1651
+ extracted: Set<string>,
1652
+ ): SourcePoint | null => {
1653
+ for (const [location, point] of references.locations) {
1654
+ if (!extracted.has(`${file}\0${location}`)) {
1655
+ return point;
1656
+ }
1657
+ }
1658
+
1659
+ return null;
1660
+ };
1661
+
1662
+ const assertAllReferencesExtracted = (
1663
+ root: string,
1664
+ references: Map<string, TranslationReferences>,
1665
+ entries: ExtractedKeysMap,
1666
+ ): void => {
1667
+ const extracted = extractedSourcePoints(root, entries);
1668
+
1669
+ for (const [file, sourceReferences] of references) {
1670
+ const point = missingSourcePoint(file, sourceReferences, extracted);
1671
+
1672
+ if (point !== null) {
1673
+ throw sourceExtractionError(
1674
+ root,
1675
+ file,
1676
+ point,
1677
+ new Error("A translation expression could not be extracted safely"),
1678
+ );
1679
+ }
1680
+ }
1681
+ };
1682
+
1683
+ const findSourceMessages = async (root: string, sourceFiles: string[]): Promise<SourceMessage[]> => {
1684
+ if (sourceFiles.length === 0) {
1685
+ clearI18nResources(root);
1686
+
1687
+ return [];
1688
+ }
1689
+
1690
+ const sources = new Map(sourceFiles.map((file) => [file, readFileSync(file, "utf8")]));
1691
+ const references: Map<string, TranslationReferences> = new Map();
1692
+
1693
+ for (const [file, source] of sources) {
1694
+ references.set(file, assertSupportedTranslationSyntax(root, file, source));
1695
+ }
1696
+
1697
+ let extracted: ExtractedKeysMap | undefined;
1698
+ const { logger, reports } = quietLogger();
1699
+
1700
+ const capture: Plugin = {
1701
+ name: "gtkx-gettext",
1702
+ onLoad(code, path) {
1703
+ const sourceReferences = references.get(resolve(root, path));
1704
+
1705
+ return sourceReferences === undefined
1706
+ ? code
1707
+ : maskUnrecognizedTranslations(code, sourceReferences);
1708
+ },
1709
+ onEnd(keys) {
1710
+ registerNestedLocations(root, references, keys);
1711
+ retainRecognizedEntries(root, references, keys);
1712
+ assertAllReferencesExtracted(root, references, keys);
1713
+ extracted = keys;
1714
+ },
1715
+ };
1716
+
1717
+ const result = await runExtractor(i18nToolkitConfig(root, sourceFiles, [capture]), {
1718
+ logger,
1719
+ quiet: true,
1720
+ syncPrimaryWithDefaults: true,
1721
+ trustDerivedDefaults: true,
1722
+ });
1723
+
1724
+ if (extracted === undefined || reports.length > 0 || result.hasErrors) {
1725
+ throw new Error(reports.join("\n") || "i18next extraction failed");
1726
+ }
326
1727
 
327
- return sourceMessages(extracted.values().toArray())
1728
+ return sourceMessages(root, extracted.values().toArray())
328
1729
  .map((message) => inferLocations(message, sources))
329
1730
  .toSorted(compareMessages);
330
1731
  };