intlayer-editor 7.1.4 → 7.1.5

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 (53) hide show
  1. package/client/dist/assets/{CodeBlockShiki-DKhht_eB.js → CodeBlockShiki-BJmmGo3n.js} +3 -3
  2. package/client/dist/assets/{bundle-web-Cw-fHx1Z.js → bundle-web-BGkKvp4k.js} +1 -1
  3. package/client/dist/assets/{index-wlmXkWJe.js → index-CJ2_k-Ny.js} +1 -1
  4. package/client/dist/assets/{index-HnG5A4Ve.css → index-UvWk4YN-.css} +0 -42
  5. package/client/dist/index.html +2 -2
  6. package/package.json +16 -15
  7. package/server/dist/_virtual/rolldown_runtime.cjs +0 -4
  8. package/server/dist/controllers/dictionary.controller.cjs +2 -2
  9. package/server/dist/controllers/dictionary.controller.cjs.map +1 -1
  10. package/server/dist/controllers/dictionary.controller.mjs +1 -1
  11. package/server/dist/index.cjs +1 -1
  12. package/server/dist/index.mjs +1 -1
  13. package/server/dist/_virtual/rolldown_runtime.mjs +0 -31
  14. package/server/dist/controllers/configuration.controller.d.ts.map +0 -1
  15. package/server/dist/controllers/dictionary.controller.d.ts.map +0 -1
  16. package/server/dist/packages/@intlayer/chokidar/dist/esm/_virtual/_utils_asset.cjs +0 -99
  17. package/server/dist/packages/@intlayer/chokidar/dist/esm/_virtual/_utils_asset.cjs.map +0 -1
  18. package/server/dist/packages/@intlayer/chokidar/dist/esm/_virtual/_utils_asset.mjs +0 -98
  19. package/server/dist/packages/@intlayer/chokidar/dist/esm/_virtual/_utils_asset.mjs.map +0 -1
  20. package/server/dist/packages/@intlayer/chokidar/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs +0 -36
  21. package/server/dist/packages/@intlayer/chokidar/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs.map +0 -1
  22. package/server/dist/packages/@intlayer/chokidar/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.mjs +0 -35
  23. package/server/dist/packages/@intlayer/chokidar/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.mjs.map +0 -1
  24. package/server/dist/packages/@intlayer/chokidar/dist/esm/utils/getFormatFromExtension.cjs +0 -18
  25. package/server/dist/packages/@intlayer/chokidar/dist/esm/utils/getFormatFromExtension.cjs.map +0 -1
  26. package/server/dist/packages/@intlayer/chokidar/dist/esm/utils/getFormatFromExtension.mjs +0 -17
  27. package/server/dist/packages/@intlayer/chokidar/dist/esm/utils/getFormatFromExtension.mjs.map +0 -1
  28. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/detectFormatCommand.cjs +0 -25
  29. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/detectFormatCommand.cjs.map +0 -1
  30. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/detectFormatCommand.mjs +0 -24
  31. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/detectFormatCommand.mjs.map +0 -1
  32. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.cjs +0 -72
  33. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.cjs.map +0 -1
  34. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs +0 -71
  35. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs.map +0 -1
  36. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/transformJSFile.cjs +0 -1744
  37. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/transformJSFile.cjs.map +0 -1
  38. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/transformJSFile.mjs +0 -1743
  39. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/transformJSFile.mjs.map +0 -1
  40. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.cjs +0 -115
  41. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.cjs.map +0 -1
  42. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs +0 -114
  43. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs.map +0 -1
  44. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeJSFile.cjs +0 -66
  45. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeJSFile.cjs.map +0 -1
  46. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeJSFile.mjs +0 -65
  47. package/server/dist/packages/@intlayer/chokidar/dist/esm/writeContentDeclaration/writeJSFile.mjs.map +0 -1
  48. package/server/dist/routes/config.routes.d.ts.map +0 -1
  49. package/server/dist/routes/dictionary.routes.d.ts.map +0 -1
  50. package/server/dist/types/Routes.d.ts.map +0 -1
  51. package/server/dist/utils/checkPortAvailability.d.ts.map +0 -1
  52. package/server/dist/utils/httpStatusCodes.d.ts.map +0 -1
  53. package/server/dist/utils/responseData.d.ts.map +0 -1
