@generaltranslation/compiler 1.3.21 → 1.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +23 -0
  2. package/dist/config.js +32 -27
  3. package/dist/config.js.map +1 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +120 -187
  7. package/dist/index.js.map +1 -1
  8. package/dist/passes/basePass.js +40 -83
  9. package/dist/passes/basePass.js.map +1 -1
  10. package/dist/passes/collectionPass.js +18 -11
  11. package/dist/passes/collectionPass.js.map +1 -1
  12. package/dist/passes/handleErrors.js +21 -30
  13. package/dist/passes/handleErrors.js.map +1 -1
  14. package/dist/passes/injectionPass.js +15 -9
  15. package/dist/passes/injectionPass.js.map +1 -1
  16. package/dist/passes/jsxInsertionPass.js +33 -31
  17. package/dist/passes/jsxInsertionPass.js.map +1 -1
  18. package/dist/passes/macroExpansionPass.js +28 -26
  19. package/dist/passes/macroExpansionPass.js.map +1 -1
  20. package/dist/passes/runtimeTranslatePass.js +37 -35
  21. package/dist/passes/runtimeTranslatePass.js.map +1 -1
  22. package/dist/processing/collection/processCallExpression.js +144 -249
  23. package/dist/processing/collection/processCallExpression.js.map +1 -1
  24. package/dist/processing/collection/processTaggedTemplateExpression.js +38 -75
  25. package/dist/processing/collection/processTaggedTemplateExpression.js.map +1 -1
  26. package/dist/processing/collection/processVariableDeclarator.js +20 -18
  27. package/dist/processing/collection/processVariableDeclarator.js.map +1 -1
  28. package/dist/processing/index.js +29 -31
  29. package/dist/processing/injection/processCallExpression.js +69 -142
  30. package/dist/processing/injection/processCallExpression.js.map +1 -1
  31. package/dist/processing/injection/processVariableDeclarator.js +22 -20
  32. package/dist/processing/injection/processVariableDeclarator.js.map +1 -1
  33. package/dist/processing/jsx-insertion/processCallExpression.js +284 -393
  34. package/dist/processing/jsx-insertion/processCallExpression.js.map +1 -1
  35. package/dist/processing/jsx-insertion/processImportDeclaration.js +37 -83
  36. package/dist/processing/jsx-insertion/processImportDeclaration.js.map +1 -1
  37. package/dist/processing/jsx-insertion/processProgram.js +26 -66
  38. package/dist/processing/jsx-insertion/processProgram.js.map +1 -1
  39. package/dist/processing/macro-expansion/processImportDeclaration.js +21 -55
  40. package/dist/processing/macro-expansion/processImportDeclaration.js.map +1 -1
  41. package/dist/processing/macro-expansion/processProgram.js +20 -26
  42. package/dist/processing/macro-expansion/processProgram.js.map +1 -1
  43. package/dist/processing/macro-expansion/processTaggedTemplateExpression.js +33 -69
  44. package/dist/processing/macro-expansion/processTaggedTemplateExpression.js.map +1 -1
  45. package/dist/processing/processArrowFunctionExpression.js +18 -16
  46. package/dist/processing/processArrowFunctionExpression.js.map +1 -1
  47. package/dist/processing/processAssignmentExpression.js +12 -10
  48. package/dist/processing/processAssignmentExpression.js.map +1 -1
  49. package/dist/processing/processCatchClause.js +18 -16
  50. package/dist/processing/processCatchClause.js.map +1 -1
  51. package/dist/processing/processClassDeclaration.js +18 -16
  52. package/dist/processing/processClassDeclaration.js.map +1 -1
  53. package/dist/processing/processClassMethod.js +18 -17
  54. package/dist/processing/processClassMethod.js.map +1 -1
  55. package/dist/processing/processClassPrivateMethod.js +18 -17
  56. package/dist/processing/processClassPrivateMethod.js.map +1 -1
  57. package/dist/processing/processForInStatement.js +18 -16
  58. package/dist/processing/processForInStatement.js.map +1 -1
  59. package/dist/processing/processForOfStatement.js +18 -16
  60. package/dist/processing/processForOfStatement.js.map +1 -1
  61. package/dist/processing/processFunctionDeclaration.js +20 -18
  62. package/dist/processing/processFunctionDeclaration.js.map +1 -1
  63. package/dist/processing/processFunctionExpression.js +26 -24
  64. package/dist/processing/processFunctionExpression.js.map +1 -1
  65. package/dist/processing/processImportDeclaration.js +11 -9
  66. package/dist/processing/processImportDeclaration.js.map +1 -1
  67. package/dist/processing/processObjectMethod.js +19 -18
  68. package/dist/processing/processObjectMethod.js.map +1 -1
  69. package/dist/processing/processProgram.js +13 -13
  70. package/dist/processing/processProgram.js.map +1 -1
  71. package/dist/processing/processScopeChange.js +15 -13
  72. package/dist/processing/processScopeChange.js.map +1 -1
  73. package/dist/processing/runtime-translate/processImportDeclaration.js +30 -63
  74. package/dist/processing/runtime-translate/processImportDeclaration.js.map +1 -1
  75. package/dist/processing/runtime-translate/processProgram.js +34 -57
  76. package/dist/processing/runtime-translate/processProgram.js.map +1 -1
  77. package/dist/state/ErrorTracker.js +18 -17
  78. package/dist/state/ErrorTracker.js.map +1 -1
  79. package/dist/state/Logger.js +42 -52
  80. package/dist/state/Logger.js.map +1 -1
  81. package/dist/state/ScopeTracker.d.ts +11 -2
  82. package/dist/state/ScopeTracker.d.ts.map +1 -1
  83. package/dist/state/ScopeTracker.js +154 -209
  84. package/dist/state/ScopeTracker.js.map +1 -1
  85. package/dist/state/StringCollector.d.ts +9 -2
  86. package/dist/state/StringCollector.d.ts.map +1 -1
  87. package/dist/state/StringCollector.js +165 -184
  88. package/dist/state/StringCollector.js.map +1 -1
  89. package/dist/state/types.js +1 -3
  90. package/dist/state/utils/initializeState.js +62 -67
  91. package/dist/state/utils/initializeState.js.map +1 -1
  92. package/dist/transform/getTrackedVariable.js +33 -38
  93. package/dist/transform/getTrackedVariable.js.map +1 -1
  94. package/dist/transform/injection/callbacks/injectUseGTCallbackParameters.js +19 -27
  95. package/dist/transform/injection/callbacks/injectUseGTCallbackParameters.js.map +1 -1
  96. package/dist/transform/injection/callbacks/injectUseMessagesCallbackParameters.js +8 -6
  97. package/dist/transform/injection/callbacks/injectUseMessagesCallbackParameters.js.map +1 -1
  98. package/dist/transform/injection/callbacks/injectUseTranslationsCallbackParameters.js +8 -6
  99. package/dist/transform/injection/callbacks/injectUseTranslationsCallbackParameters.js.map +1 -1
  100. package/dist/transform/injection/injectCallbackDeclaratorFunctionParameters.js +74 -155
  101. package/dist/transform/injection/injectCallbackDeclaratorFunctionParameters.js.map +1 -1
  102. package/dist/transform/injection/injectHashIntoTranslationOptions.js +21 -61
  103. package/dist/transform/injection/injectHashIntoTranslationOptions.js.map +1 -1
  104. package/dist/transform/injection/injectStandaloneTFunctionParameters.js +21 -27
  105. package/dist/transform/injection/injectStandaloneTFunctionParameters.js.map +1 -1
  106. package/dist/transform/injection/injectTComponentParameters.js +25 -65
  107. package/dist/transform/injection/injectTComponentParameters.js.map +1 -1
  108. package/dist/transform/jsx-children/constructJsxChildren.d.ts +1 -1
  109. package/dist/transform/jsx-children/constructJsxChildren.d.ts.map +1 -1
  110. package/dist/transform/jsx-children/constructJsxChildren.js +273 -392
  111. package/dist/transform/jsx-children/constructJsxChildren.js.map +1 -1
  112. package/dist/transform/jsx-children/errors.js +16 -6
  113. package/dist/transform/jsx-children/errors.js.map +1 -1
  114. package/dist/transform/jsx-children/index.js +3 -6
  115. package/dist/transform/jsx-children/utils/getBranchComponentParameters.js +26 -85
  116. package/dist/transform/jsx-children/utils/getBranchComponentParameters.js.map +1 -1
  117. package/dist/transform/jsx-children/utils/getCalleeNameFromJsxExpressionParam.js +33 -66
  118. package/dist/transform/jsx-children/utils/getCalleeNameFromJsxExpressionParam.js.map +1 -1
  119. package/dist/transform/jsx-children/utils/id.js +18 -17
  120. package/dist/transform/jsx-children/utils/id.js.map +1 -1
  121. package/dist/transform/jsx-children/validation/validateChildrenElement.js +13 -47
  122. package/dist/transform/jsx-children/validation/validateChildrenElement.js.map +1 -1
  123. package/dist/transform/jsx-children/validation/validateChildrenFromArgs.js +25 -63
  124. package/dist/transform/jsx-children/validation/validateChildrenFromArgs.js.map +1 -1
  125. package/dist/transform/jsx-children/validation/validateIdentifier.js +38 -41
  126. package/dist/transform/jsx-children/validation/validateIdentifier.js.map +1 -1
  127. package/dist/transform/jsx-children/validation/validateJsxCall.js +15 -26
  128. package/dist/transform/jsx-children/validation/validateJsxCall.js.map +1 -1
  129. package/dist/transform/jsx-children/validation/validateNameFieldForVarComponent.js +33 -61
  130. package/dist/transform/jsx-children/validation/validateNameFieldForVarComponent.js.map +1 -1
  131. package/dist/transform/jsx-children/validation/validateStringLiteralPropertyFromArg.js +38 -66
  132. package/dist/transform/jsx-children/validation/validateStringLiteralPropertyFromArg.js.map +1 -1
  133. package/dist/transform/jsx-children/validation/validateTemplateLiteral.js +17 -13
  134. package/dist/transform/jsx-children/validation/validateTemplateLiteral.js.map +1 -1
  135. package/dist/transform/jsx-children/validation/validateUnaryExpression.js +26 -61
  136. package/dist/transform/jsx-children/validation/validateUnaryExpression.js.map +1 -1
  137. package/dist/transform/jsx-insertion/injectJsxInsertionImport.js +16 -48
  138. package/dist/transform/jsx-insertion/injectJsxInsertionImport.js.map +1 -1
  139. package/dist/transform/macro-expansion/injectMacroImport.js +14 -43
  140. package/dist/transform/macro-expansion/injectMacroImport.js.map +1 -1
  141. package/dist/transform/macro-expansion/transformTemplateLiteral.js +23 -22
  142. package/dist/transform/macro-expansion/transformTemplateLiteral.js.map +1 -1
  143. package/dist/transform/registration/callbacks/registerUseGTCallback.js +27 -34
  144. package/dist/transform/registration/callbacks/registerUseGTCallback.js.map +1 -1
  145. package/dist/transform/registration/callbacks/registerUseMessagesCallback.js +9 -9
  146. package/dist/transform/registration/callbacks/registerUseMessagesCallback.js.map +1 -1
  147. package/dist/transform/registration/callbacks/registerUseTranslationsCallback.js +9 -9
  148. package/dist/transform/registration/callbacks/registerUseTranslationsCallback.js.map +1 -1
  149. package/dist/transform/registration/registerStandaloneTranslation.js +27 -28
  150. package/dist/transform/registration/registerStandaloneTranslation.js.map +1 -1
  151. package/dist/transform/registration/registerTranslationComponent.js +15 -15
  152. package/dist/transform/registration/registerTranslationComponent.js.map +1 -1
  153. package/dist/transform/runtime-translate/buildRuntimeTranslateCalls.js +34 -85
  154. package/dist/transform/runtime-translate/buildRuntimeTranslateCalls.js.map +1 -1
  155. package/dist/transform/runtime-translate/injectRuntimeTranslateImport.js +25 -55
  156. package/dist/transform/runtime-translate/injectRuntimeTranslateImport.js.map +1 -1
  157. package/dist/transform/tracking/trackAssignmentExpression.js +26 -63
  158. package/dist/transform/tracking/trackAssignmentExpression.js.map +1 -1
  159. package/dist/transform/tracking/trackCatchClause.js +17 -48
  160. package/dist/transform/tracking/trackCatchClause.js.map +1 -1
  161. package/dist/transform/tracking/trackClassDeclaration.js +13 -13
  162. package/dist/transform/tracking/trackClassDeclaration.js.map +1 -1
  163. package/dist/transform/tracking/trackForDeclaration.js +17 -53
  164. package/dist/transform/tracking/trackForDeclaration.js.map +1 -1
  165. package/dist/transform/tracking/trackFunctionName.js +10 -10
  166. package/dist/transform/tracking/trackFunctionName.js.map +1 -1
  167. package/dist/transform/tracking/trackFunctionParams.js +19 -54
  168. package/dist/transform/tracking/trackFunctionParams.js.map +1 -1
  169. package/dist/transform/tracking/trackImportDeclaration.js +36 -89
  170. package/dist/transform/tracking/trackImportDeclaration.js.map +1 -1
  171. package/dist/transform/tracking/trackOverridingVariable.js +9 -11
  172. package/dist/transform/tracking/trackOverridingVariable.js.map +1 -1
  173. package/dist/transform/tracking/trackVariableDeclarator.js +51 -102
  174. package/dist/transform/tracking/trackVariableDeclarator.js.map +1 -1
  175. package/dist/transform/validation/validateTranslationComponentArgs.d.ts +1 -1
  176. package/dist/transform/validation/validateTranslationComponentArgs.d.ts.map +1 -1
  177. package/dist/transform/validation/validateTranslationComponentArgs.js +156 -232
  178. package/dist/transform/validation/validateTranslationComponentArgs.js.map +1 -1
  179. package/dist/transform/validation/validateTranslationFunction.js +218 -329
  180. package/dist/transform/validation/validateTranslationFunction.js.map +1 -1
  181. package/dist/utils/calculateHash.d.ts +1 -1
  182. package/dist/utils/calculateHash.d.ts.map +1 -1
  183. package/dist/utils/calculateHash.js +55 -70
  184. package/dist/utils/calculateHash.js.map +1 -1
  185. package/dist/utils/constants/gt/constants.js +142 -132
  186. package/dist/utils/constants/gt/constants.js.map +1 -1
  187. package/dist/utils/constants/gt/helpers.d.ts +1 -1
  188. package/dist/utils/constants/gt/helpers.d.ts.map +1 -1
  189. package/dist/utils/constants/gt/helpers.js +147 -149
  190. package/dist/utils/constants/gt/helpers.js.map +1 -1
  191. package/dist/utils/constants/other/constants.js +45 -41
  192. package/dist/utils/constants/other/constants.js.map +1 -1
  193. package/dist/utils/constants/other/helpers.js +13 -11
  194. package/dist/utils/constants/other/helpers.js.map +1 -1
  195. package/dist/utils/constants/react/constants.js +47 -41
  196. package/dist/utils/constants/react/constants.js.map +1 -1
  197. package/dist/utils/constants/react/helpers.js +17 -16
  198. package/dist/utils/constants/react/helpers.js.map +1 -1
  199. package/dist/utils/constants/resolveIdentifier/isGTComponent.js +41 -76
  200. package/dist/utils/constants/resolveIdentifier/isGTComponent.js.map +1 -1
  201. package/dist/utils/constants/resolveIdentifier/isReactJsxFunction.js +33 -83
  202. package/dist/utils/constants/resolveIdentifier/isReactJsxFunction.js.map +1 -1
  203. package/dist/utils/errors.js +18 -20
  204. package/dist/utils/errors.js.map +1 -1
  205. package/dist/utils/multiplication/multiply.js +31 -35
  206. package/dist/utils/multiplication/multiply.js.map +1 -1
  207. package/dist/utils/multiplication/types.js +1 -3
  208. package/dist/utils/parsing/extractIdentifiersFromLVal.js +30 -96
  209. package/dist/utils/parsing/extractIdentifiersFromLVal.js.map +1 -1
  210. package/dist/utils/parsing/getCalleeNameFromExpression.js +31 -65
  211. package/dist/utils/parsing/getCalleeNameFromExpression.js.map +1 -1
  212. package/dist/utils/parsing/getCalleeNameFromExpressionWrapper.js +13 -10
  213. package/dist/utils/parsing/getCalleeNameFromExpressionWrapper.js.map +1 -1
  214. package/dist/utils/parsing/getObjectPropertyFromObjectExpression.js +19 -56
  215. package/dist/utils/parsing/getObjectPropertyFromObjectExpression.js.map +1 -1
  216. package/dist/utils/parsing/getStringLiteralFromExpression.js +18 -49
  217. package/dist/utils/parsing/getStringLiteralFromExpression.js.map +1 -1
  218. package/dist/utils/parsing/isDeriveInvocation.js +25 -61
  219. package/dist/utils/parsing/isDeriveInvocation.js.map +1 -1
  220. package/dist/utils/parsing/isStringTranslationTaggedTemplate.js +23 -59
  221. package/dist/utils/parsing/isStringTranslationTaggedTemplate.js.map +1 -1
  222. package/dist/utils/string-expressions/buildTransformationResult.js +57 -96
  223. package/dist/utils/string-expressions/buildTransformationResult.js.map +1 -1
  224. package/dist/utils/string-expressions/flattenExpressionToParts.js +131 -142
  225. package/dist/utils/string-expressions/flattenExpressionToParts.js.map +1 -1
  226. package/dist/utils/string-expressions/resolveStaticExpression.js +36 -34
  227. package/dist/utils/string-expressions/resolveStaticExpression.js.map +1 -1
  228. package/dist/utils/validation/validateChildrenFromObjectExpression.js +29 -24
  229. package/dist/utils/validation/validateChildrenFromObjectExpression.js.map +1 -1
  230. package/dist/utils/validation/validateExpressionIsNumericLiteral.js +13 -47
  231. package/dist/utils/validation/validateExpressionIsNumericLiteral.js.map +1 -1
  232. package/dist/utils/validation/validateExpressionIsStringLiteral.js +15 -48
  233. package/dist/utils/validation/validateExpressionIsStringLiteral.js.map +1 -1
  234. package/package.json +8 -4
  235. package/dist/processing/index.js.map +0 -1
  236. package/dist/state/types.js.map +0 -1
  237. package/dist/transform/jsx-children/index.js.map +0 -1
  238. package/dist/utils/multiplication/types.js.map +0 -1
@@ -1,355 +1,244 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.validateTranslationFunction = validateTranslationFunction;
37
- exports.validateUseTranslationsCallback = validateUseTranslationsCallback;
38
- exports.validateUseMessagesCallback = validateUseMessagesCallback;
39
- exports.validateDerive = validateDerive;
40
- const t = __importStar(require("@babel/types"));
41
- const constants_1 = require("../../utils/constants/gt/constants");
42
- const getCalleeNameFromExpression_1 = require("../../utils/parsing/getCalleeNameFromExpression");
43
- const resolveStaticExpression_1 = require("../../utils/string-expressions/resolveStaticExpression");
44
- const getTrackedVariable_1 = require("../getTrackedVariable");
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_runtime = require("../../_virtual/_rolldown/runtime.js");
3
+ require("../../utils/constants/gt/constants.js");
4
+ const require_utils_parsing_getCalleeNameFromExpression = require("../../utils/parsing/getCalleeNameFromExpression.js");
5
+ const require_utils_string_expressions_resolveStaticExpression = require("../../utils/string-expressions/resolveStaticExpression.js");
6
+ const require_transform_getTrackedVariable = require("../getTrackedVariable.js");
7
+ let _babel_types = require("@babel/types");
8
+ _babel_types = require_runtime.__toESM(_babel_types);
9
+ //#region src/transform/validation/validateTranslationFunction.ts
45
10
  /**
46
- * Validate useGT_callback / getGT_callback / msg() / t()
47
- * - first argument must be a statically resolvable string expression
48
- * (string literal, template literal, binary '+' concatenation, or derive() call)
49
- * - second argument, if present, $id field + $context field must be a string literal
50
- */
11
+ * Validate useGT_callback / getGT_callback / msg() / t()
12
+ * - first argument must be a statically resolvable string expression
13
+ * (string literal, template literal, binary '+' concatenation, or derive() call)
14
+ * - second argument, if present, $id field + $context field must be a string literal
15
+ */
51
16
  function validateTranslationFunction(callExprPath, state) {
52
- var _a;
53
- const callExpr = callExprPath.node;
54
- const errors = [];
55
- // Validate that the function has at least 1 argument
56
- if (callExpr.arguments.length < 1) {
57
- errors.push('registration function must have at least 1 argument');
58
- return { errors };
59
- }
60
- // Validate first argument
61
- if (!t.isExpression(callExpr.arguments[0])) {
62
- errors.push('registration function must use a string literal or derive() call as the first argument. Variable content is not allowed.');
63
- return { errors };
64
- }
65
- // Try to resolve the expression to a static string (handles concat, nested templates, etc.)
66
- const resolvedStaticExpression = (0, resolveStaticExpression_1.resolveStaticExpression)(callExprPath.get('arguments')[0]);
67
- // TODO: until we implement derivation, we will only need to check the first value
68
- const content = (_a = resolvedStaticExpression.values) === null || _a === void 0 ? void 0 : _a[0];
69
- const resolutionErrors = getResolutionErrorMessages(resolvedStaticExpression.errors, false);
70
- if (resolutionErrors.length > 0) {
71
- return { errors: resolutionErrors };
72
- }
73
- if (content === undefined && !state.settings.autoderive.strings) {
74
- // Not a static expression — check if it contains a derive() function invocation
75
- validateDerive(callExpr.arguments[0], state, errors);
76
- if (errors.length > 0) {
77
- errors.push(...getResolutionErrorMessages(resolvedStaticExpression.errors));
78
- errors.push('registration function must use a string literal or derive() call as the first argument. Variable content is not allowed.');
79
- return { errors };
80
- }
81
- }
82
- // TODO: hasDeriveContext should be refactored to enforce no hash generated HERE in this function
83
- // instead of passing that information outside of this function.
84
- // We skip hash gen with autoderive, derive in content, and derive in $context. This flag is being
85
- // reused for all 3 cases.
86
- const contentHasAutoderive = state.settings.autoderive.strings && content === undefined;
87
- // Validate second argument
88
- let context;
89
- let id;
90
- let hash;
91
- let maxChars;
92
- let format;
93
- let hasDeriveContext;
94
- if (callExpr.arguments.length === 1) {
95
- return {
96
- errors,
97
- content,
98
- hasDeriveContext: contentHasAutoderive || undefined,
99
- };
100
- }
101
- if (t.isObjectExpression(callExpr.arguments[1])) {
102
- const objExprPath = callExprPath.get('arguments')[1];
103
- const contextProperty = validatePropertyFromObjectExpression(objExprPath, constants_1.USEGT_CALLBACK_OPTIONS.$context, 'string-or-derive', state);
104
- errors.push(...contextProperty.errors);
105
- context = contextProperty.value;
106
- hasDeriveContext =
107
- contentHasAutoderive || contextProperty.hasDeriveExpression;
108
- const idProperty = validatePropertyFromObjectExpression(objExprPath, constants_1.USEGT_CALLBACK_OPTIONS.$id, 'string');
109
- errors.push(...idProperty.errors);
110
- id = idProperty.value;
111
- const maxCharsProperty = validatePropertyFromObjectExpression(objExprPath, constants_1.USEGT_CALLBACK_OPTIONS.$maxChars, 'number');
112
- errors.push(...maxCharsProperty.errors);
113
- maxChars = maxCharsProperty.value;
114
- const hashProperty = validatePropertyFromObjectExpression(objExprPath, constants_1.USEGT_CALLBACK_OPTIONS.$_hash, 'string');
115
- errors.push(...hashProperty.errors);
116
- hash = hashProperty.value;
117
- const formatProperty = validatePropertyFromObjectExpression(objExprPath, constants_1.USEGT_CALLBACK_OPTIONS.$format, 'string');
118
- errors.push(...formatProperty.errors);
119
- format = formatProperty.value;
120
- }
121
- return {
122
- errors,
123
- content,
124
- context,
125
- id,
126
- hash,
127
- maxChars,
128
- format,
129
- hasDeriveContext,
130
- };
17
+ var _resolvedStaticExpres;
18
+ const callExpr = callExprPath.node;
19
+ const errors = [];
20
+ if (callExpr.arguments.length < 1) {
21
+ errors.push("registration function must have at least 1 argument");
22
+ return { errors };
23
+ }
24
+ if (!_babel_types.isExpression(callExpr.arguments[0])) {
25
+ errors.push("registration function must use a string literal or derive() call as the first argument. Variable content is not allowed.");
26
+ return { errors };
27
+ }
28
+ const resolvedStaticExpression = require_utils_string_expressions_resolveStaticExpression.resolveStaticExpression(callExprPath.get("arguments")[0]);
29
+ const content = (_resolvedStaticExpres = resolvedStaticExpression.values) === null || _resolvedStaticExpres === void 0 ? void 0 : _resolvedStaticExpres[0];
30
+ const resolutionErrors = getResolutionErrorMessages(resolvedStaticExpression.errors, false);
31
+ if (resolutionErrors.length > 0) return { errors: resolutionErrors };
32
+ if (content === void 0 && !state.settings.autoderive.strings) {
33
+ validateDerive(callExpr.arguments[0], state, errors);
34
+ if (errors.length > 0) {
35
+ errors.push(...getResolutionErrorMessages(resolvedStaticExpression.errors));
36
+ errors.push("registration function must use a string literal or derive() call as the first argument. Variable content is not allowed.");
37
+ return { errors };
38
+ }
39
+ }
40
+ const contentHasAutoderive = state.settings.autoderive.strings && content === void 0;
41
+ let context;
42
+ let id;
43
+ let hash;
44
+ let maxChars;
45
+ let format;
46
+ let hasDeriveContext;
47
+ if (callExpr.arguments.length === 1) return {
48
+ errors,
49
+ content,
50
+ hasDeriveContext: contentHasAutoderive || void 0
51
+ };
52
+ if (_babel_types.isObjectExpression(callExpr.arguments[1])) {
53
+ const objExprPath = callExprPath.get("arguments")[1];
54
+ const contextProperty = validatePropertyFromObjectExpression(objExprPath, "$context", "string-or-derive", state);
55
+ errors.push(...contextProperty.errors);
56
+ context = contextProperty.value;
57
+ hasDeriveContext = contentHasAutoderive || contextProperty.hasDeriveExpression;
58
+ const idProperty = validatePropertyFromObjectExpression(objExprPath, "$id", "string");
59
+ errors.push(...idProperty.errors);
60
+ id = idProperty.value;
61
+ const maxCharsProperty = validatePropertyFromObjectExpression(objExprPath, "$maxChars", "number");
62
+ errors.push(...maxCharsProperty.errors);
63
+ maxChars = maxCharsProperty.value;
64
+ const hashProperty = validatePropertyFromObjectExpression(objExprPath, "$_hash", "string");
65
+ errors.push(...hashProperty.errors);
66
+ hash = hashProperty.value;
67
+ const formatProperty = validatePropertyFromObjectExpression(objExprPath, "$format", "string");
68
+ errors.push(...formatProperty.errors);
69
+ format = formatProperty.value;
70
+ }
71
+ return {
72
+ errors,
73
+ content,
74
+ context,
75
+ id,
76
+ hash,
77
+ maxChars,
78
+ format,
79
+ hasDeriveContext
80
+ };
131
81
  }
