@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
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import {
|
|
1
|
+
import { a as DEFAULT_IMPORT_SOURCES, n as isDiagnosticError, t as diagnosticError } from "./diagnostics-1onDF6Ua.js";
|
|
2
|
+
import { d as withoutQuery, g as isValidManualId, n as validateSource } from "./validate-zv4-IZyZ.js";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
|
-
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
5
5
|
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
6
|
-
|
|
6
|
+
// src/core/extract/imports.ts
|
|
7
7
|
const memo = /* @__PURE__ */ new WeakMap();
|
|
8
8
|
function exportedName(node) {
|
|
9
9
|
return node.type === "Literal" ? String(node.value) : node.name;
|
|
@@ -54,8 +54,7 @@ function listImports(parsed) {
|
|
|
54
54
|
memo.set(parsed, records);
|
|
55
55
|
return records;
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
//#region src/core/extract/locate.ts
|
|
57
|
+
// src/core/extract/locate.ts
|
|
59
58
|
/**
|
|
60
59
|
* Positions in original files (INTERNALS §2.5.1). Post-compile offsets of
|
|
61
60
|
* `.vue`/`.svelte`/`.astro`/`.mdx` output mean nothing in the file the user
|
|
@@ -66,12 +65,17 @@ let cachedText = null;
|
|
|
66
65
|
let cachedStarts = [];
|
|
67
66
|
function lineStarts(text) {
|
|
68
67
|
if (text === cachedText) return cachedStarts;
|
|
69
|
-
const starts =
|
|
70
|
-
for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) starts.push(i + 1);
|
|
68
|
+
const starts = lineStartsOf(text);
|
|
71
69
|
cachedText = text;
|
|
72
70
|
cachedStarts = starts;
|
|
73
71
|
return starts;
|
|
74
72
|
}
|
|
73
|
+
/** The offset each line of `text` starts at; lines end at `\n`. */
|
|
74
|
+
function lineStartsOf(text) {
|
|
75
|
+
const starts = [0];
|
|
76
|
+
for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) starts.push(i + 1);
|
|
77
|
+
return starts;
|
|
78
|
+
}
|
|
75
79
|
/**
|
|
76
80
|
* 1-based line and column of a UTF-16 offset. Lines end at `\n` (a `\r`
|
|
77
81
|
* before it belongs to the line it ends); columns count UTF-16 code units, as
|
|
@@ -173,8 +177,195 @@ function locateLiteral(fileText, literal, from = 0) {
|
|
|
173
177
|
offset
|
|
174
178
|
};
|
|
175
179
|
}
|
|
176
|
-
|
|
177
|
-
|
|
180
|
+
// src/core/extract/parser.ts
|
|
181
|
+
/**
|
|
182
|
+
* Parsing for extraction (INTERNALS §2.5.4–§2.5.5). The parser is Oxc as Vite
|
|
183
|
+
* ships it (`parseSync`), so there is no parser dependency of our own (§6 D16)
|
|
184
|
+
* and the CLI and the plugin see the same ESTree. Once raw transfer starts
|
|
185
|
+
* ({@link armRawParsing}, {@link enableRawParsing}), the same binding parses
|
|
186
|
+
* through it (`raw.ts`, §6 D104) when its Oxc version allows: the same tree,
|
|
187
|
+
* faster.
|
|
188
|
+
*/
|
|
189
|
+
let parseSync = null;
|
|
190
|
+
/** The raw-transfer parser, once {@link enableRawParsing} found one usable. */
|
|
191
|
+
let parseRaw = null;
|
|
192
|
+
let rawReady = null;
|
|
193
|
+
function loadParser(filename) {
|
|
194
|
+
if (parseSync !== null) return parseSync;
|
|
195
|
+
let mod;
|
|
196
|
+
try {
|
|
197
|
+
mod = createRequire(import.meta.url)("vite");
|
|
198
|
+
} catch (error) {
|
|
199
|
+
throw diagnosticError({
|
|
200
|
+
code: "extract-parse-error",
|
|
201
|
+
severity: "error",
|
|
202
|
+
message: `Cannot load the parser from "vite" (${error.message}); install vite 8 as a dev dependency`,
|
|
203
|
+
...filename === void 0 ? {} : { file: filename }
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
if (typeof mod.parseSync !== "function") throw diagnosticError({
|
|
207
|
+
code: "extract-parse-error",
|
|
208
|
+
severity: "error",
|
|
209
|
+
message: `The installed "vite" does not export parseSync; install vite 8 or later`,
|
|
210
|
+
...filename === void 0 ? {} : { file: filename }
|
|
211
|
+
});
|
|
212
|
+
parseSync = mod.parseSync;
|
|
213
|
+
return parseSync;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Names of the platform packages a binding loads from. An owner's other
|
|
217
|
+
* optional dependencies are not where its binding lives, and some load a
|
|
218
|
+
* `.node` file of their own (`fsevents`, which Vite+ declares, on macOS).
|
|
219
|
+
*/
|
|
220
|
+
const PLATFORM_PACKAGE = /^(?:@voidzero-dev\/vite-plus|@rolldown\/binding)-/;
|
|
221
|
+
/**
|
|
222
|
+
* {@link BindingOwner} of the `vite` that `from` resolves, or null when it
|
|
223
|
+
* cannot be told for certain. Only what the owner declares counts: a
|
|
224
|
+
* hoisted `rolldown` or `@oxc-project/types` that belongs to another package
|
|
225
|
+
* (Astro's or Nuxt's Rolldown in the same tree) would vouch for a layout
|
|
226
|
+
* the binding in use does not write. The owner must pin
|
|
227
|
+
* `@oxc-project/types` exactly, and the copy it resolves must be that
|
|
228
|
+
* version.
|
|
229
|
+
*/
|
|
230
|
+
function bindingOwner(from = import.meta.url) {
|
|
231
|
+
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) return null;
|
|
232
|
+
try {
|
|
233
|
+
let dir = dirname(realpathSync(createRequire(from).resolve("vite/package.json")));
|
|
234
|
+
let pkg = readPackage(dir);
|
|
235
|
+
if (pkg.dependencies?.["@oxc-project/types"] === void 0) {
|
|
236
|
+
if (pkg.dependencies?.rolldown === void 0) return null;
|
|
237
|
+
dir = dirname(realpathSync(createRequire(join(dir, "package.json")).resolve("rolldown/package.json")));
|
|
238
|
+
pkg = readPackage(dir);
|
|
239
|
+
}
|
|
240
|
+
const pinned = /^=?(\d+\.\d+\.\d+)$/.exec(pkg.dependencies?.["@oxc-project/types"] ?? "")?.[1];
|
|
241
|
+
if (pinned === void 0) return null;
|
|
242
|
+
const ownerRequire = createRequire(join(dir, "package.json"));
|
|
243
|
+
if (ownerRequire("@oxc-project/types/package.json").version !== pinned) return null;
|
|
244
|
+
const dirs = [dir];
|
|
245
|
+
for (const name of Object.keys(pkg.optionalDependencies ?? {})) {
|
|
246
|
+
if (!PLATFORM_PACKAGE.test(name)) continue;
|
|
247
|
+
try {
|
|
248
|
+
dirs.push(dirname(realpathSync(ownerRequire.resolve(`${name}/package.json`))));
|
|
249
|
+
} catch {}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
dirs,
|
|
253
|
+
oxcVersion: pinned
|
|
254
|
+
};
|
|
255
|
+
} catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function readPackage(dir) {
|
|
260
|
+
return JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The native binding of `owner` among the loaded native modules, if it has
|
|
264
|
+
* raw transfer. `vite` exports no handle to it, but loading it put it in
|
|
265
|
+
* the module cache; other bindings sit there too (another Rolldown, the
|
|
266
|
+
* `oxc-parser` devDependency's) and are told apart by where they live, never
|
|
267
|
+
* by load order or file name.
|
|
268
|
+
*/
|
|
269
|
+
function ownBinding(owner, cache = createRequire(import.meta.url).cache) {
|
|
270
|
+
for (const key in cache) {
|
|
271
|
+
if (!key.endsWith(".node")) continue;
|
|
272
|
+
let real;
|
|
273
|
+
try {
|
|
274
|
+
real = realpathSync(key);
|
|
275
|
+
} catch {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (!owner.dirs.some((dir) => real.startsWith(dir + sep))) continue;
|
|
279
|
+
const exports = cache[key]?.exports;
|
|
280
|
+
if (typeof exports?.parseRawSync === "function" && typeof exports.getBufferOffset === "function" && typeof exports.rawTransferSupported === "function") return exports;
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
let parses = 0;
|
|
285
|
+
let rawAfter = Infinity;
|
|
286
|
+
/**
|
|
287
|
+
* Starts raw transfer once {@link RAW_AFTER} (or `after`) modules more have
|
|
288
|
+
* parsed with `parseSync`, without delaying any of them; `after` 0, or
|
|
289
|
+
* `VI18N_RAW_PARSE=1`, starts it now. Idempotent.
|
|
290
|
+
*/
|
|
291
|
+
function armRawParsing(after = 500) {
|
|
292
|
+
if (rawReady !== null || process.env.VI18N_RAW_PARSE === "0") return;
|
|
293
|
+
if (process.env.VI18N_RAW_PARSE === "1") after = 0;
|
|
294
|
+
if (after > 0) rawAfter = Math.min(rawAfter, parses + after);
|
|
295
|
+
else startRaw();
|
|
296
|
+
}
|
|
297
|
+
function startRaw() {
|
|
298
|
+
rawReady ??= (async () => {
|
|
299
|
+
if (process.env.VI18N_RAW_PARSE === "0") return false;
|
|
300
|
+
try {
|
|
301
|
+
const sync = loadParser(void 0);
|
|
302
|
+
const owner = bindingOwner();
|
|
303
|
+
const binding = owner === null ? null : ownBinding(owner);
|
|
304
|
+
if (owner === null || binding === null) return false;
|
|
305
|
+
const raw = await import("./raw-YsYZMS9n.js");
|
|
306
|
+
if (!raw.LAYOUT_VERSIONS.has(owner.oxcVersion)) return false;
|
|
307
|
+
parseRaw = raw.createRawParser(binding, sync);
|
|
308
|
+
return parseRaw !== null;
|
|
309
|
+
} catch {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
})();
|
|
313
|
+
return rawReady;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The parser language for a module id, by the extension of its path (query
|
|
317
|
+
* and `\0` prefix ignored). Framework files (`.vue`, `.svelte`, `.astro`,
|
|
318
|
+
* `.mdx`) reach the post pass as plain JS.
|
|
319
|
+
*/
|
|
320
|
+
function langFromId(id) {
|
|
321
|
+
const query = id.indexOf("?");
|
|
322
|
+
const end = query === -1 ? id.length : query;
|
|
323
|
+
const dot = id.lastIndexOf(".", end);
|
|
324
|
+
if (dot === -1 || id.lastIndexOf("/", end) > dot) return "js";
|
|
325
|
+
switch (id.slice(dot + 1, end).toLowerCase()) {
|
|
326
|
+
case "ts":
|
|
327
|
+
case "mts":
|
|
328
|
+
case "cts": return "ts";
|
|
329
|
+
case "tsx": return "tsx";
|
|
330
|
+
case "jsx": return "jsx";
|
|
331
|
+
default: return "js";
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Parses `code` as an ES module. Throws a `DiagnosticError`
|
|
336
|
+
* (`extract-parse-error`) on the first syntax error: a partially recovered
|
|
337
|
+
* tree could make the rewriter edit the wrong ranges.
|
|
338
|
+
*/
|
|
339
|
+
function parseModule(code, lang, filename) {
|
|
340
|
+
if (parseRaw === null && ++parses >= rawAfter) {
|
|
341
|
+
rawAfter = Infinity;
|
|
342
|
+
startRaw();
|
|
343
|
+
}
|
|
344
|
+
const result = (parseRaw ?? loadParser(filename))(filename ?? `module.${lang}`, code, {
|
|
345
|
+
lang: lang === "js" ? "jsx" : lang,
|
|
346
|
+
sourceType: "module",
|
|
347
|
+
preserveParens: false
|
|
348
|
+
});
|
|
349
|
+
const first = result.errors[0];
|
|
350
|
+
if (first !== void 0) {
|
|
351
|
+
const offset = first.labels?.[0]?.start;
|
|
352
|
+
throw diagnosticError({
|
|
353
|
+
code: "extract-parse-error",
|
|
354
|
+
severity: "error",
|
|
355
|
+
message: `Cannot parse${filename === void 0 ? "" : ` ${filename}`}: ${first.message}`,
|
|
356
|
+
...filename === void 0 ? {} : { file: filename },
|
|
357
|
+
...offset === void 0 ? {} : lineColumn(code, offset)
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
const parsed = {
|
|
361
|
+
code,
|
|
362
|
+
lang,
|
|
363
|
+
program: result.program
|
|
364
|
+
};
|
|
365
|
+
if (result.comments !== void 0) parsed.comments = result.comments;
|
|
366
|
+
return parsed;
|
|
367
|
+
}
|
|
368
|
+
// src/core/extract/estree.ts
|
|
178
369
|
const WRAPPERS = /* @__PURE__ */ new Set([
|
|
179
370
|
"TSAsExpression",
|
|
180
371
|
"TSSatisfiesExpression",
|
|
@@ -183,6 +374,15 @@ const WRAPPERS = /* @__PURE__ */ new Set([
|
|
|
183
374
|
"TSInstantiationExpression",
|
|
184
375
|
"ParenthesizedExpression"
|
|
185
376
|
]);
|
|
377
|
+
/** Keys that hold only TypeScript types; nothing under them runs. Every AST walk skips them. */
|
|
378
|
+
const TYPE_KEYS = /* @__PURE__ */ new Set([
|
|
379
|
+
"typeAnnotation",
|
|
380
|
+
"typeParameters",
|
|
381
|
+
"typeArguments",
|
|
382
|
+
"superTypeArguments",
|
|
383
|
+
"returnType",
|
|
384
|
+
"implements"
|
|
385
|
+
]);
|
|
186
386
|
/** Strips parentheses and TypeScript-only wrappers, which change nothing at runtime. */
|
|
187
387
|
function unwrap(node) {
|
|
188
388
|
let current = node;
|
|
@@ -195,97 +395,114 @@ function keyName(key, computed) {
|
|
|
195
395
|
if (key.type === "Literal" && typeof key.value === "string") return key.value;
|
|
196
396
|
return null;
|
|
197
397
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return null;
|
|
398
|
+
/** True when `value` is a node, not a primitive, an array or another object of the tree. */
|
|
399
|
+
function isNode(value) {
|
|
400
|
+
return value !== null && typeof value.type === "string";
|
|
201
401
|
}
|
|
202
402
|
/**
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
* `dangerouslySetInnerHTML: { __html: X }` (compiled or as a JSX attribute),
|
|
206
|
-
* Solid's `innerHTML={X}`, Astro `$$unescapeHTML(X)` and `x.innerHTML = X`.
|
|
207
|
-
* Only direct flows count: a value built from `X` is not `X`.
|
|
403
|
+
* Pushes the children of `node` that are nodes ({@link TYPE_KEYS} excluded),
|
|
404
|
+
* in source order: the iterative walkers pop them from a stack.
|
|
208
405
|
*/
|
|
209
|
-
function
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (value.type === "ObjectExpression") {
|
|
218
|
-
const html = propertyValue(value, "__html");
|
|
219
|
-
if (html !== null) mark(html);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return;
|
|
406
|
+
function childNodes(node, into) {
|
|
407
|
+
for (const key in node) {
|
|
408
|
+
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS.has(key)) continue;
|
|
409
|
+
const value = node[key];
|
|
410
|
+
if (value === null || typeof value !== "object") continue;
|
|
411
|
+
if (!Array.isArray(value)) {
|
|
412
|
+
if (isNode(value)) into.push(value);
|
|
413
|
+
continue;
|
|
223
414
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
415
|
+
for (const item of value) if (isNode(item)) into.push(item);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/** Calls `name` with every name a binding pattern declares (`{ a, b: [c], ...d } = …`). */
|
|
419
|
+
function forEachBindingName(pattern, name) {
|
|
420
|
+
if (pattern === null || pattern === void 0) return;
|
|
421
|
+
switch (pattern.type) {
|
|
422
|
+
case "Identifier":
|
|
423
|
+
name(pattern.name);
|
|
234
424
|
return;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const left = node.left;
|
|
238
|
-
if ((node.operator === "=" || node.operator === "+=") && left.type === "MemberExpression" && keyName(left.property, left.computed === true) === "innerHTML") mark(unwrap(node.right));
|
|
425
|
+
case "ObjectPattern":
|
|
426
|
+
for (const property of pattern.properties) forEachBindingName(property.type === "RestElement" ? property.argument : property.value, name);
|
|
239
427
|
return;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
428
|
+
case "ArrayPattern":
|
|
429
|
+
for (const element of pattern.elements) forEachBindingName(element, name);
|
|
430
|
+
return;
|
|
431
|
+
case "AssignmentPattern":
|
|
432
|
+
forEachBindingName(pattern.left, name);
|
|
433
|
+
return;
|
|
434
|
+
case "RestElement":
|
|
435
|
+
forEachBindingName(pattern.argument, name);
|
|
436
|
+
return;
|
|
437
|
+
case "TSParameterProperty":
|
|
438
|
+
forEachBindingName(pattern.parameter, name);
|
|
251
439
|
return;
|
|
252
|
-
}
|
|
253
440
|
}
|
|
254
441
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
return
|
|
258
|
-
code: "extract-module-scope",
|
|
259
|
-
severity,
|
|
260
|
-
message: `${callee}() runs at module scope, so its text freezes at import and has no request on the server; call it inside a function or component, or declare the message with msg()`,
|
|
261
|
-
...where
|
|
262
|
-
};
|
|
442
|
+
function isWhitespace(c, code, i) {
|
|
443
|
+
if (c === 32 || c >= 9 && c <= 13) return true;
|
|
444
|
+
return c > 127 && /\s/.test(code[i]);
|
|
263
445
|
}
|
|
264
|
-
/**
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
446
|
+
/**
|
|
447
|
+
* Offset of the first character after whitespace and comments, from `at`,
|
|
448
|
+
* never past `limit`. Callers pass ranges between tokens of parsed code, so
|
|
449
|
+
* every comment there is closed.
|
|
450
|
+
*/
|
|
451
|
+
function skipTrivia(code, at, limit) {
|
|
452
|
+
let i = at;
|
|
453
|
+
while (i < limit) {
|
|
454
|
+
const c = code.charCodeAt(i);
|
|
455
|
+
if (c === 47 && code.charCodeAt(i + 1) === 42) {
|
|
456
|
+
const close = code.indexOf("*/", i + 2);
|
|
457
|
+
i = close === -1 ? limit : close + 2;
|
|
458
|
+
} else if (c === 47 && code.charCodeAt(i + 1) === 47) {
|
|
459
|
+
const close = code.indexOf("\n", i + 2);
|
|
460
|
+
i = close === -1 ? limit : close + 1;
|
|
461
|
+
} else if (isWhitespace(c, code, i)) i++;
|
|
462
|
+
else break;
|
|
463
|
+
}
|
|
464
|
+
return i;
|
|
273
465
|
}
|
|
274
|
-
/**
|
|
275
|
-
function
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
message: `This module imports from the wrapper ${JSON.stringify(specifier)} but makes no vi18n call; add the wrapper's translation functions to extraction.aliases`,
|
|
280
|
-
...where
|
|
281
|
-
};
|
|
466
|
+
/** The leading directives of a statement list (`"use client"`, `"use no memo"`). */
|
|
467
|
+
function directives(statements) {
|
|
468
|
+
let n = 0;
|
|
469
|
+
while (n < statements.length && typeof statements[n].directive === "string") n++;
|
|
470
|
+
return statements.slice(0, n);
|
|
282
471
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
472
|
+
/** What goes between a directive and code inserted after it: a directive without `;` followed on its line by code would not parse. */
|
|
473
|
+
function afterDirective(code, directive) {
|
|
474
|
+
return code.charCodeAt(directive.end - 1) === 59 ? "" : ";";
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Inserts an import `statement` where one may go: after the directive
|
|
478
|
+
* prologue, after a hashbang, or first. `ownLine` gives it a line of its
|
|
479
|
+
* own; without it, every line keeps its number (the CLI scans the edited
|
|
480
|
+
* text with the file's positions).
|
|
481
|
+
*/
|
|
482
|
+
function insertImport(sink, parsed, statement, ownLine) {
|
|
483
|
+
const { code } = parsed;
|
|
484
|
+
const program = parsed.program;
|
|
485
|
+
const newline = ownLine ? "\n" : "";
|
|
486
|
+
const last = directives(program.body).at(-1);
|
|
487
|
+
if (last !== void 0) {
|
|
488
|
+
sink.appendLeft(last.end, `${afterDirective(code, last)}${newline}${statement}`);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const hashbang = program.hashbang;
|
|
492
|
+
if (hashbang === null || hashbang === void 0) sink.prepend(`${statement}${newline}`);
|
|
493
|
+
else if (ownLine) sink.appendLeft(hashbang.end, `\n${statement}`);
|
|
494
|
+
else {
|
|
495
|
+
const lineEnd = code.indexOf("\n", hashbang.end);
|
|
496
|
+
if (lineEnd === -1) sink.appendLeft(code.length, `\n${statement}`);
|
|
497
|
+
else sink.appendLeft(lineEnd + 1, statement);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// src/core/extract/comments.ts
|
|
501
|
+
/** Every module without markers shares this one, so it is frozen. */
|
|
502
|
+
const NONE = Object.freeze({
|
|
503
|
+
ignored: Object.freeze([]),
|
|
504
|
+
contexts: Object.freeze([])
|
|
505
|
+
});
|
|
289
506
|
/** Keys that are not children: positions, and the back links ESLint and oxlint add. */
|
|
290
507
|
const NOT_CHILDREN = /* @__PURE__ */ new Set([
|
|
291
508
|
"type",
|
|
@@ -315,10 +532,13 @@ function markerOf(comment) {
|
|
|
315
532
|
let text = comment.value;
|
|
316
533
|
if (comment.type === "Block") text = text.split("\n").map((line) => line.replace(/^\s*\*?/, "")).join("\n");
|
|
317
534
|
text = text.trimStart();
|
|
318
|
-
if (IGNORE.test(text)) return {
|
|
535
|
+
if (IGNORE.test(text)) return { kind: "ignore" };
|
|
319
536
|
if (!text.startsWith(CONTEXT)) return null;
|
|
320
537
|
const note = text.slice(14).split("\n").map((line) => line.trim()).filter((line) => line !== "").join("\n");
|
|
321
|
-
return note === "" ? null : {
|
|
538
|
+
return note === "" ? null : {
|
|
539
|
+
kind: "context",
|
|
540
|
+
note
|
|
541
|
+
};
|
|
322
542
|
}
|
|
323
543
|
/** The child of `node` whose range holds `offset`, or null. */
|
|
324
544
|
function childAt(node, offset) {
|
|
@@ -382,10 +602,8 @@ function trails(code, start, end) {
|
|
|
382
602
|
function findMarkers(code, program, comments) {
|
|
383
603
|
if (!hasMarkers(code)) return NONE;
|
|
384
604
|
const spans = comments.map(span).sort((a, b) => a.start - b.start);
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
contexts: []
|
|
388
|
-
};
|
|
605
|
+
const ignored = [];
|
|
606
|
+
const contexts = [];
|
|
389
607
|
for (const comment of comments) {
|
|
390
608
|
const marker = markerOf(comment);
|
|
391
609
|
if (marker === null) continue;
|
|
@@ -396,13 +614,16 @@ function findMarkers(code, program, comments) {
|
|
|
396
614
|
if (found.at === null && container.type === "JSXExpressionContainer" && container.expression?.type === "JSXEmptyExpression") found = nodeAt(program, nextToken(code, span(container).end, spans));
|
|
397
615
|
if (found.at === null) continue;
|
|
398
616
|
const target = span(found.at);
|
|
399
|
-
if (marker.
|
|
400
|
-
else
|
|
617
|
+
if (marker.kind === "ignore") ignored.push([target.start, target.end]);
|
|
618
|
+
else contexts.push({
|
|
401
619
|
note: marker.note,
|
|
402
620
|
...target
|
|
403
621
|
});
|
|
404
622
|
}
|
|
405
|
-
return
|
|
623
|
+
return {
|
|
624
|
+
ignored,
|
|
625
|
+
contexts
|
|
626
|
+
};
|
|
406
627
|
}
|
|
407
628
|
/** Whether `offset` lies in code a `vi18n-ignore` covers. */
|
|
408
629
|
function isIgnored(markers, offset) {
|
|
@@ -420,8 +641,103 @@ function markersOf(parsed) {
|
|
|
420
641
|
}
|
|
421
642
|
return markers;
|
|
422
643
|
}
|
|
423
|
-
|
|
424
|
-
|
|
644
|
+
// src/core/extract/names.ts
|
|
645
|
+
/**
|
|
646
|
+
* Names the core passes write into user modules and the plugin layer reads
|
|
647
|
+
* back. Core owns them, so the hook pass, the scanner and the rewriter cannot
|
|
648
|
+
* drift from `src/vite/types.ts`, which re-exports them.
|
|
649
|
+
*/
|
|
650
|
+
/** The hook the glue module exports for in-place locale switching (`HELPER.useT`). */
|
|
651
|
+
const USE_T = "__vi18nUseT";
|
|
652
|
+
/** Prefix of the generated glue modules, + framework (`VIRTUAL.glue`). */
|
|
653
|
+
const GLUE = "virtual:vi18n/glue/";
|
|
654
|
+
/** Left in `preload()` calls for `renderChunk` to replace with bundle keys (`MARKER.keys`). */
|
|
655
|
+
const KEYS_MARKER = "__VI18N_KEYS__";
|
|
656
|
+
// src/core/extract/sinks.ts
|
|
657
|
+
function propertyValue(objectNode, name) {
|
|
658
|
+
for (const property of objectNode.properties) if (property.type === "Property" && keyName(property.key, property.computed === true) === name) return unwrap(property.value);
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Calls `mark` with every expression that `node` inserts as HTML: Svelte
|
|
663
|
+
* `$.html(…, () => X)`, Vue `innerHTML: X`, React
|
|
664
|
+
* `dangerouslySetInnerHTML: { __html: X }` (compiled or as a JSX attribute),
|
|
665
|
+
* Solid's `innerHTML={X}`, Astro `$$unescapeHTML(X)` and `x.innerHTML = X`.
|
|
666
|
+
* Only direct flows count: a value built from `X` is not `X`.
|
|
667
|
+
*/
|
|
668
|
+
function markHtmlSinks(node, mark) {
|
|
669
|
+
switch (node.type) {
|
|
670
|
+
case "Property":
|
|
671
|
+
if (node.kind === "init" && node.method !== true) markProp(keyName(node.key, node.computed === true), node.value, mark);
|
|
672
|
+
return;
|
|
673
|
+
case "JSXAttribute": {
|
|
674
|
+
const value = node.value;
|
|
675
|
+
if (value?.type !== "JSXExpressionContainer") return;
|
|
676
|
+
markProp(keyName(node.name, false), value.expression, mark);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
case "AssignmentExpression": {
|
|
680
|
+
const left = node.left;
|
|
681
|
+
if ((node.operator === "=" || node.operator === "+=") && left.type === "MemberExpression" && keyName(left.property, left.computed === true) === "innerHTML") mark(unwrap(node.right));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
case "CallExpression":
|
|
685
|
+
markCallSink(node, mark);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
/** `innerHTML: X` and `dangerouslySetInnerHTML: { __html: X }`, as a property or a JSX attribute. */
|
|
690
|
+
function markProp(name, value, mark) {
|
|
691
|
+
if (name === "innerHTML") {
|
|
692
|
+
mark(unwrap(value));
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
if (name !== "dangerouslySetInnerHTML") return;
|
|
696
|
+
const object = unwrap(value);
|
|
697
|
+
if (object.type !== "ObjectExpression") return;
|
|
698
|
+
const html = propertyValue(object, "__html");
|
|
699
|
+
if (html !== null) mark(html);
|
|
700
|
+
}
|
|
701
|
+
/** Astro's `$$unescapeHTML(X)` and Svelte's `$.html(…, () => X)`. */
|
|
702
|
+
function markCallSink(node, mark) {
|
|
703
|
+
const callee = unwrap(node.callee);
|
|
704
|
+
const args = node.arguments;
|
|
705
|
+
if (callee.type === "Identifier" && callee.name === "$$unescapeHTML") {
|
|
706
|
+
if (args[0] !== void 0) mark(unwrap(args[0]));
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (callee.type !== "MemberExpression" || callee.computed === true || callee.object.type !== "Identifier" || callee.object.name !== "$" || callee.property.name !== "html") return;
|
|
710
|
+
for (const arg of args) if (arg.type === "ArrowFunctionExpression" && arg.expression === true) mark(unwrap(arg.body));
|
|
711
|
+
}
|
|
712
|
+
/** `extract-module-scope`; `severity` follows `strict.moduleScope`. */
|
|
713
|
+
function moduleScopeDiagnostic(callee, severity, where) {
|
|
714
|
+
return {
|
|
715
|
+
code: "extract-module-scope",
|
|
716
|
+
severity,
|
|
717
|
+
message: `${callee}() runs at module scope, so its text freezes at import and has no request on the server; call it inside a function or component, or declare the message with msg()`,
|
|
718
|
+
...where
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
/** `extract-html-sink`: `t()` does not escape values, `richHtml()` does. */
|
|
722
|
+
function htmlSinkDiagnostic(callee, id, where) {
|
|
723
|
+
return {
|
|
724
|
+
code: "extract-html-sink",
|
|
725
|
+
severity: "warning",
|
|
726
|
+
message: `${callee}() returns plain text with unescaped values but is inserted as HTML here; use richHtml(), which escapes values`,
|
|
727
|
+
id,
|
|
728
|
+
...where
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
/** `extract-wrapper-unused`: usually a wrapper export missing from `extraction.aliases`. */
|
|
732
|
+
function wrapperUnusedDiagnostic(specifier, where) {
|
|
733
|
+
return {
|
|
734
|
+
code: "extract-wrapper-unused",
|
|
735
|
+
severity: "warning",
|
|
736
|
+
message: `This module imports from the wrapper ${JSON.stringify(specifier)} but makes no vi18n call; add the wrapper's translation functions to extraction.aliases`,
|
|
737
|
+
...where
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
// src/core/extract/zod.ts
|
|
425
741
|
/**
|
|
426
742
|
* The messages of the zod bridge (`vi18nZod()`, INTERNALS §6 D206). The
|
|
427
743
|
* scanner treats a `vi18nZod()` call as a `msg()` of each, in this order, and
|
|
@@ -464,8 +780,7 @@ const ZOD_MESSAGES = [
|
|
|
464
780
|
["{count, plural, one {Unrecognized key: {keys}} other {Unrecognized keys: {keys}}}", "Validation error: an object has keys it must not have"],
|
|
465
781
|
["Invalid value", "Validation error"]
|
|
466
782
|
];
|
|
467
|
-
|
|
468
|
-
//#region src/core/extract/scan.ts
|
|
783
|
+
// src/core/extract/scan.ts
|
|
469
784
|
/**
|
|
470
785
|
* The extraction scanner (INTERNALS §2.4, §2.5): finds vi18n calls by import
|
|
471
786
|
* binding in one walk over a parsed module, turns literal messages into
|
|
@@ -505,12 +820,11 @@ const TRANSLATOR_METHODS = new Map([
|
|
|
505
820
|
role: "translator",
|
|
506
821
|
api: name
|
|
507
822
|
}]));
|
|
508
|
-
/**
|
|
509
|
-
const
|
|
510
|
-
const HOOK_SPECIFIERS = /* @__PURE__ */ new Set(["virtual:vi18n/glue/react", "virtual:vi18n/glue/preact"]);
|
|
823
|
+
/** The glue modules whose `__vi18nUseT()` the hook pass injects. */
|
|
824
|
+
const HOOK_SPECIFIERS = new Set(["react", "preact"].map((framework) => GLUE + framework));
|
|
511
825
|
/** Import sources that are vi18n itself or generated glue, never a user wrapper. */
|
|
512
826
|
function isWrapperSpecifier(specifier) {
|
|
513
|
-
return specifier
|
|
827
|
+
return !DEFAULT_IMPORT_SOURCES.includes(specifier) && !specifier.startsWith("virtual:vi18n/");
|
|
514
828
|
}
|
|
515
829
|
const GLOBAL = {
|
|
516
830
|
role: "global",
|
|
@@ -545,6 +859,20 @@ const COMPONENT_PROPS = /* @__PURE__ */ new Set([
|
|
|
545
859
|
"context",
|
|
546
860
|
"maxLength"
|
|
547
861
|
]);
|
|
862
|
+
/** A `<T>` prop that extraction reads ({@link COMPONENT_PROPS}), written as a plain key. */
|
|
863
|
+
function isComponentProp(property) {
|
|
864
|
+
if (property.type !== "Property" || property.computed === true) return false;
|
|
865
|
+
const key = keyName(property.key, false);
|
|
866
|
+
return key !== null && COMPONENT_PROPS.has(key);
|
|
867
|
+
}
|
|
868
|
+
/** The value of a `<T>`'s `message` prop. Svelte passes a prop that reads state as a getter: `get message() { return … }`. */
|
|
869
|
+
function componentMessage(property) {
|
|
870
|
+
const value = unwrap(property.value);
|
|
871
|
+
if (property.kind !== "get") return value;
|
|
872
|
+
const body = value.body?.body;
|
|
873
|
+
const returned = body?.length === 1 && body[0]?.type === "ReturnStatement" ? body[0] : null;
|
|
874
|
+
return returned?.argument ? unwrap(returned.argument) : value;
|
|
875
|
+
}
|
|
548
876
|
/** Vue's compiled templates reach `<script setup>` bindings through these render parameters. */
|
|
549
877
|
const VUE_CONTEXTS = /* @__PURE__ */ new Set(["$setup", "_ctx"]);
|
|
550
878
|
/** §2.4: calls of these on a message argument build a string at runtime. */
|
|
@@ -573,15 +901,6 @@ const STRING_METHODS = /* @__PURE__ */ new Set([
|
|
|
573
901
|
"join",
|
|
574
902
|
"toString"
|
|
575
903
|
]);
|
|
576
|
-
/** Keys that hold only TypeScript types; nothing under them runs. */
|
|
577
|
-
const TYPE_KEYS = /* @__PURE__ */ new Set([
|
|
578
|
-
"typeAnnotation",
|
|
579
|
-
"typeParameters",
|
|
580
|
-
"typeArguments",
|
|
581
|
-
"superTypeArguments",
|
|
582
|
-
"returnType",
|
|
583
|
-
"implements"
|
|
584
|
-
]);
|
|
585
904
|
/** Statements with nothing to scan: type-only declarations, and imports and star re-exports (read by `listImports`). */
|
|
586
905
|
const TYPE_ONLY = /* @__PURE__ */ new Set([
|
|
587
906
|
"TSTypeAliasDeclaration",
|
|
@@ -708,6 +1027,17 @@ function nameSet(names) {
|
|
|
708
1027
|
if (set === void 0) NAME_SETS.set(names, set = new Set(names));
|
|
709
1028
|
return set;
|
|
710
1029
|
}
|
|
1030
|
+
function invalidOption(what, at) {
|
|
1031
|
+
return {
|
|
1032
|
+
ok: false,
|
|
1033
|
+
code: "extract-non-literal-option",
|
|
1034
|
+
message: `A message descriptor takes only literal id, message, context and maxLength properties: ${what}`,
|
|
1035
|
+
node: at
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
function isPositiveInteger(node) {
|
|
1039
|
+
return node.type === "Literal" && typeof node.value === "number" && Number.isInteger(node.value) && node.value > 0;
|
|
1040
|
+
}
|
|
711
1041
|
const PASS = null;
|
|
712
1042
|
const NO_DIAGNOSTICS = [];
|
|
713
1043
|
/**
|
|
@@ -772,23 +1102,17 @@ function textInit(init) {
|
|
|
772
1102
|
/** Offset after `from` and any whitespace, comments and `)` that follow it, never past `limit`. */
|
|
773
1103
|
function afterParens(code, from, limit) {
|
|
774
1104
|
let at = from;
|
|
775
|
-
let i = from;
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
if (ch === 41) at = ++i;
|
|
779
|
-
else if (ch === 47 && code[i + 1] === "*") {
|
|
780
|
-
const close = code.indexOf("*/", i + 2);
|
|
781
|
-
if (close === -1) break;
|
|
782
|
-
i = close + 2;
|
|
783
|
-
} else if (ch === 47 && code[i + 1] === "/") {
|
|
784
|
-
const close = code.indexOf("\n", i + 2);
|
|
785
|
-
if (close === -1) break;
|
|
786
|
-
i = close + 1;
|
|
787
|
-
} else if (/\s/.test(code[i])) i++;
|
|
788
|
-
else break;
|
|
1105
|
+
for (let i = skipTrivia(code, from, limit); i < limit && code.charCodeAt(i) === 41;) {
|
|
1106
|
+
at = i + 1;
|
|
1107
|
+
i = skipTrivia(code, at, limit);
|
|
789
1108
|
}
|
|
790
1109
|
return at;
|
|
791
1110
|
}
|
|
1111
|
+
/** Where a call's argument list starts: after the callee and any type arguments. */
|
|
1112
|
+
function argsStart(call) {
|
|
1113
|
+
const typeArguments = call.typeArguments;
|
|
1114
|
+
return Math.max(call.callee.end, typeArguments?.end ?? 0);
|
|
1115
|
+
}
|
|
792
1116
|
const FUNCTIONS = /* @__PURE__ */ new Set([
|
|
793
1117
|
"FunctionExpression",
|
|
794
1118
|
"ArrowFunctionExpression",
|
|
@@ -799,15 +1123,7 @@ function suspends(nodes) {
|
|
|
799
1123
|
const stack = [...nodes];
|
|
800
1124
|
for (let node = stack.pop(); node !== void 0; node = stack.pop()) {
|
|
801
1125
|
if (node.type === "AwaitExpression" || node.type === "YieldExpression") return true;
|
|
802
|
-
if (FUNCTIONS.has(node.type))
|
|
803
|
-
for (const key in node) {
|
|
804
|
-
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS.has(key)) continue;
|
|
805
|
-
const value = node[key];
|
|
806
|
-
if (value === null || typeof value !== "object") continue;
|
|
807
|
-
if (Array.isArray(value)) {
|
|
808
|
-
for (const item of value) if (item !== null && typeof item.type === "string") stack.push(item);
|
|
809
|
-
} else if (typeof value.type === "string") stack.push(value);
|
|
810
|
-
}
|
|
1126
|
+
if (!FUNCTIONS.has(node.type)) childNodes(node, stack);
|
|
811
1127
|
}
|
|
812
1128
|
return false;
|
|
813
1129
|
}
|
|
@@ -850,7 +1166,7 @@ var Scanner = class {
|
|
|
850
1166
|
withLocaleDepth = 0;
|
|
851
1167
|
/** Start of the innermost expression statement or concise arrow body. */
|
|
852
1168
|
leftEdge = -1;
|
|
853
|
-
/** The left edge starts a statement of a statement list (
|
|
1169
|
+
/** The left edge starts a statement of a statement list (`MessageSite.asi`). */
|
|
854
1170
|
leftEdgeAsi = false;
|
|
855
1171
|
/** The statement a statement list is visiting, so an expression statement knows it sits in one. */
|
|
856
1172
|
listStatement = null;
|
|
@@ -935,7 +1251,7 @@ var Scanner = class {
|
|
|
935
1251
|
return COMPONENT;
|
|
936
1252
|
}
|
|
937
1253
|
if (this.aliases.has(name)) return ALIAS;
|
|
938
|
-
if (name ===
|
|
1254
|
+
if (name === "__vi18nUseT" && HOOK_SPECIFIERS.has(specifier)) return USE_T_BINDING;
|
|
939
1255
|
return null;
|
|
940
1256
|
}
|
|
941
1257
|
/** True when the module has anything a walk could find. */
|
|
@@ -1028,21 +1344,13 @@ var Scanner = class {
|
|
|
1028
1344
|
if (binding !== null) this.interesting.add(name);
|
|
1029
1345
|
return map;
|
|
1030
1346
|
}
|
|
1347
|
+
/** Declares every name `pattern` binds, as shadowing (null) unless already {@link interesting}. */
|
|
1031
1348
|
declarePattern(vars, pattern) {
|
|
1032
|
-
if (pattern ===
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
return vars;
|
|
1038
|
-
case "ArrayPattern":
|
|
1039
|
-
for (const element of pattern.elements) vars = this.declarePattern(vars, element);
|
|
1040
|
-
return vars;
|
|
1041
|
-
case "AssignmentPattern": return this.declarePattern(vars, pattern.left);
|
|
1042
|
-
case "RestElement": return this.declarePattern(vars, pattern.argument);
|
|
1043
|
-
case "TSParameterProperty": return this.declarePattern(vars, pattern.parameter);
|
|
1044
|
-
default: return vars;
|
|
1045
|
-
}
|
|
1349
|
+
if (pattern?.type === "Identifier") return this.bind(vars, pattern.name, null);
|
|
1350
|
+
forEachBindingName(pattern, (name) => {
|
|
1351
|
+
vars = this.bind(vars, name, null);
|
|
1352
|
+
});
|
|
1353
|
+
return vars;
|
|
1046
1354
|
}
|
|
1047
1355
|
/** The role of `init` when it creates a translator or a hook local, else null. */
|
|
1048
1356
|
initRole(init) {
|
|
@@ -1185,7 +1493,7 @@ var Scanner = class {
|
|
|
1185
1493
|
this.settle(this.lexical(this.hoistVars(this.moduleVars, body), body));
|
|
1186
1494
|
this.visitStatements(body);
|
|
1187
1495
|
}
|
|
1188
|
-
/** A statement list: its expression statements may need
|
|
1496
|
+
/** A statement list: its expression statements may need `MessageSite.asi`. */
|
|
1189
1497
|
visitStatements(statements) {
|
|
1190
1498
|
for (const statement of statements) {
|
|
1191
1499
|
this.listStatement = statement;
|
|
@@ -1214,36 +1522,15 @@ var Scanner = class {
|
|
|
1214
1522
|
case "ForOfStatement":
|
|
1215
1523
|
this.visitFor(node);
|
|
1216
1524
|
return;
|
|
1217
|
-
case "CatchClause":
|
|
1218
|
-
|
|
1219
|
-
const saved = this.enter(this.declarePattern(null, param));
|
|
1220
|
-
if (param !== null) this.visit(param);
|
|
1221
|
-
this.visitBlock(node.body);
|
|
1222
|
-
this.scope = saved;
|
|
1525
|
+
case "CatchClause":
|
|
1526
|
+
this.visitCatch(node);
|
|
1223
1527
|
return;
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
this.visit(node.discriminant);
|
|
1227
|
-
const cases = node.cases;
|
|
1228
|
-
let vars = null;
|
|
1229
|
-
for (const switchCase of cases) vars = this.lexical(vars, switchCase.consequent);
|
|
1230
|
-
const saved = this.enter(this.settle(vars));
|
|
1231
|
-
for (const switchCase of cases) {
|
|
1232
|
-
if (switchCase.test !== null) this.visit(switchCase.test);
|
|
1233
|
-
this.visitStatements(switchCase.consequent);
|
|
1234
|
-
}
|
|
1235
|
-
this.scope = saved;
|
|
1528
|
+
case "SwitchStatement":
|
|
1529
|
+
this.visitSwitch(node);
|
|
1236
1530
|
return;
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
const id = node.id;
|
|
1240
|
-
const saved = this.enter(id === null ? null : this.bind(null, id.name, null));
|
|
1241
|
-
this.visitAll(node.decorators ?? []);
|
|
1242
|
-
if (node.superClass !== null) this.visit(node.superClass);
|
|
1243
|
-
this.visit(node.body);
|
|
1244
|
-
this.scope = saved;
|
|
1531
|
+
case "ClassExpression":
|
|
1532
|
+
this.visitClassExpression(node);
|
|
1245
1533
|
return;
|
|
1246
|
-
}
|
|
1247
1534
|
case "ExpressionStatement": {
|
|
1248
1535
|
const saved = this.leftEdge;
|
|
1249
1536
|
const savedAsi = this.leftEdgeAsi;
|
|
@@ -1257,26 +1544,10 @@ var Scanner = class {
|
|
|
1257
1544
|
case "MemberExpression":
|
|
1258
1545
|
this.visitChain(node);
|
|
1259
1546
|
return;
|
|
1260
|
-
case "IfStatement":
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
this.visit(current.test);
|
|
1264
|
-
this.visit(current.consequent);
|
|
1265
|
-
current = current.alternate;
|
|
1266
|
-
}
|
|
1267
|
-
if (current !== null) this.visit(current);
|
|
1268
|
-
return;
|
|
1269
|
-
}
|
|
1270
|
-
case "ConditionalExpression": {
|
|
1271
|
-
let current = node;
|
|
1272
|
-
while (current.type === "ConditionalExpression") {
|
|
1273
|
-
this.visit(current.test);
|
|
1274
|
-
this.visit(current.consequent);
|
|
1275
|
-
current = current.alternate;
|
|
1276
|
-
}
|
|
1277
|
-
this.visit(current);
|
|
1547
|
+
case "IfStatement":
|
|
1548
|
+
case "ConditionalExpression":
|
|
1549
|
+
this.visitBranches(node);
|
|
1278
1550
|
return;
|
|
1279
|
-
}
|
|
1280
1551
|
case "Property":
|
|
1281
1552
|
case "MethodDefinition":
|
|
1282
1553
|
case "PropertyDefinition":
|
|
@@ -1291,36 +1562,81 @@ var Scanner = class {
|
|
|
1291
1562
|
if (this.options.warnings) markHtmlSinks(node, this.markSink);
|
|
1292
1563
|
break;
|
|
1293
1564
|
case "BinaryExpression":
|
|
1294
|
-
case "LogicalExpression":
|
|
1295
|
-
|
|
1296
|
-
let current = node;
|
|
1297
|
-
while (current.type === "BinaryExpression" || current.type === "LogicalExpression") {
|
|
1298
|
-
rights.push(current.right);
|
|
1299
|
-
current = current.left;
|
|
1300
|
-
}
|
|
1301
|
-
this.visit(current);
|
|
1302
|
-
for (let i = rights.length - 1; i >= 0; i--) this.visit(rights[i]);
|
|
1565
|
+
case "LogicalExpression":
|
|
1566
|
+
this.visitOperands(node);
|
|
1303
1567
|
return;
|
|
1304
|
-
}
|
|
1305
1568
|
}
|
|
1306
1569
|
this.visitChildren(node);
|
|
1307
1570
|
}
|
|
1571
|
+
visitCatch(node) {
|
|
1572
|
+
const param = node.param;
|
|
1573
|
+
const saved = this.enter(this.declarePattern(null, param));
|
|
1574
|
+
if (param !== null) this.visit(param);
|
|
1575
|
+
this.visitBlock(node.body);
|
|
1576
|
+
this.scope = saved;
|
|
1577
|
+
}
|
|
1578
|
+
visitSwitch(node) {
|
|
1579
|
+
this.visit(node.discriminant);
|
|
1580
|
+
const cases = node.cases;
|
|
1581
|
+
let vars = null;
|
|
1582
|
+
for (const switchCase of cases) vars = this.lexical(vars, switchCase.consequent);
|
|
1583
|
+
const saved = this.enter(this.settle(vars));
|
|
1584
|
+
for (const switchCase of cases) {
|
|
1585
|
+
if (switchCase.test !== null) this.visit(switchCase.test);
|
|
1586
|
+
this.visitStatements(switchCase.consequent);
|
|
1587
|
+
}
|
|
1588
|
+
this.scope = saved;
|
|
1589
|
+
}
|
|
1590
|
+
visitClassExpression(node) {
|
|
1591
|
+
const id = node.id;
|
|
1592
|
+
const saved = this.enter(id === null ? null : this.bind(null, id.name, null));
|
|
1593
|
+
this.visitAll(node.decorators ?? []);
|
|
1594
|
+
if (node.superClass !== null) this.visit(node.superClass);
|
|
1595
|
+
this.visit(node.body);
|
|
1596
|
+
this.scope = saved;
|
|
1597
|
+
}
|
|
1598
|
+
/** `else if` and `? :` chains nest to the right: walked in a loop. */
|
|
1599
|
+
visitBranches(node) {
|
|
1600
|
+
let current = node;
|
|
1601
|
+
while (current?.type === node.type) {
|
|
1602
|
+
this.visit(current.test);
|
|
1603
|
+
this.visit(current.consequent);
|
|
1604
|
+
current = current.alternate;
|
|
1605
|
+
}
|
|
1606
|
+
if (current !== null) this.visit(current);
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* `a + b + …` nests to the left; walking the spine in a loop keeps long
|
|
1610
|
+
* generated chains from exhausting the stack.
|
|
1611
|
+
*/
|
|
1612
|
+
visitOperands(node) {
|
|
1613
|
+
const rights = [];
|
|
1614
|
+
let current = node;
|
|
1615
|
+
while (current.type === "BinaryExpression" || current.type === "LogicalExpression") {
|
|
1616
|
+
rights.push(current.right);
|
|
1617
|
+
current = current.left;
|
|
1618
|
+
}
|
|
1619
|
+
this.visit(current);
|
|
1620
|
+
for (let i = rights.length - 1; i >= 0; i--) this.visit(rights[i]);
|
|
1621
|
+
}
|
|
1308
1622
|
visitChildren(node) {
|
|
1309
1623
|
const keys = walkPlan(node.type);
|
|
1310
1624
|
if (keys === SKIP) return;
|
|
1311
|
-
if (keys
|
|
1312
|
-
for (const key
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
if (Array.isArray(value)) {
|
|
1316
|
-
for (const item of value) if (item !== null) this.visit(item);
|
|
1317
|
-
} else this.visit(value);
|
|
1625
|
+
if (keys === ALL_KEYS) {
|
|
1626
|
+
for (const key in node) {
|
|
1627
|
+
if (key === "type" || key === "start" || key === "end" || TYPE_KEYS.has(key)) continue;
|
|
1628
|
+
this.visitValue(node[key]);
|
|
1318
1629
|
}
|
|
1319
1630
|
return;
|
|
1320
1631
|
}
|
|
1321
|
-
for (const key
|
|
1322
|
-
|
|
1323
|
-
|
|
1632
|
+
for (const key of keys) {
|
|
1633
|
+
const value = node[key];
|
|
1634
|
+
if (value === null || typeof value !== "object") continue;
|
|
1635
|
+
if (!Array.isArray(value)) {
|
|
1636
|
+
this.visit(value);
|
|
1637
|
+
continue;
|
|
1638
|
+
}
|
|
1639
|
+
for (const item of value) if (item !== null) this.visit(item);
|
|
1324
1640
|
}
|
|
1325
1641
|
}
|
|
1326
1642
|
visitValue(value) {
|
|
@@ -1457,47 +1773,48 @@ var Scanner = class {
|
|
|
1457
1773
|
componentCall(node, args) {
|
|
1458
1774
|
const props = args[1] === void 0 ? null : unwrap(args[1]);
|
|
1459
1775
|
if (props?.type !== "ObjectExpression") return false;
|
|
1460
|
-
const read =
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
const key = keyName(property.key, false);
|
|
1465
|
-
if (key === null || !COMPONENT_PROPS.has(key)) continue;
|
|
1466
|
-
read.push(property);
|
|
1467
|
-
if (key !== "message") continue;
|
|
1468
|
-
message = unwrap(property.value);
|
|
1469
|
-
if (property.kind === "get") {
|
|
1470
|
-
const body = message.body?.body;
|
|
1471
|
-
const returned = body?.length === 1 && body[0]?.type === "ReturnStatement" ? body[0] : null;
|
|
1472
|
-
message = returned?.argument ? unwrap(returned.argument) : message;
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
if (message === null) return false;
|
|
1776
|
+
const read = props.properties.filter(isComponentProp);
|
|
1777
|
+
const messageProp = read.findLast((property) => keyName(property.key, false) === "message");
|
|
1778
|
+
if (messageProp === void 0) return false;
|
|
1779
|
+
const message = componentMessage(messageProp);
|
|
1476
1780
|
const analysis = stringValue(message) === null ? this.analyse(message, false, "T") : this.descriptor({
|
|
1477
1781
|
...props,
|
|
1478
1782
|
properties: read
|
|
1479
1783
|
});
|
|
1480
|
-
if (analysis !== PASS) this.
|
|
1481
|
-
if (analysis !== PASS && !analysis.ok) this.report(analysis.code, "error", analysis.message, analysis.node);
|
|
1482
|
-
else if (analysis !== PASS) {
|
|
1483
|
-
const callee = node.callee;
|
|
1484
|
-
const occurrence = this.occurrence(analysis, "rich", callee);
|
|
1485
|
-
if (occurrence !== null) {
|
|
1486
|
-
this.calls.push({
|
|
1487
|
-
kind: "rich",
|
|
1488
|
-
occurrence: this.occurrences.length,
|
|
1489
|
-
start: analysis.literal.start,
|
|
1490
|
-
end: analysis.literal.end,
|
|
1491
|
-
callStart: node.start,
|
|
1492
|
-
callEnd: node.end
|
|
1493
|
-
});
|
|
1494
|
-
this.occurrences.push(occurrence);
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1784
|
+
if (analysis !== PASS) this.recordComponentMessage(node, analysis);
|
|
1497
1785
|
this.visit(node.callee);
|
|
1498
1786
|
this.visitAll(args);
|
|
1499
1787
|
return true;
|
|
1500
1788
|
}
|
|
1789
|
+
/** Records a `<T>`'s analysed message: an error, or a `rich` site. */
|
|
1790
|
+
recordComponentMessage(node, analysis) {
|
|
1791
|
+
this.messageCalls++;
|
|
1792
|
+
if (!analysis.ok) {
|
|
1793
|
+
this.report(analysis.code, "error", analysis.message, analysis.node);
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
const occurrence = this.occurrence(analysis, "rich", node.callee);
|
|
1797
|
+
if (occurrence === null) return;
|
|
1798
|
+
const { literal } = analysis;
|
|
1799
|
+
this.addOccurrence({
|
|
1800
|
+
kind: "rich",
|
|
1801
|
+
occurrence: this.occurrences.length,
|
|
1802
|
+
start: literal.start,
|
|
1803
|
+
end: literal.end,
|
|
1804
|
+
callStart: node.start,
|
|
1805
|
+
callEnd: node.end
|
|
1806
|
+
}, occurrence, literal.start);
|
|
1807
|
+
}
|
|
1808
|
+
/**
|
|
1809
|
+
* Records a rewrite site and its occurrence. `literalStart` is where the
|
|
1810
|
+
* message is written, which a `vi18n-context` note is matched against: kept
|
|
1811
|
+
* index for index with `occurrences`, so every kind of site records it here.
|
|
1812
|
+
*/
|
|
1813
|
+
addOccurrence(site, occurrence, literalStart) {
|
|
1814
|
+
this.calls.push(site);
|
|
1815
|
+
this.occurrences.push(occurrence);
|
|
1816
|
+
this.literalStarts.push(literalStart);
|
|
1817
|
+
}
|
|
1501
1818
|
/**
|
|
1502
1819
|
* `vi18nZod()` (INTERNALS §6 D206): one `msg()` occurrence per bridge
|
|
1503
1820
|
* message, at the call, and one site each that inserts its rewritten form
|
|
@@ -1507,8 +1824,7 @@ var Scanner = class {
|
|
|
1507
1824
|
*/
|
|
1508
1825
|
zodCall(node) {
|
|
1509
1826
|
const callee = node.callee;
|
|
1510
|
-
const
|
|
1511
|
-
const insertAt = this.code.indexOf("(", afterParens(this.code, from, node.end - 1)) + 1;
|
|
1827
|
+
const insertAt = this.code.indexOf("(", afterParens(this.code, argsStart(node), node.end - 1)) + 1;
|
|
1512
1828
|
for (const [source, context] of ZOD_MESSAGES) {
|
|
1513
1829
|
const occurrence = this.occurrence({
|
|
1514
1830
|
ok: true,
|
|
@@ -1517,7 +1833,7 @@ var Scanner = class {
|
|
|
1517
1833
|
context
|
|
1518
1834
|
}, "msg", callee, false);
|
|
1519
1835
|
if (occurrence === null) continue;
|
|
1520
|
-
this.
|
|
1836
|
+
this.addOccurrence({
|
|
1521
1837
|
kind: "zod",
|
|
1522
1838
|
occurrence: this.occurrences.length,
|
|
1523
1839
|
start: insertAt,
|
|
@@ -1525,52 +1841,75 @@ var Scanner = class {
|
|
|
1525
1841
|
insertAt,
|
|
1526
1842
|
callStart: node.start,
|
|
1527
1843
|
callEnd: node.end
|
|
1528
|
-
});
|
|
1529
|
-
this.occurrences.push(occurrence);
|
|
1844
|
+
}, occurrence, node.start);
|
|
1530
1845
|
}
|
|
1531
1846
|
}
|
|
1532
1847
|
messageCall(node, binding, args, argIndex) {
|
|
1533
1848
|
const callee = node.callee;
|
|
1534
1849
|
const kind = binding.role;
|
|
1535
|
-
|
|
1850
|
+
this.checkModuleScope(node, kind);
|
|
1536
1851
|
const arg = args[argIndex];
|
|
1537
|
-
let analysis;
|
|
1538
|
-
if (kind === "msg"
|
|
1539
|
-
else if (arg !== void 0 && arg.type !== "SpreadElement") analysis = this.analyse(arg,
|
|
1540
|
-
|
|
1541
|
-
else analysis = PASS;
|
|
1542
|
-
if (analysis !== PASS) this.messageCalls++;
|
|
1543
|
-
if (analysis === PASS || !analysis.ok) {
|
|
1544
|
-
if (analysis !== PASS) this.report(analysis.code, "error", analysis.message, analysis.node);
|
|
1852
|
+
let analysis = PASS;
|
|
1853
|
+
if (kind === "msg") analysis = this.msgAnalysis(node, argIndex);
|
|
1854
|
+
else if (arg !== void 0 && arg.type !== "SpreadElement") analysis = this.analyse(arg, false, binding.api);
|
|
1855
|
+
if (analysis === PASS) {
|
|
1545
1856
|
this.visit(callee);
|
|
1546
|
-
|
|
1547
|
-
else this.visit(value);
|
|
1857
|
+
this.visitAll(args);
|
|
1548
1858
|
return;
|
|
1549
1859
|
}
|
|
1550
|
-
|
|
1551
|
-
if (
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
kind,
|
|
1555
|
-
occurrence: this.occurrences.length,
|
|
1556
|
-
start: top ? node.start : arg.start,
|
|
1557
|
-
end: top ? node.end : arg.end,
|
|
1558
|
-
callStart: node.start,
|
|
1559
|
-
callEnd: node.end
|
|
1560
|
-
};
|
|
1561
|
-
if (node.start === this.leftEdge) {
|
|
1562
|
-
if (top) site.parenthesize = true;
|
|
1563
|
-
if (this.leftEdgeAsi) site.asi = true;
|
|
1564
|
-
}
|
|
1565
|
-
if (!top && this.awaitsOrYields(node, args)) site.argsAt = afterParens(this.code, Math.max(callee.end, node.typeArguments?.end ?? 0), node.end - 1);
|
|
1566
|
-
this.occurrences.push(occurrence);
|
|
1567
|
-
this.literalStarts.push(analysis.literal.start);
|
|
1568
|
-
this.calls.push(site);
|
|
1569
|
-
if (binding.api === "t" && this.sinks?.has(node) && !this.silenced("extract-html-sink", "warning", node)) this.diagnostics.push(htmlSinkDiagnostic(snippet(this.code, callee), occurrence.id, this.where(node)));
|
|
1860
|
+
this.messageCalls++;
|
|
1861
|
+
if (!analysis.ok) {
|
|
1862
|
+
this.failedCall(analysis, callee, args, arg);
|
|
1863
|
+
return;
|
|
1570
1864
|
}
|
|
1865
|
+
const occurrence = this.occurrence(analysis, kind, callee);
|
|
1866
|
+
if (occurrence !== null) this.recordMessage(node, binding, arg, analysis, occurrence);
|
|
1571
1867
|
this.visit(callee);
|
|
1572
1868
|
for (const value of args) if (value !== arg) this.visit(value);
|
|
1573
1869
|
}
|
|
1870
|
+
/** `extract-module-scope`: a message resolved at module scope reads the locale once. */
|
|
1871
|
+
checkModuleScope(node, kind) {
|
|
1872
|
+
if (this.fnDepth !== 0 || this.withLocaleDepth !== 0 || this.options.moduleScope === "off" || kind === "msg" || kind === "translator" || this.silenced("extract-module-scope", "warning", node)) return;
|
|
1873
|
+
this.diagnostics.push(moduleScopeDiagnostic(snippet(this.code, node.callee), this.options.moduleScope === "error" ? "error" : "warning", this.where(node)));
|
|
1874
|
+
}
|
|
1875
|
+
/** A `msg()` call's message: one argument, which is there and no spread. */
|
|
1876
|
+
msgAnalysis(node, argIndex) {
|
|
1877
|
+
const args = node.arguments;
|
|
1878
|
+
if (args.length > argIndex + 1) return this.extraArguments(node);
|
|
1879
|
+
const value = args[argIndex];
|
|
1880
|
+
if (value === void 0 || value.type === "SpreadElement") return this.missingMessage(node);
|
|
1881
|
+
return this.analyse(value, true, "msg");
|
|
1882
|
+
}
|
|
1883
|
+
/** Reports a call whose message cannot be extracted, and walks the rest of it. */
|
|
1884
|
+
failedCall(analysis, callee, args, arg) {
|
|
1885
|
+
this.report(analysis.code, "error", analysis.message, analysis.node);
|
|
1886
|
+
this.visit(callee);
|
|
1887
|
+
for (const value of args) if (value === arg && analysis.inline !== void 0) this.visitAll(analysis.inline.arguments);
|
|
1888
|
+
else this.visit(value);
|
|
1889
|
+
}
|
|
1890
|
+
/** The rewrite site of an extracted call, and the HTML-sink warning for `t()`. */
|
|
1891
|
+
recordMessage(node, binding, arg, analysis, occurrence) {
|
|
1892
|
+
const kind = binding.role;
|
|
1893
|
+
const top = kind === "msg";
|
|
1894
|
+
const site = {
|
|
1895
|
+
kind,
|
|
1896
|
+
occurrence: this.occurrences.length,
|
|
1897
|
+
start: top ? node.start : arg.start,
|
|
1898
|
+
end: top ? node.end : arg.end,
|
|
1899
|
+
callStart: node.start,
|
|
1900
|
+
callEnd: node.end
|
|
1901
|
+
};
|
|
1902
|
+
if (node.start === this.leftEdge) {
|
|
1903
|
+
if (top) site.parenthesize = true;
|
|
1904
|
+
if (this.leftEdgeAsi) site.asi = true;
|
|
1905
|
+
}
|
|
1906
|
+
if (!top && this.awaitsOrYields(node, node.arguments)) site.argsAt = afterParens(this.code, argsStart(node), node.end - 1);
|
|
1907
|
+
this.addOccurrence(site, occurrence, analysis.literal.start);
|
|
1908
|
+
if (binding.api === "t" && this.sinks?.has(node) && !this.silenced("extract-html-sink", "warning", node)) {
|
|
1909
|
+
const callee = node.callee;
|
|
1910
|
+
this.diagnostics.push(htmlSinkDiagnostic(snippet(this.code, callee), occurrence.id, this.where(node)));
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1574
1913
|
occurrence(analysis, kind, callee, validate = true) {
|
|
1575
1914
|
const { codec } = this.options;
|
|
1576
1915
|
let canonical;
|
|
@@ -1662,9 +2001,7 @@ var Scanner = class {
|
|
|
1662
2001
|
if (!isMsg) {
|
|
1663
2002
|
const inner = this.resolveCallee(node.callee);
|
|
1664
2003
|
if (inner?.binding.role === "msg") {
|
|
1665
|
-
const
|
|
1666
|
-
const value = innerArgs[inner.argIndex];
|
|
1667
|
-
const result = innerArgs.length > inner.argIndex + 1 ? this.extraArguments(node) : value === void 0 || value.type === "SpreadElement" ? this.missingMessage(node) : this.analyse(value, true, "msg");
|
|
2004
|
+
const result = this.msgAnalysis(node, inner.argIndex);
|
|
1668
2005
|
return result.ok ? result : {
|
|
1669
2006
|
...result,
|
|
1670
2007
|
inline: node
|
|
@@ -1792,59 +2129,19 @@ var Scanner = class {
|
|
|
1792
2129
|
}
|
|
1793
2130
|
/** `{ id?, message, context?, maxLength? }` with literal values only. */
|
|
1794
2131
|
descriptor(node) {
|
|
1795
|
-
const
|
|
1796
|
-
ok: false,
|
|
1797
|
-
code: "extract-non-literal-option",
|
|
1798
|
-
message: `A message descriptor takes only literal id, message, context and maxLength properties: ${what}`,
|
|
1799
|
-
node: at
|
|
1800
|
-
});
|
|
1801
|
-
let source;
|
|
1802
|
-
let literal;
|
|
1803
|
-
let id;
|
|
1804
|
-
let idNode;
|
|
1805
|
-
let context;
|
|
1806
|
-
let maxLength;
|
|
2132
|
+
const fields = {};
|
|
1807
2133
|
for (const property of node.properties) {
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
const value = unwrap(property.value);
|
|
1811
|
-
switch (key) {
|
|
1812
|
-
case "message": {
|
|
1813
|
-
const text = stringValue(value);
|
|
1814
|
-
if (text === null) return invalid(`message is ${snippet(this.code, value)}`, value);
|
|
1815
|
-
source = text;
|
|
1816
|
-
literal = value;
|
|
1817
|
-
break;
|
|
1818
|
-
}
|
|
1819
|
-
case "context": {
|
|
1820
|
-
const text = stringValue(value);
|
|
1821
|
-
if (text === null) return invalid(`context is ${snippet(this.code, value)}`, value);
|
|
1822
|
-
context = text;
|
|
1823
|
-
break;
|
|
1824
|
-
}
|
|
1825
|
-
case "id": {
|
|
1826
|
-
const text = stringValue(value);
|
|
1827
|
-
if (text === null) return invalid(`id is ${snippet(this.code, value)}`, value);
|
|
1828
|
-
if (!isValidManualId(text)) return {
|
|
1829
|
-
ok: false,
|
|
1830
|
-
code: "extract-invalid-manual-id",
|
|
1831
|
-
message: `Manual id ${JSON.stringify(text)} is invalid; use two or more dot-separated segments of letters, digits, "_", ":" and "-", such as "hero.cta"`,
|
|
1832
|
-
node: value
|
|
1833
|
-
};
|
|
1834
|
-
id = text;
|
|
1835
|
-
idNode = value;
|
|
1836
|
-
break;
|
|
1837
|
-
}
|
|
1838
|
-
case "maxLength":
|
|
1839
|
-
if (value.type !== "Literal" || typeof value.value !== "number" || !Number.isInteger(value.value) || value.value <= 0) return invalid(`maxLength is ${snippet(this.code, value)}, not a positive integer`, value);
|
|
1840
|
-
maxLength = value.value;
|
|
1841
|
-
break;
|
|
1842
|
-
default: return invalid(`unknown property ${snippet(this.code, property.key)}`, property);
|
|
1843
|
-
}
|
|
2134
|
+
const failure = this.descriptorProperty(property, fields);
|
|
2135
|
+
if (failure !== null) return failure;
|
|
1844
2136
|
}
|
|
2137
|
+
let { source, literal } = fields;
|
|
2138
|
+
const { id, idNode, context, maxLength } = fields;
|
|
1845
2139
|
if (source === void 0 || literal === void 0) {
|
|
1846
2140
|
const library = id === void 0 ? void 0 : this.options.libraryIds?.get(id);
|
|
1847
|
-
if (library === void 0 || idNode === void 0)
|
|
2141
|
+
if (library === void 0 || idNode === void 0) {
|
|
2142
|
+
const missing = "the message property is missing";
|
|
2143
|
+
return invalidOption(id === void 0 ? missing : `${missing}, and no package in extraction.dependencies declares ${JSON.stringify(id)} in its vi18n.messages.json`, node);
|
|
2144
|
+
}
|
|
1848
2145
|
source = library;
|
|
1849
2146
|
literal = idNode;
|
|
1850
2147
|
}
|
|
@@ -1858,6 +2155,34 @@ var Scanner = class {
|
|
|
1858
2155
|
if (maxLength !== void 0) result.maxLength = maxLength;
|
|
1859
2156
|
return result;
|
|
1860
2157
|
}
|
|
2158
|
+
/** Reads one descriptor property into `fields`; the failure when it is not a literal option. */
|
|
2159
|
+
descriptorProperty(property, fields) {
|
|
2160
|
+
if (property.type !== "Property" || property.kind !== "init" || property.method === true) return invalidOption(`${snippet(this.code, property)} is not a plain property`, property);
|
|
2161
|
+
const key = keyName(property.key, property.computed === true);
|
|
2162
|
+
const value = unwrap(property.value);
|
|
2163
|
+
if (key === "maxLength") {
|
|
2164
|
+
if (!isPositiveInteger(value)) return invalidOption(`maxLength is ${snippet(this.code, value)}, not a positive integer`, value);
|
|
2165
|
+
fields.maxLength = value.value;
|
|
2166
|
+
return null;
|
|
2167
|
+
}
|
|
2168
|
+
if (key !== "message" && key !== "context" && key !== "id") return invalidOption(`unknown property ${snippet(this.code, property.key)}`, property);
|
|
2169
|
+
const text = stringValue(value);
|
|
2170
|
+
if (text === null) return invalidOption(`${key} is ${snippet(this.code, value)}`, value);
|
|
2171
|
+
if (key === "message") {
|
|
2172
|
+
fields.source = text;
|
|
2173
|
+
fields.literal = value;
|
|
2174
|
+
} else if (key === "context") fields.context = text;
|
|
2175
|
+
else if (isValidManualId(text)) {
|
|
2176
|
+
fields.id = text;
|
|
2177
|
+
fields.idNode = value;
|
|
2178
|
+
} else return {
|
|
2179
|
+
ok: false,
|
|
2180
|
+
code: "extract-invalid-manual-id",
|
|
2181
|
+
message: `Manual id ${JSON.stringify(text)} is invalid; use two or more dot-separated segments of letters, digits, "_", ":" and "-", such as "hero.cta"`,
|
|
2182
|
+
node: value
|
|
2183
|
+
};
|
|
2184
|
+
return null;
|
|
2185
|
+
}
|
|
1861
2186
|
};
|
|
1862
2187
|
/**
|
|
1863
2188
|
* Gives each `vi18n-context:` note to the first message (by position) in the
|
|
@@ -1912,8 +2237,7 @@ function scanModule(parsed, options) {
|
|
|
1912
2237
|
importsWrapper: wrapper !== null
|
|
1913
2238
|
};
|
|
1914
2239
|
}
|
|
1915
|
-
|
|
1916
|
-
//#region src/core/extract/sources.ts
|
|
2240
|
+
// src/core/extract/sources.ts
|
|
1917
2241
|
/**
|
|
1918
2242
|
* Which import specifiers of a module are vi18n sources for the CLI (INTERNALS
|
|
1919
2243
|
* §2.5.2). The plugin asks Vite's resolver; the CLI has none, so a specifier
|
|
@@ -1945,8 +2269,7 @@ function isRelative(specifier) {
|
|
|
1945
2269
|
}
|
|
1946
2270
|
/** The last path segment without a script extension and without a `?query`. */
|
|
1947
2271
|
function lastName(specifier) {
|
|
1948
|
-
const
|
|
1949
|
-
const path = query === -1 ? specifier : specifier.slice(0, query);
|
|
2272
|
+
const path = withoutQuery(specifier);
|
|
1950
2273
|
return path.slice(path.lastIndexOf("/") + 1).replace(SCRIPT_EXTENSION, "");
|
|
1951
2274
|
}
|
|
1952
2275
|
function isFile(path) {
|
|
@@ -1965,8 +2288,7 @@ function real(path) {
|
|
|
1965
2288
|
* directory's `index.*`. Returns its realpath, as the file walk lists files.
|
|
1966
2289
|
*/
|
|
1967
2290
|
function probeModule(path) {
|
|
1968
|
-
|
|
1969
|
-
if (query !== -1) path = path.slice(0, query);
|
|
2291
|
+
path = withoutQuery(path);
|
|
1970
2292
|
if (isFile(path)) return real(path);
|
|
1971
2293
|
for (const ext of PROBE_EXTENSIONS) if (isFile(path + ext)) return real(path + ext);
|
|
1972
2294
|
const ext = /\.[cm]?jsx?$/.exec(path)?.[0];
|
|
@@ -1981,27 +2303,43 @@ function probeModule(path) {
|
|
|
1981
2303
|
return null;
|
|
1982
2304
|
}
|
|
1983
2305
|
/**
|
|
1984
|
-
* JSONC as tsconfig files are written: comments and trailing
|
|
1985
|
-
* outside strings.
|
|
2306
|
+
* JSONC as tsconfig files are written, as JSON: comments and trailing
|
|
2307
|
+
* commas removed outside strings.
|
|
1986
2308
|
*/
|
|
1987
|
-
function
|
|
2309
|
+
function stripJsonc(text) {
|
|
1988
2310
|
let out = "";
|
|
2311
|
+
let comma = -1;
|
|
1989
2312
|
for (let i = 0; i < text.length; i++) {
|
|
1990
2313
|
const ch = text[i];
|
|
1991
|
-
if (ch === "
|
|
1992
|
-
let j = i + 1;
|
|
1993
|
-
while (j < text.length && text[j] !== "\"") j += text[j] === "\\" ? 2 : 1;
|
|
1994
|
-
out += text.slice(i, j + 1);
|
|
1995
|
-
i = j;
|
|
1996
|
-
} else if (ch === "/" && text[i + 1] === "/") {
|
|
2314
|
+
if (ch === "/" && text[i + 1] === "/") {
|
|
1997
2315
|
while (i < text.length && text[i] !== "\n") i++;
|
|
1998
2316
|
out += "\n";
|
|
1999
|
-
|
|
2317
|
+
continue;
|
|
2318
|
+
}
|
|
2319
|
+
if (ch === "/" && text[i + 1] === "*") {
|
|
2000
2320
|
const end = text.indexOf("*/", i + 2);
|
|
2001
2321
|
i = end === -1 ? text.length : end + 1;
|
|
2002
|
-
|
|
2322
|
+
continue;
|
|
2323
|
+
}
|
|
2324
|
+
if (/\s/.test(ch)) {
|
|
2325
|
+
out += ch;
|
|
2326
|
+
continue;
|
|
2327
|
+
}
|
|
2328
|
+
if (comma !== -1 && (ch === "}" || ch === "]")) out = out.slice(0, comma) + out.slice(comma + 1);
|
|
2329
|
+
comma = ch === "," ? out.length : -1;
|
|
2330
|
+
if (ch !== "\"") {
|
|
2331
|
+
out += ch;
|
|
2332
|
+
continue;
|
|
2333
|
+
}
|
|
2334
|
+
let j = i + 1;
|
|
2335
|
+
while (j < text.length && text[j] !== "\"") j += text[j] === "\\" ? 2 : 1;
|
|
2336
|
+
out += text.slice(i, j + 1);
|
|
2337
|
+
i = j;
|
|
2003
2338
|
}
|
|
2004
|
-
return
|
|
2339
|
+
return out;
|
|
2340
|
+
}
|
|
2341
|
+
function parseJsonc(text) {
|
|
2342
|
+
return JSON.parse(stripJsonc(text));
|
|
2005
2343
|
}
|
|
2006
2344
|
function readJson(file, jsonc) {
|
|
2007
2345
|
try {
|
|
@@ -2068,6 +2406,17 @@ function resolvePackageImport(root, specifier) {
|
|
|
2068
2406
|
if (dirname(dir) === dir) return null;
|
|
2069
2407
|
}
|
|
2070
2408
|
}
|
|
2409
|
+
/** The file an `extends` entry of tsconfig `file` names: a path (`.json` optional) or a package; null when it does not resolve. */
|
|
2410
|
+
function extendedFile(file, parent) {
|
|
2411
|
+
if (!isRelative(parent) && !isAbsolute(parent)) try {
|
|
2412
|
+
return createRequire(file).resolve(parent);
|
|
2413
|
+
} catch {
|
|
2414
|
+
return null;
|
|
2415
|
+
}
|
|
2416
|
+
const candidate = resolve(dirname(file), parent);
|
|
2417
|
+
if (isFile(candidate)) return candidate;
|
|
2418
|
+
return isFile(candidate + ".json") ? candidate + ".json" : null;
|
|
2419
|
+
}
|
|
2071
2420
|
/** `compilerOptions.paths` in effect for `file`, following relative and package `extends`. */
|
|
2072
2421
|
function tsconfigPaths(file, depth = 0) {
|
|
2073
2422
|
const json = readJson(file, true);
|
|
@@ -2084,15 +2433,7 @@ function tsconfigPaths(file, depth = 0) {
|
|
|
2084
2433
|
for (let i = parents.length - 1; i >= 0; i--) {
|
|
2085
2434
|
const parent = parents[i];
|
|
2086
2435
|
if (typeof parent !== "string") continue;
|
|
2087
|
-
|
|
2088
|
-
if (isRelative(parent) || isAbsolute(parent)) {
|
|
2089
|
-
const candidate = resolve(dir, parent);
|
|
2090
|
-
parentFile = isFile(candidate) ? candidate : isFile(candidate + ".json") ? candidate + ".json" : null;
|
|
2091
|
-
} else try {
|
|
2092
|
-
parentFile = createRequire(file).resolve(parent);
|
|
2093
|
-
} catch {
|
|
2094
|
-
parentFile = null;
|
|
2095
|
-
}
|
|
2436
|
+
const parentFile = extendedFile(file, parent);
|
|
2096
2437
|
if (parentFile === null) continue;
|
|
2097
2438
|
const inherited = tsconfigPaths(parentFile, depth + 1);
|
|
2098
2439
|
if (inherited !== null) return baseUrl === null ? inherited : {
|
|
@@ -2153,8 +2494,7 @@ function resolveWrapper(root, specifier) {
|
|
|
2153
2494
|
function writtenName(specifier) {
|
|
2154
2495
|
const name = lastName(specifier);
|
|
2155
2496
|
if (name !== "index") return name;
|
|
2156
|
-
const
|
|
2157
|
-
const path = query === -1 ? specifier : specifier.slice(0, query);
|
|
2497
|
+
const path = withoutQuery(specifier);
|
|
2158
2498
|
return lastName(path.slice(0, Math.max(0, path.lastIndexOf("/"))));
|
|
2159
2499
|
}
|
|
2160
2500
|
function sourceInfo(config) {
|
|
@@ -2238,5 +2578,4 @@ function sourceSpecifiers(imports, file, config) {
|
|
|
2238
2578
|
}
|
|
2239
2579
|
return out;
|
|
2240
2580
|
}
|
|
2241
|
-
|
|
2242
|
-
export { isIgnored as a, unwrap as c, listImports as d, findMarkers as i, lineColumn as l, sourceSpecifiers as n, markersOf as o, scanModule as r, keyName as s, sourceNeedles as t, locateLiteral as u };
|
|
2581
|
+
export { langFromId as C, locateLiteral as D, lineStartsOf as E, listImports as O, armRawParsing as S, lineColumn as T, insertImport as _, scanModule as a, skipTrivia as b, USE_T as c, markersOf as d, TYPE_KEYS as f, forEachBindingName as g, directives as h, stripJsonc as i, findMarkers as l, childNodes as m, sourceNeedles as n, GLUE as o, afterDirective as p, sourceSpecifiers as r, KEYS_MARKER as s, lastName as t, isIgnored as u, isNode as v, parseModule as w, unwrap as x, keyName as y };
|