gtx-cli 1.2.30-alpha.29 → 1.2.30-alpha.31

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.
@@ -1,2 +1,4 @@
1
1
  export declare const GT_TRANSLATION_FUNCS: string[];
2
2
  export declare const VARIABLE_COMPONENTS: string[];
3
+ export declare const GT_ATTRIBUTES: string[];
4
+ export declare function mapAttributeName(attrName: string): string;
@@ -12,3 +12,11 @@ export const GT_TRANSLATION_FUNCS = [
12
12
  ];
13
13
  // Valid variable components
14
14
  export const VARIABLE_COMPONENTS = ['Var', 'DateTime', 'Currency', 'Num'];
15
+ export const GT_ATTRIBUTES = ['$id', '$context'];
16
+ export function mapAttributeName(attrName) {
17
+ if (attrName === '$id')
18
+ return 'id';
19
+ if (attrName === '$context')
20
+ return 'context';
21
+ return attrName;
22
+ }
@@ -7,7 +7,7 @@ import { warnHasUnwrappedExpressionSync, warnVariablePropSync, warnNestedTCompon
7
7
  import { isAcceptedPluralForm } from 'generaltranslation/internal';
8
8
  import { handleChildrenWhitespace } from '../trimJsxStringChildren.js';
9
9
  import { isStaticExpression } from '../evaluateJsx.js';
10
- import { VARIABLE_COMPONENTS } from './constants.js';
10
+ import { GT_ATTRIBUTES, VARIABLE_COMPONENTS } from './constants.js';
11
11
  /**
12
12
  * Builds a JSX tree from a given node, recursively handling children.
13
13
  * @param node - The node to build the tree from
@@ -181,7 +181,7 @@ export function parseJSXElement(importAliases, node, updates, errors, file) {
181
181
  const expr = attr.value.expression;
182
182
  const code = generate(expr).code;
183
183
  // Only check for static expressions on id and context props
184
- if (attrName === 'id' || attrName === 'context') {
184
+ if (GT_ATTRIBUTES.includes(attrName)) {
185
185
  const staticAnalysis = isStaticExpression(expr);
186
186
  if (!staticAnalysis.isStatic) {
187
187
  componentErrors.push(warnVariablePropSync(file, attrName, code, `${expr.loc?.start?.line}:${expr.loc?.start?.column}`));
@@ -1,6 +1,5 @@
1
1
  import { NodePath } from '@babel/traverse';
2
2
  import { Updates } from '../../../types/index.js';
3
- export declare const attributes: string[];
4
3
  /**
5
4
  * Main entry point for parsing translation strings from useGT() and getGT() calls.
6
5
  *
@@ -1,5 +1,6 @@
1
1
  import * as t from '@babel/types';
2
2
  import { isStaticExpression } from '../evaluateJsx.js';
3
+ import { GT_ATTRIBUTES, mapAttributeName } from './constants.js';
3
4
  import { warnNonStaticExpressionSync, warnNonStringSync, warnTemplateLiteralSync, } from '../../../console/index.js';
4
5
  import generateModule from '@babel/generator';
5
6
  import traverseModule from '@babel/traverse';
@@ -11,7 +12,6 @@ import path from 'node:path';
11
12
  import { parse } from '@babel/parser';
12
13
  import { createMatchPath, loadConfig } from 'tsconfig-paths';
13
14
  import * as resolve from 'resolve';
14
- export const attributes = ['id', 'context'];
15
15
  /**
16
16
  * Processes a single translation function call (e.g., t('hello world', { id: 'greeting' })).
17
17
  * Extracts the translatable string content and metadata, then adds it to the updates array.
@@ -37,13 +37,15 @@ function processTranslationCall(tPath, updates, errors, file) {
37
37
  if (prop.type === 'ObjectProperty' &&
38
38
  prop.key.type === 'Identifier') {
39
39
  const attribute = prop.key.name;
40
- if (attributes.includes(attribute) && t.isExpression(prop.value)) {
40
+ if (GT_ATTRIBUTES.includes(attribute) &&
41
+ t.isExpression(prop.value)) {
41
42
  const result = isStaticExpression(prop.value);
42
43
  if (!result.isStatic) {
43
44
  errors.push(warnNonStaticExpressionSync(file, attribute, generate(prop.value).code, `${prop.loc?.start?.line}:${prop.loc?.start?.column}`));
44
45
  }
45
46
  if (result.isStatic && result.value) {
46
- metadata[attribute] = result.value;
47
+ // Map $id and $context to id and context
48
+ metadata[mapAttributeName(attribute)] = result.value;
47
49
  }
48
50
  }
49
51
  }
@@ -44,7 +44,8 @@ export default async function createDictionaryUpdates(dictionaryPath, esbuildCon
44
44
  for (const id of Object.keys(dictionary)) {
45
45
  const { entry, metadata: props, // context, etc.
46
46
  } = getEntryAndMetadata(dictionary[id]);
47
- const context = props?.context;
47
+ // Map $context to context
48
+ const context = props?.$context;
48
49
  const metadata = {
49
50
  id,
50
51
  ...(context && { context }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "1.2.30-alpha.29",
3
+ "version": "1.2.30-alpha.31",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [
@@ -87,7 +87,7 @@
87
87
  "esbuild": "^0.25.4",
88
88
  "fast-glob": "^3.3.3",
89
89
  "form-data": "^4.0.2",
90
- "generaltranslation": "^7.0.0-alpha.29",
90
+ "generaltranslation": "^7.0.0-alpha.31",
91
91
  "open": "^10.1.1",
92
92
  "ora": "^8.2.0",
93
93
  "resolve": "^1.22.10",
@@ -1,12 +0,0 @@
1
- import { NodePath } from '@babel/traverse';
2
- import { Updates } from '../../../types/index.js';
3
- export declare const attributes: string[];
4
- /**
5
- * For the following example code:
6
- * const tx = useGT();
7
- * tx('string to translate', { id: 'exampleId', context: 'exampleContext' });
8
- *
9
- * This function will find all call expressions of useGT(), then find all call expressions
10
- * of the subsequent tx() calls, and append the content and metadata to the updates array.
11
- */
12
- export declare function parseStrings(importName: string, path: NodePath, updates: Updates, errors: string[], file: string): void;
@@ -1,77 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { isStaticExpression } from '../evaluateJsx.js';
3
- import { warnNonStaticExpressionSync, warnTemplateLiteralSync, } from '../../../console/index.js';
4
- import generateModule from '@babel/generator';
5
- // Handle CommonJS/ESM interop
6
- const generate = generateModule.default || generateModule;
7
- export const attributes = ['id', 'context'];
8
- /**
9
- * For the following example code:
10
- * const tx = useGT();
11
- * tx('string to translate', { id: 'exampleId', context: 'exampleContext' });
12
- *
13
- * This function will find all call expressions of useGT(), then find all call expressions
14
- * of the subsequent tx() calls, and append the content and metadata to the updates array.
15
- */
16
- export function parseStrings(importName, path, updates, errors, file) {
17
- path.scope.bindings[importName]?.referencePaths.forEach((refPath) => {
18
- // Find call expressions of useGT() / await getGT()
19
- const callExpr = refPath.findParent((p) => p.isCallExpression());
20
- if (callExpr) {
21
- // Get the parent, handling both await and non-await cases
22
- const parentPath = callExpr.parentPath;
23
- const effectiveParent = parentPath?.node.type === 'AwaitExpression'
24
- ? parentPath.parentPath
25
- : parentPath;
26
- if (effectiveParent &&
27
- effectiveParent.node.type === 'VariableDeclarator' &&
28
- effectiveParent.node.id.type === 'Identifier') {
29
- const tFuncName = effectiveParent.node.id.name;
30
- // Get the scope from the variable declaration
31
- const variableScope = effectiveParent.scope;
32
- variableScope.bindings[tFuncName]?.referencePaths.forEach((tPath) => {
33
- if (tPath.parent.type === 'CallExpression' &&
34
- tPath.parent.arguments.length > 0) {
35
- const arg = tPath.parent.arguments[0];
36
- if (arg.type === 'StringLiteral' ||
37
- (t.isTemplateLiteral(arg) && arg.expressions.length === 0)) {
38
- const source = arg.type === 'StringLiteral'
39
- ? arg.value
40
- : arg.quasis[0].value.raw;
41
- // get metadata and id from options
42
- const options = tPath.parent.arguments[1];
43
- const metadata = {};
44
- if (options && options.type === 'ObjectExpression') {
45
- options.properties.forEach((prop) => {
46
- if (prop.type === 'ObjectProperty' &&
47
- prop.key.type === 'Identifier') {
48
- const attribute = prop.key.name;
49
- if (attributes.includes(attribute) &&
50
- t.isExpression(prop.value)) {
51
- const result = isStaticExpression(prop.value);
52
- if (!result.isStatic) {
53
- errors.push(warnNonStaticExpressionSync(file, attribute, generate(prop.value).code, `${prop.loc?.start?.line}:${prop.loc?.start?.column}`));
54
- }
55
- if (result.isStatic && result.value) {
56
- metadata[attribute] = result.value;
57
- }
58
- }
59
- }
60
- });
61
- }
62
- updates.push({
63
- dataFormat: 'ICU',
64
- source,
65
- metadata,
66
- });
67
- }
68
- else if (t.isTemplateLiteral(arg)) {
69
- // warn if template literal
70
- errors.push(warnTemplateLiteralSync(file, generate(arg).code, `${arg.loc?.start?.line}:${arg.loc?.start?.column}`));
71
- }
72
- }
73
- });
74
- }
75
- }
76
- });
77
- }