132
82
  /**
133
- * Validate useTranslations_callback / getTranslations_callback
134
- * - always valid (arguments can be dynamic)
135
- */
136
- // eslint-disable-next-line no-unused-vars
83
+ * Validate useTranslations_callback / getTranslations_callback
84
+ * - always valid (arguments can be dynamic)
85
+ */
137
86
  function validateUseTranslationsCallback(_callExpr) {
138
- const errors = [];
139
- return { errors };
87
+ return { errors: [] };
140
88
  }
141
89
  /**
142
- * Validate useMessages_callback / getMessages_callback
143
- * - always valid
144
- */
145
- // eslint-disable-next-line no-unused-vars
90
+ * Validate useMessages_callback / getMessages_callback
91
+ * - always valid
92
+ */
146
93
  function validateUseMessagesCallback(_callExpr) {
147
- const errors = [];
148
- return { errors };
94
+ return { errors: [] };
149
95
  }
150
- /* =============================== */
151
- /* Helper Functions */
152
- /* =============================== */
153
96
  function getResolutionErrorMessages(errors, includeDynamic = true) {
154
- return errors
155
- .filter((error) => includeDynamic || error.kind !== 'dynamic-expression')
156
- .map(({ message }) => message);
97
+ return errors.filter((error) => includeDynamic || error.kind !== "dynamic-expression").map(({ message }) => message);
157
98
  }
158
99
  function validatePropertyFromObjectExpression(objExprPath, name, type, state) {
159
- var _a;
160
- const result = { errors: [] };
161
- let valuePath;
162
- for (const propertyPath of objExprPath.get('properties')) {
163
- if (!propertyPath.isObjectProperty()) {
164
- continue;
165
- }
166
- const property = propertyPath.node;
167
- if (t.isIdentifier(property.key) && property.key.name === name) {
168
- valuePath = propertyPath;
169
- break;
170
- }
171
- if (t.isStringLiteral(property.key) && property.key.value === name) {
172
- valuePath = propertyPath;
173
- break;
174
- }
175
- }
176
- // return result if no value found
177
- if (!valuePath) {
178
- return result;
179
- }
180
- const value = valuePath.node;
181
- // validate value
182
- if (!t.isExpression(value.value)) {
183
- result.errors.push(`registration function must use a string literal for its ${name} field. Variable content is not allowed.`);
184
- return result;
185
- }
186
- // extract value
187
- if (type === 'string-or-derive') {
188
- const resolved = (0, resolveStaticExpression_1.resolveStaticExpression)(valuePath.get('value'));
189
- const resolutionErrors = getResolutionErrorMessages(resolved.errors, false);
190
- // TODO: until we implement derivation, we will only need to check the first value
191
- if (((_a = resolved.values) === null || _a === void 0 ? void 0 : _a[0]) !== undefined) {
192
- result.value = resolved.values[0];
193
- }
194
- else if (resolutionErrors.length > 0) {
195
- result.errors.push(...resolutionErrors);
196
- }
197
- else if (state) {
198
- // Static resolution failed — check if it's a valid derive() expression
199
- const deriveErrors = [];
200
- validateDerive(value.value, state, deriveErrors);
201
- if (deriveErrors.length === 0) {
202
- result.hasDeriveExpression = true;
203
- }
204
- else {
205
- result.errors.push(...getResolutionErrorMessages(resolved.errors), ...deriveErrors);
206
- }
207
- }
208
- else {
209
- result.errors.push(...getResolutionErrorMessages(resolved.errors));
210
- }
211
- }
212
- else {
213
- const validatedValue = type === 'string'
214
- ? validateExpressionIsStringLiteral(value.value)
215
- : validateExpressionIsNumericLiteral(value.value);
216
- result.errors.push(...validatedValue.errors);
217
- result.value = validatedValue.value;
218
- }
219
- return result;
100
+ const result = { errors: [] };
101
+ let valuePath;
102
+ for (const propertyPath of objExprPath.get("properties")) {
103
+ if (!propertyPath.isObjectProperty()) continue;
104
+ const property = propertyPath.node;
105
+ if (_babel_types.isIdentifier(property.key) && property.key.name === name) {
106
+ valuePath = propertyPath;
107
+ break;
108
+ }
109
+ if (_babel_types.isStringLiteral(property.key) && property.key.value === name) {
110
+ valuePath = propertyPath;
111
+ break;
112
+ }
113
+ }
114
+ if (!valuePath) return result;
115
+ const value = valuePath.node;
116
+ if (!_babel_types.isExpression(value.value)) {
117
+ result.errors.push(`registration function must use a string literal for its ${name} field. Variable content is not allowed.`);
118
+ return result;
119
+ }
120
+ if (type === "string-or-derive") {
121
+ var _resolved$values;
122
+ const resolved = require_utils_string_expressions_resolveStaticExpression.resolveStaticExpression(valuePath.get("value"));
123
+ const resolutionErrors = getResolutionErrorMessages(resolved.errors, false);
124
+ if (((_resolved$values = resolved.values) === null || _resolved$values === void 0 ? void 0 : _resolved$values[0]) !== void 0) result.value = resolved.values[0];
125
+ else if (resolutionErrors.length > 0) result.errors.push(...resolutionErrors);
126
+ else if (state) {
127
+ const deriveErrors = [];
128
+ validateDerive(value.value, state, deriveErrors);
129
+ if (deriveErrors.length === 0) result.hasDeriveExpression = true;
130
+ else result.errors.push(...getResolutionErrorMessages(resolved.errors), ...deriveErrors);
131
+ } else result.errors.push(...getResolutionErrorMessages(resolved.errors));
132
+ } else {
133
+ const validatedValue = type === "string" ? validateExpressionIsStringLiteral(value.value) : validateExpressionIsNumericLiteral(value.value);
134
+ result.errors.push(...validatedValue.errors);
135
+ result.value = validatedValue.value;
136
+ }
137
+ return result;
220
138
  }
