wuchale 0.17.2 → 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
+ checkHeuristicTemplateLiteral: (node: Estree.TemplateLiteral) => boolean;
77
+ visitTemplateLiteralQuasis: (node: Estree.TemplateLiteral) => [number, Message[]];
76
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;
@@ -429,26 +429,21 @@ export class Transformer {
429
429
  this.declaring = prevDecl; // restore
430
430
  return msgs;
431
431
  };
432
- visitTemplateLiteral = (node, ignoreHeuristic = false) => {
433
- if (!ignoreHeuristic) {
434
- let heurTxt = '';
435
- for (const quasi of node.quasis) {
436
- heurTxt += quasi.value.cooked ?? '';
437
- if (!quasi.tail) {
438
- heurTxt += '#';
439
- }
440
- }
441
- heurTxt = heurTxt.trim();
442
- const [pass] = this.checkHeuristic(heurTxt, { scope: 'script' });
443
- if (!pass) {
444
- return node.expressions.map(this.visit).flat();
432
+ checkHeuristicTemplateLiteral = (node) => {
433
+ let heurTxt = '';
434
+ for (const quasi of node.quasis) {
435
+ heurTxt += quasi.value.cooked ?? '';
436
+ if (!quasi.tail) {
437
+ heurTxt += '#';
445
438
  }
446
439
  }
440
+ heurTxt = heurTxt.trim();
441
+ const [pass] = this.checkHeuristic(heurTxt, { scope: 'script' });
442
+ return pass;
443
+ };
444
+ visitTemplateLiteralQuasis = (node) => {
447
445
  const msgs = [];
448
- const quasi0 = node.quasis[0];
449
- // @ts-ignore
450
- const { start: start0, end: end0 } = quasi0;
451
- let msgStr = quasi0.value?.cooked ?? '';
446
+ let msgStr = node.quasis[0].value?.cooked ?? '';
452
447
  const comments = [];
453
448
  for (const [i, expr] of node.expressions.entries()) {
454
449
  msgs.push(...this.visit(expr));
@@ -456,7 +451,6 @@ export class Transformer {
456
451
  const placeholder = `{${i}}`;
457
452
  msgStr += `${placeholder}${quasi.value.cooked}`;
458
453
  comments.push(`placeholder ${placeholder}: ${this.content.slice(expr.start, expr.end)}`);
459
- // @ts-ignore
460
454
  const { start, end } = quasi;
461
455
  this.mstr.remove(start - 1, end);
462
456
  if (i + 1 === node.expressions.length) {
@@ -466,7 +460,19 @@ export class Transformer {
466
460
  }
467
461
  const msgInfo = new Message(msgStr, this.fullHeuristicDetails({ scope: 'script' }), this.commentDirectives.context);
468
462
  msgInfo.comments = comments;
469
- 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}`;
470
476
  let end = ')';
471
477
  if (node.expressions.length) {
472
478
  begin += ', [';
@@ -478,7 +484,27 @@ export class Transformer {
478
484
  else {
479
485
  this.mstr.update(start0 - 1, end0 + 1, begin + end);
480
486
  }
481
- 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;
482
508
  return msgs;
483
509
  };
484
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.2",
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
  }