wuchale 0.21.0 → 0.21.2

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.
@@ -49,13 +49,15 @@ export declare class Transformer<RTCtxT = {}> {
49
49
  visitAwaitExpression: (node: Estree.AwaitExpression) => Message[];
50
50
  visitAssignmentExpression: (node: Estree.BinaryExpression) => Message[];
51
51
  visitAssignmentPattern: (node: Estree.AssignmentPattern) => Message[];
52
- visitExpressionStatement: (node: Estree.ExpressionStatement) => Message[];
53
52
  visitForOfStatement: (node: Estree.ForOfStatement) => Message[];
54
53
  visitForInStatement: (node: Estree.ForInStatement) => Message[];
55
54
  visitForStatement: (node: Estree.ForStatement) => Message[];
56
55
  getMemberChainName: (node: Estree.MemberExpression) => string;
57
56
  getCalleeName: (callee: Estree.Expression | Estree.Super) => string;
57
+ getActualExpression(expr?: Estree.Expression | Estree.TSAsExpression | Estree.TSTypeAssertion): Estree.Expression | null;
58
+ withUpdateTLDetails(visit: (atTopLevel: boolean) => Message[]): Message[];
58
59
  defaultVisitVariableDeclarator: (node: Estree.VariableDeclarator) => Message[];
60
+ visitExpressionStatement: (node: Estree.ExpressionStatement) => Message[];
59
61
  visitVariableDeclarator: (node: Estree.VariableDeclarator) => Message[];
60
62
  visitVariableDeclaration: (node: Estree.VariableDeclaration) => Message[];
61
63
  visitExportNamedDeclaration: (node: Estree.ExportNamedDeclaration) => Message[];
@@ -76,12 +78,8 @@ export declare class Transformer<RTCtxT = {}> {
76
78
  visitTaggedTemplateExpression: (node: Estree.TaggedTemplateExpression) => Message[];
77
79
  visitSwitchStatement: (node: Estree.SwitchStatement) => Message[];
78
80
  visitTryStatement: (node: Estree.TryStatement) => Message[];
79
- visitTSAsExpression: (node: {
80
- expression: Estree.AnyNode;
81
- }) => Message[];
82
- visitTSTypeAssertion: (node: {
83
- expression: Estree.AnyNode;
84
- }) => Message[];
81
+ visitTSAsExpression: (node: Estree.TSAsExpression) => Message[];
82
+ visitTSTypeAssertion: (node: Estree.TSTypeAssertion) => Message[];
85
83
  visitProgram: (node: Estree.Program) => Message[];
86
84
  visitWithCommentDirectives: (node: Estree.AnyNode, func: Function) => any;
87
85
  visit: (node: Estree.AnyNode) => Message[];
@@ -116,13 +116,10 @@ export class Transformer {
116
116
  return `\nconst ${this.currentRtVar} = ${wrapInit(expr)}\n`;
117
117
  };
118
118
  }
119
- fullHeuristicDetails = (detailsBase) => {
120
- const details = { ...this.heuristciDetails, ...detailsBase };
121
- if (details.declaring == null && details.insideProgram) {
122
- details.declaring = 'expression';
123
- }
124
- return details;
125
- };
119
+ fullHeuristicDetails = (detailsBase) => ({
120
+ ...this.heuristciDetails,
121
+ ...detailsBase,
122
+ });
126
123
  getHeuristicMessageType = (msg) => {
127
124
  const msgStr = msg.msgStr.join('\n');
128
125
  if (!msgStr) {
@@ -334,7 +331,6 @@ export class Transformer {
334
331
  ...this.visit(node.left),
335
332
  ...this.visit(node.right),
336
333
  ];
337
- visitExpressionStatement = (node) => this.visit(node.expression);
338
334
  visitForOfStatement = (node) => [
339
335
  ...this.visit(node.left),
340
336
  ...this.visit(node.right),
@@ -390,29 +386,52 @@ export class Transformer {
390
386
  }
391
387
  return `[${callee.type}]`;
392
388
  };
393
- defaultVisitVariableDeclarator = (node) => {
389
+ getActualExpression(expr) {
390
+ if (!expr) {
391
+ return null;
392
+ }
393
+ if (expr.type === 'TSAsExpression' || expr.type === 'TSTypeAssertion') {
394
+ return expr.expression;
395
+ }
396
+ return expr;
397
+ }
398
+ withUpdateTLDetails(visit) {
394
399
  const atTopLevelDefn = this.heuristciDetails.insideProgram && !this.heuristciDetails.declaring;
400
+ const msgs = visit(atTopLevelDefn);
401
+ if (atTopLevelDefn) {
402
+ this.heuristciDetails.topLevelCall = undefined; // reset
403
+ this.heuristciDetails.declaring = undefined;
404
+ }
405
+ return msgs;
406
+ }
407
+ defaultVisitVariableDeclarator = (node) => this.withUpdateTLDetails(topLevel => {
395
408
  if (!node.init) {
396
409
  return [];
397
410
  }
398
- if (atTopLevelDefn) {
399
- if (node.init.type === 'ArrowFunctionExpression' || node.init.type === 'FunctionExpression') {
411
+ if (topLevel) {
412
+ const init = this.getActualExpression(node.init);
413
+ if (init?.type === 'ArrowFunctionExpression' || init?.type === 'FunctionExpression') {
400
414
  this.heuristciDetails.declaring = 'function';
401
415
  }
402
416
  else {
403
417
  this.heuristciDetails.declaring = 'variable';
404
- if (node.init.type === 'CallExpression') {
405
- this.heuristciDetails.topLevelCall = this.getCalleeName(node.init.callee);
418
+ if (init?.type === 'CallExpression') {
419
+ this.heuristciDetails.topLevelCall = this.getCalleeName(init.callee);
406
420
  }
407
421
  }
408
422
  }
409
- const msgs = [...this.visit(node.id), ...this.visit(node.init)];
410
- if (atTopLevelDefn) {
411
- this.heuristciDetails.topLevelCall = undefined; // reset
412
- this.heuristciDetails.declaring = undefined;
423
+ return [...this.visit(node.id), ...this.visit(node.init)];
424
+ });
425
+ visitExpressionStatement = (node) => this.withUpdateTLDetails(topLevel => {
426
+ const expr = this.getActualExpression(node.expression);
427
+ if (topLevel) {
428
+ this.heuristciDetails.declaring = 'expression';
429
+ if (expr.type === 'CallExpression') {
430
+ this.heuristciDetails.topLevelCall = this.getCalleeName(expr.callee);
431
+ }
413
432
  }
414
- return msgs;
415
- };
433
+ return this.visit(expr);
434
+ });
416
435
  // for e.g. svelte to surrounded with $derived
417
436
  visitVariableDeclarator = this.defaultVisitVariableDeclarator;
418
437
  visitVariableDeclaration = (node) => node.declarations.flatMap(this.visitVariableDeclarator);
package/dist/adapters.js CHANGED
@@ -71,7 +71,7 @@ export function createHeuristic(opts) {
71
71
  if (msg.details.scope === 'attribute') {
72
72
  return 'message';
73
73
  }
74
- if (msg.details.declaring === 'expression' && !msg.details.funcName) {
74
+ if (msg.details.declaring === 'expression' && msg.details.funcName == null) {
75
75
  return false;
76
76
  }
77
77
  if (!msg.details.call ||
package/dist/ai/index.js CHANGED
@@ -65,7 +65,7 @@ export default class AIQueue {
65
65
  #requestName = (id, targetLocales) => `${color.cyan(this.ai.name)}: ${this.sourceLocale}..[${targetLocales.join(',')}] [${id}]`;
66
66
  translate = async (batch, attempt = 0) => {
67
67
  const logStart = this.#requestName(batch.id, batch.targetLocales);
68
- let translated;
68
+ let translated = [];
69
69
  try {
70
70
  const translatedstr = await this.ai.translate(JSON.stringify(batch.messages.map(item => ({
71
71
  id: item.id,
@@ -73,6 +73,12 @@ export default class AIQueue {
73
73
  references: item.references,
74
74
  }))), instruct(this.sourceLocale, batch.targetLocales));
75
75
  translated = JSON.parse(translatedstr);
76
+ if (Array.isArray(translated)) {
77
+ translated = translated.slice(0, batch.messages.length); // may return more
78
+ }
79
+ else {
80
+ translated = [];
81
+ }
76
82
  }
77
83
  catch (err) {
78
84
  this.log.error(`${logStart}: ${color.red(`error: ${err}`)}`);
@@ -84,7 +90,7 @@ export default class AIQueue {
84
90
  const sourceComp = item.id.map(i => compileTranslation(i, ''));
85
91
  for (const loc of batch.targetLocales) {
86
92
  const translation = outItem[loc];
87
- if (translation.length !== item.id.length) {
93
+ if (translation?.length !== item.id.length) {
88
94
  unTranslated.push(item);
89
95
  break;
90
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wuchale",
3
- "version": "0.21.0",
3
+ "version": "0.21.2",
4
4
  "description": "Protobuf-like i18n from plain code",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",