wuchale 0.17.0 → 0.17.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.
@@ -73,7 +73,7 @@ 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
+ visitTemplateLiteral: (node: Estree.TemplateLiteral, ignoreHeuristic?: boolean) => Message[];
77
77
  visitProgram: (node: Estree.Program) => Message[];
78
78
  visit: (node: Estree.AnyNode) => Message[];
79
79
  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,18 +429,20 @@ export class Transformer {
425
429
  this.declaring = prevDecl; // restore
426
430
  return msgs;
427
431
  };
428
- visitTemplateLiteral = (node) => {
429
- let heurTxt = '';
430
- for (const quasi of node.quasis) {
431
- heurTxt += quasi.value.cooked ?? '';
432
- if (!quasi.tail) {
433
- heurTxt += '#';
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();
434
445
  }
435
- }
436
- heurTxt = heurTxt.trim();
437
- const [pass] = this.checkHeuristic(heurTxt, { scope: 'script' });
438
- if (!pass) {
439
- return node.expressions.map(this.visit).flat();
440
446
  }
441
447
  const msgs = [];
442
448
  const quasi0 = node.quasis[0];
@@ -23,7 +23,9 @@ export declare class Message {
23
23
  toKey: () => string;
24
24
  }
25
25
  export type HeuristicFunc = (msg: Message) => boolean | null | undefined;
26
+ /** Default heuristic */
26
27
  export declare function defaultHeuristic(msg: Message): boolean;
28
+ /** Default heuristic which ignores messages outside functions in the `script` scope */
27
29
  export declare const defaultHeuristicFuncOnly: HeuristicFunc;
28
30
  export declare const defaultGenerateLoadID: (filename: string) => string;
29
31
  export declare class IndexTracker {
package/dist/adapters.js CHANGED
@@ -19,6 +19,7 @@ export class Message {
19
19
  }
20
20
  const ignoreElements = ['style', 'path', 'code', 'pre'];
21
21
  const ignoreAttribs = [['form', 'method']];
22
+ /** Default heuristic */
22
23
  export function defaultHeuristic(msg) {
23
24
  const msgStr = msg.msgStr.join('\n');
24
25
  if (msgStr.search(/\p{L}/u) === -1) {
@@ -50,7 +51,7 @@ export function defaultHeuristic(msg) {
50
51
  }
51
52
  return !msg.details.call?.startsWith('console.') && msg.details.call !== 'fetch';
52
53
  }
53
- // only allow inside function definitions for script scope
54
+ /** Default heuristic which ignores messages outside functions in the `script` scope */
54
55
  export const defaultHeuristicFuncOnly = msg => {
55
56
  return defaultHeuristic(msg) && (msg.details.scope !== 'script' || msg.details.funcName != null);
56
57
  };
package/dist/config.js CHANGED
@@ -19,7 +19,7 @@ function deepAssign(fromObj, toObj) {
19
19
  continue;
20
20
  }
21
21
  // objects
22
- if (!toObj[key]) {
22
+ if (!toObj[key] || Array.isArray(toObj[key]) || typeof toObj[key] !== 'object') {
23
23
  toObj[key] = {};
24
24
  }
25
25
  deepAssign(fromObj[key], toObj[key]);
package/dist/handler.js CHANGED
@@ -564,13 +564,13 @@ export class AdapterHandler {
564
564
  });
565
565
  if (this.#log.checkLevel('verbose')) {
566
566
  if (msgs.length) {
567
- this.#log.verbose(`Extracted ${msgs.length} messages from ${filename}:`);
567
+ this.#log.verbose(`${this.key}: ${msgs.length} messages from ${filename}:`);
568
568
  for (const msg of msgs) {
569
569
  this.#log.verbose(` ${msg.msgStr.join(', ')} [${msg.details.scope}]`);
570
570
  }
571
571
  }
572
572
  else {
573
- this.#log.verbose(`No messages extracted from ${filename}.`);
573
+ this.#log.verbose(`${this.key}: No messages from ${filename}.`);
574
574
  }
575
575
  }
576
576
  const hmrKeys = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wuchale",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "description": "Protobuf-like i18n from plain code",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",