@menapo/vi18n 0.1.1 → 0.2.0
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/README.md +19 -0
- package/dist/{apply-Bre-lY9J.js → apply-Bg-3rB2m.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-DfLk50Ji.js} +196 -217
- package/dist/{check-BIKEuu6B.js → check-CunekuYs.js} +76 -85
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-CDSdtLQm.js +215 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- package/dist/compile-hva0qjhZ.js +240 -0
- package/dist/concurrency-BCY_4zcr.js +1 -0
- package/dist/concurrency-Dy2ny50V.js +107 -0
- package/dist/config.d.ts +1 -5
- package/dist/config.js +4 -5
- package/dist/{types-D3QGM8XO.js → diagnostics-LgIWyeSp.js} +28 -27
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-8cudcHNj.js} +10 -27
- package/dist/engine-ecRD8-IE.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DjwlrTQ9.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-BFETmvde.js} +2 -3
- package/dist/{export-C9Sy6UWv.js → export-D2tsqWIr.js} +17 -38
- package/dist/{extract-C3S8btx0.js → extract-BxYchVmY.js} +114 -82
- package/dist/{formats-9unqj_fZ.js → formats-BaryZ2N7.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-COO6o0tX.js +1907 -0
- package/dist/i18next-Dr7DCQlQ.js +1 -0
- package/dist/{import-nANvQutw.js → import-vSC29Cn4.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-C2ScuW6a.js → init-Cc7A-8VX.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-tp_xqzSY.js} +975 -451
- package/dist/lingui-BnnlqGXT.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +70 -82
- package/dist/{load-gW-oJt1Q.js → load-BSIeGJ5v.js} +275 -148
- package/dist/{load-CQRz4PpM.js → load-SSrAeJVB.js} +1214 -1116
- package/dist/{locales-DX4MOBFE.js → locales-D0U-yVRm.js} +271 -237
- package/dist/{lsp-RMMMc88P.js → lsp-DvC11pBZ.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-CHVwBoRP.js → mcp-D2zzOTBy.js} +4 -5
- package/dist/{merge-driver-BTX87aVm.js → merge-driver-BLI8OtAc.js} +138 -122
- package/dist/migrate-1r43OBfe.js +1 -0
- package/dist/migrate-ChDGhlC-.js +2169 -0
- package/dist/{openai-CTaMDcl-.js → openai-CLn4puY8.js} +128 -87
- package/dist/{output-DWE3SIUA.js → output-D5tAt8q4.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-C9Dbotkh.js → pipeline-BJSkkRYC.js} +387 -359
- package/dist/{pipeline-DbX6rlo5.js → pipeline-BeCc441z.js} +1 -1
- package/dist/project-B5KIO0q-.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-B2DUpoLu.js} +126 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-C8JehBBL.js +634 -0
- package/dist/rename-ZVGq9ZG1.js +577 -0
- package/dist/repeated-uuI1tUGg.js +85 -0
- package/dist/restore-xlBIrtRt.js +199 -0
- package/dist/rewrite-BwzXoBiA.js +415 -0
- package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
- package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/dist/run-CQ9pDLSa.js +674 -0
- package/dist/scan-worker.d.ts +2 -4
- package/dist/scan-worker.js +1 -1
- package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
- package/dist/{server-Da1Ot9IN.js → server-Br8tAuG2.js} +312 -386
- package/dist/{server-deBOq2vq.js → server-DJRpUQVZ.js} +1059 -333
- package/dist/server.d.ts +1 -17
- package/dist/server.js +419 -379
- package/dist/{sitemap-DJXzKoza.js → sitemap-CSAkm6oi.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DNVbYsgs.js} +713 -373
- package/dist/{status-zr4jY59Y.js → status-q6M3rBrh.js} +30 -30
- package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
- package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
- package/dist/sync-BNIAwMhR.js +1352 -0
- package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
- package/dist/test.d.ts +5 -16
- package/dist/test.js +126 -56
- package/dist/translate-BP4iVhOR.js +336 -0
- package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
- package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
- package/dist/{types-DsdWBG0b.d.ts → types-B4wNX6PA.d.ts} +6 -8
- package/dist/{types-CgUCkFU1.d.ts → types-_4UdFhR9.d.ts} +24 -21
- package/dist/types-xR2exI3L.js +42 -0
- package/dist/{units-CQ5Q99RC.js → units-DPPfCN7j.js} +7 -16
- package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
- package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
- package/dist/{validate-BxpupdAf.js → validate-CgzaDdOY.js} +690 -377
- package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{version-Byqi2guX.js → version-DC-J5WvV.js} +3 -5
- package/dist/{vite-scan-CT_vsp4g.js → vite-scan-kM8Ta3rO.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6500 -6032
- package/dist/vue-i18n-_uIXlFVh.js +806 -0
- package/dist/{which-BOemZ5Q8.js → which-DFWk_KwN.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-zlhNj4y3.js} +454 -608
- package/package.json +5 -5
- package/dist/check-HsmmpJZg.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-BaDh0cHG.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-C2ENBRJe.js +0 -531
- package/dist/i18next-9HAoxwUI.js +0 -1
- package/dist/i18next-BEJgv_S8.js +0 -1596
- package/dist/lingui-zUiCl9Um.js +0 -833
- package/dist/messages-BfjIExyB.js +0 -166
- package/dist/migrate-IpLDMXmq.js +0 -1484
- package/dist/migrate-rCQuOGsf.js +0 -1
- package/dist/project-Bw9UPIdx.js +0 -1046
- package/dist/project-C4GpaN-n.js +0 -381
- package/dist/react-intl-BP0u-D5m.js +0 -519
- package/dist/restore-CiYv-jxS.js +0 -175
- package/dist/rewrite-EsIw_lsq.js +0 -803
- package/dist/run-BEcnBbu-.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-B9V2KFwb.js +0 -658
- package/dist/translate-CQnJfLjO.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-g0iGy6CZ.js +0 -651
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/** `HELPER.useT` in `src/vite/types.ts`; core does not import the plugin layer. */
|
|
4
|
-
const USE_T = "__vi18nUseT";
|
|
5
|
-
/** `VIRTUAL.glue` in `src/vite/types.ts`. */
|
|
6
|
-
const GLUE = "virtual:vi18n/glue/";
|
|
1
|
+
import { _ as insertImport, b as skipTrivia, c as USE_T, d as markersOf, f as TYPE_KEYS, g as forEachBindingName, h as directives, k as listImports, m as childNodes, o as GLUE, p as afterDirective, u as isIgnored, v as isNode, x as unwrap, y as keyName } from "./sources-DNVbYsgs.js";
|
|
2
|
+
// src/core/extract/hooks.ts
|
|
7
3
|
/** Preferred local of the `__vi18nUseT` import: hook-shaped, see the module comment. */
|
|
8
4
|
const USE_T_LOCAL = "useVi18nT";
|
|
9
|
-
/** `HELPER.hookLocal` in `src/vite/types.ts`. */
|
|
10
|
-
const HOOK_LOCAL = "__t";
|
|
11
5
|
/** React Compiler's opt-out directives, which also opt out of the hook (PLAN §5.15). */
|
|
12
6
|
const NO_MEMO = /* @__PURE__ */ new Set(["use no memo", "use no forget"]);
|
|
13
7
|
const NO_HOOK_COMMENT = "vi18n-no-hook";
|
|
@@ -51,15 +45,6 @@ const PLAIN_CALLERS = /* @__PURE__ */ new Set([
|
|
|
51
45
|
"reduceRight",
|
|
52
46
|
"from"
|
|
53
47
|
]);
|
|
54
|
-
/** Keys that hold only TypeScript types; nothing under them runs. */
|
|
55
|
-
const TYPE_KEYS$1 = /* @__PURE__ */ new Set([
|
|
56
|
-
"typeAnnotation",
|
|
57
|
-
"typeParameters",
|
|
58
|
-
"typeArguments",
|
|
59
|
-
"superTypeArguments",
|
|
60
|
-
"returnType",
|
|
61
|
-
"implements"
|
|
62
|
-
]);
|
|
63
48
|
/** `rich`-like members of a namespace import; `fmt` is an object whose members are called. */
|
|
64
49
|
const NAMESPACE_TRACKED = /* @__PURE__ */ new Set([
|
|
65
50
|
"rich",
|
|
@@ -96,23 +81,6 @@ function freeName(code, base) {
|
|
|
96
81
|
if (!mentions(code, base)) return base;
|
|
97
82
|
for (let i = 2;; i++) if (!mentions(code, base + i)) return base + i;
|
|
98
83
|
}
|
|
99
|
-
/** Offset of the first character after whitespace and comments, from `at`, never past `limit`. */
|
|
100
|
-
function skipTrivia(code, at, limit) {
|
|
101
|
-
let i = at;
|
|
102
|
-
while (i < limit) {
|
|
103
|
-
const c = code.charCodeAt(i);
|
|
104
|
-
if (c === 47 && code.charCodeAt(i + 1) === 42) {
|
|
105
|
-
const close = code.indexOf("*/", i + 2);
|
|
106
|
-
i = close === -1 ? limit : close + 2;
|
|
107
|
-
} else if (c === 47 && code.charCodeAt(i + 1) === 47) {
|
|
108
|
-
const close = code.indexOf("\n", i + 2);
|
|
109
|
-
i = close === -1 ? limit : close + 1;
|
|
110
|
-
} else if (c === 32 || c === 9 || c === 10 || c === 13 || c === 160 || c === 65279) i++;
|
|
111
|
-
else if (c === 8232 || c === 8233 || c > 127 && /\s/.test(code[i])) i++;
|
|
112
|
-
else break;
|
|
113
|
-
}
|
|
114
|
-
return i;
|
|
115
|
-
}
|
|
116
84
|
/** True when a comment in `code[from, to)` contains `vi18n-no-hook`. */
|
|
117
85
|
function optedOut(code, from, to) {
|
|
118
86
|
const region = code.slice(from, to);
|
|
@@ -126,12 +94,6 @@ function optedOut(code, from, to) {
|
|
|
126
94
|
}
|
|
127
95
|
return false;
|
|
128
96
|
}
|
|
129
|
-
/** The leading directives of a statement list (`"use client"`, `"use no memo"`). */
|
|
130
|
-
function directives(statements) {
|
|
131
|
-
let n = 0;
|
|
132
|
-
while (n < statements.length && typeof statements[n].directive === "string") n++;
|
|
133
|
-
return statements.slice(0, n);
|
|
134
|
-
}
|
|
135
97
|
function hasNoMemo(statements) {
|
|
136
98
|
for (const d of directives(statements)) if (NO_MEMO.has(d.directive)) return true;
|
|
137
99
|
return false;
|
|
@@ -143,7 +105,7 @@ function wrappedFunction(init) {
|
|
|
143
105
|
if (isFunction$1(node)) return node;
|
|
144
106
|
if (node.type !== "CallExpression") return null;
|
|
145
107
|
const callee = unwrap(node.callee);
|
|
146
|
-
const name = callee.type === "Identifier" ? callee.name :
|
|
108
|
+
const name = callee.type === "Identifier" ? callee.name : memberName(callee);
|
|
147
109
|
if (name === null || !COMPONENT_WRAPPERS.has(name)) return null;
|
|
148
110
|
const first = node.arguments[0];
|
|
149
111
|
if (first === void 0 || first.type === "SpreadElement") return null;
|
|
@@ -234,27 +196,9 @@ function candidates(program) {
|
|
|
234
196
|
}
|
|
235
197
|
/** Records which watched names a binding pattern declares in `scope`. */
|
|
236
198
|
function declare(pattern, scope, watched) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (watched.has(pattern.name)) (scope.shadowed ??= /* @__PURE__ */ new Set()).add(pattern.name);
|
|
241
|
-
return;
|
|
242
|
-
case "ObjectPattern":
|
|
243
|
-
for (const property of pattern.properties) declare(property.type === "RestElement" ? property.argument : property.value, scope, watched);
|
|
244
|
-
return;
|
|
245
|
-
case "ArrayPattern":
|
|
246
|
-
for (const element of pattern.elements) declare(element, scope, watched);
|
|
247
|
-
return;
|
|
248
|
-
case "AssignmentPattern":
|
|
249
|
-
declare(pattern.left, scope, watched);
|
|
250
|
-
return;
|
|
251
|
-
case "RestElement":
|
|
252
|
-
declare(pattern.argument, scope, watched);
|
|
253
|
-
return;
|
|
254
|
-
case "TSParameterProperty":
|
|
255
|
-
declare(pattern.parameter, scope, watched);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
199
|
+
forEachBindingName(pattern, (name) => {
|
|
200
|
+
if (watched.has(name)) (scope.shadowed ??= /* @__PURE__ */ new Set()).add(name);
|
|
201
|
+
});
|
|
258
202
|
}
|
|
259
203
|
/** `onClick`, `onChange`…: an event handler prop. */
|
|
260
204
|
function isHandlerName(name) {
|
|
@@ -337,12 +281,14 @@ function isHookCall(node, context) {
|
|
|
337
281
|
/** Walks every child node of `node` (types excluded). */
|
|
338
282
|
function visitChildren(node, scope, analysis, context) {
|
|
339
283
|
for (const key in node) {
|
|
340
|
-
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS
|
|
284
|
+
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS.has(key)) continue;
|
|
341
285
|
const value = node[key];
|
|
342
286
|
if (value === null || typeof value !== "object") continue;
|
|
343
|
-
if (Array.isArray(value)) {
|
|
344
|
-
|
|
345
|
-
|
|
287
|
+
if (!Array.isArray(value)) {
|
|
288
|
+
if (isNode(value)) visit(value, scope, analysis, context);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
for (const item of value) if (isNode(item)) visit(item, scope, analysis, context);
|
|
346
292
|
}
|
|
347
293
|
}
|
|
348
294
|
/** A nested function: its own scope, whose calls run during render unless shadowed. */
|
|
@@ -438,32 +384,27 @@ function plainlyCalled(program) {
|
|
|
438
384
|
const names = /* @__PURE__ */ new Set();
|
|
439
385
|
const stack = [program];
|
|
440
386
|
for (let node = stack.pop(); node !== void 0; node = stack.pop()) {
|
|
441
|
-
if (node.type === "CallExpression")
|
|
442
|
-
|
|
443
|
-
if (callee.type === "Identifier") names.add(callee.name);
|
|
444
|
-
else if (callee.type === "MemberExpression") {
|
|
445
|
-
const method = keyName(callee.property, callee.computed === true);
|
|
446
|
-
if (method !== null && PLAIN_CALLERS.has(method)) {
|
|
447
|
-
const object = unwrap(callee.object);
|
|
448
|
-
if ((method === "call" || method === "apply") && object.type === "Identifier") names.add(object.name);
|
|
449
|
-
for (const argument of node.arguments) {
|
|
450
|
-
const value = unwrap(argument);
|
|
451
|
-
if (value.type === "Identifier") names.add(value.name);
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
for (const key in node) {
|
|
457
|
-
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS$1.has(key)) continue;
|
|
458
|
-
const value = node[key];
|
|
459
|
-
if (value === null || typeof value !== "object") continue;
|
|
460
|
-
if (Array.isArray(value)) {
|
|
461
|
-
for (const item of value) if (item !== null && typeof item.type === "string") stack.push(item);
|
|
462
|
-
} else if (typeof value.type === "string") stack.push(value);
|
|
463
|
-
}
|
|
387
|
+
if (node.type === "CallExpression") addPlainCallees(node, names);
|
|
388
|
+
childNodes(node, stack);
|
|
464
389
|
}
|
|
465
390
|
return names;
|
|
466
391
|
}
|
|
392
|
+
/** The names one call uses as plain functions: its callee, or what `.call`/`.map`… receive. */
|
|
393
|
+
function addPlainCallees(call, names) {
|
|
394
|
+
const callee = unwrap(call.callee);
|
|
395
|
+
if (callee.type === "Identifier") {
|
|
396
|
+
names.add(callee.name);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const method = memberName(callee);
|
|
400
|
+
if (method === null || !PLAIN_CALLERS.has(method)) return;
|
|
401
|
+
const object = unwrap(callee.object);
|
|
402
|
+
if ((method === "call" || method === "apply") && object.type === "Identifier") names.add(object.name);
|
|
403
|
+
for (const argument of call.arguments) {
|
|
404
|
+
const value = unwrap(argument);
|
|
405
|
+
if (value.type === "Identifier") names.add(value.name);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
467
408
|
const NONE = /* @__PURE__ */ new Set();
|
|
468
409
|
function moduleContext(imports, options, glue) {
|
|
469
410
|
const { tNames, trackNames = NONE, namespaces = NONE } = options;
|
|
@@ -481,7 +422,7 @@ function moduleContext(imports, options, glue) {
|
|
|
481
422
|
reactNamespaces: /* @__PURE__ */ new Set()
|
|
482
423
|
};
|
|
483
424
|
for (const record of imports) if (record.specifier === glue) {
|
|
484
|
-
for (const binding of record.bindings) if (binding.imported ===
|
|
425
|
+
for (const binding of record.bindings) if (binding.imported === "__vi18nUseT") context.useTLocal = binding.local;
|
|
485
426
|
} else if (JSX_MODULES.has(record.specifier)) {
|
|
486
427
|
for (const { local, imported } of record.bindings) if (imported === "default" || imported === "*") context.reactNamespaces.add(local);
|
|
487
428
|
else if (JSX_FACTORIES.has(imported)) context.factories.add(local);
|
|
@@ -546,19 +487,55 @@ function injectHooks(parsed, sink, options) {
|
|
|
546
487
|
if (hasNoMemo(program.body)) return false;
|
|
547
488
|
const found = candidates(program);
|
|
548
489
|
if (found.length === 0) return false;
|
|
549
|
-
const code = parsed.code;
|
|
550
490
|
const glue = GLUE + options.framework;
|
|
551
491
|
const context = moduleContext(listImports(parsed), options, glue);
|
|
552
|
-
const
|
|
553
|
-
let plain = null;
|
|
554
|
-
let useTLocal = context.useTLocal;
|
|
555
|
-
let hookLocal = null;
|
|
492
|
+
const injector = new HookInjector(parsed.code, program, sink, context);
|
|
556
493
|
let edited = false;
|
|
557
|
-
for (const
|
|
494
|
+
for (const candidate of found) if (injector.inject(candidate)) edited = true;
|
|
495
|
+
if (edited && context.useTLocal === null) {
|
|
496
|
+
const statement = `import { ${USE_T} as ${injector.useTLocal} } from ${JSON.stringify(glue)};`;
|
|
497
|
+
insertImport(sink, parsed, statement, true);
|
|
498
|
+
}
|
|
499
|
+
return edited;
|
|
500
|
+
}
|
|
501
|
+
/** One module's hook injection: the locals it picked and the facts it computed once. */
|
|
502
|
+
var HookInjector = class {
|
|
503
|
+
code;
|
|
504
|
+
program;
|
|
505
|
+
sink;
|
|
506
|
+
context;
|
|
507
|
+
anyOptOut;
|
|
508
|
+
/** {@link plainlyCalled}, computed on first need. */
|
|
509
|
+
plain = null;
|
|
510
|
+
/** The local of the hook import: the existing one, or a free name once one is injected. */
|
|
511
|
+
useTLocal;
|
|
512
|
+
hookLocal = null;
|
|
513
|
+
constructor(code, program, sink, context) {
|
|
514
|
+
this.code = code;
|
|
515
|
+
this.program = program;
|
|
516
|
+
this.sink = sink;
|
|
517
|
+
this.context = context;
|
|
518
|
+
this.anyOptOut = code.includes(NO_HOOK_COMMENT);
|
|
519
|
+
this.useTLocal = context.useTLocal;
|
|
520
|
+
}
|
|
521
|
+
/** Hooks one candidate; true when it was edited. */
|
|
522
|
+
inject({ name, anonymous, fn, regionStart }) {
|
|
558
523
|
const body = fn.body;
|
|
559
524
|
const block = body.type === "BlockStatement";
|
|
560
525
|
const statements = block ? body.body : [];
|
|
561
|
-
if (block && hasNoMemo(statements))
|
|
526
|
+
if (block && hasNoMemo(statements)) return false;
|
|
527
|
+
const calls = this.renderCalls(name, anonymous === true, fn);
|
|
528
|
+
if (calls === null) return false;
|
|
529
|
+
const prologue = directives(statements);
|
|
530
|
+
const bodyHead = block ? statements[prologue.length]?.start ?? body.end - 1 : body.start;
|
|
531
|
+
if (this.anyOptOut && optedOut(this.code, regionStart, bodyHead)) return false;
|
|
532
|
+
const local = block ? existingHookLocal(statements, this.useTLocal) ?? this.declareInBlock(body, prologue) : this.declareInConcise(fn);
|
|
533
|
+
if (local === null) return false;
|
|
534
|
+
rewriteCalls(this.sink, calls, local);
|
|
535
|
+
return true;
|
|
536
|
+
}
|
|
537
|
+
/** The locale calls that run while `fn` renders, or null when it is no component or hook to inject into. */
|
|
538
|
+
renderCalls(name, anonymous, fn) {
|
|
562
539
|
const root = {
|
|
563
540
|
calls: [],
|
|
564
541
|
shadowed: null,
|
|
@@ -569,64 +546,61 @@ function injectHooks(parsed, sink, options) {
|
|
|
569
546
|
jsx: false,
|
|
570
547
|
hooks: false
|
|
571
548
|
};
|
|
572
|
-
for (const param of fn.params) declare(param, root, context.watched);
|
|
573
|
-
visit(body, root, analysis, context);
|
|
549
|
+
for (const param of fn.params) declare(param, root, this.context.watched);
|
|
550
|
+
visit(fn.body, root, analysis, this.context);
|
|
574
551
|
const calls = liveCalls(root, null, []);
|
|
575
|
-
if (calls.length === 0 || !(analysis.jsx || analysis.hooks))
|
|
576
|
-
if (anonymous
|
|
577
|
-
plain ??= plainlyCalled(program);
|
|
578
|
-
if (plain.has(name))
|
|
552
|
+
if (calls.length === 0 || !(analysis.jsx || analysis.hooks)) return null;
|
|
553
|
+
if (!anonymous && isComponentName(name) && !analysis.hooks) {
|
|
554
|
+
this.plain ??= plainlyCalled(this.program);
|
|
555
|
+
if (this.plain.has(name)) return null;
|
|
579
556
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
local = hookLocal;
|
|
591
|
-
const declaration = `const ${local} = ${useTLocal}();`;
|
|
592
|
-
if (!block) {
|
|
593
|
-
sink.appendLeft(head, `{ ${declaration} return `);
|
|
594
|
-
sink.appendLeft(fn.end, "; }");
|
|
595
|
-
} else {
|
|
596
|
-
const last = prologue[prologue.length - 1];
|
|
597
|
-
if (last === void 0) sink.appendLeft(body.start + 1, ` ${declaration}`);
|
|
598
|
-
else {
|
|
599
|
-
const semi = code.charCodeAt(last.end - 1) === 59 ? "" : ";";
|
|
600
|
-
sink.appendLeft(last.end, `${semi} ${declaration}`);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
for (const { kind, call, callee } of calls) {
|
|
605
|
-
if (kind === "t") {
|
|
606
|
-
sink.overwrite(callee.start, callee.end, local);
|
|
607
|
-
continue;
|
|
608
|
-
}
|
|
609
|
-
sink.appendLeft(callee.end, ".call");
|
|
610
|
-
const first = call.arguments[0];
|
|
611
|
-
if (first === void 0) sink.appendLeft(call.end - 1, local);
|
|
612
|
-
else sink.appendLeft(first.start, `${local}, `);
|
|
613
|
-
}
|
|
614
|
-
edited = true;
|
|
557
|
+
return calls;
|
|
558
|
+
}
|
|
559
|
+
/** `const __t = useVi18nT();` with the locals picked once per module. */
|
|
560
|
+
declaration() {
|
|
561
|
+
this.useTLocal ??= freeName(this.code, USE_T_LOCAL);
|
|
562
|
+
this.hookLocal ??= freeName(this.code, "__t");
|
|
563
|
+
return {
|
|
564
|
+
local: this.hookLocal,
|
|
565
|
+
text: `const ${this.hookLocal} = ${this.useTLocal}();`
|
|
566
|
+
};
|
|
615
567
|
}
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const
|
|
619
|
-
const last = prologue
|
|
620
|
-
if (last
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
568
|
+
/** The declaration as the block's first statement, after its directives. */
|
|
569
|
+
declareInBlock(body, prologue) {
|
|
570
|
+
const { local, text } = this.declaration();
|
|
571
|
+
const last = prologue.at(-1);
|
|
572
|
+
if (last === void 0) this.sink.appendLeft(body.start + 1, ` ${text}`);
|
|
573
|
+
else this.sink.appendLeft(last.end, `${afterDirective(this.code, last)} ${text}`);
|
|
574
|
+
return local;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Concise bodies become blocks: `(…) => expr` → `(…) => { const __t = useVi18nT(); return expr; }`.
|
|
578
|
+
* `return` goes right before the expression (after any `(`), as a line break after it would
|
|
579
|
+
* return undefined. Null when the body's head is not found.
|
|
580
|
+
*/
|
|
581
|
+
declareInConcise(fn) {
|
|
582
|
+
const head = conciseBodyHead(this.code, fn);
|
|
583
|
+
if (head === -1) return null;
|
|
584
|
+
const { local, text } = this.declaration();
|
|
585
|
+
this.sink.appendLeft(head, `{ ${text} return `);
|
|
586
|
+
this.sink.appendLeft(fn.end, "; }");
|
|
587
|
+
return local;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
/** `t(…)` → `__t(…)`; `rich(…)` → `rich.call(__t, …)`. */
|
|
591
|
+
function rewriteCalls(sink, calls, local) {
|
|
592
|
+
for (const { kind, call, callee } of calls) {
|
|
593
|
+
if (kind === "t") {
|
|
594
|
+
sink.overwrite(callee.start, callee.end, local);
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
sink.appendLeft(callee.end, ".call");
|
|
598
|
+
const first = call.arguments[0];
|
|
599
|
+
if (first === void 0) sink.appendLeft(call.end - 1, local);
|
|
600
|
+
else sink.appendLeft(first.start, `${local}, `);
|
|
625
601
|
}
|
|
626
|
-
return edited;
|
|
627
602
|
}
|
|
628
|
-
|
|
629
|
-
//#region src/core/extract/captured.ts
|
|
603
|
+
// src/core/extract/captured.ts
|
|
630
604
|
/** Callee kinds whose result is translated text (not `msg`, which declares a message). */
|
|
631
605
|
const TRANSLATING = /* @__PURE__ */ new Set([
|
|
632
606
|
"t",
|
|
@@ -673,30 +647,10 @@ const VUE_COMPONENT_KEYS = /* @__PURE__ */ new Set([
|
|
|
673
647
|
"name",
|
|
674
648
|
"__name"
|
|
675
649
|
]);
|
|
676
|
-
/** Keys that hold only TypeScript types; nothing under them runs. */
|
|
677
|
-
const TYPE_KEYS = /* @__PURE__ */ new Set([
|
|
678
|
-
"typeAnnotation",
|
|
679
|
-
"typeParameters",
|
|
680
|
-
"typeArguments",
|
|
681
|
-
"superTypeArguments",
|
|
682
|
-
"returnType",
|
|
683
|
-
"implements"
|
|
684
|
-
]);
|
|
685
650
|
const RETURNED = "the returned value";
|
|
686
651
|
function isFunction(node) {
|
|
687
652
|
return node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration";
|
|
688
653
|
}
|
|
689
|
-
/** Pushes the children of `node` that are nodes (types excluded). */
|
|
690
|
-
function children(node, into) {
|
|
691
|
-
for (const key in node) {
|
|
692
|
-
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS.has(key)) continue;
|
|
693
|
-
const value = node[key];
|
|
694
|
-
if (value === null || typeof value !== "object") continue;
|
|
695
|
-
if (Array.isArray(value)) {
|
|
696
|
-
for (const item of value) if (item !== null && typeof item.type === "string") into.push(item);
|
|
697
|
-
} else if (typeof value.type === "string") into.push(value);
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
654
|
var Finder = class {
|
|
701
655
|
/** Reported calls by start offset, so diagnostics come out in source order whatever the walk order. */
|
|
702
656
|
found = [];
|
|
@@ -843,7 +797,7 @@ var Finder = class {
|
|
|
843
797
|
return;
|
|
844
798
|
default: {
|
|
845
799
|
const list = [];
|
|
846
|
-
|
|
800
|
+
childNodes(node, list);
|
|
847
801
|
for (const child of list) this.once(child, subject);
|
|
848
802
|
}
|
|
849
803
|
}
|
|
@@ -877,31 +831,38 @@ function vueOnceFunctions(program, define) {
|
|
|
877
831
|
const stack = [program];
|
|
878
832
|
for (let node = stack.pop(); node !== void 0; node = stack.pop()) {
|
|
879
833
|
if (node.type === "CallExpression") {
|
|
880
|
-
const
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
if (defines && setup !== null && isFunction(setup)) out.push(setup);
|
|
885
|
-
} else if (node.type === "ObjectExpression") {
|
|
886
|
-
let component = false;
|
|
887
|
-
const found = [];
|
|
888
|
-
for (const property of node.properties) {
|
|
889
|
-
if (property.type !== "Property") continue;
|
|
890
|
-
const name = keyName(property.key, property.computed === true);
|
|
891
|
-
if (name === null) continue;
|
|
892
|
-
if (VUE_COMPONENT_KEYS.has(name)) component = true;
|
|
893
|
-
const value = unwrap(property.value);
|
|
894
|
-
if (VUE_ONCE.has(name) && isFunction(value)) {
|
|
895
|
-
if (name === "setup") component = true;
|
|
896
|
-
found.push(value);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
if (component) out.push(...found);
|
|
900
|
-
}
|
|
901
|
-
children(node, stack);
|
|
834
|
+
const setup = defineSetup(node, define);
|
|
835
|
+
if (setup !== null) out.push(setup);
|
|
836
|
+
} else if (node.type === "ObjectExpression") out.push(...optionsOnceFunctions(node));
|
|
837
|
+
childNodes(node, stack);
|
|
902
838
|
}
|
|
903
839
|
return out;
|
|
904
840
|
}
|
|
841
|
+
/** The setup function of `defineComponent((props) => { … })`, or null for any other call. */
|
|
842
|
+
function defineSetup(call, define) {
|
|
843
|
+
const callee = unwrap(call.callee);
|
|
844
|
+
if (!(callee.type === "Identifier" ? define.has(callee.name) : callee.type === "MemberExpression" && VUE_DEFINE.has(keyName(callee.property, callee.computed === true) ?? ""))) return null;
|
|
845
|
+
const first = call.arguments[0];
|
|
846
|
+
const setup = first === void 0 ? null : unwrap(first);
|
|
847
|
+
return setup !== null && isFunction(setup) ? setup : null;
|
|
848
|
+
}
|
|
849
|
+
/** `setup()`/`data()` of an object that looks like component options (it has a `setup` or another component key). */
|
|
850
|
+
function optionsOnceFunctions(object) {
|
|
851
|
+
let component = false;
|
|
852
|
+
const found = [];
|
|
853
|
+
for (const property of object.properties) {
|
|
854
|
+
if (property.type !== "Property") continue;
|
|
855
|
+
const name = keyName(property.key, property.computed === true);
|
|
856
|
+
if (name === null) continue;
|
|
857
|
+
if (VUE_COMPONENT_KEYS.has(name)) component = true;
|
|
858
|
+
const value = unwrap(property.value);
|
|
859
|
+
if (VUE_ONCE.has(name) && isFunction(value)) {
|
|
860
|
+
if (name === "setup") component = true;
|
|
861
|
+
found.push(value);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return component ? found : [];
|
|
865
|
+
}
|
|
905
866
|
/** The name a callee calls (`useMemo`, `React.useMemo`), or null. */
|
|
906
867
|
function calleeName(call) {
|
|
907
868
|
const callee = unwrap(call.callee);
|
|
@@ -918,7 +879,7 @@ function localeLocals(program, getLocale) {
|
|
|
918
879
|
if (getLocale.size === 0) return out;
|
|
919
880
|
const stack = [program];
|
|
920
881
|
for (let node = stack.pop(); node !== void 0; node = stack.pop()) {
|
|
921
|
-
|
|
882
|
+
childNodes(node, stack);
|
|
922
883
|
if (node.type !== "VariableDeclarator") continue;
|
|
923
884
|
const id = node.id;
|
|
924
885
|
const init = node.init === null ? null : unwrap(node.init);
|
|
@@ -947,36 +908,55 @@ function reactHooks(program, finder, imports) {
|
|
|
947
908
|
let locales = null;
|
|
948
909
|
const stack = [program];
|
|
949
910
|
for (let node = stack.pop(); node !== void 0; node = stack.pop()) {
|
|
950
|
-
|
|
911
|
+
childNodes(node, stack);
|
|
951
912
|
if (node.type !== "CallExpression") continue;
|
|
952
913
|
const name = calleeName(node);
|
|
953
914
|
if (name === null) continue;
|
|
954
915
|
const args = node.arguments;
|
|
955
|
-
if (name
|
|
956
|
-
|
|
957
|
-
if (deps === null || deps.type !== "ArrayExpression") continue;
|
|
958
|
-
locales ??= localeLocals(program, getLocale);
|
|
959
|
-
const live = /* @__PURE__ */ new Set();
|
|
960
|
-
let keyedOnLocale = false;
|
|
961
|
-
for (const element of deps.elements) {
|
|
962
|
-
const value = element === null ? null : unwrap(element);
|
|
963
|
-
if (value?.type === "Identifier") {
|
|
964
|
-
const dep = value.name;
|
|
965
|
-
if (locales.has(dep)) keyedOnLocale = true;
|
|
966
|
-
else if (!imported.has(dep)) live.add(dep);
|
|
967
|
-
} else if (value?.type === "CallExpression" && isLocaleCall(value, getLocale)) keyedOnLocale = true;
|
|
968
|
-
}
|
|
969
|
-
if (!keyedOnLocale && args[0] !== void 0) finder.memo(args[0], "a useMemo() without the translator or the locale in its dependencies", live);
|
|
916
|
+
if (name !== "useMemo") {
|
|
917
|
+
onceHookArguments(name, args, finder);
|
|
970
918
|
continue;
|
|
971
919
|
}
|
|
972
|
-
const
|
|
973
|
-
if (
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
920
|
+
const deps = args[1] === void 0 ? null : unwrap(args[1]);
|
|
921
|
+
if (deps === null || deps.type !== "ArrayExpression" || args[0] === void 0) continue;
|
|
922
|
+
locales ??= localeLocals(program, getLocale);
|
|
923
|
+
const { live, keyedOnLocale } = memoDependencies(deps, {
|
|
924
|
+
locales,
|
|
925
|
+
imported,
|
|
926
|
+
getLocale
|
|
927
|
+
});
|
|
928
|
+
if (!keyedOnLocale) finder.memo(args[0], "a useMemo() without the translator or the locale in its dependencies", live);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
/** The arguments `ONCE_HOOKS` says hook `name` keeps from the first render. */
|
|
932
|
+
function onceHookArguments(name, args, finder) {
|
|
933
|
+
const positions = ONCE_HOOKS.get(name);
|
|
934
|
+
if (positions === void 0) return;
|
|
935
|
+
for (const [position, function_] of positions) {
|
|
936
|
+
const argument = args[position];
|
|
937
|
+
if (argument !== void 0 && argument.type !== "SpreadElement") finder.kept(argument, function_, `the initial value of ${name}()`);
|
|
978
938
|
}
|
|
979
939
|
}
|
|
940
|
+
/**
|
|
941
|
+
* What a `useMemo` dependency list names: whether it changes with the locale,
|
|
942
|
+
* and the local (non-imported) names in it, which may be the translator.
|
|
943
|
+
*/
|
|
944
|
+
function memoDependencies(deps, names) {
|
|
945
|
+
const live = /* @__PURE__ */ new Set();
|
|
946
|
+
let keyedOnLocale = false;
|
|
947
|
+
for (const element of deps.elements) {
|
|
948
|
+
const value = element === null ? null : unwrap(element);
|
|
949
|
+
if (value?.type === "Identifier") {
|
|
950
|
+
const dep = value.name;
|
|
951
|
+
if (names.locales.has(dep)) keyedOnLocale = true;
|
|
952
|
+
else if (!names.imported.has(dep)) live.add(dep);
|
|
953
|
+
} else if (value?.type === "CallExpression" && isLocaleCall(value, names.getLocale)) keyedOnLocale = true;
|
|
954
|
+
}
|
|
955
|
+
return {
|
|
956
|
+
live,
|
|
957
|
+
keyedOnLocale
|
|
958
|
+
};
|
|
959
|
+
}
|
|
980
960
|
/** The identifier a callee starts from (`__t` in `__t(…)`, `tr` in `tr.t(…)`), or "". */
|
|
981
961
|
function rootName(call) {
|
|
982
962
|
let node = unwrap(call.callee);
|
|
@@ -1030,5 +1010,4 @@ function findCapturedValues(parsed, scan, framework, options = {}) {
|
|
|
1030
1010
|
}
|
|
1031
1011
|
return finder.diagnostics;
|
|
1032
1012
|
}
|
|
1033
|
-
//#endregion
|
|
1034
1013
|
export { injectHooks as n, findCapturedValues as t };
|