221
139
  /**
222
- * Validate that an expression is a string literal
223
- */
140
+ * Validate that an expression is a string literal
141
+ */
224
142
  function validateExpressionIsStringLiteral(expr) {
225
- var _a;
226
- if (t.isStringLiteral(expr)) {
227
- return { errors: [], value: expr.value };
228
- }
229
- if (t.isTemplateLiteral(expr) && expr.expressions.length === 0) {
230
- return { errors: [], value: (_a = expr.quasis[0]) === null || _a === void 0 ? void 0 : _a.value.cooked };
231
- }
232
- return { errors: ['Expression is not a string literal'] };
143
+ if (_babel_types.isStringLiteral(expr)) return {
144
+ errors: [],
145
+ value: expr.value
146
+ };
147
+ if (_babel_types.isTemplateLiteral(expr) && expr.expressions.length === 0) {
148
+ var _expr$quasis$;
149
+ return {
150
+ errors: [],
151
+ value: (_expr$quasis$ = expr.quasis[0]) === null || _expr$quasis$ === void 0 ? void 0 : _expr$quasis$.value.cooked
152
+ };
153
+ }
154
+ return { errors: ["Expression is not a string literal"] };
233
155
  }
234
156
  /**
235
- * Validates if an expression uses the derive() function correctly
236
- */
157
+ * Validates if an expression uses the derive() function correctly
158
+ */
237
159
  function validateDerive(expr, state, errors) {
238
- if (!expr) {
239
- errors.push('Expression is empty');
240
- return { errors };
241
- }
242
- // 1. Direct call: derive(node)
243
- if (t.isCallExpression(expr)) {
244
- // Find the canonical function name
245
- const { namespaceName, functionName } = (0, getCalleeNameFromExpression_1.getCalleeNameFromExpression)(expr);
246
- // Get the canonical function name
247
- const { canonicalName, type } = (0, getTrackedVariable_1.getTrackedVariable)(state.scopeTracker, namespaceName, functionName);
248
- if (!canonicalName) {
249
- errors.push('Expression does not use an allowed call expression');
250
- return { errors };
251
- }
252
- // Validate the function is actually the GT derive function
253
- if (type !== 'generaltranslation' ||
254
- (canonicalName !== constants_1.GT_OTHER_FUNCTIONS.declareStatic &&
255
- canonicalName !== constants_1.GT_OTHER_FUNCTIONS.derive)) {
256
- errors.push('Expression does not use an allowed call expression');
257
- return { errors };
258
- }
259
- // Validate that the call expression has exactly one argument and the argument is a call expression
260
- validateDeclareStaticExpression(expr, errors);
261
- return { errors };
262
- }
263
- // 2. String concatenation: "Hello there " + derive(getName())
264
- if (t.isBinaryExpression(expr) && expr.operator === '+') {
265
- if (!t.isExpression(expr.left) || !t.isExpression(expr.right)) {
266
- errors.push('Operands must be expressions');
267
- return { errors };
268
- }
269
- validateDerive(expr.right, state, errors);
270
- validateDerive(expr.left, state, errors);
271
- return { errors };
272
- }
273
- // 3. Template literal: `Hello there ${derive(getName())}` or `static text`
274
- if (t.isTemplateLiteral(expr)) {
275
- if (expr.expressions.length === 0) {
276
- return { errors };
277
- }
278
- if (!expr.expressions.some((expression) => t.isExpression(expression) &&
279
- validateDerive(expression, state, errors).errors.length === 0)) {
280
- errors.push('Expression does not use an allowed call expression');
281
- }
282
- return {
283
- errors,
284
- };
285
- }
286
- // 4. Static literals (string, number, boolean, null)
287
- if (t.isStringLiteral(expr)) {
288
- return { errors };
289
- }
290
- if (t.isNumericLiteral(expr)) {
291
- return { errors };
292
- }
293
- if (t.isBooleanLiteral(expr)) {
294
- return { errors };
295
- }
296
- if (t.isNullLiteral(expr)) {
297
- return { errors };
298
- }
299
- // Fallthrough: expression type not supported (e.g., plain identifiers/variables)
300
- errors.push('Variables are not allowed');
301
- return { errors };
160
+ if (!expr) {
161
+ errors.push("Expression is empty");
162
+ return { errors };
163
+ }
164
+ if (_babel_types.isCallExpression(expr)) {
165
+ const { namespaceName, functionName } = require_utils_parsing_getCalleeNameFromExpression.getCalleeNameFromExpression(expr);
166
+ const { canonicalName, type } = require_transform_getTrackedVariable.getTrackedVariable(state.scopeTracker, namespaceName, functionName);
167
+ if (!canonicalName) {
168
+ errors.push("Expression does not use an allowed call expression");
169
+ return { errors };
170
+ }
171
+ if (type !== "generaltranslation" || canonicalName !== "declareStatic" && canonicalName !== "derive") {
172
+ errors.push("Expression does not use an allowed call expression");
173
+ return { errors };
174
+ }
175
+ validateDeclareStaticExpression(expr, errors);
176
+ return { errors };
177
+ }
178
+ if (_babel_types.isBinaryExpression(expr) && expr.operator === "+") {
179
+ if (!_babel_types.isExpression(expr.left) || !_babel_types.isExpression(expr.right)) {
180
+ errors.push("Operands must be expressions");
181
+ return { errors };
182
+ }
183
+ validateDerive(expr.right, state, errors);
184
+ validateDerive(expr.left, state, errors);
185
+ return { errors };
186
+ }
187
+ if (_babel_types.isTemplateLiteral(expr)) {
188
+ if (expr.expressions.length === 0) return { errors };
189
+ if (!expr.expressions.some((expression) => _babel_types.isExpression(expression) && validateDerive(expression, state, errors).errors.length === 0)) errors.push("Expression does not use an allowed call expression");
190
+ return { errors };
191
+ }
192
+ if (_babel_types.isStringLiteral(expr)) return { errors };
193
+ if (_babel_types.isNumericLiteral(expr)) return { errors };
194
+ if (_babel_types.isBooleanLiteral(expr)) return { errors };
195
+ if (_babel_types.isNullLiteral(expr)) return { errors };
196
+ errors.push("Variables are not allowed");
197
+ return { errors };
302
198
  }
