gtx-cli 2.1.8-alpha.1 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # gtx-cli
2
2
 
3
+ ## 2.1.9
4
+
5
+ ### Patch Changes
6
+
7
+ - [#604](https://github.com/generaltranslation/gt/pull/604) [`43c6a76`](https://github.com/generaltranslation/gt/commit/43c6a76be3d3be420e892b86188ef41c45ae8ffe) Thanks [@archie-mckenzie](https://github.com/archie-mckenzie)! - Refactored useGT and useMessages in order to make useMessages function like an unlintable useGT
8
+
9
+ ## 2.1.8
10
+
11
+ ### Patch Changes
12
+
13
+ - [#599](https://github.com/generaltranslation/gt/pull/599) [`5950592`](https://github.com/generaltranslation/gt/commit/5950592ca44197915216ec5c8e26f9714cb4f55c) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - feat: msg() function
14
+
3
15
  ## 2.1.8
4
16
 
5
17
  ### Patch Changes
@@ -1,6 +1,8 @@
1
1
  export declare const MSG_TRANSLATION_HOOK = "msg";
2
2
  export declare const INLINE_TRANSLATION_HOOK = "useGT";
3
3
  export declare const INLINE_TRANSLATION_HOOK_ASYNC = "getGT";
4
+ export declare const INLINE_MESSAGE_HOOK = "useMessages";
5
+ export declare const INLINE_MESSAGE_HOOK_ASYNC = "getMessages";
4
6
  export declare const GT_TRANSLATION_FUNCS: string[];
5
7
  export declare const VARIABLE_COMPONENTS: string[];
6
8
  export declare const GT_ATTRIBUTES_WITH_SUGAR: string[];
@@ -1,10 +1,14 @@
1
1
  export const MSG_TRANSLATION_HOOK = 'msg';
2
2
  export const INLINE_TRANSLATION_HOOK = 'useGT';
3
3
  export const INLINE_TRANSLATION_HOOK_ASYNC = 'getGT';
4
+ export const INLINE_MESSAGE_HOOK = 'useMessages';
5
+ export const INLINE_MESSAGE_HOOK_ASYNC = 'getMessages';
4
6
  // GT translation functions
5
7
  export const GT_TRANSLATION_FUNCS = [
6
8
  INLINE_TRANSLATION_HOOK,
7
9
  INLINE_TRANSLATION_HOOK_ASYNC,
10
+ INLINE_MESSAGE_HOOK,
11
+ INLINE_MESSAGE_HOOK_ASYNC,
8
12
  MSG_TRANSLATION_HOOK,
9
13
  'T',
10
14
  'Var',
@@ -1,6 +1,6 @@
1
1
  import * as t from '@babel/types';
2
2
  import { isStaticExpression } from '../evaluateJsx.js';
3
- import { GT_ATTRIBUTES_WITH_SUGAR, MSG_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK_ASYNC, mapAttributeName, } from './constants.js';
3
+ import { GT_ATTRIBUTES_WITH_SUGAR, MSG_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK_ASYNC, mapAttributeName, INLINE_MESSAGE_HOOK, INLINE_MESSAGE_HOOK_ASYNC, } from './constants.js';
4
4
  import { warnNonStaticExpressionSync, warnNonStringSync, warnTemplateLiteralSync, warnAsyncUseGT, warnSyncGetGT, } from '../../../console/index.js';
5
5
  import generateModule from '@babel/generator';
6
6
  import traverseModule from '@babel/traverse';
@@ -22,7 +22,7 @@ import resolve from 'resolve';
22
22
  * - Metadata extraction from options object
23
23
  * - Error reporting for non-static expressions and template literals with expressions
24
24
  */
25
- function processTranslationCall(tPath, updates, errors, file, ignoreAdditionalData) {
25
+ function processTranslationCall(tPath, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent) {
26
26
  if (tPath.parent.type === 'CallExpression' &&
27
27
  tPath.parent.arguments.length > 0) {
28
28
  const arg = tPath.parent.arguments[0];
@@ -59,10 +59,14 @@ function processTranslationCall(tPath, updates, errors, file, ignoreAdditionalDa
59
59
  }
60
60
  else if (t.isTemplateLiteral(arg)) {
61
61
  // warn if template literal
62
- errors.push(warnTemplateLiteralSync(file, generate(arg).code, `${arg.loc?.start?.line}:${arg.loc?.start?.column}`));
62
+ if (!ignoreDynamicContent) {
63
+ errors.push(warnTemplateLiteralSync(file, generate(arg).code, `${arg.loc?.start?.line}:${arg.loc?.start?.column}`));
64
+ }
63
65
  }
64
66
  else {
65
- errors.push(warnNonStringSync(file, generate(arg).code, `${arg.loc?.start?.line}:${arg.loc?.start?.column}`));
67
+ if (!ignoreDynamicContent) {
68
+ errors.push(warnNonStringSync(file, generate(arg).code, `${arg.loc?.start?.line}:${arg.loc?.start?.column}`));
69
+ }
66
70
  }
67
71
  }
68
72
  }
@@ -145,11 +149,11 @@ function resolveVariableAliases(scope, variableName, visited = new Set()) {
145
149
  * This covers both direct translation calls (t('hello')) and prop drilling
146
150
  * where the translation callback is passed to other functions (getData(t)).
147
151
  */
148
- function handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAdditionalData) {
152
+ function handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAdditionalData, ignoreDynamicContent) {
149
153
  if (tPath.parent.type === 'CallExpression' &&
150
154
  tPath.parent.callee === tPath.node) {
151
155
  // Direct translation call: t('hello')
152
- processTranslationCall(tPath, updates, errors, file, ignoreAdditionalData);
156
+ processTranslationCall(tPath, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent);
153
157
  }
154
158
  else if (tPath.parent.type === 'CallExpression' &&
155
159
  t.isExpression(tPath.node) &&
@@ -161,7 +165,7 @@ function handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAddit
161
165
  const calleeBinding = tPath.scope.getBinding(callee.name);
162
166
  if (calleeBinding && calleeBinding.path.isFunction()) {
163
167
  const functionPath = calleeBinding.path;
164
- processFunctionIfMatches(callee.name, argIndex, functionPath.node, functionPath, updates, errors, file, ignoreAdditionalData);
168
+ processFunctionIfMatches(callee.name, argIndex, functionPath.node, functionPath, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent);
165
169
  }
166
170
  // Handle arrow functions assigned to variables: const getData = (t) => {...}
167
171
  else if (calleeBinding &&
@@ -170,14 +174,14 @@ function handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAddit
170
174
  (t.isArrowFunctionExpression(calleeBinding.path.node.init) ||
171
175
  t.isFunctionExpression(calleeBinding.path.node.init))) {
172
176
  const initPath = calleeBinding.path.get('init');
173
- processFunctionIfMatches(callee.name, argIndex, calleeBinding.path.node.init, initPath, updates, errors, file, ignoreAdditionalData);
177
+ processFunctionIfMatches(callee.name, argIndex, calleeBinding.path.node.init, initPath, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent);
174
178
  }
175
179
  // If not found locally, check if it's an imported function
176
180
  else if (importMap.has(callee.name)) {
177
181
  const importPath = importMap.get(callee.name);
178
182
  const resolvedPath = resolveImportPath(file, importPath);
179
183
  if (resolvedPath) {
180
- findFunctionInFile(resolvedPath, callee.name, argIndex, updates, errors, ignoreAdditionalData);
184
+ findFunctionInFile(resolvedPath, callee.name, argIndex, updates, errors, ignoreAdditionalData, ignoreDynamicContent);
181
185
  }
182
186
  }
183
187
  }
@@ -188,12 +192,12 @@ function handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAddit
188
192
  * Validates the function has enough parameters and traces how the translation callback
189
193
  * is used within that function's body.
190
194
  */
191
- function processFunctionIfMatches(_functionName, argIndex, functionNode, functionPath, updates, errors, filePath, ignoreAdditionalData) {
195
+ function processFunctionIfMatches(_functionName, argIndex, functionNode, functionPath, updates, errors, filePath, ignoreAdditionalData, ignoreDynamicContent) {
192
196
  if (functionNode.params.length > argIndex) {
193
197
  const param = functionNode.params[argIndex];
194
198
  const paramName = extractParameterName(param);
195
199
  if (paramName) {
196
- findFunctionParameterUsage(functionPath, paramName, updates, errors, filePath, ignoreAdditionalData);
200
+ findFunctionParameterUsage(functionPath, paramName, updates, errors, filePath, ignoreAdditionalData, ignoreDynamicContent);
197
201
  }
198
202
  }
199
203
  }
@@ -205,7 +209,7 @@ function processFunctionIfMatches(_functionName, argIndex, functionNode, functio
205
209
  * Example: In function getInfo(t) { return t('hello'); }, this finds the t('hello') call.
206
210
  * Example: In function getData(t) { return getFooter(t); }, this finds and traces into getFooter.
207
211
  */
208
- function findFunctionParameterUsage(functionPath, parameterName, updates, errors, file, ignoreAdditionalData) {
212
+ function findFunctionParameterUsage(functionPath, parameterName, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent) {
209
213
  // Look for the function body and find all usages of the parameter
210
214
  if (functionPath.isFunction()) {
211
215
  const functionScope = functionPath.scope;
@@ -220,7 +224,7 @@ function findFunctionParameterUsage(functionPath, parameterName, updates, errors
220
224
  const binding = functionScope.bindings[name];
221
225
  if (binding) {
222
226
  binding.referencePaths.forEach((refPath) => {
223
- handleFunctionCall(refPath, updates, errors, file, importMap, ignoreAdditionalData);
227
+ handleFunctionCall(refPath, updates, errors, file, importMap, ignoreAdditionalData, ignoreDynamicContent);
224
228
  });
225
229
  }
226
230
  });
@@ -299,7 +303,7 @@ function resolveImportPath(currentFile, importPath) {
299
303
  * - export function getInfo(t) { ... }
300
304
  * - const getInfo = (t) => { ... }
301
305
  */
302
- function findFunctionInFile(filePath, functionName, argIndex, updates, errors, ignoreAdditionalData) {
306
+ function findFunctionInFile(filePath, functionName, argIndex, updates, errors, ignoreAdditionalData, ignoreDynamicContent) {
303
307
  try {
304
308
  const code = fs.readFileSync(filePath, 'utf8');
305
309
  const ast = parse(code, {
@@ -310,7 +314,7 @@ function findFunctionInFile(filePath, functionName, argIndex, updates, errors, i
310
314
  // Handle function declarations: function getInfo(t) { ... }
311
315
  FunctionDeclaration(path) {
312
316
  if (path.node.id?.name === functionName) {
313
- processFunctionIfMatches(functionName, argIndex, path.node, path, updates, errors, filePath, ignoreAdditionalData);
317
+ processFunctionIfMatches(functionName, argIndex, path.node, path, updates, errors, filePath, ignoreAdditionalData, ignoreDynamicContent);
314
318
  }
315
319
  },
316
320
  // Handle variable declarations: const getInfo = (t) => { ... }
@@ -321,7 +325,7 @@ function findFunctionInFile(filePath, functionName, argIndex, updates, errors, i
321
325
  (t.isArrowFunctionExpression(path.node.init) ||
322
326
  t.isFunctionExpression(path.node.init))) {
323
327
  const initPath = path.get('init');
324
- processFunctionIfMatches(functionName, argIndex, path.node.init, initPath, updates, errors, filePath, ignoreAdditionalData);
328
+ processFunctionIfMatches(functionName, argIndex, path.node.init, initPath, updates, errors, filePath, ignoreAdditionalData, ignoreDynamicContent);
325
329
  }
326
330
  },
327
331
  });
@@ -350,31 +354,38 @@ export function parseStrings(importName, originalName, path, updates, errors, fi
350
354
  for (const refPath of referencePaths) {
351
355
  // Handle msg() calls directly without variable assignment
352
356
  if (originalName === MSG_TRANSLATION_HOOK) {
353
- const ignoreAdditionalData = true;
357
+ const ignoreAdditionalData = false;
358
+ const ignoreDynamicContent = false;
354
359
  // Check if this is a direct call to msg('string')
355
360
  if (refPath.parent.type === 'CallExpression' &&
356
361
  refPath.parent.callee === refPath.node) {
357
- processTranslationCall(refPath, updates, errors, file, ignoreAdditionalData);
362
+ processTranslationCall(refPath, updates, errors, file, ignoreAdditionalData, ignoreDynamicContent);
358
363
  }
359
364
  continue;
360
365
  }
361
- // Handle useGT() and getGT() calls that need variable assignment
366
+ // Handle useGT(), getGT(), useMessages(), and getMessages() calls that need variable assignment
362
367
  const callExpr = refPath.findParent((p) => p.isCallExpression());
363
368
  if (callExpr) {
364
369
  // Get the parent, handling both await and non-await cases
365
370
  const parentPath = callExpr.parentPath;
366
371
  const parentFunction = refPath.getFunctionParent();
367
372
  const asyncScope = parentFunction?.node.async;
368
- if (asyncScope && originalName === INLINE_TRANSLATION_HOOK) {
373
+ if (asyncScope &&
374
+ (originalName === INLINE_TRANSLATION_HOOK ||
375
+ originalName === INLINE_MESSAGE_HOOK)) {
369
376
  errors.push(warnAsyncUseGT(file, `${refPath.node.loc?.start?.line}:${refPath.node.loc?.start?.column}`));
370
377
  return;
371
378
  }
372
379
  else if (!asyncScope &&
373
- originalName === INLINE_TRANSLATION_HOOK_ASYNC) {
380
+ (originalName === INLINE_TRANSLATION_HOOK_ASYNC ||
381
+ originalName === INLINE_MESSAGE_HOOK_ASYNC)) {
374
382
  errors.push(warnSyncGetGT(file, `${refPath.node.loc?.start?.line}:${refPath.node.loc?.start?.column}`));
375
383
  return;
376
384
  }
377
- const ignoreAdditionalData = false;
385
+ const isMessageHook = originalName === INLINE_MESSAGE_HOOK ||
386
+ originalName === INLINE_MESSAGE_HOOK_ASYNC;
387
+ const ignoreAdditionalData = isMessageHook;
388
+ const ignoreDynamicContent = isMessageHook;
378
389
  const effectiveParent = parentPath?.node.type === 'AwaitExpression'
379
390
  ? parentPath.parentPath
380
391
  : parentPath;
@@ -391,7 +402,7 @@ export function parseStrings(importName, originalName, path, updates, errors, fi
391
402
  allTranslationNames.forEach((name) => {
392
403
  const tReferencePaths = variableScope.bindings[name]?.referencePaths || [];
393
404
  for (const tPath of tReferencePaths) {
394
- handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAdditionalData);
405
+ handleFunctionCall(tPath, updates, errors, file, importMap, ignoreAdditionalData, ignoreDynamicContent);
395
406
  }
396
407
  });
397
408
  }
@@ -8,7 +8,7 @@ import { parseJSXElement } from '../jsx/utils/parseJsx.js';
8
8
  import { parseStrings } from '../jsx/utils/parseStringFunction.js';
9
9
  import { extractImportName } from '../jsx/utils/parseAst.js';
10
10
  import { logError } from '../../console/logging.js';
11
- import { GT_TRANSLATION_FUNCS, INLINE_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK_ASYNC, MSG_TRANSLATION_HOOK, } from '../jsx/utils/constants.js';
11
+ import { GT_TRANSLATION_FUNCS, INLINE_TRANSLATION_HOOK, INLINE_TRANSLATION_HOOK_ASYNC, INLINE_MESSAGE_HOOK, INLINE_MESSAGE_HOOK_ASYNC, MSG_TRANSLATION_HOOK, } from '../jsx/utils/constants.js';
12
12
  import { matchFiles } from '../../fs/matchFiles.js';
13
13
  import { DEFAULT_SRC_PATTERNS } from '../../config/generateSettings.js';
14
14
  export async function createInlineUpdates(pkg, validate, filePatterns) {
@@ -40,6 +40,8 @@ export async function createInlineUpdates(pkg, validate, filePatterns) {
40
40
  for (const name of importName) {
41
41
  if (name.original === INLINE_TRANSLATION_HOOK ||
42
42
  name.original === INLINE_TRANSLATION_HOOK_ASYNC ||
43
+ name.original === INLINE_MESSAGE_HOOK ||
44
+ name.original === INLINE_MESSAGE_HOOK_ASYNC ||
43
45
  name.original === MSG_TRANSLATION_HOOK) {
44
46
  translationPaths.push({
45
47
  localName: name.local,
@@ -69,6 +71,8 @@ export async function createInlineUpdates(pkg, validate, filePatterns) {
69
71
  for (const name of importName) {
70
72
  if (name.original === INLINE_TRANSLATION_HOOK ||
71
73
  name.original === INLINE_TRANSLATION_HOOK_ASYNC ||
74
+ name.original === INLINE_MESSAGE_HOOK ||
75
+ name.original === INLINE_MESSAGE_HOOK_ASYNC ||
72
76
  name.original === MSG_TRANSLATION_HOOK) {
73
77
  translationPaths.push({
74
78
  localName: name.local,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.1.8-alpha.1",
3
+ "version": "2.1.9",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [