wuchale 0.17.1 → 0.17.3

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.
@@ -11,6 +11,7 @@ export declare function runtimeVars(wrapFunc: (expr: string) => string, base?: s
11
11
  rtLocale: string;
12
12
  rtCtx: string;
13
13
  rtTransCtx: string;
14
+ rtTransTag: string;
14
15
  /** for when nesting, used in adapters with elements */
15
16
  nestCtx: string;
16
17
  };
@@ -15,6 +15,7 @@ export function runtimeVars(wrapFunc, base = varNames.rt) {
15
15
  rtLocale: `${wrapFunc(base)}._.l`,
16
16
  rtCtx: `${wrapFunc(base)}.cx`,
17
17
  rtTransCtx: `${wrapFunc(base)}.tx`,
18
+ rtTransTag: `${wrapFunc(base)}.tt`,
18
19
  /** for when nesting, used in adapters with elements */
19
20
  nestCtx: '_w_ctx_',
20
21
  };
@@ -73,7 +73,10 @@ export declare class Transformer {
73
73
  visitReturnStatement: (node: Estree.ReturnStatement) => Message[];
74
74
  visitIfStatement: (node: Estree.IfStatement) => Message[];
75
75
  visitClassDeclaration: (node: Estree.ClassDeclaration) => Message[];
76
- visitTemplateLiteral: (node: Estree.TemplateLiteral) => Message[];
76
+ checkHeuristicTemplateLiteral: (node: Estree.TemplateLiteral) => boolean;
77
+ visitTemplateLiteralQuasis: (node: Estree.TemplateLiteral) => [number, Message[]];
78
+ visitTemplateLiteral: (node: Estree.TemplateLiteral, ignoreHeuristic?: boolean) => Message[];
79
+ visitTaggedTemplateExpression: (node: Estree.TaggedTemplateExpression) => Message[];
77
80
  visitProgram: (node: Estree.Program) => Message[];
78
81
  visit: (node: Estree.AnyNode) => Message[];
79
82
  finalize: (msgs: Message[], hmrHeaderIndex: number, additionalHeader?: string) => TransformOutput;
@@ -227,6 +227,10 @@ export class Transformer {
227
227
  msgs.push(msgInfo);
228
228
  continue;
229
229
  }
230
+ if (argVal.type === 'TemplateLiteral') {
231
+ msgs.push(...this.visitTemplateLiteral(argVal, true));
232
+ continue;
233
+ }
230
234
  if (argVal.type !== 'ArrayExpression') {
231
235
  return this.defaultVisitCallExpression(node);
232
236
  }
@@ -425,7 +429,7 @@ export class Transformer {
425
429
  this.declaring = prevDecl; // restore
426
430
  return msgs;
427
431
  };
428
- visitTemplateLiteral = (node) => {
432
+ checkHeuristicTemplateLiteral = (node) => {
429
433
  let heurTxt = '';
430
434
  for (const quasi of node.quasis) {
431
435
  heurTxt += quasi.value.cooked ?? '';
@@ -435,14 +439,11 @@ export class Transformer {
435
439
  }
436
440
  heurTxt = heurTxt.trim();
437
441
  const [pass] = this.checkHeuristic(heurTxt, { scope: 'script' });
438
- if (!pass) {
439
- return node.expressions.map(this.visit).flat();
440
- }
442
+ return pass;
443
+ };
444
+ visitTemplateLiteralQuasis = (node) => {
441
445
  const msgs = [];
442
- const quasi0 = node.quasis[0];
443
- // @ts-ignore
444
- const { start: start0, end: end0 } = quasi0;
445
- let msgStr = quasi0.value?.cooked ?? '';
446
+ let msgStr = node.quasis[0].value?.cooked ?? '';
446
447
  const comments = [];
447
448
  for (const [i, expr] of node.expressions.entries()) {
448
449
  msgs.push(...this.visit(expr));
@@ -450,7 +451,6 @@ export class Transformer {
450
451
  const placeholder = `{${i}}`;
451
452
  msgStr += `${placeholder}${quasi.value.cooked}`;
452
453
  comments.push(`placeholder ${placeholder}: ${this.content.slice(expr.start, expr.end)}`);
453
- // @ts-ignore
454
454
  const { start, end } = quasi;
455
455
  this.mstr.remove(start - 1, end);
456
456
  if (i + 1 === node.expressions.length) {
@@ -460,7 +460,19 @@ export class Transformer {
460
460
  }
461
461
  const msgInfo = new Message(msgStr, this.fullHeuristicDetails({ scope: 'script' }), this.commentDirectives.context);
462
462
  msgInfo.comments = comments;
463
- let begin = `${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())}`;
463
+ const index = this.index.get(msgInfo.toKey());
464
+ msgs.push(msgInfo);
465
+ return [index, msgs];
466
+ };
467
+ visitTemplateLiteral = (node, ignoreHeuristic = false) => {
468
+ if (!ignoreHeuristic) {
469
+ if (!this.checkHeuristicTemplateLiteral(node)) {
470
+ return node.expressions.map(this.visit).flat();
471
+ }
472
+ }
473
+ const [index, msgs] = this.visitTemplateLiteralQuasis(node);
474
+ const { start: start0, end: end0 } = node.quasis[0];
475
+ let begin = `${this.vars().rtTrans}(${index}`;
464
476
  let end = ')';
465
477
  if (node.expressions.length) {
466
478
  begin += ', [';
@@ -472,7 +484,27 @@ export class Transformer {
472
484
  else {
473
485
  this.mstr.update(start0 - 1, end0 + 1, begin + end);
474
486
  }
475
- msgs.push(msgInfo);
487
+ return msgs;
488
+ };
489
+ visitTaggedTemplateExpression = (node) => {
490
+ const prevCall = this.currentCall;
491
+ this.currentCall = this.getCalleeName(node.tag);
492
+ let msgs = [];
493
+ if (this.checkHeuristicTemplateLiteral(node.quasi)) {
494
+ const [index, msgsNew] = this.visitTemplateLiteralQuasis(node.quasi);
495
+ msgs = msgsNew;
496
+ this.mstr.appendRight(node.tag.start, `${this.vars().rtTransTag}(`);
497
+ const { start, end, expressions } = node.quasi;
498
+ if (expressions.length > 0) {
499
+ this.mstr.update(start, expressions[0].start, `, ${index}, [`);
500
+ this.mstr.update(end - 1, end, `])`);
501
+ }
502
+ else {
503
+ this.mstr.remove(start, start + 1);
504
+ this.mstr.update(start, end, `, ${index})`);
505
+ }
506
+ }
507
+ this.currentCall = prevCall;
476
508
  return msgs;
477
509
  };
478
510
  visitProgram = (node) => {
package/dist/runtime.d.ts CHANGED
@@ -15,6 +15,8 @@ export declare class Runtime {
15
15
  cx: (id: number) => Mixed | import("./compile.js").Composite;
16
16
  /** get translation using composite context */
17
17
  tx: (ctx: Mixed, args?: any[], start?: number) => string;
18
+ /** for tagged template strings */
19
+ tt: (tag: CallableFunction, id: number, args?: any[]) => any;
18
20
  /** get translation for plural */
19
21
  tp: (id: number) => CompiledElement;
20
22
  /** get translation */
package/dist/runtime.js CHANGED
@@ -44,6 +44,11 @@ export class Runtime {
44
44
  }
45
45
  return msgStr;
46
46
  };
47
+ /** for tagged template strings */
48
+ tt = (tag, id, args) => {
49
+ const ctx = this.cx(id);
50
+ return tag(ctx.filter(m => typeof m === 'string'), ...ctx.filter(m => typeof m === 'number').map(a => args?.[a]));
51
+ };
47
52
  /** get translation for plural */
48
53
  tp = (id) => this._.c[id] ?? [];
49
54
  /** get translation */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wuchale",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "description": "Protobuf-like i18n from plain code",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -83,9 +83,9 @@
83
83
  "tinyglobby": "^0.2.15"
84
84
  },
85
85
  "devDependencies": {
86
- "@types/node": "^24.5.2",
86
+ "@types/node": "^24.7.2",
87
87
  "@types/picomatch": "^4.0.1",
88
- "typescript": "^5.8.3"
88
+ "typescript": "^5.9.3"
89
89
  },
90
90
  "type": "module"
91
91
  }