303
199
  /**
304
- * Takes in a call expression to check if:
305
- * - it has exactly one argument
306
- * - the argument is a call expression
307
- * Example: derive(getName())
308
- */
200
+ * Takes in a call expression to check if:
201
+ * - it has exactly one argument
202
+ * - the argument is a call expression
203
+ * Example: derive(getName())
204
+ */
309
205
  function validateDeclareStaticExpression(expr, errors) {
310
- // Validate that the function has 1 argument
311
- if (expr.arguments.length !== 1) {
312
- errors.push('derive() must have one argument');
313
- return { errors };
314
- }
315
- const [onlyArg] = expr.arguments;
316
- // Await expression: derive(await ...)
317
- if (t.isAwaitExpression(onlyArg)) {
318
- // Validate that the awaited expression is a call expression
319
- if (!t.isCallExpression(onlyArg.argument)) {
320
- errors.push('derive() must have a call expression as the argument');
321
- return { errors };
322
- }
323
- // Valid: derive(await someFunction())
324
- return { errors };
325
- }
326
- // Validate that the argument is a call expression
327
- if (!t.isCallExpression(onlyArg)) {
328
- errors.push('derive() must have a call expression as the argument');
329
- return { errors };
330
- }
331
- return { errors };
206
+ if (expr.arguments.length !== 1) {
207
+ errors.push("derive() must have one argument");
208
+ return { errors };
209
+ }
210
+ const [onlyArg] = expr.arguments;
211
+ if (_babel_types.isAwaitExpression(onlyArg)) {
212
+ if (!_babel_types.isCallExpression(onlyArg.argument)) {
213
+ errors.push("derive() must have a call expression as the argument");
214
+ return { errors };
215
+ }
216
+ return { errors };
217
+ }
218
+ if (!_babel_types.isCallExpression(onlyArg)) {
219
+ errors.push("derive() must have a call expression as the argument");
220
+ return { errors };
221
+ }
222
+ return { errors };
332
223
  }