@@ -1,1744 +0,0 @@
1
- const require_rolldown_runtime = require('../../../../../../_virtual/rolldown_runtime.cjs');
2
- const require_ts_morph$1 = require('../../../../../../node_modules/.bun/ts-morph@27.0.0/node_modules/ts-morph/dist/ts-morph.cjs');
3
- let __intlayer_core = require("@intlayer/core");
4
- let __intlayer_types = require("@intlayer/types");
5
-
6
- //#region ../@intlayer/chokidar/dist/esm/writeContentDeclaration/transformJSFile.mjs
7
- var import_ts_morph = require_ts_morph$1.default;
8
- /**
9
- * Builds a translation initializer string for the 't' function call.
10
- * Creates a properly formatted translation object with locale keys and values.
11
- *
12
- * @param translationMap - Map of locale codes to translation values
13
- * @param typeArgumentsText - Optional generic type arguments for the translation function
14
- * @returns Formatted string for the translation function call
15
- */
16
- const buildTranslationInitializer = (translationMap, typeArgumentsText) => {
17
- const translationEntries = Object.entries(translationMap).sort(([firstKey], [secondKey]) => firstKey.localeCompare(secondKey));
18
- const translationParts = [];
19
- for (const [localeCode, translationValue] of translationEntries) {
20
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
21
- if (typeof translationValue === "string") translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
22
- else if (Array.isArray(translationValue)) {
23
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
24
- translationParts.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
25
- } else translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
26
- }
27
- return `t${typeArgumentsText ?? ""}({ ${translationParts.join(", ")} })`;
28
- };
29
- /**
30
- * Synchronizes numeric suffixes across locales to maintain consistency.
31
- * When updating a fallback locale's numeric suffix, this function updates
32
- * the corresponding numeric suffixes in other locales to match.
33
- *
34
- * This is useful for maintaining numbered lists across translations,
35
- * e.g., "Hello 1" / "Bonjour 1" when updating to "Hello 3".
36
- *
37
- * @param existingTranslationMap - Current translation map with locale values
38
- * @param fallbackLocaleCode - The locale being updated (fallback)
39
- * @param newFallbackValue - The new value for the fallback locale
40
- * @returns Updated translation map with synchronized numeric suffixes
41
- */
42
- const syncNumericSuffixAcrossLocales = (existingTranslationMap, fallbackLocaleCode, newFallbackValue) => {
43
- const updatedTranslationMap = {
44
- ...existingTranslationMap,
45
- [fallbackLocaleCode]: newFallbackValue
46
- };
47
- const trailingNumberMatch = newFallbackValue.match(/\d+(?!.*\d)/);
48
- if (!trailingNumberMatch) return updatedTranslationMap;
49
- const newTrailingNumber = trailingNumberMatch[0];
50
- for (const [localeCode, currentValue] of Object.entries(existingTranslationMap)) {
51
- if (localeCode === fallbackLocaleCode) continue;
52
- if (!currentValue.match(/\d+(?!.*\d)/)) continue;
53
- updatedTranslationMap[localeCode] = currentValue.replace(/(\d+)(?!.*\d)/, newTrailingNumber);
54
- }
55
- return updatedTranslationMap;
56
- };
57
- /**
58
- * Safely formats a key for use in object literals.
59
- * Handles special cases like reserved keywords and non-identifier keys.
60
- *
61
- * @param objectKey - The key to format
62
- * @returns Properly formatted key string
63
- */
64
- const stringifyKey = (objectKey) => {
65
- if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(objectKey)) return JSON.stringify(objectKey);
66
- if (objectKey === "true" || objectKey === "false") return JSON.stringify(objectKey);
67
- return objectKey;
68
- };
69
- /**
70
- * Builds an enumeration initializer string for the 'enu' function call.
71
- * Creates a properly formatted enumeration object with key-value pairs.
72
- *
73
- * @param enumerationMap - Map of enumeration keys to string values
74
- * @returns Formatted string for the enumeration function call, or empty string if invalid
75
- */
76
- const buildEnumerationInitializer = (enumerationMap) => {
77
- const enumerationParts = [];
78
- for (const [enumerationKey, enumerationValue] of Object.entries(enumerationMap)) {
79
- if (typeof enumerationValue !== "string") return "";
80
- enumerationParts.push(`${stringifyKey(enumerationKey)}: ${JSON.stringify(enumerationValue)}`);
81
- }
82
- return `enu({ ${enumerationParts.join(", ")} })`;
83
- };
84
- /**
85
- * Builds a condition initializer string for the 'cond' function call.
86
- * Creates a properly formatted condition object with key-value pairs.
87
- *
88
- * @param conditionMap - Map of condition keys to string values
89
- * @returns Formatted string for the condition function call, or empty string if invalid
90
- */
91
- const buildConditionInitializer = (conditionMap) => {
92
- const conditionParts = [];
93
- for (const [conditionKey, conditionValue] of Object.entries(conditionMap)) {
94
- if (typeof conditionValue !== "string") return "";
95
- conditionParts.push(`${stringifyKey(conditionKey)}: ${JSON.stringify(conditionValue)}`);
96
- }
97
- return `cond({ ${conditionParts.join(", ")} })`;
98
- };
99
- /**
100
- * Builds a gender initializer string for the 'gender' function call.
101
- * Creates a properly formatted gender object with key-value pairs.
102
- *
103
- * @param genderMap - Map of gender keys to string values
104
- * @returns Formatted string for the gender function call, or empty string if invalid
105
- */
106
- const buildGenderInitializer = (genderMap) => {
107
- const genderParts = [];
108
- for (const [genderKey, genderValue] of Object.entries(genderMap)) {
109
- if (typeof genderValue !== "string") return "";
110
- genderParts.push(`${stringifyKey(genderKey)}: ${JSON.stringify(genderValue)}`);
111
- }
112
- return `gender({ ${genderParts.join(", ")} })`;
113
- };
114
- /**
115
- * Builds an insertion initializer string for the 'insert' function call.
116
- * Handles both string content and translation content for insertions.
117
- *
118
- * @param insertionContent - The content to be inserted (string or translation)
119
- * @returns Formatted string for the insertion function call, or undefined if invalid
120
- */
121
- const buildInsertionInitializer = (insertionContent) => {
122
- if (typeof insertionContent === "string") return `insert(${JSON.stringify(insertionContent)})`;
123
- if ((0, __intlayer_core.getNodeType)(insertionContent) === __intlayer_types.NodeType.Translation) {
124
- const translationMap = insertionContent[__intlayer_types.NodeType.Translation] ?? {};
125
- if (!Object.values(translationMap).every((translationValue) => typeof translationValue === "string")) return void 0;
126
- return `insert(${buildTranslationInitializer(translationMap)})`;
127
- }
128
- };
129
- /**
130
- * Builds a file initializer string for the 'file' function call.
131
- * Creates a properly formatted file path reference.
132
- *
133
- * @param filePath - The file path to reference
134
- * @returns Formatted string for the file function call, or undefined if invalid
135
- */
136
- const buildFileInitializer = (filePath) => {
137
- if (typeof filePath === "string") return `file(${JSON.stringify(filePath)})`;
138
- };
139
- /**
140
- * Builds a markdown initializer string for the 'md' function call.
141
- * Handles string content, translation content, and file references for markdown.
142
- *
143
- * @param markdownContent - The markdown content (string, translation, or file reference)
144
- * @returns Formatted string for the markdown function call, or undefined if invalid
145
- */
146
- const buildMarkdownInitializer = (markdownContent) => {
147
- if (typeof markdownContent === "string") return `md(${JSON.stringify(markdownContent)})`;
148
- if ((0, __intlayer_core.getNodeType)(markdownContent) === __intlayer_types.NodeType.Translation) {
149
- const translationMap = markdownContent[__intlayer_types.NodeType.Translation] ?? {};
150
- if (!Object.values(translationMap).every((translationValue) => typeof translationValue === "string")) return void 0;
151
- return `md(${buildTranslationInitializer(translationMap)})`;
152
- }
153
- if ((0, __intlayer_core.getNodeType)(markdownContent) === __intlayer_types.NodeType.File) {
154
- const filePath = markdownContent[__intlayer_types.NodeType.File];
155
- const fileInitializer = buildFileInitializer(filePath);
156
- if (!fileInitializer) return void 0;
157
- return `md(${fileInitializer})`;
158
- }
159
- };
160
- /**
161
- * Builds a nested initializer string for the 'nest' function call.
162
- * Creates a properly formatted nested dictionary reference.
163
- *
164
- * @param nestedContent - The nested content with dictionary key and optional path
165
- * @returns Formatted string for the nested function call, or undefined if invalid
166
- */
167
- const buildNestedInitializer = (nestedContent) => {
168
- if (!nestedContent || typeof nestedContent.dictionaryKey !== "string") return void 0;
169
- if (nestedContent.path && typeof nestedContent.path === "string") return `nest(${JSON.stringify(nestedContent.dictionaryKey)}, ${JSON.stringify(nestedContent.path)})`;
170
- return `nest(${JSON.stringify(nestedContent.dictionaryKey)})`;
171
- };
172
- /**
173
- * Reads an existing translation map from a property in a content object.
174
- * Parses the 't' function call and extracts the translation key-value pairs.
175
- *
176
- * @param contentObject - The object containing the property
177
- * @param propertyName - The name of the property to read
178
- * @returns Translation map with locale keys and values, or undefined if not found
179
- */
180
- const readExistingTranslationMap = (contentObject, propertyName) => {
181
- const property = contentObject.getProperty(propertyName);
182
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
183
- const propertyInitializer = property.getInitializer();
184
- if (!propertyInitializer) return void 0;
185
- if (!import_ts_morph.Node.isCallExpression(propertyInitializer)) return void 0;
186
- const callExpression = propertyInitializer.getExpression();
187
- if (!import_ts_morph.Node.isIdentifier(callExpression) || callExpression.getText() !== "t") return void 0;
188
- const translationArgument = propertyInitializer.getArguments()[0];
189
- if (!translationArgument || !import_ts_morph.Node.isObjectLiteralExpression(translationArgument)) return void 0;
190
- const translationMap = {};
191
- for (const propertyAssignment of translationArgument.getProperties()) {
192
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) continue;
193
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
194
- const valueInitializer = propertyAssignment.getInitializer();
195
- if (valueInitializer && import_ts_morph.Node.isStringLiteral(valueInitializer)) translationMap[cleanPropertyName] = valueInitializer.getLiteralValue();
196
- else if (valueInitializer && import_ts_morph.Node.isArrayLiteralExpression(valueInitializer)) {
197
- const stringArray = [];
198
- for (const arrayElement of valueInitializer.getElements()) {
199
- if (!import_ts_morph.Node.isStringLiteral(arrayElement)) return void 0;
200
- stringArray.push(arrayElement.getLiteralValue());
201
- }
202
- translationMap[cleanPropertyName] = stringArray;
203
- } else return;
204
- }
205
- return translationMap;
206
- };
207
- /**
208
- * Reads an existing map from a function call (enu, cond, or gender).
209
- * Parses the function call and extracts the key-value pairs.
210
- *
211
- * @param contentObject - The object containing the property
212
- * @param propertyName - The name of the property to read
213
- * @param functionName - The name of the function to look for ('enu', 'cond', or 'gender')
214
- * @returns Map with keys and string values, or undefined if not found
215
- */
216
- const readExistingMapFromCall = (contentObject, propertyName, functionName) => {
217
- const property = contentObject.getProperty(propertyName);
218
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
219
- const propertyInitializer = property.getInitializer();
220
- if (!propertyInitializer || !import_ts_morph.Node.isCallExpression(propertyInitializer)) return void 0;
221
- const callExpression = propertyInitializer.getExpression();
222
- if (!import_ts_morph.Node.isIdentifier(callExpression) || callExpression.getText() !== functionName) return void 0;
223
- const functionArgument = propertyInitializer.getArguments()[0];
224
- if (!functionArgument || !import_ts_morph.Node.isObjectLiteralExpression(functionArgument)) return void 0;
225
- const keyValueMap = {};
226
- for (const propertyAssignment of functionArgument.getProperties()) {
227
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) continue;
228
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
229
- const valueInitializer = propertyAssignment.getInitializer();
230
- if (valueInitializer && import_ts_morph.Node.isStringLiteral(valueInitializer)) keyValueMap[cleanPropertyName] = valueInitializer.getLiteralValue();
231
- }
232
- return keyValueMap;
233
- };
234
- /**
235
- * Extracts generic type arguments text from a call expression.
236
- * Returns the type arguments as a string (e.g., "<string[]>").
237
- *
238
- * @param callExpression - The call expression to extract type arguments from
239
- * @returns Type arguments as a string, or undefined if none found
240
- */
241
- const getCallExpressionTypeArgsText = (callExpression) => {
242
- try {
243
- const typeArguments = callExpression.getTypeArguments();
244
- if (!typeArguments || typeArguments.length === 0) return void 0;
245
- return `<${typeArguments.map((typeArgument) => typeArgument.getText()).join(", ")}>`;
246
- } catch {
247
- return;
248
- }
249
- };
250
- /**
251
- * Reads existing type arguments used in a specific property call.
252
- * Supports both direct calls and nested calls (e.g., md(t<...>(...))).
253
- *
254
- * @param contentObject - The object containing the property
255
- * @param propertyName - The name of the property to read
256
- * @param functionName - The name of the function to look for
257
- * @returns Type arguments as a string, or undefined if not found
258
- */
259
- const readExistingTypeArgsForCall = (contentObject, propertyName, functionName) => {
260
- const property = contentObject.getProperty(propertyName);
261
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
262
- const propertyInitializer = property.getInitializer();
263
- if (!propertyInitializer || !import_ts_morph.Node.isCallExpression(propertyInitializer)) return void 0;
264
- const callExpression = propertyInitializer.getExpression();
265
- if (import_ts_morph.Node.isIdentifier(callExpression) && callExpression.getText() === functionName) return getCallExpressionTypeArgsText(propertyInitializer);
266
- if (functionName === "t" && import_ts_morph.Node.isIdentifier(callExpression) && callExpression.getText() === "md") {
267
- const markdownArgument = propertyInitializer.getArguments()[0];
268
- if (markdownArgument && import_ts_morph.Node.isCallExpression(markdownArgument)) {
269
- const innerExpression = markdownArgument.getExpression();
270
- if (import_ts_morph.Node.isIdentifier(innerExpression) && innerExpression.getText() === "t") return getCallExpressionTypeArgsText(markdownArgument);
271
- }
272
- }
273
- };
274
- /**
275
- * Compares two string maps for equality.
276
- * Filters out non-string values from the first map before comparison.
277
- *
278
- * @param firstMap - First map to compare (may contain non-string values)
279
- * @param secondMap - Second map to compare (should contain only strings)
280
- * @returns True if the string values in both maps are equal
281
- */
282
- const areStringMapsEqual = (firstMap, secondMap) => {
283
- if (!secondMap) return false;
284
- const firstMapStringEntries = Object.entries(firstMap).filter(([, value]) => typeof value === "string");
285
- if (firstMapStringEntries.length !== Object.keys(firstMap).length) return false;
286
- if (firstMapStringEntries.length !== Object.keys(secondMap).length) return false;
287
- for (const [key, value] of firstMapStringEntries) {
288
- if (!(key in secondMap)) return false;
289
- if (secondMap[key] !== value) return false;
290
- }
291
- return true;
292
- };
293
- /**
294
- * Compares translation maps for equality.
295
- * Handles both string and array values in translations.
296
- *
297
- * @param desiredTranslationMap - The desired translation map
298
- * @param existingTranslationMap - The existing translation map to compare against
299
- * @returns True if both translation maps are equal
300
- */
301
- const areTranslationsEqual = (desiredTranslationMap, existingTranslationMap) => {
302
- if (!existingTranslationMap) return false;
303
- for (const [localeCode, desiredValue] of Object.entries(desiredTranslationMap)) {
304
- if (!(localeCode in existingTranslationMap)) return false;
305
- const existingValue = existingTranslationMap[localeCode];
306
- if (typeof desiredValue === "string") {
307
- if (typeof existingValue !== "string") return false;
308
- if (existingValue !== desiredValue) return false;
309
- } else if (Array.isArray(desiredValue)) {
310
- if (!Array.isArray(existingValue)) return false;
311
- if (existingValue.length !== desiredValue.length) return false;
312
- for (let arrayIndex = 0; arrayIndex < desiredValue.length; arrayIndex++) if (existingValue[arrayIndex] !== desiredValue[arrayIndex]) return false;
313
- } else return false;
314
- }
315
- return true;
316
- };
317
- /**
318
- * Gets existing property names from the content object.
319
- * Handles both regular property assignments and shorthand properties.
320
- *
321
- * @param contentObject - The object literal expression to extract property names from
322
- * @returns Set of existing property names
323
- */
324
- const getExistingPropertyNames = (contentObject) => {
325
- const existingPropertyNames = /* @__PURE__ */ new Set();
326
- for (const property of contentObject.getProperties()) {
327
- if (import_ts_morph.Node.isPropertyAssignment(property)) {
328
- const propertyName = property.getName();
329
- if (propertyName) existingPropertyNames.add(propertyName.replace(/^['"]|['"]$/g, ""));
330
- continue;
331
- }
332
- if (import_ts_morph.Node.isShorthandPropertyAssignment(property)) {
333
- const shorthandPropertyName = property.getNameNode().getText();
334
- if (shorthandPropertyName) existingPropertyNames.add(shorthandPropertyName);
335
- }
336
- }
337
- return existingPropertyNames;
338
- };
339
- /**
340
- * Processes array content entries.
341
- * Handles arrays of various content types including strings, objects, and complex content nodes.
342
- * Supports nested objects within arrays and maintains existing translation structures.
343
- *
344
- * @param contentObject - The object containing the array property
345
- * @param propertyKey - The key of the array property
346
- * @param arrayValue - The array of values to process
347
- * @param existingPropertyKeys - Set of existing property names in the content object
348
- * @param effectiveFallbackLocale - The fallback locale for translations
349
- * @param requiredImports - Set to track required imports
350
- * @param sourceFile - The source file being processed
351
- * @returns True if the content was modified
352
- */
353
- const processArrayContent = (contentObject, propertyKey, arrayValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
354
- if (!existingPropertyKeys.has(propertyKey)) {
355
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
356
- if (spreadTargetObject) return processArrayContent(spreadTargetObject, propertyKey, arrayValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
357
- }
358
- const serializedArrayElements = [];
359
- let hasUnsupportedContent = false;
360
- let existingArrayElements;
361
- let existingArrayHasTranslation = false;
362
- let existingArrayTypeArguments;
363
- let arrayWasChanged = false;
364
- const existingProperty = contentObject.getProperty(propertyKey);
365
- if (existingProperty && import_ts_morph.Node.isPropertyAssignment(existingProperty)) {
366
- const propertyInitializer = existingProperty.getInitializer();
367
- let existingPropertyTypeArguments;
368
- const areAllDesiredValuesStrings = arrayValue.every((arrayElement) => typeof arrayElement === "string");
369
- if (propertyInitializer && import_ts_morph.Node.isCallExpression(propertyInitializer) && import_ts_morph.Node.isIdentifier(propertyInitializer.getExpression()) && propertyInitializer.getExpression().getText() === "t" && areAllDesiredValuesStrings) {
370
- existingPropertyTypeArguments = getCallExpressionTypeArgsText(propertyInitializer);
371
- const existingTranslationMap = readExistingTranslationMap(contentObject, propertyKey);
372
- if (existingTranslationMap) {
373
- const translationInitializerText = buildTranslationInitializer({
374
- ...existingTranslationMap,
375
- [effectiveFallbackLocale]: arrayValue
376
- }, existingPropertyTypeArguments);
377
- requiredImports.add("t");
378
- const property$1 = contentObject.getProperty(propertyKey);
379
- if (property$1 && import_ts_morph.Node.isPropertyAssignment(property$1)) {
380
- if (property$1.getInitializer()?.getText() !== translationInitializerText) {
381
- property$1.setInitializer(translationInitializerText);
382
- return true;
383
- }
384
- }
385
- return false;
386
- }
387
- }
388
- if (propertyInitializer && import_ts_morph.Node.isArrayLiteralExpression(propertyInitializer)) {
389
- existingArrayElements = propertyInitializer.getElements();
390
- existingArrayHasTranslation = propertyInitializer.getElements().some((arrayElement) => {
391
- if (!import_ts_morph.Node.isCallExpression(arrayElement)) return false;
392
- const callExpression = arrayElement.getExpression();
393
- return import_ts_morph.Node.isIdentifier(callExpression) && callExpression.getText() === "t";
394
- });
395
- if (existingArrayHasTranslation) {
396
- for (const arrayElement of existingArrayElements) if (import_ts_morph.Node.isCallExpression(arrayElement)) {
397
- const callExpression = arrayElement.getExpression();
398
- if (import_ts_morph.Node.isIdentifier(callExpression) && callExpression.getText() === "t") {
399
- existingArrayTypeArguments = getCallExpressionTypeArgsText(arrayElement);
400
- if (existingArrayTypeArguments) break;
401
- }
402
- }
403
- }
404
- }
405
- }
406
- for (let elementIndex = 0; elementIndex < arrayValue.length; elementIndex++) {
407
- const currentElement = arrayValue[elementIndex];
408
- if (currentElement === null || currentElement === void 0 || typeof currentElement === "string" || typeof currentElement === "number" || typeof currentElement === "boolean") {
409
- let serializedElementValue = serializeValue(currentElement);
410
- if (typeof currentElement === "string" && existingArrayElements && elementIndex < existingArrayElements.length) {
411
- const existingArrayElement = existingArrayElements[elementIndex];
412
- if (import_ts_morph.Node.isCallExpression(existingArrayElement)) {
413
- const callExpression = existingArrayElement.getExpression();
414
- if (import_ts_morph.Node.isIdentifier(callExpression) && callExpression.getText() === "t") {
415
- const translationArgument = existingArrayElement.getArguments()[0];
416
- if (translationArgument && import_ts_morph.Node.isObjectLiteralExpression(translationArgument)) {
417
- const translationMap = {};
418
- for (const propertyAssignment of translationArgument.getProperties()) {
419
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) continue;
420
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
421
- const propertyValue = propertyAssignment.getInitializer();
422
- if (propertyValue && import_ts_morph.Node.isStringLiteral(propertyValue)) translationMap[cleanPropertyName] = propertyValue.getLiteralValue();
423
- }
424
- serializedElementValue = buildTranslationInitializer(syncNumericSuffixAcrossLocales(translationMap, effectiveFallbackLocale, currentElement), getCallExpressionTypeArgsText(existingArrayElement));
425
- requiredImports.add("t");
426
- }
427
- }
428
- }
429
- }
430
- if (typeof currentElement === "string" && existingArrayHasTranslation && serializedElementValue && serializedElementValue.startsWith("\"")) {
431
- serializedElementValue = buildTranslationInitializer({ [effectiveFallbackLocale]: currentElement }, existingArrayTypeArguments);
432
- requiredImports.add("t");
433
- }
434
- if (serializedElementValue === void 0) {
435
- hasUnsupportedContent = true;
436
- break;
437
- }
438
- serializedArrayElements.push(serializedElementValue);
439
- } else if (typeof currentElement === "object" && currentElement !== null) {
440
- if (existingArrayElements && elementIndex < existingArrayElements.length) {
441
- const existingArrayElement = existingArrayElements[elementIndex];
442
- if (import_ts_morph.Node.isObjectLiteralExpression(existingArrayElement)) {
443
- if (processContentEntries(existingArrayElement, currentElement, effectiveFallbackLocale, requiredImports, sourceFile)) arrayWasChanged = true;
444
- serializedArrayElements.push(existingArrayElement.getText());
445
- } else {
446
- const serializedElementValue = serializeValue(currentElement);
447
- if (serializedElementValue === void 0) {
448
- hasUnsupportedContent = true;
449
- break;
450
- }
451
- serializedArrayElements.push(serializedElementValue);
452
- }
453
- } else {
454
- const serializedElementValue = serializeValue(currentElement);
455
- if (serializedElementValue === void 0) {
456
- hasUnsupportedContent = true;
457
- break;
458
- }
459
- serializedArrayElements.push(serializedElementValue);
460
- }
461
- const elementNodeType = (0, __intlayer_core.getNodeType)(currentElement);
462
- if (elementNodeType === __intlayer_types.NodeType.Translation) requiredImports.add("t");
463
- else if (elementNodeType === __intlayer_types.NodeType.Enumeration) requiredImports.add("enu");
464
- else if (elementNodeType === __intlayer_types.NodeType.Condition) requiredImports.add("cond");
465
- else if (elementNodeType === __intlayer_types.NodeType.Gender) requiredImports.add("gender");
466
- else if (elementNodeType === __intlayer_types.NodeType.Insertion) {
467
- requiredImports.add("insert");
468
- const insertionContent = currentElement[__intlayer_types.NodeType.Insertion];
469
- if (typeof insertionContent === "object" && insertionContent !== null && (0, __intlayer_core.getNodeType)(insertionContent) === __intlayer_types.NodeType.Translation) requiredImports.add("t");
470
- } else if (elementNodeType === __intlayer_types.NodeType.Markdown) {
471
- requiredImports.add("md");
472
- const markdownContent = currentElement[__intlayer_types.NodeType.Markdown];
473
- if (typeof markdownContent === "object" && markdownContent !== null && (0, __intlayer_core.getNodeType)(markdownContent) === __intlayer_types.NodeType.File) requiredImports.add("file");
474
- } else if (elementNodeType === __intlayer_types.NodeType.File) requiredImports.add("file");
475
- else if (elementNodeType === __intlayer_types.NodeType.Nested) requiredImports.add("nest");
476
- } else {
477
- hasUnsupportedContent = true;
478
- break;
479
- }
480
- }
481
- if (hasUnsupportedContent) return false;
482
- if (arrayWasChanged) return true;
483
- const arrayInitializerText = `[ ${serializedArrayElements.join(", ")} ]`;
484
- if (!existingPropertyKeys.has(propertyKey)) {
485
- contentObject.addPropertyAssignment({
486
- name: propertyKey,
487
- initializer: arrayInitializerText
488
- });
489
- return true;
490
- }
491
- const property = contentObject.getProperty(propertyKey);
492
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
493
- const existingSerializedArray = readExistingArraySerialized(contentObject, propertyKey);
494
- if (!(existingSerializedArray !== void 0 && existingSerializedArray.length === serializedArrayElements.length && existingSerializedArray.every((existingElement, elementIndex) => existingElement === serializedArrayElements[elementIndex]))) {
495
- property.setInitializer(arrayInitializerText);
496
- return true;
497
- }
498
- }
499
- return false;
500
- };
501
- /**
502
- * Processes primitive content entries (string, number, boolean, null).
503
- * Handles simple value types and updates existing translation maps when appropriate.
504
- *
505
- * @param contentObject - The object containing the property
506
- * @param propertyKey - The key of the property to process
507
- * @param primitiveValue - The primitive value to process
508
- * @param existingPropertyKeys - Set of existing property names
509
- * @param effectiveFallbackLocale - The fallback locale for translations
510
- * @param requiredImports - Set to track required imports
511
- * @param sourceFile - The source file being processed
512
- * @returns True if the content was modified
513
- */
514
- const processPrimitiveContent = (contentObject, propertyKey, primitiveValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
515
- if (typeof primitiveValue === "string" && existingPropertyKeys.has(propertyKey)) {
516
- const property$1 = contentObject.getProperty(propertyKey);
517
- if (property$1 && import_ts_morph.Node.isPropertyAssignment(property$1)) {
518
- const propertyInitializer = property$1.getInitializer();
519
- if (propertyInitializer && !import_ts_morph.Node.isStringLiteral(propertyInitializer) && !import_ts_morph.Node.isCallExpression(propertyInitializer)) {
520
- console.log(`Skipping update for key "${propertyKey}" because existing value is not a string literal`);
521
- return false;
522
- }
523
- }
524
- const existingTranslationMap = readExistingTranslationMap(contentObject, propertyKey);
525
- if (existingTranslationMap) {
526
- const translationInitializerText = buildTranslationInitializer({
527
- ...existingTranslationMap,
528
- [effectiveFallbackLocale]: primitiveValue
529
- }, readExistingTypeArgsForCall(contentObject, propertyKey, "t"));
530
- requiredImports.add("t");
531
- if (property$1 && import_ts_morph.Node.isPropertyAssignment(property$1)) {
532
- property$1.setInitializer(translationInitializerText);
533
- return true;
534
- }
535
- return false;
536
- }
537
- }
538
- if (!existingPropertyKeys.has(propertyKey)) {
539
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
540
- if (spreadTargetObject) return processPrimitiveContent(spreadTargetObject, propertyKey, primitiveValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
541
- contentObject.addPropertyAssignment({
542
- name: propertyKey,
543
- initializer: typeof primitiveValue === "string" ? JSON.stringify(primitiveValue) : String(primitiveValue)
544
- });
545
- return true;
546
- }
547
- const property = contentObject.getProperty(propertyKey);
548
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
549
- const propertyInitializer = property.getInitializer();
550
- const isPrimitiveLiteral = propertyInitializer && (import_ts_morph.Node.isStringLiteral(propertyInitializer) || import_ts_morph.Node.isNumericLiteral(propertyInitializer) || propertyInitializer.getKind() === import_ts_morph.SyntaxKind.TrueKeyword || propertyInitializer.getKind() === import_ts_morph.SyntaxKind.FalseKeyword || import_ts_morph.Node.isNullLiteral(propertyInitializer) || import_ts_morph.Node.isCallExpression(propertyInitializer));
551
- if (propertyInitializer && !isPrimitiveLiteral) {
552
- console.log(`Skipping update for key "${propertyKey}" because existing value is not a primitive literal`);
553
- return false;
554
- }
555
- const currentInitializerText = propertyInitializer?.getText();
556
- const desiredInitializerText = typeof primitiveValue === "string" ? JSON.stringify(primitiveValue) : String(primitiveValue);
557
- if (currentInitializerText !== desiredInitializerText) {
558
- property.setInitializer(desiredInitializerText);
559
- return true;
560
- }
561
- }
562
- return false;
563
- };
564
- /**
565
- * Processes complex content entries (translation, enumeration, condition, etc.).
566
- * Routes content to the appropriate specialized processor based on node type.
567
- *
568
- * @param contentObject - The object containing the property
569
- * @param propertyKey - The key of the property to process
570
- * @param contentNode - The complex content node to process
571
- * @param existingPropertyKeys - Set of existing property names
572
- * @param effectiveFallbackLocale - The fallback locale for translations
573
- * @param requiredImports - Set to track required imports
574
- * @param sourceFile - The source file being processed
575
- * @returns True if the content was modified
576
- */
577
- const processComplexContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
578
- switch ((0, __intlayer_core.getNodeType)(contentNode)) {
579
- case __intlayer_types.NodeType.Translation: return processTranslationContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
580
- case __intlayer_types.NodeType.Enumeration: return processEnumerationContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
581
- case __intlayer_types.NodeType.Condition: return processConditionContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
582
- case __intlayer_types.NodeType.Gender: return processGenderContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
583
- case __intlayer_types.NodeType.Insertion: return processInsertionContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
584
- case __intlayer_types.NodeType.Markdown: return processMarkdownContent(contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile);
585
- case __intlayer_types.NodeType.File: return processFileContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
586
- case __intlayer_types.NodeType.Nested: return processNestedContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
587
- default: return false;
588
- }
589
- };
590
- /**
591
- * Processes translation content.
592
- * Handles translation objects with locale keys and string/array values.
593
- *
594
- * @param contentObject - The object containing the property
595
- * @param propertyKey - The key of the property to process
596
- * @param contentNode - The translation content node
597
- * @param existingPropertyKeys - Set of existing property names
598
- * @param requiredImports - Set to track required imports
599
- * @param sourceFile - The source file being processed
600
- * @returns True if the content was modified
601
- */
602
- const processTranslationContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
603
- const translationMap = contentNode[__intlayer_types.NodeType.Translation] ?? {};
604
- const areAllValuesStringsOrArrays = Object.values(translationMap).every((translationValue) => typeof translationValue === "string" || Array.isArray(translationValue));
605
- if (Object.values(translationMap).some((translationValue) => typeof translationValue === "object" && translationValue !== null && !Array.isArray(translationValue) && (0, __intlayer_core.getNodeType)(translationValue) !== __intlayer_types.NodeType.Text) && !areAllValuesStringsOrArrays) {
606
- if (!existingPropertyKeys.has(propertyKey)) {
607
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
608
- if (spreadTargetObject) return processTranslationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
609
- }
610
- const translationParts$1 = [];
611
- let hasUnsupportedValue = false;
612
- for (const [localeCode, translationValue] of Object.entries(translationMap)) {
613
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
614
- if (typeof translationValue === "object" && translationValue !== null && !Array.isArray(translationValue)) {
615
- const serializedValue = serializeValue(translationValue);
616
- if (serializedValue === void 0) {
617
- hasUnsupportedValue = true;
618
- break;
619
- }
620
- translationParts$1.push(`${formattedLocaleKey}: ${serializedValue}`);
621
- const nodeType = (0, __intlayer_core.getNodeType)(translationValue);
622
- if (nodeType === __intlayer_types.NodeType.Markdown) {
623
- requiredImports.add("md");
624
- const markdownContent = translationValue[__intlayer_types.NodeType.Markdown];
625
- if (typeof markdownContent === "object" && markdownContent !== null && (0, __intlayer_core.getNodeType)(markdownContent) === __intlayer_types.NodeType.File) requiredImports.add("file");
626
- } else if (nodeType === __intlayer_types.NodeType.File) requiredImports.add("file");
627
- else if (nodeType === __intlayer_types.NodeType.Insertion) requiredImports.add("insert");
628
- else if (nodeType === __intlayer_types.NodeType.Enumeration) requiredImports.add("enu");
629
- else if (nodeType === __intlayer_types.NodeType.Condition) requiredImports.add("cond");
630
- else if (nodeType === __intlayer_types.NodeType.Gender) requiredImports.add("gender");
631
- else if (nodeType === __intlayer_types.NodeType.Nested) requiredImports.add("nest");
632
- } else if (typeof translationValue === "string") translationParts$1.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
633
- else if (Array.isArray(translationValue)) {
634
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
635
- translationParts$1.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
636
- }
637
- }
638
- if (hasUnsupportedValue) return false;
639
- const translationInitializerText$1 = `t${readExistingTypeArgsForCall(contentObject, propertyKey, "t") ?? ""}({ ${translationParts$1.join(", ")} })`;
640
- if (!existingPropertyKeys.has(propertyKey)) {
641
- requiredImports.add("t");
642
- contentObject.addPropertyAssignment({
643
- name: propertyKey,
644
- initializer: translationInitializerText$1
645
- });
646
- return true;
647
- }
648
- const property = contentObject.getProperty(propertyKey);
649
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
650
- if (property.getInitializer()?.getText() !== translationInitializerText$1) {
651
- requiredImports.add("t");
652
- property.setInitializer(translationInitializerText$1);
653
- return true;
654
- }
655
- }
656
- return false;
657
- }
658
- if (!areAllValuesStringsOrArrays) return false;
659
- if (!existingPropertyKeys.has(propertyKey)) {
660
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
661
- if (spreadTargetObject) return processTranslationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
662
- }
663
- const translationParts = [];
664
- for (const [localeCode, translationValue] of Object.entries(translationMap)) {
665
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
666
- if (typeof translationValue === "string") translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
667
- else if (Array.isArray(translationValue)) {
668
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
669
- translationParts.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
670
- }
671
- }
672
- const translationInitializerText = `t${readExistingTypeArgsForCall(contentObject, propertyKey, "t") ?? ""}({ ${translationParts.join(", ")} })`;
673
- if (!existingPropertyKeys.has(propertyKey)) {
674
- requiredImports.add("t");
675
- contentObject.addPropertyAssignment({
676
- name: propertyKey,
677
- initializer: translationInitializerText
678
- });
679
- return true;
680
- }
681
- if (!areTranslationsEqual(translationMap, readExistingTranslationMap(contentObject, propertyKey))) {
682
- requiredImports.add("t");
683
- const property = contentObject.getProperty(propertyKey);
684
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
685
- property.setInitializer(translationInitializerText);
686
- return true;
687
- }
688
- }
689
- return false;
690
- };
691
- /**
692
- * Processes enumeration content.
693
- * Handles enumeration objects with key-value string pairs.
694
- *
695
- * @param contentObject - The object containing the property
696
- * @param propertyKey - The key of the property to process
697
- * @param contentNode - The enumeration content node
698
- * @param existingPropertyKeys - Set of existing property names
699
- * @param requiredImports - Set to track required imports
700
- * @param sourceFile - The source file being processed
701
- * @returns True if the content was modified
702
- */
703
- const processEnumerationContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
704
- const enumerationMap = contentNode[__intlayer_types.NodeType.Enumeration];
705
- if (!Object.values(enumerationMap).every((enumerationValue) => typeof enumerationValue === "string")) return false;
706
- const enumerationInitializerText = buildEnumerationInitializer(enumerationMap);
707
- if (!enumerationInitializerText) return false;
708
- if (!existingPropertyKeys.has(propertyKey)) {
709
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
710
- if (spreadTargetObject) return processEnumerationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
711
- requiredImports.add("enu");
712
- contentObject.addPropertyAssignment({
713
- name: propertyKey,
714
- initializer: enumerationInitializerText
715
- });
716
- return true;
717
- }
718
- if (!areStringMapsEqual(enumerationMap, readExistingMapFromCall(contentObject, propertyKey, "enu"))) {
719
- requiredImports.add("enu");
720
- const property = contentObject.getProperty(propertyKey);
721
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
722
- property.setInitializer(enumerationInitializerText);
723
- return true;
724
- }
725
- }
726
- return false;
727
- };
728
- /**
729
- * Processes condition content.
730
- * Handles condition objects with key-value string pairs.
731
- *
732
- * @param contentObject - The object containing the property
733
- * @param propertyKey - The key of the property to process
734
- * @param contentNode - The condition content node
735
- * @param existingPropertyKeys - Set of existing property names
736
- * @param requiredImports - Set to track required imports
737
- * @param sourceFile - The source file being processed
738
- * @returns True if the content was modified
739
- */
740
- const processConditionContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
741
- const conditionMap = contentNode[__intlayer_types.NodeType.Condition];
742
- if (Object.values(conditionMap).every((conditionValue) => typeof conditionValue === "string")) {
743
- const conditionInitializerText = buildConditionInitializer(conditionMap);
744
- if (!conditionInitializerText) return false;
745
- if (!existingPropertyKeys.has(propertyKey)) {
746
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
747
- if (spreadTargetObject) return processConditionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
748
- requiredImports.add("cond");
749
- contentObject.addPropertyAssignment({
750
- name: propertyKey,
751
- initializer: conditionInitializerText
752
- });
753
- return true;
754
- }
755
- if (!areStringMapsEqual(conditionMap, readExistingMapFromCall(contentObject, propertyKey, "cond"))) {
756
- requiredImports.add("cond");
757
- const property$1 = contentObject.getProperty(propertyKey);
758
- if (property$1 && import_ts_morph.Node.isPropertyAssignment(property$1)) {
759
- property$1.setInitializer(conditionInitializerText);
760
- return true;
761
- }
762
- }
763
- return false;
764
- }
765
- if (!existingPropertyKeys.has(propertyKey)) {
766
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
767
- if (spreadTargetObject) return processConditionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
768
- return false;
769
- }
770
- const property = contentObject.getProperty(propertyKey);
771
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return false;
772
- const propertyInitializer = property.getInitializer();
773
- if (!propertyInitializer || !import_ts_morph.Node.isCallExpression(propertyInitializer)) return false;
774
- const callExpression = propertyInitializer.getExpression();
775
- if (!import_ts_morph.Node.isIdentifier(callExpression) || callExpression.getText() !== "cond") return false;
776
- const condArgument = propertyInitializer.getArguments()[0];
777
- if (!condArgument || !import_ts_morph.Node.isObjectLiteralExpression(condArgument)) return false;
778
- requiredImports.add("cond");
779
- let hasModifications = false;
780
- for (const [conditionKey, conditionValue] of Object.entries(conditionMap)) {
781
- const nodeType = (0, __intlayer_core.getNodeType)(conditionValue);
782
- if (!nodeType) continue;
783
- let condProperty = condArgument.getProperty(conditionKey);
784
- if (!condProperty) condProperty = condArgument.getProperty(stringifyKey(conditionKey));
785
- if (!condProperty || !import_ts_morph.Node.isPropertyAssignment(condProperty)) continue;
786
- const condValueInitializer = condProperty.getInitializer();
787
- if (!condValueInitializer) continue;
788
- if (nodeType === __intlayer_types.NodeType.Translation) {
789
- if (!import_ts_morph.Node.isCallExpression(condValueInitializer)) continue;
790
- const tCallExpression = condValueInitializer.getExpression();
791
- if (!import_ts_morph.Node.isIdentifier(tCallExpression) || tCallExpression.getText() !== "t") continue;
792
- const tArgument = condValueInitializer.getArguments()[0];
793
- if (!tArgument || !import_ts_morph.Node.isObjectLiteralExpression(tArgument)) continue;
794
- const translationMap = conditionValue[__intlayer_types.NodeType.Translation];
795
- if (!translationMap || typeof translationMap !== "object") continue;
796
- const existingTranslationMap = {};
797
- for (const propertyAssignment of tArgument.getProperties()) {
798
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) continue;
799
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
800
- const valueInitializer = propertyAssignment.getInitializer();
801
- if (valueInitializer && import_ts_morph.Node.isStringLiteral(valueInitializer)) existingTranslationMap[cleanPropertyName] = valueInitializer.getLiteralValue();
802
- else if (valueInitializer && import_ts_morph.Node.isArrayLiteralExpression(valueInitializer)) {
803
- const stringArray = [];
804
- for (const arrayElement of valueInitializer.getElements()) if (import_ts_morph.Node.isStringLiteral(arrayElement)) stringArray.push(arrayElement.getLiteralValue());
805
- existingTranslationMap[cleanPropertyName] = stringArray;
806
- }
807
- }
808
- if (!areTranslationsEqual(translationMap, existingTranslationMap)) {
809
- requiredImports.add("t");
810
- for (const [locale, localeValue] of Object.entries(translationMap)) {
811
- const isLocaleCodeValidIdentifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(locale);
812
- const formattedLocaleKey = isLocaleCodeValidIdentifier ? locale : JSON.stringify(locale);
813
- let existingProperty = tArgument.getProperty(locale);
814
- if (!existingProperty && !isLocaleCodeValidIdentifier) existingProperty = tArgument.getProperty(JSON.stringify(locale));
815
- if (existingProperty && import_ts_morph.Node.isPropertyAssignment(existingProperty)) {
816
- const currentValue = existingProperty.getInitializer();
817
- const newValue = Array.isArray(localeValue) ? `[${localeValue.map((v) => JSON.stringify(v)).join(", ")}]` : JSON.stringify(localeValue);
818
- if (currentValue?.getText() !== newValue) {
819
- existingProperty.setInitializer(newValue);
820
- hasModifications = true;
821
- }
822
- } else if (!existingProperty) {
823
- const newValue = Array.isArray(localeValue) ? `[${localeValue.map((v) => JSON.stringify(v)).join(", ")}]` : JSON.stringify(localeValue);
824
- tArgument.addPropertyAssignment({
825
- name: formattedLocaleKey,
826
- initializer: newValue
827
- });
828
- hasModifications = true;
829
- }
830
- }
831
- }
832
- }
833
- }
834
- return hasModifications;
835
- };
836
- /**
837
- * Processes gender content.
838
- * Handles gender objects with key-value string pairs.
839
- *
840
- * @param contentObject - The object containing the property
841
- * @param propertyKey - The key of the property to process
842
- * @param contentNode - The gender content node
843
- * @param existingPropertyKeys - Set of existing property names
844
- * @param requiredImports - Set to track required imports
845
- * @param sourceFile - The source file being processed
846
- * @returns True if the content was modified
847
- */
848
- const processGenderContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
849
- const genderMap = contentNode[__intlayer_types.NodeType.Gender];
850
- if (!Object.values(genderMap).every((genderValue) => typeof genderValue === "string")) return false;
851
- const genderInitializerText = buildGenderInitializer(genderMap);
852
- if (!genderInitializerText) return false;
853
- if (!existingPropertyKeys.has(propertyKey)) {
854
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
855
- if (spreadTargetObject) return processGenderContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
856
- requiredImports.add("gender");
857
- contentObject.addPropertyAssignment({
858
- name: propertyKey,
859
- initializer: genderInitializerText
860
- });
861
- return true;
862
- }
863
- if (!areStringMapsEqual(genderMap, readExistingMapFromCall(contentObject, propertyKey, "gender"))) {
864
- requiredImports.add("gender");
865
- const property = contentObject.getProperty(propertyKey);
866
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
867
- property.setInitializer(genderInitializerText);
868
- return true;
869
- }
870
- }
871
- return false;
872
- };
873
- /**
874
- * Processes insertion content.
875
- * Handles insertion objects with string or translation content.
876
- *
877
- * @param contentObject - The object containing the property
878
- * @param propertyKey - The key of the property to process
879
- * @param contentNode - The insertion content node
880
- * @param existingPropertyKeys - Set of existing property names
881
- * @param requiredImports - Set to track required imports
882
- * @param sourceFile - The source file being processed
883
- * @returns True if the content was modified
884
- */
885
- const processInsertionContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
886
- const insertionContent = contentNode[__intlayer_types.NodeType.Insertion];
887
- const insertionInitializerText = buildInsertionInitializer(insertionContent);
888
- if (!insertionInitializerText) return false;
889
- if (!existingPropertyKeys.has(propertyKey)) {
890
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
891
- if (spreadTargetObject) return processInsertionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
892
- requiredImports.add("insert");
893
- if (typeof insertionContent === "object" && insertionContent !== null && (0, __intlayer_core.getNodeType)(insertionContent) === __intlayer_types.NodeType.Translation) requiredImports.add("t");
894
- contentObject.addPropertyAssignment({
895
- name: propertyKey,
896
- initializer: insertionInitializerText
897
- });
898
- return true;
899
- }
900
- const existingInsertion = readExistingInsertion(contentObject, propertyKey);
901
- if (!(typeof insertionContent === "string" && existingInsertion?.kind === "string" && existingInsertion.value === insertionContent || typeof insertionContent === "object" && insertionContent !== null && (0, __intlayer_core.getNodeType)(insertionContent) === __intlayer_types.NodeType.Translation && existingInsertion?.kind === "translation" && areStringMapsEqual(insertionContent[__intlayer_types.NodeType.Translation] ?? {}, existingInsertion.map))) {
902
- requiredImports.add("insert");
903
- if (typeof insertionContent === "object" && insertionContent !== null && (0, __intlayer_core.getNodeType)(insertionContent) === __intlayer_types.NodeType.Translation) requiredImports.add("t");
904
- const property = contentObject.getProperty(propertyKey);
905
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
906
- property.setInitializer(insertionInitializerText);
907
- return true;
908
- }
909
- }
910
- return false;
911
- };
912
- /**
913
- * Processes markdown content.
914
- * Handles markdown objects with string, translation, or file content.
915
- *
916
- * @param contentObject - The object containing the property
917
- * @param propertyKey - The key of the property to process
918
- * @param contentNode - The markdown content node
919
- * @param existingPropertyKeys - Set of existing property names
920
- * @param effectiveFallbackLocale - The fallback locale for translations
921
- * @param requiredImports - Set to track required imports
922
- * @param sourceFile - The source file being processed
923
- * @returns True if the content was modified
924
- */
925
- const processMarkdownContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
926
- const markdownContent = contentNode[__intlayer_types.NodeType.Markdown];
927
- const markdownInitializerText = buildMarkdownInitializer(markdownContent);
928
- if (!markdownInitializerText) return false;
929
- if (!existingPropertyKeys.has(propertyKey)) {
930
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
931
- if (spreadTargetObject) return processMarkdownContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
932
- requiredImports.add("md");
933
- const markdownNodeType$1 = (0, __intlayer_core.getNodeType)(markdownContent);
934
- if (markdownNodeType$1 === __intlayer_types.NodeType.File) requiredImports.add("file");
935
- else if (markdownNodeType$1 === __intlayer_types.NodeType.Translation) requiredImports.add("t");
936
- contentObject.addPropertyAssignment({
937
- name: propertyKey,
938
- initializer: markdownInitializerText
939
- });
940
- return true;
941
- }
942
- const markdownNodeType = (0, __intlayer_core.getNodeType)(markdownContent);
943
- const existingSimpleMarkdown = readExistingMarkdown(contentObject, propertyKey);
944
- const existingMarkdownTranslationMap = readExistingMarkdownTranslationMap(contentObject, propertyKey);
945
- const existingTranslationTypeArguments = readExistingTypeArgsForCall(contentObject, propertyKey, "t");
946
- if (typeof markdownContent === "string" && existingMarkdownTranslationMap && effectiveFallbackLocale) {
947
- const updatedTranslationMap = {
948
- ...existingMarkdownTranslationMap,
949
- [effectiveFallbackLocale]: markdownContent
950
- };
951
- requiredImports.add("md");
952
- requiredImports.add("t");
953
- const property = contentObject.getProperty(propertyKey);
954
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
955
- property.setInitializer(`md(${buildTranslationInitializer(updatedTranslationMap, existingTranslationTypeArguments)})`);
956
- return true;
957
- }
958
- return false;
959
- }
960
- if (markdownNodeType === __intlayer_types.NodeType.Translation) {
961
- const markdownTranslationMap = markdownContent[__intlayer_types.NodeType.Translation];
962
- if (!Object.values(markdownTranslationMap).every((translationValue) => typeof translationValue === "string")) return false;
963
- if (!areStringMapsEqual(markdownTranslationMap, existingMarkdownTranslationMap)) {
964
- requiredImports.add("md");
965
- requiredImports.add("t");
966
- const property = contentObject.getProperty(propertyKey);
967
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
968
- property.setInitializer(`md(${buildTranslationInitializer(markdownTranslationMap, existingTranslationTypeArguments)})`);
969
- return true;
970
- }
971
- }
972
- return false;
973
- }
974
- if (!(typeof markdownContent === "string" && existingSimpleMarkdown?.kind === "string" && existingSimpleMarkdown.value === markdownContent || markdownNodeType === __intlayer_types.NodeType.File && existingSimpleMarkdown?.kind === "file" && existingSimpleMarkdown.path === markdownContent[__intlayer_types.NodeType.File])) {
975
- requiredImports.add("md");
976
- if (markdownNodeType === __intlayer_types.NodeType.File) requiredImports.add("file");
977
- const property = contentObject.getProperty(propertyKey);
978
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
979
- property.setInitializer(markdownInitializerText);
980
- return true;
981
- }
982
- }
983
- return false;
984
- };
985
- /**
986
- * Processes file content.
987
- * Handles file objects with file path references.
988
- *
989
- * @param contentObject - The object containing the property
990
- * @param propertyKey - The key of the property to process
991
- * @param contentNode - The file content node
992
- * @param existingPropertyKeys - Set of existing property names
993
- * @param requiredImports - Set to track required imports
994
- * @param sourceFile - The source file being processed
995
- * @returns True if the content was modified
996
- */
997
- const processFileContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
998
- const filePath = contentNode[__intlayer_types.NodeType.File];
999
- const fileInitializerText = buildFileInitializer(filePath);
1000
- if (!fileInitializerText) return false;
1001
- if (!existingPropertyKeys.has(propertyKey)) {
1002
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1003
- if (spreadTargetObject) return processFileContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
1004
- requiredImports.add("file");
1005
- contentObject.addPropertyAssignment({
1006
- name: propertyKey,
1007
- initializer: fileInitializerText
1008
- });
1009
- return true;
1010
- }
1011
- if (readExistingFilePath(contentObject, propertyKey) !== filePath) {
1012
- requiredImports.add("file");
1013
- const property = contentObject.getProperty(propertyKey);
1014
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
1015
- property.setInitializer(fileInitializerText);
1016
- return true;
1017
- }
1018
- }
1019
- return false;
1020
- };
1021
- /**
1022
- * Processes nested content.
1023
- * Handles nested objects with dictionary key and optional path references.
1024
- *
1025
- * @param contentObject - The object containing the property
1026
- * @param propertyKey - The key of the property to process
1027
- * @param contentNode - The nested content node
1028
- * @param existingPropertyKeys - Set of existing property names
1029
- * @param requiredImports - Set to track required imports
1030
- * @param sourceFile - The source file being processed
1031
- * @returns True if the content was modified
1032
- */
1033
- const processNestedContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
1034
- const nestedContent = contentNode[__intlayer_types.NodeType.Nested];
1035
- const nestedInitializerText = buildNestedInitializer(nestedContent);
1036
- if (!nestedInitializerText) return false;
1037
- if (!existingPropertyKeys.has(propertyKey)) {
1038
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1039
- if (spreadTargetObject) return processNestedContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
1040
- requiredImports.add("nest");
1041
- contentObject.addPropertyAssignment({
1042
- name: propertyKey,
1043
- initializer: nestedInitializerText
1044
- });
1045
- return true;
1046
- }
1047
- const existingNestedContent = readExistingNest(contentObject, propertyKey);
1048
- if (!(!!nestedContent && existingNestedContent?.dictionaryKey === nestedContent.dictionaryKey && existingNestedContent?.path === nestedContent.path)) {
1049
- requiredImports.add("nest");
1050
- const property = contentObject.getProperty(propertyKey);
1051
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
1052
- property.setInitializer(nestedInitializerText);
1053
- return true;
1054
- }
1055
- }
1056
- return false;
1057
- };
1058
- /**
1059
- * Processes nested object content.
1060
- * Handles nested objects within content structures.
1061
- *
1062
- * @param contentObject - The object containing the property
1063
- * @param propertyKey - The key of the property to process
1064
- * @param nestedObjectValue - The nested object value to process
1065
- * @param _existingPropertyKeys - Set of existing property names (unused)
1066
- * @param effectiveFallbackLocale - The fallback locale for translations
1067
- * @param requiredImports - Set to track required imports
1068
- * @param sourceFile - The source file being processed
1069
- * @returns True if the content was modified
1070
- */
1071
- const processNestedObjectContent = (contentObject, propertyKey, nestedObjectValue, _existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
1072
- let childObject;
1073
- const existingProperty = contentObject.getProperty(propertyKey);
1074
- if (existingProperty && import_ts_morph.Node.isPropertyAssignment(existingProperty)) childObject = existingProperty.getInitializerIfKind(import_ts_morph.SyntaxKind.ObjectLiteralExpression);
1075
- if (!childObject) {
1076
- const shorthandProperty = contentObject.getProperty(propertyKey);
1077
- if (shorthandProperty && import_ts_morph.Node.isShorthandPropertyAssignment(shorthandProperty)) childObject = resolveNameToObjectLiteral(contentObject.getSourceFile(), propertyKey);
1078
- else if (existingProperty && import_ts_morph.Node.isPropertyAssignment(existingProperty)) {
1079
- const propertyInitializer = existingProperty.getInitializer();
1080
- if (propertyInitializer) {
1081
- if (import_ts_morph.Node.isIdentifier(propertyInitializer)) childObject = resolveNameToObjectLiteral(sourceFile, propertyInitializer.getText());
1082
- else if (import_ts_morph.Node.isPropertyAccessExpression(propertyInitializer)) childObject = resolveExpressionToObjectLiteral(sourceFile, propertyInitializer);
1083
- }
1084
- }
1085
- }
1086
- if (!childObject) {
1087
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1088
- if (spreadTargetObject) return processNestedObjectContent(spreadTargetObject, propertyKey, nestedObjectValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
1089
- contentObject.addPropertyAssignment({
1090
- name: propertyKey,
1091
- initializer: "{ }"
1092
- });
1093
- const newProperty = contentObject.getProperty(propertyKey);
1094
- if (newProperty && import_ts_morph.Node.isPropertyAssignment(newProperty)) childObject = newProperty.getInitializerIfKind(import_ts_morph.SyntaxKind.ObjectLiteralExpression);
1095
- }
1096
- if (childObject) return processContentEntries(childObject, nestedObjectValue, effectiveFallbackLocale, requiredImports, sourceFile);
1097
- return false;
1098
- };
1099
- /**
1100
- * Processes content entries in a dictionary object.
1101
- * Routes different content types to appropriate processors.
1102
- *
1103
- * @param contentObject - The object containing the content
1104
- * @param dictionaryContent - The dictionary content to process
1105
- * @param effectiveFallbackLocale - The fallback locale for translations
1106
- * @param requiredImports - Set to track required imports
1107
- * @param sourceFile - The source file being processed
1108
- * @returns True if any content was modified
1109
- */
1110
- const processContentEntries = (contentObject, dictionaryContent, effectiveFallbackLocale, requiredImports, sourceFile) => {
1111
- let contentWasChanged = false;
1112
- const existingPropertyKeys = getExistingPropertyNames(contentObject);
1113
- for (const [propertyKey, propertyValue] of Object.entries(dictionaryContent)) {
1114
- if (Array.isArray(propertyValue)) {
1115
- if (processArrayContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1116
- continue;
1117
- }
1118
- if (typeof propertyValue === "string" || typeof propertyValue === "number" || typeof propertyValue === "boolean" || propertyValue === null) {
1119
- if (processPrimitiveContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1120
- continue;
1121
- }
1122
- const nodeType = (0, __intlayer_core.getNodeType)(propertyValue);
1123
- if (nodeType !== __intlayer_types.NodeType.Text && nodeType !== __intlayer_types.NodeType.Number && nodeType !== __intlayer_types.NodeType.Boolean && nodeType !== __intlayer_types.NodeType.Null) {
1124
- if (processComplexContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) {
1125
- contentWasChanged = true;
1126
- continue;
1127
- }
1128
- }
1129
- if (propertyValue && typeof propertyValue === "object" && !Array.isArray(propertyValue) && !propertyValue.nodeType) {
1130
- if (processNestedObjectContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1131
- }
1132
- }
1133
- return contentWasChanged;
1134
- };
1135
- const readExistingInsertion = (contentObject, propName) => {
1136
- const prop = contentObject.getProperty(propName);
1137
- if (!prop || !import_ts_morph.Node.isPropertyAssignment(prop)) return void 0;
1138
- const init = prop.getInitializer();
1139
- if (!init || !import_ts_morph.Node.isCallExpression(init)) return void 0;
1140
- const exp = init.getExpression();
1141
- if (!import_ts_morph.Node.isIdentifier(exp) || exp.getText() !== "insert") return void 0;
1142
- const argument = init.getArguments()[0];
1143
- if (!argument) return void 0;
1144
- if (import_ts_morph.Node.isStringLiteral(argument)) return {
1145
- kind: "string",
1146
- value: argument.getLiteralValue()
1147
- };
1148
- if (import_ts_morph.Node.isCallExpression(argument)) {
1149
- const argumentExpression = argument.getExpression();
1150
- if (import_ts_morph.Node.isIdentifier(argumentExpression) && argumentExpression.getText() === "t") {
1151
- const translationArgument = argument.getArguments()[0];
1152
- if (translationArgument && import_ts_morph.Node.isObjectLiteralExpression(translationArgument)) {
1153
- const map = {};
1154
- for (const propertyAssignment of translationArgument.getProperties()) {
1155
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) continue;
1156
- const name = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1157
- const valueInitializer = propertyAssignment.getInitializer();
1158
- if (valueInitializer && import_ts_morph.Node.isStringLiteral(valueInitializer)) map[name] = valueInitializer.getLiteralValue();
1159
- }
1160
- return {
1161
- kind: "translation",
1162
- map
1163
- };
1164
- }
1165
- }
1166
- }
1167
- };
1168
- const readExistingMarkdown = (contentObject, propName) => {
1169
- const property = contentObject.getProperty(propName);
1170
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
1171
- const initializer = property.getInitializer();
1172
- if (!initializer) return void 0;
1173
- if (import_ts_morph.Node.isCallExpression(initializer)) {
1174
- const expression = initializer.getExpression();
1175
- if (!import_ts_morph.Node.isIdentifier(expression)) return void 0;
1176
- if (expression.getText() === "md") {
1177
- const argument = initializer.getArguments()[0];
1178
- if (!argument) return void 0;
1179
- if (import_ts_morph.Node.isStringLiteral(argument)) return {
1180
- kind: "string",
1181
- value: argument.getLiteralValue()
1182
- };
1183
- if (import_ts_morph.Node.isCallExpression(argument)) {
1184
- const argumentExpression = argument.getExpression();
1185
- if (import_ts_morph.Node.isIdentifier(argumentExpression) && argumentExpression.getText() === "file") {
1186
- const fileArgument = argument.getArguments()[0];
1187
- if (fileArgument && import_ts_morph.Node.isStringLiteral(fileArgument)) return {
1188
- kind: "file",
1189
- path: fileArgument.getLiteralValue()
1190
- };
1191
- }
1192
- }
1193
- }
1194
- }
1195
- };
1196
- const readExistingFilePath = (contentObject, propName) => {
1197
- const property = contentObject.getProperty(propName);
1198
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
1199
- const initializer = property.getInitializer();
1200
- if (!initializer || !import_ts_morph.Node.isCallExpression(initializer)) return void 0;
1201
- const expression = initializer.getExpression();
1202
- if (!import_ts_morph.Node.isIdentifier(expression) || expression.getText() !== "file") return void 0;
1203
- const argument = initializer.getArguments()[0];
1204
- if (argument && import_ts_morph.Node.isStringLiteral(argument)) return argument.getLiteralValue();
1205
- };
1206
- const readExistingMarkdownTranslationMap = (contentObject, propName) => {
1207
- const property = contentObject.getProperty(propName);
1208
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
1209
- const initializer = property.getInitializer();
1210
- if (!initializer) return void 0;
1211
- if (import_ts_morph.Node.isCallExpression(initializer)) {
1212
- const exp = initializer.getExpression();
1213
- if (import_ts_morph.Node.isIdentifier(exp) && exp.getText() === "md") {
1214
- const arg = initializer.getArguments()[0];
1215
- if (arg && import_ts_morph.Node.isCallExpression(arg)) {
1216
- const tExp = arg.getExpression();
1217
- if (import_ts_morph.Node.isIdentifier(tExp) && tExp.getText() === "t") {
1218
- const tArg = arg.getArguments()[0];
1219
- if (tArg && import_ts_morph.Node.isObjectLiteralExpression(tArg)) {
1220
- const map = {};
1221
- for (const prop of tArg.getProperties()) {
1222
- if (!import_ts_morph.Node.isPropertyAssignment(prop)) continue;
1223
- const name = prop.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1224
- const valueInit = prop.getInitializer();
1225
- if (valueInit && import_ts_morph.Node.isStringLiteral(valueInit)) map[name] = valueInit.getLiteralValue();
1226
- else return;
1227
- }
1228
- return map;
1229
- }
1230
- }
1231
- }
1232
- }
1233
- }
1234
- if (import_ts_morph.Node.isCallExpression(initializer)) {
1235
- const exp = initializer.getExpression();
1236
- if (import_ts_morph.Node.isIdentifier(exp) && exp.getText() === "t") {
1237
- const tArg = initializer.getArguments()[0];
1238
- if (tArg && import_ts_morph.Node.isObjectLiteralExpression(tArg)) {
1239
- const map = {};
1240
- for (const prop of tArg.getProperties()) {
1241
- if (!import_ts_morph.Node.isPropertyAssignment(prop)) continue;
1242
- const name = prop.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1243
- const valueInit = prop.getInitializer();
1244
- if (valueInit && import_ts_morph.Node.isCallExpression(valueInit) && import_ts_morph.Node.isIdentifier(valueInit.getExpression()) && valueInit.getExpression().getText() === "md") {
1245
- const mdArg = valueInit.getArguments()[0];
1246
- if (mdArg && import_ts_morph.Node.isStringLiteral(mdArg)) map[name] = mdArg.getLiteralValue();
1247
- else return;
1248
- } else return;
1249
- }
1250
- return map;
1251
- }
1252
- }
1253
- }
1254
- };
1255
- const readExistingNest = (contentObject, propName) => {
1256
- const property = contentObject.getProperty(propName);
1257
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
1258
- let initializer = property.getInitializer();
1259
- if (!initializer) return void 0;
1260
- let safetyCounter = 0;
1261
- while (safetyCounter++ < 5) {
1262
- if (import_ts_morph.Node.isCallExpression(initializer)) break;
1263
- const nextExpression = initializer.getExpression?.();
1264
- if (nextExpression && typeof nextExpression === "object" && nextExpression !== initializer) {
1265
- initializer = nextExpression;
1266
- continue;
1267
- }
1268
- break;
1269
- }
1270
- if (!import_ts_morph.Node.isCallExpression(initializer)) return void 0;
1271
- const expression = initializer.getExpression();
1272
- if (!import_ts_morph.Node.isIdentifier(expression) || expression.getText() !== "nest") return void 0;
1273
- const [firstArgument, secondArgument] = initializer.getArguments();
1274
- if (!firstArgument || !import_ts_morph.Node.isStringLiteral(firstArgument)) return void 0;
1275
- const dictionaryKey = firstArgument.getLiteralValue();
1276
- let path;
1277
- if (secondArgument && import_ts_morph.Node.isStringLiteral(secondArgument)) path = secondArgument.getLiteralValue();
1278
- return {
1279
- dictionaryKey,
1280
- path
1281
- };
1282
- };
1283
- const unwrapToObjectLiteral = (node) => {
1284
- if (!node || typeof node !== "object") return void 0;
1285
- let current = node;
1286
- let safetyCounter = 0;
1287
- while (safetyCounter++ < 8) {
1288
- if (import_ts_morph.Node.isObjectLiteralExpression(current)) return current;
1289
- const next = current?.getExpression?.();
1290
- if (next && typeof next === "object" && next !== current) {
1291
- current = next;
1292
- continue;
1293
- }
1294
- break;
1295
- }
1296
- };
1297
- const resolveNameToObjectLiteral = (sourceFile, name) => {
1298
- const varDecl = sourceFile.getVariableDeclaration(name);
1299
- if (varDecl) {
1300
- const obj = unwrapToObjectLiteral(varDecl.getInitializer());
1301
- if (obj) return obj;
1302
- }
1303
- const decl = sourceFile.getDescendants().find((n) => {
1304
- return import_ts_morph.Node.isIdentifier(n) && n.getText() === name;
1305
- })?.getSymbol()?.getDeclarations()?.[0];
1306
- if (decl && import_ts_morph.Node.isVariableDeclaration(decl)) {
1307
- const obj = unwrapToObjectLiteral(decl.getInitializer());
1308
- if (obj) return obj;
1309
- }
1310
- };
1311
- const resolveExpressionToObjectLiteral = (sourceFile, expr) => {
1312
- if (import_ts_morph.Node.isIdentifier(expr)) return resolveNameToObjectLiteral(sourceFile, expr.getText());
1313
- if (import_ts_morph.Node.isPropertyAccessExpression(expr)) {
1314
- const leftResolved = resolveExpressionToObjectLiteral(sourceFile, expr.getExpression());
1315
- if (!leftResolved) return void 0;
1316
- const propName = expr.getName();
1317
- const prop = leftResolved.getProperty(propName);
1318
- if (prop && import_ts_morph.Node.isPropertyAssignment(prop)) {
1319
- const init = prop.getInitializer();
1320
- const obj = unwrapToObjectLiteral(init);
1321
- if (obj) return obj;
1322
- if (init && import_ts_morph.Node.isIdentifier(init)) return resolveNameToObjectLiteral(sourceFile, init.getText());
1323
- }
1324
- }
1325
- };
1326
- const getSpreadSourceObjects = (contentObject, sourceFile) => {
1327
- const spreads = [];
1328
- for (const prop of contentObject.getProperties()) if (import_ts_morph.Node.isSpreadAssignment(prop)) {
1329
- const resolved = resolveExpressionToObjectLiteral(sourceFile, prop.getExpression());
1330
- if (resolved) spreads.push(resolved);
1331
- }
1332
- return spreads;
1333
- };
1334
- const findSpreadTargetObjectForKey = (contentObject, key, sourceFile) => {
1335
- const spreads = getSpreadSourceObjects(contentObject, sourceFile);
1336
- for (let i = spreads.length - 1; i >= 0; i--) {
1337
- const spreadObj = spreads[i];
1338
- const prop = spreadObj.getProperty(key);
1339
- if (prop && import_ts_morph.Node.isPropertyAssignment(prop)) return spreadObj;
1340
- }
1341
- };
1342
- const readExistingArraySerialized = (contentObject, propName) => {
1343
- const property = contentObject.getProperty(propName);
1344
- if (!property || !import_ts_morph.Node.isPropertyAssignment(property)) return void 0;
1345
- const initializer = property.getInitializer();
1346
- if (!initializer || !import_ts_morph.Node.isArrayLiteralExpression(initializer)) return void 0;
1347
- const serialized = [];
1348
- for (const element of initializer.getElements()) {
1349
- if (import_ts_morph.Node.isStringLiteral(element)) {
1350
- serialized.push(JSON.stringify(element.getLiteralValue()));
1351
- continue;
1352
- }
1353
- if (import_ts_morph.Node.isNumericLiteral(element)) {
1354
- serialized.push(element.getText());
1355
- continue;
1356
- }
1357
- if (element.getKind() === import_ts_morph.SyntaxKind.TrueKeyword || element.getKind() === import_ts_morph.SyntaxKind.FalseKeyword) {
1358
- serialized.push(element.getText());
1359
- continue;
1360
- }
1361
- if (import_ts_morph.Node.isNullLiteral(element)) {
1362
- serialized.push("null");
1363
- continue;
1364
- }
1365
- if (import_ts_morph.Node.isCallExpression(element)) {
1366
- const expression = element.getExpression();
1367
- if (import_ts_morph.Node.isIdentifier(expression) && expression.getText() === "t") {
1368
- const argument = element.getArguments()[0];
1369
- if (argument && import_ts_morph.Node.isObjectLiteralExpression(argument)) {
1370
- const map = {};
1371
- for (const propertyAssignment of argument.getProperties()) {
1372
- if (!import_ts_morph.Node.isPropertyAssignment(propertyAssignment)) return void 0;
1373
- const name = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1374
- const valueInitializer = propertyAssignment.getInitializer();
1375
- if (valueInitializer && import_ts_morph.Node.isStringLiteral(valueInitializer)) map[name] = valueInitializer.getLiteralValue();
1376
- else return;
1377
- }
1378
- serialized.push(buildTranslationInitializer(map));
1379
- continue;
1380
- }
1381
- }
1382
- }
1383
- return;
1384
- }
1385
- return serialized;
1386
- };
1387
- const serializeValue = (value) => {
1388
- const nodeType = (0, __intlayer_core.getNodeType)(value);
1389
- if (nodeType === __intlayer_types.NodeType.Text) return JSON.stringify(value);
1390
- if (nodeType === __intlayer_types.NodeType.Number || nodeType === __intlayer_types.NodeType.Boolean) return String(value);
1391
- if (nodeType === __intlayer_types.NodeType.Null) return "null";
1392
- if (nodeType === __intlayer_types.NodeType.Translation) {
1393
- const translations = value[__intlayer_types.NodeType.Translation] ?? {};
1394
- if (!Object.values(translations).every((v) => typeof v === "string")) return void 0;
1395
- return buildTranslationInitializer(translations);
1396
- }
1397
- if (nodeType === __intlayer_types.NodeType.Enumeration) {
1398
- const map = value[__intlayer_types.NodeType.Enumeration];
1399
- return buildEnumerationInitializer(map);
1400
- }
1401
- if (nodeType === __intlayer_types.NodeType.Condition) {
1402
- const map = value[__intlayer_types.NodeType.Condition];
1403
- return buildConditionInitializer(map);
1404
- }
1405
- if (nodeType === __intlayer_types.NodeType.Gender) {
1406
- const map = value[__intlayer_types.NodeType.Gender];
1407
- return buildGenderInitializer(map);
1408
- }
1409
- if (nodeType === __intlayer_types.NodeType.Insertion) {
1410
- const content = value[__intlayer_types.NodeType.Insertion];
1411
- return buildInsertionInitializer(content);
1412
- }
1413
- if (nodeType === __intlayer_types.NodeType.Markdown) {
1414
- const content = value[__intlayer_types.NodeType.Markdown];
1415
- return buildMarkdownInitializer(content);
1416
- }
1417
- if (nodeType === __intlayer_types.NodeType.File) {
1418
- const path = value[__intlayer_types.NodeType.File];
1419
- return buildFileInitializer(path);
1420
- }
1421
- if (nodeType === __intlayer_types.NodeType.Nested) {
1422
- const content = value[__intlayer_types.NodeType.Nested];
1423
- return buildNestedInitializer(content);
1424
- }
1425
- };
1426
- /**
1427
- * Gets the existing imports from @intlayer/core in the source file
1428
- */
1429
- const getExistingIntlayerImports = (sourceFile) => {
1430
- const imported = /* @__PURE__ */ new Set();
1431
- for (const importDecl of sourceFile.getImportDeclarations()) {
1432
- const moduleSpecifier = importDecl.getModuleSpecifierValue();
1433
- if (moduleSpecifier === "intlayer") {
1434
- const namedImports = importDecl.getNamedImports();
1435
- for (const namedImport of namedImports) imported.add(namedImport.getName());
1436
- }
1437
- if (moduleSpecifier === "intlayer/file") {
1438
- const namedImports = importDecl.getNamedImports();
1439
- for (const namedImport of namedImports) {
1440
- const alias = namedImport.getAliasNode();
1441
- imported.add(alias ? alias.getText() : namedImport.getName());
1442
- }
1443
- }
1444
- }
1445
- return imported;
1446
- };
1447
- /**
1448
- * Adds missing imports to the source file
1449
- */
1450
- const addMissingImports = (sourceFile, requiredImports) => {
1451
- if (requiredImports.size === 0) return false;
1452
- const existingImports = getExistingIntlayerImports(sourceFile);
1453
- const missingImports = [...requiredImports].filter((imp) => !existingImports.has(imp));
1454
- if (missingImports.length === 0) return false;
1455
- const hasMissingFile = missingImports.includes("file");
1456
- const otherMissingImports = missingImports.filter((imp) => imp !== "file");
1457
- if (otherMissingImports.length > 0) {
1458
- const coreImport = sourceFile.getImportDeclarations().find((imp) => imp.getModuleSpecifierValue() === "intlayer");
1459
- if (coreImport) {
1460
- const existingNamedImports = coreImport.getNamedImports().map((ni) => ni.getName());
1461
- const allImports = [...new Set([...existingNamedImports, ...otherMissingImports])].sort();
1462
- coreImport.removeNamedImports();
1463
- coreImport.addNamedImports(allImports.map((name) => ({ name })));
1464
- } else sourceFile.insertImportDeclaration(0, {
1465
- moduleSpecifier: "intlayer",
1466
- namedImports: otherMissingImports.sort().map((name) => ({ name }))
1467
- });
1468
- }
1469
- if (hasMissingFile) {
1470
- if (!sourceFile.getImportDeclarations().find((imp) => imp.getModuleSpecifierValue() === "intlayer/file")) {
1471
- const coreImportIndex = sourceFile.getImportDeclarations().findIndex((imp) => imp.getModuleSpecifierValue() === "intlayer");
1472
- const insertIndex = coreImportIndex >= 0 ? coreImportIndex + 1 : 0;
1473
- sourceFile.insertImportDeclaration(insertIndex, {
1474
- moduleSpecifier: "intlayer/file",
1475
- namedImports: [{ name: "file" }]
1476
- });
1477
- }
1478
- }
1479
- return true;
1480
- };
1481
- /**
1482
- * Detect whether the current source file is written in CommonJS style.
1483
- * Prefers ESM when import/export syntax is present; otherwise detects CJS via require/module.exports.
1484
- */
1485
- const isCommonJS = (sourceFile) => {
1486
- if (sourceFile.getImportDeclarations().length > 0) return false;
1487
- if (sourceFile.getExportDeclarations().length > 0) return false;
1488
- if (sourceFile.getExportAssignments().length > 0) return false;
1489
- for (const statement of sourceFile.getStatements()) {
1490
- if (!import_ts_morph.Node.isExpressionStatement(statement)) continue;
1491
- const expression = statement.getExpression();
1492
- if (!import_ts_morph.Node.isBinaryExpression(expression)) continue;
1493
- const leftSide = expression.getLeft();
1494
- if (!import_ts_morph.Node.isPropertyAccessExpression(leftSide)) continue;
1495
- const leftExpression = leftSide.getExpression();
1496
- const leftName = leftSide.getName();
1497
- const isModuleExports = import_ts_morph.Node.isIdentifier(leftExpression) && leftExpression.getText() === "module" && leftName === "exports";
1498
- const isExportsDefault = import_ts_morph.Node.isIdentifier(leftExpression) && leftExpression.getText() === "exports";
1499
- if (isModuleExports || isExportsDefault) return true;
1500
- }
1501
- return sourceFile.getDescendantsOfKind(import_ts_morph.SyntaxKind.CallExpression).some((call) => {
1502
- const exp = call.getExpression();
1503
- return import_ts_morph.Node.isIdentifier(exp) && exp.getText() === "require";
1504
- });
1505
- };
1506
- /**
1507
- * Adds missing CommonJS requires for intlayer helpers.
1508
- * - Core helpers (t, md, insert, enu, cond, gender, nest) come from require('intlayer') via destructuring
1509
- * - file helper comes from require('intlayer/file') via destructuring
1510
- * Existing destructured requires are respected to avoid duplicates.
1511
- */
1512
- const addMissingRequires = (sourceFile, requiredImports) => {
1513
- if (requiredImports.size === 0) return false;
1514
- const existingCoreNames = /* @__PURE__ */ new Set();
1515
- let hasFileHelper = false;
1516
- for (const varDecl of sourceFile.getVariableDeclarations()) {
1517
- const init = varDecl.getInitializer();
1518
- if (!init || !import_ts_morph.Node.isCallExpression(init)) continue;
1519
- const callee = init.getExpression();
1520
- if (!import_ts_morph.Node.isIdentifier(callee) || callee.getText() !== "require") continue;
1521
- const arg = init.getArguments()[0];
1522
- if (!arg || !import_ts_morph.Node.isStringLiteral(arg)) continue;
1523
- const spec = arg.getLiteralValue();
1524
- const nameNode = varDecl.getNameNode();
1525
- if (spec === "intlayer") {
1526
- if (import_ts_morph.Node.isObjectBindingPattern(nameNode)) for (const el of nameNode.getElements()) existingCoreNames.add(el.getNameNode().getText());
1527
- }
1528
- if (spec === "intlayer/file") {
1529
- if (import_ts_morph.Node.isObjectBindingPattern(nameNode)) {
1530
- for (const el of nameNode.getElements()) if (el.getNameNode().getText() === "file") hasFileHelper = true;
1531
- } else if (import_ts_morph.Node.isIdentifier(nameNode) && nameNode.getText() === "file") hasFileHelper = true;
1532
- }
1533
- }
1534
- const missingCore = Array.from(requiredImports).filter((n) => n !== "file").filter((n) => !existingCoreNames.has(n));
1535
- const needsFile = requiredImports.has("file") && !hasFileHelper;
1536
- if (missingCore.length === 0 && !needsFile) return false;
1537
- let insertIndex = 0;
1538
- const statements = sourceFile.getStatements();
1539
- for (const st of statements) {
1540
- if (import_ts_morph.Node.isExpressionStatement(st)) {
1541
- const expr = st.getExpression();
1542
- if (import_ts_morph.Node.isStringLiteral(expr)) {
1543
- insertIndex += 1;
1544
- continue;
1545
- }
1546
- }
1547
- break;
1548
- }
1549
- const lines = [];
1550
- if (missingCore.length > 0) {
1551
- const sorted = Array.from(new Set(missingCore)).sort();
1552
- lines.push(`const { ${sorted.join(", ")} } = require('intlayer');`);
1553
- }
1554
- if (needsFile) lines.push("const { file } = require('intlayer/file');");
1555
- if (lines.length > 0) {
1556
- sourceFile.insertStatements(insertIndex, lines.join("\n"));
1557
- return true;
1558
- }
1559
- return false;
1560
- };
1561
- /**
1562
- * Serializes a metadata value to its string representation for code generation
1563
- * Handles: boolean, number, string, and array of strings
1564
- */
1565
- const serializeMetadataValue = (value) => {
1566
- if (Array.isArray(value)) return `[${value.map((item) => JSON.stringify(item)).join(", ")}]`;
1567
- if (typeof value === "boolean" || typeof value === "number") return String(value);
1568
- return JSON.stringify(value);
1569
- };
1570
- /**
1571
- * Updates a single property in the root object if the value has changed
1572
- */
1573
- const updateMetadataProperty = (rootObject, propertyName, value) => {
1574
- const property = rootObject.getProperty(propertyName);
1575
- const serializedValue = serializeMetadataValue(value);
1576
- if (property && import_ts_morph.Node.isPropertyAssignment(property)) {
1577
- if (property.getInitializer()?.getText() !== serializedValue) {
1578
- property.setInitializer(serializedValue);
1579
- return true;
1580
- }
1581
- } else if (!property) {
1582
- rootObject.addPropertyAssignment({
1583
- name: propertyName,
1584
- initializer: serializedValue
1585
- });
1586
- return true;
1587
- }
1588
- return false;
1589
- };
1590
- /**
1591
- * Updates dictionary metadata properties in the root object
1592
- * Supports: id, locale, filled, fill, title, description, tags, version, priority, live
1593
- * and any future fields that may be added
1594
- */
1595
- const updateDictionaryMetadata = (rootObject, dictionary) => {
1596
- let changed = false;
1597
- for (const prop of [
1598
- "id",
1599
- "locale",
1600
- "filled",
1601
- "fill",
1602
- "title",
1603
- "description",
1604
- "tags",
1605
- "version",
1606
- "priority",
1607
- "live"
1608
- ]) {
1609
- const value = dictionary[prop];
1610
- if (value !== void 0) {
1611
- if (updateMetadataProperty(rootObject, prop, value)) changed = true;
1612
- }
1613
- }
1614
- return changed;
1615
- };
1616
- /**
1617
- * Locates the root dictionary object in the source file
1618
- */
1619
- const findRootDictionaryObject = (sourceFile) => {
1620
- const exportAssignment = sourceFile.getExportAssignment((_) => true);
1621
- if (exportAssignment) {
1622
- const expression = exportAssignment.getExpression();
1623
- if (import_ts_morph.Node.isIdentifier(expression)) {
1624
- const declarationByName = expression.getSymbol()?.getDeclarations()?.[0] ?? sourceFile.getVariableDeclaration(expression.getText());
1625
- if (declarationByName && import_ts_morph.Node.isVariableDeclaration(declarationByName)) {
1626
- const objectLiteral = unwrapToObjectLiteral(declarationByName.getInitializer());
1627
- if (objectLiteral) return objectLiteral;
1628
- }
1629
- } else {
1630
- const objectLiteral = unwrapToObjectLiteral(expression);
1631
- if (objectLiteral) return objectLiteral;
1632
- }
1633
- }
1634
- const variableDeclaration = sourceFile.getVariableDeclaration((variable) => {
1635
- try {
1636
- return variable.getType().getText().includes("Dictionary") || variable.getName() === "content" || variable.getName().toLowerCase().includes("dictionary");
1637
- } catch {
1638
- return variable.getName() === "content";
1639
- }
1640
- });
1641
- if (variableDeclaration) {
1642
- const objectLiteral = unwrapToObjectLiteral(variableDeclaration.getInitializer());
1643
- if (objectLiteral) return objectLiteral;
1644
- }
1645
- for (const statement of sourceFile.getStatements()) {
1646
- if (!import_ts_morph.Node.isExpressionStatement(statement)) continue;
1647
- const expression = statement.getExpression();
1648
- if (!import_ts_morph.Node.isBinaryExpression(expression)) continue;
1649
- if (expression.getOperatorToken().getText() !== "=") continue;
1650
- const leftSide = expression.getLeft();
1651
- if (!import_ts_morph.Node.isPropertyAccessExpression(leftSide)) continue;
1652
- const leftExpression = leftSide.getExpression();
1653
- const leftName = leftSide.getName();
1654
- const isModuleExports = import_ts_morph.Node.isIdentifier(leftExpression) && leftExpression.getText() === "module" && leftName === "exports";
1655
- const isExportsDefault = import_ts_morph.Node.isIdentifier(leftExpression) && leftExpression.getText() === "exports" && leftName === "default";
1656
- if (!isModuleExports && !isExportsDefault) continue;
1657
- const rightSide = expression.getRight();
1658
- if (import_ts_morph.Node.isObjectLiteralExpression(rightSide)) return rightSide;
1659
- if (import_ts_morph.Node.isIdentifier(rightSide)) {
1660
- const declaration = rightSide.getSymbol()?.getDeclarations()?.[0];
1661
- if (declaration && import_ts_morph.Node.isVariableDeclaration(declaration)) {
1662
- const objectLiteral = unwrapToObjectLiteral(declaration.getInitializer());
1663
- if (objectLiteral) return objectLiteral;
1664
- }
1665
- }
1666
- }
1667
- };
1668
- /**
1669
- * Updates a JavaScript/TypeScript file based on the provided dictionary.
1670
- * It targets a specific dictionary object within the file and updates its
1671
- * metadata (title, description, tags) and content entries.
1672
- *
1673
- * This function now supports inserting translation keys into nested objects
1674
- * within arrays. For example, if you have:
1675
- * ```
1676
- * content: [
1677
- * { question: t({ en: '...', fr: '...' }) }
1678
- * ]
1679
- * ```
1680
- *
1681
- * You can add a new locale (e.g., 'pl') by providing a dictionary with:
1682
- * ```
1683
- * {
1684
- * content: [
1685
- * { question: { [NodeType.Translation]: { en: '...', fr: '...', pl: '...' } } }
1686
- * ]
1687
- * }
1688
- * ```
1689
- *
1690
- * The function will:
1691
- * 1. Detect the existing array structure
1692
- * 2. Navigate into each array element (if it's an object)
1693
- * 3. Recursively process nested properties
1694
- * 4. Update translation maps while preserving existing locales
1695
- */
1696
- const transformJSFile = async (fileContent, dictionary, fallbackLocale) => {
1697
- try {
1698
- if (!dictionary || typeof dictionary !== "object") return fileContent;
1699
- const sourceFile = new import_ts_morph.Project({
1700
- useInMemoryFileSystem: true,
1701
- skipAddingFilesFromTsConfig: true,
1702
- skipFileDependencyResolution: true,
1703
- compilerOptions: {
1704
- allowJs: true,
1705
- jsx: import_ts_morph.ts.JsxEmit.Preserve
1706
- },
1707
- manipulationSettings: {
1708
- indentationText: import_ts_morph.IndentationText.TwoSpaces,
1709
- quoteKind: import_ts_morph.QuoteKind.Double,
1710
- newLineKind: import_ts_morph.NewLineKind.LineFeed
1711
- }
1712
- }).createSourceFile("file.tsx", fileContent, { overwrite: true });
1713
- const rootObject = findRootDictionaryObject(sourceFile);
1714
- if (!rootObject) return fileContent;
1715
- let changed = false;
1716
- const requiredImports = /* @__PURE__ */ new Set();
1717
- if (updateDictionaryMetadata(rootObject, dictionary)) changed = true;
1718
- if (dictionary.content) {
1719
- const contentProperty = rootObject.getProperty("content");
1720
- let contentObject;
1721
- let isContentArrayInSource = false;
1722
- if (contentProperty && import_ts_morph.Node.isPropertyAssignment(contentProperty)) {
1723
- contentObject = contentProperty.getInitializerIfKind(import_ts_morph.SyntaxKind.ObjectLiteralExpression);
1724
- isContentArrayInSource = !!contentProperty.getInitializerIfKind(import_ts_morph.SyntaxKind.ArrayLiteralExpression);
1725
- }
1726
- const effectiveFallbackLocale = fallbackLocale ?? "en";
1727
- if (contentObject && !Array.isArray(dictionary.content)) {
1728
- const dictContent = dictionary.content ?? {};
1729
- if (processContentEntries(contentObject, dictContent, effectiveFallbackLocale, requiredImports, sourceFile)) changed = true;
1730
- } else if (Array.isArray(dictionary.content) && isContentArrayInSource) {
1731
- if (processArrayContent(rootObject, "content", dictionary.content ?? [], getExistingPropertyNames(rootObject), effectiveFallbackLocale, requiredImports, sourceFile)) changed = true;
1732
- }
1733
- }
1734
- if (!changed) return fileContent;
1735
- if ((isCommonJS(sourceFile) ? addMissingRequires(sourceFile, requiredImports) : addMissingImports(sourceFile, requiredImports)) || changed) return sourceFile.getFullText();
1736
- return fileContent;
1737
- } catch {
1738
- return fileContent;
1739
- }
1740
- };
1741
-
1742
- //#endregion
1743
- exports.transformJSFile = transformJSFile;
1744
- //# sourceMappingURL=transformJSFile.cjs.map