gtx-cli 1.2.30-alpha.22 → 1.2.30-alpha.24
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/dist/react/data-_gt/addGTIdentifierToSyntaxTree.d.ts +8 -1
- package/dist/react/data-_gt/addGTIdentifierToSyntaxTree.js +90 -63
- package/dist/react/jsx/utils/parseJsx.d.ts +8 -1
- package/dist/react/jsx/utils/parseJsx.js +25 -12
- package/dist/react/utils/getVariableName.d.ts +24 -1
- package/dist/react/utils/getVariableName.js +29 -5
- package/package.json +3 -2
|
@@ -1 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { JsxChildren } from 'generaltranslation/types';
|
|
2
|
+
/**
|
|
3
|
+
* Add GT Identifier and minify the tree (recreates addGTIdentifier and writeChildrenAsObjects)
|
|
4
|
+
* @param tree - The tree to add GT identifiers to
|
|
5
|
+
* @param startingIndex - The starting index for GT IDs
|
|
6
|
+
* @returns The tree with GT identifiers added
|
|
7
|
+
*/
|
|
8
|
+
export default function addGTIdentifierToSyntaxTree(tree: any, startingIndex?: number): JsxChildren;
|
|
@@ -1,80 +1,107 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTML_CONTENT_PROPS, } from 'generaltranslation/types';
|
|
2
|
+
import { defaultVariableNames, getVariableName, minifyVariableType, } from '../utils/getVariableName.js';
|
|
2
3
|
import { isAcceptedPluralForm } from 'generaltranslation/internal';
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Construct the data-_gt prop
|
|
6
|
+
* @param type - The type of the element
|
|
7
|
+
* @param props - The props of the element
|
|
8
|
+
* @param id - The id of the element
|
|
9
|
+
* @returns The data-_gt prop
|
|
10
|
+
*/
|
|
11
|
+
function constructGTProp(type, props, id) {
|
|
12
|
+
// Add translatable HTML content props
|
|
13
|
+
const generaltranslation = Object.entries(HTML_CONTENT_PROPS).reduce((acc, [minifiedName, fullName]) => {
|
|
14
|
+
if (props[fullName]) {
|
|
15
|
+
acc[minifiedName] = props[fullName];
|
|
16
|
+
}
|
|
17
|
+
return acc;
|
|
18
|
+
}, {});
|
|
19
|
+
// Plural
|
|
20
|
+
if (type === 'Plural') {
|
|
21
|
+
const pluralBranches = Object.entries(props).reduce((acc, [branchName, branch]) => {
|
|
22
|
+
if (isAcceptedPluralForm(branchName)) {
|
|
23
|
+
acc[branchName] = addGTIdentifierToSyntaxTree(branch, id);
|
|
24
|
+
}
|
|
25
|
+
return acc;
|
|
26
|
+
}, {});
|
|
27
|
+
// Add plural branches to the generaltranslation
|
|
28
|
+
if (Object.keys(pluralBranches).length) {
|
|
29
|
+
generaltranslation.t = 'p';
|
|
30
|
+
generaltranslation.b = pluralBranches;
|
|
31
|
+
}
|
|
32
|
+
// Branch
|
|
33
|
+
}
|
|
34
|
+
else if (type === 'Branch') {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
|
|
36
|
+
const { children, branch, ...branches } = props;
|
|
37
|
+
const resultBranches = Object.entries(branches).reduce((acc, [branchName, branch]) => {
|
|
38
|
+
acc[branchName] = addGTIdentifierToSyntaxTree(branch, id);
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
// Add branches to the generaltranslation
|
|
42
|
+
if (Object.keys(resultBranches).length) {
|
|
43
|
+
generaltranslation.t = 'b';
|
|
44
|
+
generaltranslation.b = resultBranches;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Object.keys(generaltranslation).length
|
|
48
|
+
? generaltranslation
|
|
49
|
+
: undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Add GT Identifier and minify the tree (recreates addGTIdentifier and writeChildrenAsObjects)
|
|
53
|
+
* @param tree - The tree to add GT identifiers to
|
|
54
|
+
* @param startingIndex - The starting index for GT IDs
|
|
55
|
+
* @returns The tree with GT identifiers added
|
|
56
|
+
*/
|
|
4
57
|
export default function addGTIdentifierToSyntaxTree(tree, startingIndex = 0) {
|
|
5
58
|
// Object to keep track of the current index for GT IDs
|
|
6
|
-
|
|
59
|
+
const indexObject = { index: startingIndex };
|
|
60
|
+
/**
|
|
61
|
+
* Handle a single child
|
|
62
|
+
* @param child - The child to handle
|
|
63
|
+
* @returns The handled child
|
|
64
|
+
*/
|
|
7
65
|
const handleSingleChild = (child) => {
|
|
66
|
+
// Handle JSX elements
|
|
8
67
|
if (child && typeof child === 'object') {
|
|
9
|
-
|
|
68
|
+
let { type } = child;
|
|
69
|
+
const { props } = child;
|
|
10
70
|
indexObject.index += 1;
|
|
11
|
-
|
|
12
|
-
if (type === '
|
|
13
|
-
|
|
14
|
-
variable: 'variable',
|
|
15
|
-
id: indexObject.index,
|
|
16
|
-
key: getVariableName(props, 'variable', indexObject.index),
|
|
17
|
-
};
|
|
71
|
+
// Handle fragments
|
|
72
|
+
if (type === 'React.Fragment') {
|
|
73
|
+
type = '';
|
|
18
74
|
}
|
|
19
|
-
|
|
75
|
+
// Variables
|
|
76
|
+
if (Object.keys(defaultVariableNames).includes(type)) {
|
|
77
|
+
const variableType = minifyVariableType(type);
|
|
78
|
+
const variableName = getVariableName(props, type, indexObject.index);
|
|
20
79
|
return {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
80
|
+
i: indexObject.index,
|
|
81
|
+
k: variableName,
|
|
82
|
+
v: variableType,
|
|
24
83
|
};
|
|
25
84
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
id: indexObject.index,
|
|
30
|
-
key: getVariableName(props, 'currency', indexObject.index),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
else if (type === 'DateTime') {
|
|
34
|
-
return {
|
|
35
|
-
variable: 'datetime',
|
|
36
|
-
id: indexObject.index,
|
|
37
|
-
key: getVariableName(props, 'datetime', indexObject.index),
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
else if (type === '' || type === 'React.Fragment') {
|
|
41
|
-
generaltranslation.transformation = 'fragment';
|
|
42
|
-
}
|
|
43
|
-
if (type === 'Plural') {
|
|
44
|
-
generaltranslation.transformation = 'plural';
|
|
45
|
-
const pluralBranches = Object.entries(props).reduce((acc, [branchName, branch]) => {
|
|
46
|
-
if (isAcceptedPluralForm(branchName)) {
|
|
47
|
-
acc[branchName] =
|
|
48
|
-
addGTIdentifierToSyntaxTree(branch, indexObject.index);
|
|
49
|
-
}
|
|
50
|
-
return acc;
|
|
51
|
-
}, {});
|
|
52
|
-
if (Object.keys(pluralBranches).length)
|
|
53
|
-
generaltranslation.branches = pluralBranches;
|
|
54
|
-
}
|
|
55
|
-
else if (type === 'Branch') {
|
|
56
|
-
generaltranslation.transformation = 'branch';
|
|
57
|
-
const { children, branch, ...branches } = props;
|
|
58
|
-
const resultBranches = Object.entries(branches).reduce((acc, [branchName, branch]) => {
|
|
59
|
-
acc[branchName] =
|
|
60
|
-
addGTIdentifierToSyntaxTree(branch, indexObject.index);
|
|
61
|
-
return acc;
|
|
62
|
-
}, {});
|
|
63
|
-
if (Object.keys(resultBranches).length)
|
|
64
|
-
generaltranslation.branches = resultBranches;
|
|
65
|
-
}
|
|
85
|
+
// Construct the data-_gt prop
|
|
86
|
+
const generaltranslation = constructGTProp(type, props, indexObject.index);
|
|
87
|
+
// Return the result
|
|
66
88
|
return {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
children: handleChildren(props.children),
|
|
72
|
-
}),
|
|
73
|
-
},
|
|
89
|
+
t: type || `C${indexObject.index}`,
|
|
90
|
+
i: indexObject.index,
|
|
91
|
+
c: handleChildren(props.children),
|
|
92
|
+
...(generaltranslation && { d: generaltranslation }),
|
|
74
93
|
};
|
|
75
94
|
}
|
|
76
|
-
|
|
95
|
+
if (typeof child === 'number') {
|
|
96
|
+
return child.toString();
|
|
97
|
+
}
|
|
98
|
+
return child;
|
|
77
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* Handle children
|
|
102
|
+
* @param children - The children to handle
|
|
103
|
+
* @returns The handled children
|
|
104
|
+
*/
|
|
78
105
|
const handleChildren = (children) => {
|
|
79
106
|
return Array.isArray(children)
|
|
80
107
|
? children.map(handleSingleChild)
|
|
@@ -10,5 +10,12 @@ import * as t from '@babel/types';
|
|
|
10
10
|
* @param insideT - Whether the current node is inside a <T> component
|
|
11
11
|
* @returns The built JSX tree
|
|
12
12
|
*/
|
|
13
|
-
export declare function buildJSXTree(importAliases: Record<string, string>, node: any, unwrappedExpressions: string[], updates: Updates, errors: string[], file: string, insideT: boolean):
|
|
13
|
+
export declare function buildJSXTree(importAliases: Record<string, string>, node: any, unwrappedExpressions: string[], updates: Updates, errors: string[], file: string, insideT: boolean): {
|
|
14
|
+
expression?: boolean;
|
|
15
|
+
result?: string;
|
|
16
|
+
type?: string;
|
|
17
|
+
props?: {
|
|
18
|
+
children?: any;
|
|
19
|
+
};
|
|
20
|
+
} | string | null;
|
|
14
21
|
export declare function parseJSXElement(importAliases: Record<string, string>, node: t.JSXElement, updates: Updates, errors: string[], file: string): void;
|
|
@@ -161,7 +161,7 @@ export function parseJSXElement(importAliases, node, updates, errors, file) {
|
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
163
|
const componentErrors = [];
|
|
164
|
-
const
|
|
164
|
+
const metadata = {};
|
|
165
165
|
// We'll track this flag to know if any unwrapped {variable} is found in children
|
|
166
166
|
const unwrappedExpressions = [];
|
|
167
167
|
// Gather <T>'s props
|
|
@@ -174,7 +174,7 @@ export function parseJSXElement(importAliases, node, updates, errors, file) {
|
|
|
174
174
|
if (attr.value) {
|
|
175
175
|
// If it's a plain string literal like id="hello"
|
|
176
176
|
if (t.isStringLiteral(attr.value)) {
|
|
177
|
-
|
|
177
|
+
metadata[attrName] = attr.value.value;
|
|
178
178
|
}
|
|
179
179
|
// If it's an expression container like id={"hello"}, id={someVar}, etc.
|
|
180
180
|
else if (t.isJSXExpressionContainer(attr.value)) {
|
|
@@ -188,30 +188,43 @@ export function parseJSXElement(importAliases, node, updates, errors, file) {
|
|
|
188
188
|
}
|
|
189
189
|
// Use the static value if available
|
|
190
190
|
if (staticAnalysis.isStatic && staticAnalysis.value !== undefined) {
|
|
191
|
-
|
|
191
|
+
metadata[attrName] = staticAnalysis.value;
|
|
192
192
|
}
|
|
193
193
|
else {
|
|
194
194
|
// Only store the code if we couldn't extract a static value
|
|
195
|
-
|
|
195
|
+
metadata[attrName] = code;
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
else {
|
|
199
199
|
// For other attributes that aren't id or context
|
|
200
|
-
|
|
200
|
+
metadata[attrName] = code;
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
});
|
|
205
205
|
// Build the JSX tree for this component
|
|
206
|
-
const
|
|
206
|
+
const treeResult = buildJSXTree(importAliases, node, unwrappedExpressions, updates, componentErrors, file, false);
|
|
207
|
+
let jsxTree = undefined;
|
|
208
|
+
if (treeResult && typeof treeResult === 'object') {
|
|
209
|
+
jsxTree = treeResult.props?.children;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
jsxTree = treeResult;
|
|
213
|
+
}
|
|
207
214
|
if (componentErrors.length > 0) {
|
|
208
215
|
errors.push(...componentErrors);
|
|
209
216
|
return;
|
|
210
217
|
}
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
218
|
+
// Handle whitespace in children
|
|
219
|
+
const whitespaceHandledTree = handleChildrenWhitespace(jsxTree);
|
|
220
|
+
// Add GT identifiers to the tree
|
|
221
|
+
let minifiedTree = addGTIdentifierToSyntaxTree(whitespaceHandledTree);
|
|
222
|
+
console.log('minifiedTree', JSON.stringify(minifiedTree, null, 2));
|
|
223
|
+
minifiedTree =
|
|
224
|
+
Array.isArray(minifiedTree) && minifiedTree.length === 1
|
|
225
|
+
? minifiedTree[0]
|
|
226
|
+
: minifiedTree;
|
|
227
|
+
const id = metadata.id;
|
|
215
228
|
// If we found an unwrapped expression, skip
|
|
216
229
|
if (unwrappedExpressions.length > 0) {
|
|
217
230
|
errors.push(warnHasUnwrappedExpressionSync(file, unwrappedExpressions, id, `${node.loc?.start?.line}:${node.loc?.start?.column}`));
|
|
@@ -220,7 +233,7 @@ export function parseJSXElement(importAliases, node, updates, errors, file) {
|
|
|
220
233
|
// <T> is valid here
|
|
221
234
|
updates.push({
|
|
222
235
|
dataFormat: 'JSX',
|
|
223
|
-
source:
|
|
224
|
-
metadata
|
|
236
|
+
source: minifiedTree,
|
|
237
|
+
metadata,
|
|
225
238
|
});
|
|
226
239
|
}
|
|
@@ -1,2 +1,25 @@
|
|
|
1
|
+
import { VariableType } from 'generaltranslation/types';
|
|
2
|
+
/**
|
|
3
|
+
* These are the names of the variable components as they appear in gt-next and gt-react
|
|
4
|
+
*/
|
|
5
|
+
export declare const defaultVariableNames: {
|
|
6
|
+
readonly Var: "value";
|
|
7
|
+
readonly Num: "n";
|
|
8
|
+
readonly DateTime: "date";
|
|
9
|
+
readonly Currency: "cost";
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Minify the variable type from the name of the variable component (from gt-next and gt-react)
|
|
13
|
+
* @param variableType - The type of the variable (Var, Num, DateTime, Currency)
|
|
14
|
+
* @returns The minified variable type
|
|
15
|
+
*/
|
|
16
|
+
export declare const minifyVariableType: (variableType: keyof typeof defaultVariableNames) => VariableType;
|
|
1
17
|
export declare const baseVariablePrefix = "_gt_";
|
|
2
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Get the name of a variable
|
|
20
|
+
* @param props - The props of the variable
|
|
21
|
+
* @param variableType - The type of the variable (Var, Num, DateTime, Currency)
|
|
22
|
+
* @param id - The id of the variable
|
|
23
|
+
* @returns The name of the variable
|
|
24
|
+
*/
|
|
25
|
+
export declare function getVariableName(props: Record<string, any> | undefined, variableType: keyof typeof defaultVariableNames, id: number): string;
|
|
@@ -1,10 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* These are the names of the variable components as they appear in gt-next and gt-react
|
|
3
|
+
*/
|
|
4
|
+
export const defaultVariableNames = {
|
|
5
|
+
Var: 'value',
|
|
6
|
+
Num: 'n',
|
|
7
|
+
DateTime: 'date',
|
|
8
|
+
Currency: 'cost',
|
|
9
|
+
};
|
|
10
|
+
const minifyVariableTypeMap = {
|
|
11
|
+
Var: 'v',
|
|
12
|
+
Num: 'n',
|
|
13
|
+
DateTime: 'd',
|
|
14
|
+
Currency: 'c',
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Minify the variable type from the name of the variable component (from gt-next and gt-react)
|
|
18
|
+
* @param variableType - The type of the variable (Var, Num, DateTime, Currency)
|
|
19
|
+
* @returns The minified variable type
|
|
20
|
+
*/
|
|
21
|
+
export const minifyVariableType = (variableType) => {
|
|
22
|
+
return minifyVariableTypeMap[variableType];
|
|
6
23
|
};
|
|
7
24
|
export const baseVariablePrefix = '_gt_';
|
|
25
|
+
/**
|
|
26
|
+
* Get the name of a variable
|
|
27
|
+
* @param props - The props of the variable
|
|
28
|
+
* @param variableType - The type of the variable (Var, Num, DateTime, Currency)
|
|
29
|
+
* @param id - The id of the variable
|
|
30
|
+
* @returns The name of the variable
|
|
31
|
+
*/
|
|
8
32
|
export function getVariableName(props = {}, variableType, id) {
|
|
9
33
|
if (props.name)
|
|
10
34
|
return props.name;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gtx-cli",
|
|
3
|
-
"version": "1.2.30-alpha.
|
|
3
|
+
"version": "1.2.30-alpha.24",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"bin": "dist/main.js",
|
|
6
6
|
"files": [
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsc",
|
|
13
13
|
"build:clean": "rm -rf dist; npm run build",
|
|
14
|
+
"build:release": "npm run build:clean",
|
|
14
15
|
"lint": "eslint \"src/**/*.{js,ts}\" \"__tests__/**/*.{js,ts}\"",
|
|
15
16
|
"lint:fix": "eslint \"src/**/*.{js,ts}\" \"__tests__/**/*.{js,ts}\" --fix",
|
|
16
17
|
"test": "vitest run",
|
|
@@ -86,7 +87,7 @@
|
|
|
86
87
|
"esbuild": "^0.25.4",
|
|
87
88
|
"fast-glob": "^3.3.3",
|
|
88
89
|
"form-data": "^4.0.2",
|
|
89
|
-
"generaltranslation": "^7.0.0-alpha.
|
|
90
|
+
"generaltranslation": "^7.0.0-alpha.24",
|
|
90
91
|
"open": "^10.1.1",
|
|
91
92
|
"ora": "^8.2.0",
|
|
92
93
|
"resolve": "^1.22.10",
|