333
224
  /**
334
- * Validate that an expression is a number literal
335
- */
225
+ * Validate that an expression is a number literal
226
+ */
336
227
  function validateExpressionIsNumericLiteral(expr) {
337
- let candidateValue;
338
- if (t.isNumericLiteral(expr)) {
339
- candidateValue = expr.value;
340
- }
341
- else if (t.isUnaryExpression(expr) && t.isNumericLiteral(expr.argument)) {
342
- // Note: taking the absolute value of the number literal
343
- candidateValue = expr.argument.value;
344
- }
345
- // validate is integer
346
- if (candidateValue !== undefined && !Number.isInteger(candidateValue)) {
347
- return { errors: ['Expression is not an integer'] };
348
- }
349
- // no value found
350
- if (candidateValue === undefined) {
351
- return { errors: ['Expression is not a number literal'] };
352
- }
353
- return { errors: [], value: candidateValue };
228
+ let candidateValue;
229
+ if (_babel_types.isNumericLiteral(expr)) candidateValue = expr.value;
230
+ else if (_babel_types.isUnaryExpression(expr) && _babel_types.isNumericLiteral(expr.argument)) candidateValue = expr.argument.value;
231
+ if (candidateValue !== void 0 && !Number.isInteger(candidateValue)) return { errors: ["Expression is not an integer"] };
232
+ if (candidateValue === void 0) return { errors: ["Expression is not a number literal"] };
233
+ return {
234
+ errors: [],
235
+ value: candidateValue
236
+ };
354
237
  }
238
+ //#endregion
239
+ exports.validateDerive = validateDerive;
240
+ exports.validateTranslationFunction = validateTranslationFunction;
241
+ exports.validateUseMessagesCallback = validateUseMessagesCallback;
242
+ exports.validateUseTranslationsCallback = validateUseTranslationsCallback;
243
+
355
244
  //# sourceMappingURL=validateTranslationFunction.js.map