@menapo/vi18n 0.1.1 → 0.1.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.
- package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-BIKEuu6B.js → check-DwFwOB1d.js} +66 -84
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-uMTuq36M.js +215 -0
- package/dist/compile-CEvH-6sY.js +240 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- 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-1onDF6Ua.js} +26 -26
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
- package/dist/engine-Bf7nVigL.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DYG9RZoT.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
- package/dist/{export-C9Sy6UWv.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-C3S8btx0.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-9unqj_fZ.js → formats-ChV1LVH_.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-1waHziMy.js +1 -0
- package/dist/i18next-D66Icz3L.js +1907 -0
- package/dist/{import-nANvQutw.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-C2ScuW6a.js → init-J7AwwA5e.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
- package/dist/lingui-Ee2farVo.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +68 -81
- package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
- package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
- package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
- package/dist/{lsp-RMMMc88P.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-CHVwBoRP.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-BTX87aVm.js → merge-driver-B04Apm29.js} +138 -122
- package/dist/migrate-D7TNl6SD.js +2169 -0
- package/dist/migrate-khCY8TE0.js +1 -0
- package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
- package/dist/{output-DWE3SIUA.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-DbX6rlo5.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C9Dbotkh.js → pipeline-sgl_q_op.js} +387 -359
- package/dist/project-DSzWKU3C.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-tzGIk6G3.js +634 -0
- package/dist/restore-BG_zMpSj.js +199 -0
- package/dist/rewrite-hAcRJwiZ.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-CFzR3V93.js +647 -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-deBOq2vq.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-Da1Ot9IN.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DJXzKoza.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-zr4jY59Y.js → status-CTH3qI-0.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-CpL0Qsgw.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 +77 -48
- package/dist/translate-BrfVRkjl.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-BaG1LboH.d.ts} +2 -6
- package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
- package/dist/types-Ds_vebja.js +41 -0
- package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.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-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
- package/dist/{version-Byqi2guX.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-CT_vsp4g.js → vite-scan-DFKSGVE9.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6482 -6018
- package/dist/vue-i18n-CBmZqHbG.js +806 -0
- package/dist/{which-BOemZ5Q8.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.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
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
import { n as isDiagnosticError } from "./diagnostics-1onDF6Ua.js";
|
|
2
|
+
import { w as parseModule } from "./sources-DuOdM02f.js";
|
|
3
|
+
import { y as printICU } from "./validate-zv4-IZyZ.js";
|
|
4
|
+
import { B as objectProperties, E as identifierName, U as templateKeys, W as unwrap, f as readString, g as skipSpace, h as skipBalanced, o as importLineEdit, p as readTemplate, r as argumentEnd, v as vueRegions, w as childEntries, x as analyzeModule, z as memberParts } from "./migrate-D7TNl6SD.js";
|
|
5
|
+
import { b as pushLiteral, m as flattenTree, s as entry, t as MODULE_EXTENSIONS, v as parseJson, x as yamlKeyLines, y as parseYaml } from "./i18next-D66Icz3L.js";
|
|
6
|
+
import { t as ModuleBuilder } from "./rewrite-hAcRJwiZ.js";
|
|
7
|
+
import { extname } from "node:path";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
// src/cli/commands/migrate/vue-i18n.ts
|
|
10
|
+
/**
|
|
11
|
+
* `vi18n migrate --from vue-i18n` (INTERNALS §2.14): vue-i18n 9–11 (and the
|
|
12
|
+
* v8 legacy API it keeps).
|
|
13
|
+
*
|
|
14
|
+
* Catalogs are JSON or YAML per locale, or one file keyed by locale; nested
|
|
15
|
+
* keys join with `.`. Messages convert to ICU: `{name}` and `{0}` stay
|
|
16
|
+
* arguments, `{'@'}` literals become text, linked messages (`@:key`,
|
|
17
|
+
* `@.lower:key`, `@:(key)`) are inlined from the same locale, and `|`
|
|
18
|
+
* pluralization becomes a plural on `count` (two forms `one`/`other`, three
|
|
19
|
+
* `=0`/`one`/`other`; `{count}` and `{n}` inside are `#`). Call sites:
|
|
20
|
+
* `t()`/`$t()`/`tc()`/`$tc()` in templates (interpolations and bound
|
|
21
|
+
* attributes) and scripts, `i18n.global.t()`, `this.$t()`. `useI18n()`
|
|
22
|
+
* bindings and imports go once unused; `<i18n-t>`, `v-t`, `$d`/`$n` and the
|
|
23
|
+
* setup are reported, their messages kept in the `msg()` table.
|
|
24
|
+
*/
|
|
25
|
+
function isOldSource(source) {
|
|
26
|
+
return source === "vue-i18n" || source.startsWith("vue-i18n/") || source === "@intlify/core-base" || source === "petite-vue-i18n";
|
|
27
|
+
}
|
|
28
|
+
const MODIFIERS = {
|
|
29
|
+
upper: (s) => s.toUpperCase(),
|
|
30
|
+
lower: (s) => s.toLowerCase(),
|
|
31
|
+
capitalize: (s) => s === "" ? s : s[0].toUpperCase() + s.slice(1)
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* One vue-i18n message as ICU. `linked(key)` returns the raw text of a
|
|
35
|
+
* linked message in the same locale.
|
|
36
|
+
*/
|
|
37
|
+
function convertVueMessage(raw, linked, depth = 0) {
|
|
38
|
+
const reader = new VueMessageReader(raw, linked, depth);
|
|
39
|
+
const error = reader.read();
|
|
40
|
+
if (error !== null) return { error };
|
|
41
|
+
return reader.result();
|
|
42
|
+
}
|
|
43
|
+
/** A vue-i18n message read character by character into `|`-separated forms of ICU nodes. */
|
|
44
|
+
var VueMessageReader = class {
|
|
45
|
+
raw;
|
|
46
|
+
linked;
|
|
47
|
+
depth;
|
|
48
|
+
forms = [[]];
|
|
49
|
+
current;
|
|
50
|
+
i = 0;
|
|
51
|
+
constructor(raw, linked, depth) {
|
|
52
|
+
this.raw = raw;
|
|
53
|
+
this.linked = linked;
|
|
54
|
+
this.depth = depth;
|
|
55
|
+
this.current = this.forms[0];
|
|
56
|
+
}
|
|
57
|
+
/** Reads the whole message; the first error, or null. */
|
|
58
|
+
read() {
|
|
59
|
+
while (this.i < this.raw.length) {
|
|
60
|
+
const c = this.raw[this.i];
|
|
61
|
+
let error = null;
|
|
62
|
+
if (c === "{") error = this.interpolation();
|
|
63
|
+
else if (c === "@") error = this.link();
|
|
64
|
+
else if (c === "|") {
|
|
65
|
+
this.current = [];
|
|
66
|
+
this.forms.push(this.current);
|
|
67
|
+
this.i++;
|
|
68
|
+
} else {
|
|
69
|
+
pushLiteral(this.current, c);
|
|
70
|
+
this.i++;
|
|
71
|
+
}
|
|
72
|
+
if (error !== null) return error;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/** `{name}`, `{0}` or a `{'literal'}`. */
|
|
77
|
+
interpolation() {
|
|
78
|
+
const { raw, i } = this;
|
|
79
|
+
const quote = /^\{\s*'(?:[^'\\]|\\.)*'\s*\}/.exec(raw.slice(i));
|
|
80
|
+
const close = quote === null ? raw.indexOf("}", i + 1) : i + quote[0].length - 1;
|
|
81
|
+
if (close === -1) return "an unclosed {";
|
|
82
|
+
const body = raw.slice(i + 1, close).trim();
|
|
83
|
+
this.i = close + 1;
|
|
84
|
+
const literal = /^'((?:[^'\\]|\\.)*)'$/.exec(body);
|
|
85
|
+
if (literal !== null) {
|
|
86
|
+
pushLiteral(this.current, literal[1].replace(/\\(.)/g, "$1"));
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
if (!/^[A-Za-z_$][\w$]*$|^\d+$/.test(body)) return `{${body}} is not a named or list interpolation`;
|
|
90
|
+
this.current.push({
|
|
91
|
+
type: "argument",
|
|
92
|
+
name: body
|
|
93
|
+
});
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/** The key of a link (`@:key`, `@:(key)`), and the characters it takes. */
|
|
97
|
+
linkKey(m) {
|
|
98
|
+
const key = (m[2] ?? m[3] ?? "").trim();
|
|
99
|
+
if (m[3] !== void 0 && key.endsWith(".") && this.linked(key) === void 0 && this.linked(key.slice(0, -1)) !== void 0) return {
|
|
100
|
+
key: key.slice(0, -1),
|
|
101
|
+
consumed: m[0].length - 1
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
key,
|
|
105
|
+
consumed: m[0].length
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** A linked message (`@:key`, `@.lower:key`), inlined; a lone `@` is text. */
|
|
109
|
+
link() {
|
|
110
|
+
const m = /^@(?:\.(\w+))?:(?:\(([^)]*)\)|([\w$\-.]+))/.exec(this.raw.slice(this.i));
|
|
111
|
+
if (m === null) {
|
|
112
|
+
pushLiteral(this.current, "@");
|
|
113
|
+
this.i++;
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const modifier = m[1];
|
|
117
|
+
const { key, consumed } = this.linkKey(m);
|
|
118
|
+
this.i += consumed;
|
|
119
|
+
if (this.depth > 8) return "linked messages nest deeper than 8 levels";
|
|
120
|
+
const text = this.linked(key);
|
|
121
|
+
if (text === void 0) return `@:${key} links to no message`;
|
|
122
|
+
const inner = convertVueMessage(text, this.linked, this.depth + 1);
|
|
123
|
+
if ("error" in inner) return inner.error;
|
|
124
|
+
if (inner.nodes.some((n) => n.type === "plural")) return `@:${key} links to a plural message`;
|
|
125
|
+
if (modifier !== void 0) return this.modified(modifier, key, inner.nodes);
|
|
126
|
+
for (const node of inner.nodes) if (node.type === "literal") pushLiteral(this.current, node.value);
|
|
127
|
+
else this.current.push(node);
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
/** A linked message through a modifier (`@.upper:key`): only plain text can change case. */
|
|
131
|
+
modified(modifier, key, nodes) {
|
|
132
|
+
const apply = Object.hasOwn(MODIFIERS, modifier) ? MODIFIERS[modifier] : void 0;
|
|
133
|
+
if (apply === void 0) return `@.${modifier}: is not a built-in modifier`;
|
|
134
|
+
if (nodes.some((n) => n.type !== "literal")) return `@.${modifier}:${key} changes a message with placeholders, which ICU cannot`;
|
|
135
|
+
pushLiteral(this.current, apply(nodes.map((n) => n.value).join("")));
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
/** One form as is; several as a plural on `count`. */
|
|
139
|
+
result() {
|
|
140
|
+
if (this.forms.length === 1) return { nodes: this.forms[0] };
|
|
141
|
+
const trimmed = this.forms.map((form) => trimForm(form));
|
|
142
|
+
const keys = PLURAL_KEYS[trimmed.length];
|
|
143
|
+
if (keys === void 0) return { error: `${trimmed.length} plural forms need custom pluralRules; write the ICU plural by hand` };
|
|
144
|
+
return { nodes: [{
|
|
145
|
+
type: "plural",
|
|
146
|
+
pluralType: "cardinal",
|
|
147
|
+
name: "count",
|
|
148
|
+
offset: 0,
|
|
149
|
+
options: trimmed.map((form, index) => ({
|
|
150
|
+
key: keys[index],
|
|
151
|
+
value: form.map((node) => node.type === "argument" && (node.name === "count" || node.name === "n") ? { type: "pound" } : node)
|
|
152
|
+
}))
|
|
153
|
+
}] };
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
function trimForm(form) {
|
|
157
|
+
const out = form.map((n) => ({ ...n }));
|
|
158
|
+
const first = out[0];
|
|
159
|
+
if (first?.type === "literal") {
|
|
160
|
+
first.value = first.value.replace(/^\s+/, "");
|
|
161
|
+
if (first.value === "") out.shift();
|
|
162
|
+
}
|
|
163
|
+
const last = out[out.length - 1];
|
|
164
|
+
if (last?.type === "literal") {
|
|
165
|
+
last.value = last.value.replace(/\s+$/, "");
|
|
166
|
+
if (last.value === "") out.pop();
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/** Plural form keys by the number of `|`-separated forms, as vue-i18n's default rule picks them. */
|
|
171
|
+
const PLURAL_KEYS = {
|
|
172
|
+
2: ["one", "other"],
|
|
173
|
+
3: [
|
|
174
|
+
"=0",
|
|
175
|
+
"one",
|
|
176
|
+
"other"
|
|
177
|
+
]
|
|
178
|
+
};
|
|
179
|
+
/** vue-i18n calls with no vi18n call to become, and what to use instead. */
|
|
180
|
+
const UNREWRITTEN_HINTS = /* @__PURE__ */ new Map([
|
|
181
|
+
["d", "use fmt.date from @menapo/vi18n"],
|
|
182
|
+
["n", "use fmt.number from @menapo/vi18n"],
|
|
183
|
+
["te", "vi18n needs no existence check, since every message the code names exists"],
|
|
184
|
+
["rt", "translate the text with t() where it is known"]
|
|
185
|
+
]);
|
|
186
|
+
/** What a call argument is, read from how it starts. */
|
|
187
|
+
function argumentKind(code) {
|
|
188
|
+
if (code.startsWith("{")) return "object";
|
|
189
|
+
if (code.startsWith("[")) return "array";
|
|
190
|
+
return /^["'`]/.test(code) ? "string" : "other";
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* A `t`-family method call in script code (`this.$t`, `i18n.global.t`,
|
|
194
|
+
* `x.t`): its name, whether it is surely vue-i18n's (`this.$…`, `.global.`),
|
|
195
|
+
* and the receiver's root identifier, which the rewrite consumes. Null for
|
|
196
|
+
* any other call.
|
|
197
|
+
*/
|
|
198
|
+
function memberCallee(callee) {
|
|
199
|
+
const member = memberParts(callee);
|
|
200
|
+
if (member === null || !/^\$?(?:tc?|te|d|n|rt)$/.test(member.property)) return null;
|
|
201
|
+
const object = unwrap(member.object);
|
|
202
|
+
const thisCall = object.type === "ThisExpression" && member.property.startsWith("$");
|
|
203
|
+
const global = object.type === "MemberExpression" && memberParts(object)?.property === "global";
|
|
204
|
+
const known = thisCall || global;
|
|
205
|
+
if (!known && member.property !== "t" && member.property !== "tc") return null;
|
|
206
|
+
let root = object;
|
|
207
|
+
while (root.type === "MemberExpression") root = unwrap(root.object);
|
|
208
|
+
return {
|
|
209
|
+
name: member.property,
|
|
210
|
+
known,
|
|
211
|
+
consume: root.type === "Identifier" ? [root] : []
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function createVueI18nAdapter() {
|
|
215
|
+
return new VueI18nAdapter();
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* The roles of a `t`-family call's value arguments: `t(key, named | list |
|
|
219
|
+
* values)`, `t(key, count)` and `t(key, named | list, count)` for a plural.
|
|
220
|
+
*/
|
|
221
|
+
function argumentRoles(kinds, plural) {
|
|
222
|
+
const roles = {
|
|
223
|
+
named: null,
|
|
224
|
+
list: null,
|
|
225
|
+
count: null
|
|
226
|
+
};
|
|
227
|
+
const [a, b] = kinds;
|
|
228
|
+
if (kinds.length > 2) return "more than two value arguments";
|
|
229
|
+
if (a === void 0) return roles;
|
|
230
|
+
if (b === void 0) {
|
|
231
|
+
if (a === "array") roles.list = 0;
|
|
232
|
+
else if (a !== "object" && plural) roles.count = 0;
|
|
233
|
+
else roles.named = 0;
|
|
234
|
+
return roles;
|
|
235
|
+
}
|
|
236
|
+
if (a !== "object" && a !== "array" || b !== "other") return "options vi18n has no counterpart for";
|
|
237
|
+
if (a === "object") roles.named = 0;
|
|
238
|
+
else roles.list = 0;
|
|
239
|
+
roles.count = 1;
|
|
240
|
+
return roles;
|
|
241
|
+
}
|
|
242
|
+
/** The values of a call as code (`, { … }`), from its value arguments' source. */
|
|
243
|
+
function renderValues(roles, args, src) {
|
|
244
|
+
const entries = [];
|
|
245
|
+
if (roles.count !== null) entries.push(entry("count", src(roles.count).trim()));
|
|
246
|
+
if (roles.named !== null) {
|
|
247
|
+
const code = src(roles.named).trim();
|
|
248
|
+
if (args[roles.named]?.kind === "object") {
|
|
249
|
+
const inner = code.slice(1, -1).trim();
|
|
250
|
+
if (inner !== "") entries.push(inner);
|
|
251
|
+
} else if (entries.length === 0) return `, ${code}`;
|
|
252
|
+
else entries.push(`...${code}`);
|
|
253
|
+
}
|
|
254
|
+
if (roles.list !== null) splitTopLevel(src(roles.list).trim().slice(1, -1)).forEach((item, index) => entries.push(`${index}: ${item.trim()}`));
|
|
255
|
+
return entries.length === 0 ? "" : `, { ${entries.join(", ")} }`;
|
|
256
|
+
}
|
|
257
|
+
/** A literal key: a string, or a template without placeholders; null for a computed one. */
|
|
258
|
+
function literalKey(code) {
|
|
259
|
+
const literal = /^["']/.test(code) ? readString(code, 0) : null;
|
|
260
|
+
if (literal !== null && literal.end === code.length) return literal.value;
|
|
261
|
+
if (!code.startsWith("`")) return null;
|
|
262
|
+
const t = readTemplate(code, 0);
|
|
263
|
+
return t !== null && t.quasis.length === 1 && t.end === code.length ? t.quasis[0] : null;
|
|
264
|
+
}
|
|
265
|
+
/** The locale each top-level key of a catalog names, when the file holds every locale (`{ en: {…}, de: {…} }`). */
|
|
266
|
+
function localesOf(value, locales) {
|
|
267
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
268
|
+
const keys = Object.keys(value);
|
|
269
|
+
const found = keys.map((k) => locales.find((l) => l.toLowerCase() === k.toLowerCase().replace(/_/g, "-")));
|
|
270
|
+
if (keys.length === 0 || found.some((l) => l === void 0)) return null;
|
|
271
|
+
if (!Object.values(value).every((v) => typeof v === "object" && v !== null)) return null;
|
|
272
|
+
return found;
|
|
273
|
+
}
|
|
274
|
+
/** The vue-i18n catalogs of a run, and the messages its call sites define. */
|
|
275
|
+
var VueI18nAdapter = class {
|
|
276
|
+
catalogExtensions = [
|
|
277
|
+
".json",
|
|
278
|
+
".yaml",
|
|
279
|
+
".yml"
|
|
280
|
+
];
|
|
281
|
+
catalogsRequired = true;
|
|
282
|
+
extensions = /* @__PURE__ */ new Set([...MODULE_EXTENSIONS, ".vue"]);
|
|
283
|
+
/** locale → key → text. */
|
|
284
|
+
catalogs = /* @__PURE__ */ new Map();
|
|
285
|
+
referenced = /* @__PURE__ */ new Set();
|
|
286
|
+
sourceLocale = "";
|
|
287
|
+
mentions(text) {
|
|
288
|
+
return /vue-i18n|\$tc?\s*\(|\bt\s*\(|i18n/.test(text);
|
|
289
|
+
}
|
|
290
|
+
load(files, env) {
|
|
291
|
+
this.sourceLocale = env.config.sourceLocale;
|
|
292
|
+
for (const file of files) this.readCatalog(file, env);
|
|
293
|
+
}
|
|
294
|
+
planFile(file, text, env) {
|
|
295
|
+
return new VueModule(this, env, file, text).plan();
|
|
296
|
+
}
|
|
297
|
+
unusedKeys() {
|
|
298
|
+
return [...this.catalogs.get(this.sourceLocale)?.keys() ?? []].filter((key) => !this.referenced.has(key));
|
|
299
|
+
}
|
|
300
|
+
textOf(locale, key) {
|
|
301
|
+
return this.catalogs.get(locale)?.get(key);
|
|
302
|
+
}
|
|
303
|
+
add(locale, key, value, file) {
|
|
304
|
+
let texts = this.catalogs.get(locale);
|
|
305
|
+
if (texts === void 0) this.catalogs.set(locale, texts = /* @__PURE__ */ new Map());
|
|
306
|
+
texts.set(key, {
|
|
307
|
+
...value,
|
|
308
|
+
file
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
readCatalog(file, env) {
|
|
312
|
+
let text;
|
|
313
|
+
try {
|
|
314
|
+
text = readFileSync(file.file, "utf8");
|
|
315
|
+
} catch (error) {
|
|
316
|
+
const message = `cannot read: ${error.message}`;
|
|
317
|
+
env.report("catalog-parse", message, { file: file.file }, "error");
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const yaml = extname(file.file) === ".yaml" || extname(file.file) === ".yml";
|
|
321
|
+
const parsed = yaml ? parseYaml(text) : parseJson(text);
|
|
322
|
+
if (parsed.problem !== null) {
|
|
323
|
+
const at = {
|
|
324
|
+
file: file.file,
|
|
325
|
+
line: parsed.problem.line
|
|
326
|
+
};
|
|
327
|
+
env.report("catalog-parse", parsed.problem.message, at, "error");
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const keyLines = yaml ? yamlKeyLines(text) : null;
|
|
331
|
+
const read = (tree, locale) => {
|
|
332
|
+
const flat = flattenTree(tree, yaml ? "" : text);
|
|
333
|
+
for (const problem of flat.problems) env.report("migrate-unconverted", problem.message, {
|
|
334
|
+
file: file.file,
|
|
335
|
+
line: problem.line
|
|
336
|
+
});
|
|
337
|
+
for (const [key, value] of flat.texts) {
|
|
338
|
+
const line = keyLines === null ? value.line : keyLines.get(key.split(".").pop()) ?? 0;
|
|
339
|
+
this.add(locale, key, {
|
|
340
|
+
value: value.value,
|
|
341
|
+
line
|
|
342
|
+
}, file.file);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
const value = parsed.value;
|
|
346
|
+
const locales = localesOf(value, env.config.locales);
|
|
347
|
+
if (locales === null) return read(value, file.locale);
|
|
348
|
+
Object.keys(value).forEach((k, index) => read(value[k], locales[index]));
|
|
349
|
+
}
|
|
350
|
+
convert(locale, raw) {
|
|
351
|
+
return convertVueMessage(raw, (linkedKey) => {
|
|
352
|
+
this.referenced.add(linkedKey);
|
|
353
|
+
return this.textOf(locale, linkedKey)?.value;
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
/** Registers a message; its registry key, or why it cannot be. */
|
|
357
|
+
define(env, key, fallback) {
|
|
358
|
+
this.referenced.add(key);
|
|
359
|
+
if (env.message(key) !== void 0) return { key };
|
|
360
|
+
const sourceText = this.textOf(this.sourceLocale, key);
|
|
361
|
+
const raw = sourceText?.value ?? fallback;
|
|
362
|
+
if (raw === null) return { problem: `${key} is in no ${this.sourceLocale} catalog` };
|
|
363
|
+
const source = this.convert(this.sourceLocale, raw);
|
|
364
|
+
if ("error" in source) return { problem: `${key}: ${source.error}` };
|
|
365
|
+
return env.define({
|
|
366
|
+
key,
|
|
367
|
+
named: true,
|
|
368
|
+
source: printICU(source.nodes),
|
|
369
|
+
context: null,
|
|
370
|
+
hint: null,
|
|
371
|
+
translations: this.translations(key),
|
|
372
|
+
file: sourceText?.file ?? "",
|
|
373
|
+
line: sourceText?.line ?? 0
|
|
374
|
+
}) ? { key } : { problem: `${key} could not be converted` };
|
|
375
|
+
}
|
|
376
|
+
translations(key) {
|
|
377
|
+
const translations = /* @__PURE__ */ new Map();
|
|
378
|
+
for (const [locale, texts] of this.catalogs) {
|
|
379
|
+
if (locale === this.sourceLocale) continue;
|
|
380
|
+
const found = texts.get(key);
|
|
381
|
+
if (found === void 0) continue;
|
|
382
|
+
const converted = this.convert(locale, found.value);
|
|
383
|
+
if (!("error" in converted)) {
|
|
384
|
+
translations.set(locale, { icu: printICU(converted.nodes) });
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const at = {
|
|
388
|
+
file: found.file,
|
|
389
|
+
line: found.line
|
|
390
|
+
};
|
|
391
|
+
translations.set(locale, {
|
|
392
|
+
unconverted: found.value,
|
|
393
|
+
reason: converted.error,
|
|
394
|
+
...at
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
return translations;
|
|
398
|
+
}
|
|
399
|
+
isPluralKey(env, key) {
|
|
400
|
+
const message = env.message(key);
|
|
401
|
+
return message !== void 0 && message.canonical.startsWith("{count, plural,") && message.canonical.endsWith("}");
|
|
402
|
+
}
|
|
403
|
+
/** Keys a computed key can produce (string literals and matching templates in it). */
|
|
404
|
+
keysIn(code) {
|
|
405
|
+
const keys = [...this.catalogs.get(this.sourceLocale)?.keys() ?? []];
|
|
406
|
+
const found = /* @__PURE__ */ new Set();
|
|
407
|
+
for (let i = 0; i < code.length; i++) {
|
|
408
|
+
const c = code[i];
|
|
409
|
+
if (c === "\"" || c === "'") {
|
|
410
|
+
const s = readString(code, i);
|
|
411
|
+
if (s === null) break;
|
|
412
|
+
if (keys.includes(s.value)) found.add(s.value);
|
|
413
|
+
i = s.end - 1;
|
|
414
|
+
} else if (c === "`") {
|
|
415
|
+
const s = readTemplate(code, i);
|
|
416
|
+
if (s === null) break;
|
|
417
|
+
for (const key of templateKeys(s.quasis, keys)) found.add(key);
|
|
418
|
+
i = s.end - 1;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return [...found];
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* The values of a `t`-family call from its arguments as written (default
|
|
425
|
+
* message strings left out), or why vi18n migrate cannot read them
|
|
426
|
+
* (`tc` always pluralizes).
|
|
427
|
+
*/
|
|
428
|
+
callValues(env, key, args, tc) {
|
|
429
|
+
const plural = tc || this.isPluralKey(env, key);
|
|
430
|
+
const roles = argumentRoles(args.map((a) => a.kind), plural);
|
|
431
|
+
if (typeof roles === "string") return { problem: roles };
|
|
432
|
+
const namedArg = roles.named === null ? void 0 : args[roles.named];
|
|
433
|
+
const namesCount = namedArg?.kind === "object" && /[{,]\s*count\s*[:,}]/.test(namedArg.code);
|
|
434
|
+
if (plural && roles.count === null && !namesCount) return { problem: "a plural message without a count" };
|
|
435
|
+
return { values: (src) => renderValues(roles, args, src) };
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
function scriptBlocks(text) {
|
|
439
|
+
const scripts = [];
|
|
440
|
+
for (const m of text.matchAll(/<script\b([^>]*)>/g)) {
|
|
441
|
+
const attrs = m[1];
|
|
442
|
+
if (/\bsrc\s*=/.test(attrs)) continue;
|
|
443
|
+
const start = m.index + m[0].length;
|
|
444
|
+
const close = text.indexOf("<\/script>", start);
|
|
445
|
+
if (close === -1) continue;
|
|
446
|
+
scripts.push({
|
|
447
|
+
start,
|
|
448
|
+
end: close,
|
|
449
|
+
setup: /\bsetup\b/.test(attrs),
|
|
450
|
+
lang: /\blang\s*=\s*["']([^"']+)["']/.exec(attrs)?.[1] ?? null,
|
|
451
|
+
open: m.index
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
return scripts;
|
|
455
|
+
}
|
|
456
|
+
/** Script code with everything else blanked, so offsets are the file's own. */
|
|
457
|
+
function scriptCode(text, scripts) {
|
|
458
|
+
let code = text.replace(/[^\n]/g, " ");
|
|
459
|
+
for (const s of scripts) code = code.slice(0, s.start) + text.slice(s.start, s.end) + code.slice(s.end);
|
|
460
|
+
return code;
|
|
461
|
+
}
|
|
462
|
+
const pushTo = (map, name, offset) => {
|
|
463
|
+
let list = map.get(name);
|
|
464
|
+
if (list === void 0) map.set(name, list = []);
|
|
465
|
+
if (!list.includes(offset)) list.push(offset);
|
|
466
|
+
};
|
|
467
|
+
/** One file's vue-i18n call sites, in script code and a `.vue` template, planned as rewrites. */
|
|
468
|
+
var VueModule = class {
|
|
469
|
+
adapter;
|
|
470
|
+
env;
|
|
471
|
+
file;
|
|
472
|
+
text;
|
|
473
|
+
vue;
|
|
474
|
+
regions;
|
|
475
|
+
scripts;
|
|
476
|
+
outside = /* @__PURE__ */ new Map();
|
|
477
|
+
templateStart;
|
|
478
|
+
templateEnd;
|
|
479
|
+
templateCalls = [];
|
|
480
|
+
tBindings = /* @__PURE__ */ new Set();
|
|
481
|
+
plainSites = false;
|
|
482
|
+
info;
|
|
483
|
+
builder;
|
|
484
|
+
constructor(adapter, env, file, text) {
|
|
485
|
+
this.adapter = adapter;
|
|
486
|
+
this.env = env;
|
|
487
|
+
this.file = file;
|
|
488
|
+
this.text = text;
|
|
489
|
+
this.vue = extname(file) === ".vue";
|
|
490
|
+
this.regions = this.vue ? vueRegions(text) : null;
|
|
491
|
+
this.scripts = this.vue ? scriptBlocks(text) : [];
|
|
492
|
+
this.templateStart = text.indexOf("<template");
|
|
493
|
+
this.templateEnd = text.lastIndexOf("</template>");
|
|
494
|
+
}
|
|
495
|
+
plan() {
|
|
496
|
+
const { vue, scripts, text, file } = this;
|
|
497
|
+
const typed = vue ? scripts.some((s) => s.lang === "ts" || s.lang === "tsx") : extname(file).includes("ts");
|
|
498
|
+
let program;
|
|
499
|
+
try {
|
|
500
|
+
program = parseModule(vue ? scriptCode(text, scripts) : text, typed ? "ts" : "js", file).program;
|
|
501
|
+
} catch (error) {
|
|
502
|
+
if (!isDiagnosticError(error)) throw error;
|
|
503
|
+
this.env.diagnostics.push({
|
|
504
|
+
...error.diagnostic,
|
|
505
|
+
file: this.env.rel(file)
|
|
506
|
+
});
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
this.info = analyzeModule(program);
|
|
510
|
+
this.scanTemplate();
|
|
511
|
+
this.builder = new ModuleBuilder({
|
|
512
|
+
file,
|
|
513
|
+
text,
|
|
514
|
+
info: this.info,
|
|
515
|
+
isOldSource,
|
|
516
|
+
outside: this.outside,
|
|
517
|
+
...vue ? { placeImport: (line, removed) => this.placeImport(line, removed) } : {}
|
|
518
|
+
});
|
|
519
|
+
this.readBindings();
|
|
520
|
+
this.templateMentions();
|
|
521
|
+
this.visit(program);
|
|
522
|
+
if (this.regions !== null) this.templateSites(this.regions);
|
|
523
|
+
return this.builder.finish();
|
|
524
|
+
}
|
|
525
|
+
inTemplate(offset) {
|
|
526
|
+
const { regions, templateStart, templateEnd } = this;
|
|
527
|
+
return regions !== null && templateStart !== -1 && offset > templateStart && offset < templateEnd && !regions.code.some((r) => r.start <= offset && offset < r.end);
|
|
528
|
+
}
|
|
529
|
+
/** The `t`-family calls of the template, and its references to `t` and `tc`. */
|
|
530
|
+
scanTemplate() {
|
|
531
|
+
const { regions, text } = this;
|
|
532
|
+
if (regions === null) return;
|
|
533
|
+
const pattern = /(?<![\w$.-])(\$tc?|tc?|\$te|\$d|\$n|\$rt)\s*\(/g;
|
|
534
|
+
for (let m = pattern.exec(text); m !== null; m = pattern.exec(text)) {
|
|
535
|
+
if (!this.inTemplate(m.index) || !inExpression(text, m.index, regions)) continue;
|
|
536
|
+
const callee = m[1];
|
|
537
|
+
if (callee === "t" || callee === "tc") pushTo(this.outside, callee, m.index);
|
|
538
|
+
const call = lexicalCall(text, m.index, callee, m.index + m[0].length);
|
|
539
|
+
if (call !== null) this.templateCalls.push(call);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
blockOf(offset) {
|
|
543
|
+
return this.scripts.find((s) => s.start <= offset && offset <= s.end) ?? null;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* The import goes into `<script setup>`, which the template sees, unless
|
|
547
|
+
* Options API code in a plain `<script>` calls `t` too: then into that
|
|
548
|
+
* block, whose imports the template sees beside a `<script setup>` (one
|
|
549
|
+
* is added, empty, when the component has none).
|
|
550
|
+
*/
|
|
551
|
+
placeImport(line, removed) {
|
|
552
|
+
const { scripts, text } = this;
|
|
553
|
+
const setup = scripts.find((s) => s.setup);
|
|
554
|
+
const plain = scripts.find((s) => !s.setup);
|
|
555
|
+
const target = this.plainSites && plain !== void 0 ? plain : setup ?? null;
|
|
556
|
+
if (target === null) return [importLineEdit(this.file, text, line, null)];
|
|
557
|
+
const lastImport = this.info.imports.filter((d) => d.node.start >= target.start && d.node.end <= target.end && !removed.has(d.node)).pop();
|
|
558
|
+
const at = lastImport === void 0 ? target.start : lastImport.node.end;
|
|
559
|
+
const edits = [{
|
|
560
|
+
start: at,
|
|
561
|
+
end: at,
|
|
562
|
+
text: `\n${line}`
|
|
563
|
+
}];
|
|
564
|
+
if (target === plain && setup === void 0 && this.templateCalls.length > 0) {
|
|
565
|
+
const close = text.indexOf("<\/script>", plain.end) + 9;
|
|
566
|
+
const langAttr = plain.lang === null ? "" : ` lang="${plain.lang}"`;
|
|
567
|
+
edits.push({
|
|
568
|
+
start: close,
|
|
569
|
+
end: close,
|
|
570
|
+
text: `\n<script setup${langAttr}><\/script>`
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
return edits;
|
|
574
|
+
}
|
|
575
|
+
/** `const { t } = useI18n()`: its `t` and `tc`, reported when it holds component-local messages. */
|
|
576
|
+
readBindings() {
|
|
577
|
+
const useI18n = /* @__PURE__ */ new Set();
|
|
578
|
+
for (const declaration of this.info.imports) {
|
|
579
|
+
if (!isOldSource(declaration.source)) continue;
|
|
580
|
+
for (const s of declaration.specifiers) if (s.imported === "useI18n") useI18n.add(s.local);
|
|
581
|
+
}
|
|
582
|
+
for (const binding of this.info.bindings) {
|
|
583
|
+
const init = binding.declarator?.init;
|
|
584
|
+
if (init === null || init === void 0) continue;
|
|
585
|
+
const call = unwrap(init);
|
|
586
|
+
const callee = call.type === "CallExpression" ? identifierName(call.callee) : null;
|
|
587
|
+
if (callee === null || !useI18n.has(callee)) continue;
|
|
588
|
+
this.builder.hooks.push(binding);
|
|
589
|
+
const slot = binding.slot;
|
|
590
|
+
const name = slot !== null && slot.type === "Property" ? slot.key.name : null;
|
|
591
|
+
if (name === "t" || name === "tc") this.tBindings.add(binding);
|
|
592
|
+
const options = call.arguments[0];
|
|
593
|
+
if (options !== void 0 && objectProperties(options)?.properties.has("messages") === true) this.builder.note(call.start, "useI18n({ messages }) holds component-local messages, which were not migrated: move them into the locale catalogs");
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/** Every template mention of a `useI18n()` binding (`locale = 'de'` in a handler) keeps it. */
|
|
597
|
+
templateMentions() {
|
|
598
|
+
const { templateStart, templateEnd, text } = this;
|
|
599
|
+
if (!this.vue || templateStart === -1) return;
|
|
600
|
+
for (const binding of this.builder.hooks) {
|
|
601
|
+
const name = binding.name.replace(/\$/g, "$");
|
|
602
|
+
const pattern = new RegExp(String.raw`(?<![\w$.])${name}(?![\w$])`, "g");
|
|
603
|
+
for (const m of text.slice(templateStart, templateEnd).matchAll(pattern)) pushTo(this.outside, binding.name, templateStart + m.index);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
resolve(name, offset) {
|
|
607
|
+
let best = null;
|
|
608
|
+
for (const b of this.info.bindings) {
|
|
609
|
+
if (b.name !== name || offset < b.scope.start || offset >= b.scope.end) continue;
|
|
610
|
+
if (best === null || b.scope.end - b.scope.start < best.scope.end - best.scope.start) best = b;
|
|
611
|
+
}
|
|
612
|
+
return best;
|
|
613
|
+
}
|
|
614
|
+
/** A call with a computed key: reported, with the keys it can produce for the `msg()` table. */
|
|
615
|
+
computedSite(call, firstCode, known) {
|
|
616
|
+
const { env } = this;
|
|
617
|
+
const keys = this.adapter.keysIn(firstCode);
|
|
618
|
+
for (const k of keys) this.adapter.define(env, k, null);
|
|
619
|
+
if (keys.length === 0 && !known) return;
|
|
620
|
+
this.builder.note(call.start, `${call.callee}(${firstCode}) uses a computed key and was not rewritten`, keys.filter((k) => env.message(k) !== void 0));
|
|
621
|
+
}
|
|
622
|
+
/** One `t`-family call: rewrite, note, or nothing when it is not vue-i18n's. */
|
|
623
|
+
site(call, known, consume, markup) {
|
|
624
|
+
const { adapter, env, text } = this;
|
|
625
|
+
const callee = call.callee.replace(/^\$/, "");
|
|
626
|
+
const hint = UNREWRITTEN_HINTS.get(callee);
|
|
627
|
+
if (hint !== void 0) {
|
|
628
|
+
if (known || call.callee.startsWith("$")) this.builder.note(call.start, `${call.callee}() is not rewritten: ${hint}`);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
const first = call.args[0];
|
|
632
|
+
if (first === void 0) return;
|
|
633
|
+
const firstCode = text.slice(first.start, first.end).trim();
|
|
634
|
+
const key = literalKey(firstCode);
|
|
635
|
+
if (key === null) return this.computedSite(call, firstCode, known);
|
|
636
|
+
const rest = call.args.slice(1).map((a) => {
|
|
637
|
+
const argCode = text.slice(a.start, a.end).trim();
|
|
638
|
+
return {
|
|
639
|
+
code: argCode,
|
|
640
|
+
kind: argumentKind(argCode),
|
|
641
|
+
range: a
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
const fallback = rest.find((a) => a.kind === "string");
|
|
645
|
+
const fallbackText = fallback === void 0 ? null : readString(fallback.code, 0)?.value ?? null;
|
|
646
|
+
if (adapter.textOf(adapter.sourceLocale, key) === void 0 && fallbackText === null) {
|
|
647
|
+
if (known) {
|
|
648
|
+
const problem = `${call.callee}(${JSON.stringify(key)}): the key is in no ${adapter.sourceLocale} catalog; not rewritten`;
|
|
649
|
+
this.builder.note(call.start, problem);
|
|
650
|
+
}
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const defined = adapter.define(env, key, fallbackText);
|
|
654
|
+
if ("problem" in defined) {
|
|
655
|
+
this.builder.note(call.start, `${defined.problem}; ${call.callee}() was not rewritten`);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const valueArgs = rest.filter((a) => a.kind !== "string");
|
|
659
|
+
const values = adapter.callValues(env, key, valueArgs, callee === "tc");
|
|
660
|
+
if ("problem" in values) {
|
|
661
|
+
const problem = `${call.callee}(${JSON.stringify(key)}, …) passes ${values.problem}; not rewritten`;
|
|
662
|
+
this.builder.note(call.start, problem, [defined.key]);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const argRanges = valueArgs.map((a) => a.range);
|
|
666
|
+
for (const offset of consume.offsets) this.builder.consumed.add(offset);
|
|
667
|
+
if (this.blockOf(call.start)?.setup === false) this.plainSites = true;
|
|
668
|
+
this.builder.site(call, (r) => {
|
|
669
|
+
const src = (index) => {
|
|
670
|
+
const range = argRanges[index];
|
|
671
|
+
return r.src(range.start, range.end);
|
|
672
|
+
};
|
|
673
|
+
return `${r.fn.t}(${r.message(defined.key)}${values.values(src)})`;
|
|
674
|
+
}, {
|
|
675
|
+
keys: [defined.key],
|
|
676
|
+
uses: ["t"],
|
|
677
|
+
consume: consume.nodes,
|
|
678
|
+
...markup === null ? {} : { markup }
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/** A script call's callee: `t()` from useI18n, `this.$t()`, `i18n.global.t()`, `$t` members. */
|
|
682
|
+
scriptCallee(callee) {
|
|
683
|
+
if (callee.type !== "Identifier") return memberCallee(callee);
|
|
684
|
+
const binding = this.resolve(callee.name, callee.start);
|
|
685
|
+
if (binding === null || !this.tBindings.has(binding)) return null;
|
|
686
|
+
return {
|
|
687
|
+
name: callee.name,
|
|
688
|
+
known: true,
|
|
689
|
+
consume: [callee]
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
scriptCall(node) {
|
|
693
|
+
const callee = unwrap(node.callee);
|
|
694
|
+
const nodeArgs = node.arguments;
|
|
695
|
+
const found = this.scriptCallee(callee);
|
|
696
|
+
if (found !== null && nodeArgs.every((a) => a.type !== "SpreadElement")) {
|
|
697
|
+
const args = nodeArgs.map((a) => ({
|
|
698
|
+
start: a.start,
|
|
699
|
+
end: a.end
|
|
700
|
+
}));
|
|
701
|
+
const call = {
|
|
702
|
+
callee: found.name,
|
|
703
|
+
calleeStart: callee.start,
|
|
704
|
+
start: node.start,
|
|
705
|
+
end: node.end,
|
|
706
|
+
args
|
|
707
|
+
};
|
|
708
|
+
this.site(call, found.known, {
|
|
709
|
+
nodes: found.consume,
|
|
710
|
+
offsets: []
|
|
711
|
+
}, null);
|
|
712
|
+
}
|
|
713
|
+
if (identifierName(callee) === "createI18n") this.builder.note(node.start, "createI18n(): delete this setup once nothing uses vue-i18n; vi18n needs only its Vite plugin and vi18n.config.ts");
|
|
714
|
+
else if (memberParts(callee)?.property === "use" && nodeArgs.some((a) => /^i18n$/i.test(identifierName(a) ?? ""))) this.builder.note(node.start, "app.use(i18n): remove it with the vue-i18n setup");
|
|
715
|
+
}
|
|
716
|
+
visit(node) {
|
|
717
|
+
if (node.type === "CallExpression") this.scriptCall(node);
|
|
718
|
+
for (const [, child] of childEntries(node)) this.visit(child);
|
|
719
|
+
}
|
|
720
|
+
/** Template calls and components. */
|
|
721
|
+
templateSites(regions) {
|
|
722
|
+
for (const call of this.templateCalls) {
|
|
723
|
+
const bare = call.callee === "t" || call.callee === "tc";
|
|
724
|
+
if (bare) {
|
|
725
|
+
const binding = this.resolve(call.callee, this.scripts.find((s) => s.setup)?.start ?? 0);
|
|
726
|
+
if (binding !== null && !this.tBindings.has(binding)) continue;
|
|
727
|
+
}
|
|
728
|
+
const markup = { quote: regions.attributes.find((a) => a.start <= call.start && call.start < a.end)?.quote ?? null };
|
|
729
|
+
this.site(call, true, {
|
|
730
|
+
nodes: [],
|
|
731
|
+
offsets: bare ? [call.calleeStart] : []
|
|
732
|
+
}, markup);
|
|
733
|
+
}
|
|
734
|
+
this.templateNotes();
|
|
735
|
+
}
|
|
736
|
+
/** `<i18n-t>` and friends, `v-t` and `<i18n>` blocks: reported, their messages kept for the `msg()` table. */
|
|
737
|
+
templateNotes() {
|
|
738
|
+
const { text, env, adapter } = this;
|
|
739
|
+
const definedKeys = (key) => key !== null && "key" in adapter.define(env, key, null) ? [key] : [];
|
|
740
|
+
for (const m of text.matchAll(/<(i18n-t|i18n|I18nT|i18n-n|i18n-d)\b([^>]*)>/g)) {
|
|
741
|
+
if (!this.inTemplate(m.index)) continue;
|
|
742
|
+
const keypath = /\b(?:keypath|path)\s*=\s*"([^"]*)"|\b(?:keypath|path)\s*=\s*'([^']*)'/.exec(m[2]);
|
|
743
|
+
this.builder.note(m.index, `<${m[1]}> is not rewritten: render rich() from @menapo/vi18n in a component (see the rich text guide)`, definedKeys(keypath?.[1] ?? keypath?.[2] ?? null));
|
|
744
|
+
}
|
|
745
|
+
for (const m of text.matchAll(/\sv-t\s*=\s*("[^"]*"|'[^']*')/g)) {
|
|
746
|
+
if (!this.inTemplate(m.index)) continue;
|
|
747
|
+
const value = m[1].slice(1, -1).trim();
|
|
748
|
+
const literal = /^'([^']*)'$|^"([^"]*)"$/.exec(value);
|
|
749
|
+
const keys = definedKeys(literal?.[1] ?? literal?.[2] ?? null);
|
|
750
|
+
this.builder.note(m.index + 1, "v-t is not rewritten: write {{ t(…) }} with the message", keys);
|
|
751
|
+
}
|
|
752
|
+
for (const m of text.matchAll(/^<i18n\b[^>]*>/gm)) {
|
|
753
|
+
if (this.inTemplate(m.index)) continue;
|
|
754
|
+
this.builder.note(m.index, "an <i18n> block holds component-local messages, which were not migrated: move them into the locale catalogs");
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
/** Whether a template offset is inside `{{ }}` or the value of a bound attribute or directive. */
|
|
759
|
+
function inExpression(text, offset, regions) {
|
|
760
|
+
const attribute = regions.attributes.find((a) => a.start <= offset && offset < a.end);
|
|
761
|
+
if (attribute !== void 0) {
|
|
762
|
+
const name = /([^\s"'<>/=]+)\s*=\s*$/.exec(text.slice(Math.max(0, attribute.start - 200), attribute.start - 1))?.[1];
|
|
763
|
+
return name !== void 0 && /^(?::|@|#|v-)/.test(name);
|
|
764
|
+
}
|
|
765
|
+
const open = text.lastIndexOf("{{", offset);
|
|
766
|
+
if (open === -1) return false;
|
|
767
|
+
return text.lastIndexOf("}}", offset) < open;
|
|
768
|
+
}
|
|
769
|
+
/** A call at `start` whose `(` ends at `open`: argument ranges as written. */
|
|
770
|
+
function lexicalCall(text, start, callee, open) {
|
|
771
|
+
const close = skipBalanced(text, open - 1);
|
|
772
|
+
if (close === null) return null;
|
|
773
|
+
const args = [];
|
|
774
|
+
let at = skipSpace(text, open);
|
|
775
|
+
while (at < close - 1) {
|
|
776
|
+
const end = argumentEnd(text, at);
|
|
777
|
+
if (end === null || end > close - 1) return null;
|
|
778
|
+
args.push({
|
|
779
|
+
start: at,
|
|
780
|
+
end
|
|
781
|
+
});
|
|
782
|
+
at = skipSpace(text, end + 1);
|
|
783
|
+
if (text[end] === ")") break;
|
|
784
|
+
}
|
|
785
|
+
return {
|
|
786
|
+
callee,
|
|
787
|
+
calleeStart: start,
|
|
788
|
+
start,
|
|
789
|
+
end: close,
|
|
790
|
+
args
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
/** Splits `a, b, c` at top-level commas. */
|
|
794
|
+
function splitTopLevel(code) {
|
|
795
|
+
const out = [];
|
|
796
|
+
let at = 0;
|
|
797
|
+
while (at < code.length) {
|
|
798
|
+
const end = argumentEnd(code + ")", at);
|
|
799
|
+
if (end === null) break;
|
|
800
|
+
const part = code.slice(at, end).trim();
|
|
801
|
+
if (part !== "") out.push(part);
|
|
802
|
+
at = end + 1;
|
|
803
|
+
}
|
|
804
|
+
return out;
|
|
805
|
+
}
|
|
806
|
+
export { createVueI18nAdapter };
|