@menapo/vi18n 0.1.0 → 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/README.md +14 -14
- package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-DyrAbv73.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-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-KPIg-08V.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-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-BecCTvKi.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 +69 -82
- 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-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-CpiiSVIc.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-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C7g11THL.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-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-DZArW5aE.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-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-B_YW4X_a.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-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
- package/package.json +8 -8
- package/dist/check-D34P-S--.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-B5Mz6WJm.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-B7p40M__.js +0 -531
- package/dist/i18next-CrUnKFfh.js +0 -1596
- package/dist/i18next-Rgo-v9l0.js +0 -1
- package/dist/lingui--PcOOYry.js +0 -833
- package/dist/messages-Cxa-5tR0.js +0 -166
- package/dist/migrate-CntqvrW4.js +0 -1
- package/dist/migrate-dPgq9uzy.js +0 -1484
- package/dist/project-Dwx7iPm6.js +0 -381
- package/dist/project-pXipT8iG.js +0 -1046
- package/dist/react-intl-CaBVwJ9u.js +0 -519
- package/dist/restore-Btq66Tid.js +0 -175
- package/dist/rewrite-CiTRAICl.js +0 -803
- package/dist/run-DYD_VXKy.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-CcVzger_.js +0 -658
- package/dist/translate-DZR3MkSA.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-CEMnpv7k.js +0 -651
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { d as parseCatalogBytes } from "./library-BfSOBeBd.js";
|
|
2
|
+
import { _ as parseIdScheme, f as createCodec } from "./validate-zv4-IZyZ.js";
|
|
3
|
+
import { n as applyRestore } from "./apply-FAHl1R6T.js";
|
|
4
|
+
import { s as sourceIndexOf } from "./units-VVLgQ7x7.js";
|
|
5
|
+
import { i as rootRelative, o as errorMessage, r as plural } from "./output-CF9uvk_-.js";
|
|
6
|
+
import { n as sourceContext, t as applyToCatalog } from "./exchange-DYG9RZoT.js";
|
|
7
|
+
import { f as readErrors, h as targetLocales, o as loadCatalogs, s as loadProject, t as CliExit } from "./project-DSzWKU3C.js";
|
|
8
|
+
import { r as EXIT } from "./types-Ds_vebja.js";
|
|
9
|
+
import { basename, dirname } from "node:path";
|
|
10
|
+
import { promisify } from "node:util";
|
|
11
|
+
import { execFile } from "node:child_process";
|
|
12
|
+
// src/cli/commands/restore.ts
|
|
13
|
+
/**
|
|
14
|
+
* `vi18n restore --from <git-rev> [--locale l]… [--dry-run]` (PLAN §4.4,
|
|
15
|
+
* §5.19, INTERNALS §2.6.9): fills entries that are empty now from that
|
|
16
|
+
* revision's catalogs, by exact source text (generated ids) or manual id
|
|
17
|
+
* with the same source text. It undoes what `vi18n extract` rightly did
|
|
18
|
+
* when the code changed back: a reverted text, or a merge whose branch
|
|
19
|
+
* deleted an entry the other branch still uses. Only empty entries are
|
|
20
|
+
* filled, so nothing anyone translated since is overwritten.
|
|
21
|
+
*/
|
|
22
|
+
const run = promisify(execFile);
|
|
23
|
+
/** Catalogs are text, but a large one at an old revision must not hit the default 1 MB buffer. */
|
|
24
|
+
const MAX_BUFFER = 1073741824;
|
|
25
|
+
/** What a failed git command printed; execFile attaches it as a string or a Buffer. */
|
|
26
|
+
function stderrOf(error) {
|
|
27
|
+
const stderr = error.stderr;
|
|
28
|
+
if (stderr === void 0) return "";
|
|
29
|
+
return (typeof stderr === "string" ? stderr : Buffer.from(stderr).toString("utf8")).trim();
|
|
30
|
+
}
|
|
31
|
+
function gitError(message) {
|
|
32
|
+
return new CliExit(EXIT.failure, [{
|
|
33
|
+
code: "restore-git",
|
|
34
|
+
severity: "error",
|
|
35
|
+
message
|
|
36
|
+
}]);
|
|
37
|
+
}
|
|
38
|
+
/** The revision as git sees it; a typo or a missing repository ends the command. */
|
|
39
|
+
async function verifyRevision(cwd, rev) {
|
|
40
|
+
try {
|
|
41
|
+
await run("git", [
|
|
42
|
+
"rev-parse",
|
|
43
|
+
"--verify",
|
|
44
|
+
"--quiet",
|
|
45
|
+
`${rev}^{commit}`
|
|
46
|
+
], { cwd });
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (error.code === "ENOENT") throw gitError("git is not installed or not on PATH");
|
|
49
|
+
const stderr = stderrOf(error);
|
|
50
|
+
throw gitError(/not a git repository/i.test(stderr) ? `${cwd} is not inside a git repository` : `${rev} is not a commit of this repository`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** What git says when a path is not in a revision (`git show <rev>:<path>`). */
|
|
54
|
+
const NOT_IN_REVISION = /does not exist in|exists on disk, but not in/;
|
|
55
|
+
/** A file's bytes at a revision; null when it did not exist there. Any other git failure ends the command. */
|
|
56
|
+
async function showAt(rev, path) {
|
|
57
|
+
try {
|
|
58
|
+
const { stdout } = await run("git", ["show", `${rev}:./${basename(path)}`], {
|
|
59
|
+
cwd: dirname(path),
|
|
60
|
+
encoding: "buffer",
|
|
61
|
+
maxBuffer: MAX_BUFFER
|
|
62
|
+
});
|
|
63
|
+
return stdout;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
const stderr = stderrOf(error);
|
|
66
|
+
if (NOT_IN_REVISION.test(stderr)) return null;
|
|
67
|
+
throw gitError(`git show ${rev}:${basename(path)} failed: ${stderr || errorMessage(error)}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function oldCatalog(bytes, options) {
|
|
71
|
+
const read = parseCatalogBytes(bytes, options);
|
|
72
|
+
return {
|
|
73
|
+
catalog: read.catalog,
|
|
74
|
+
error: read.diagnostics.find((diagnostic) => diagnostic.severity === "error") ?? null
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async function restore(ctx, flags) {
|
|
78
|
+
const project = await loadProject(ctx);
|
|
79
|
+
const { config, layout } = project;
|
|
80
|
+
const locales = targetLocales(config, flags.locales);
|
|
81
|
+
await verifyRevision(config.root, flags.from);
|
|
82
|
+
const loaded = await loadCatalogs(project);
|
|
83
|
+
const unreadable = readErrors(loaded.diagnostics);
|
|
84
|
+
if (unreadable.length > 0) throw new CliExit(EXIT.failure, unreadable);
|
|
85
|
+
const context = sourceContext(project, loaded.set);
|
|
86
|
+
const sourceLoaded = loaded.set.get(config.sourceLocale);
|
|
87
|
+
const oldSource = layout === "id" && sourceLoaded !== void 0 ? await oldSourceCatalog(project, flags.from, sourceLoaded.path) : null;
|
|
88
|
+
const data = {
|
|
89
|
+
from: flags.from,
|
|
90
|
+
dryRun: flags.dryRun,
|
|
91
|
+
locales: [],
|
|
92
|
+
restored: []
|
|
93
|
+
};
|
|
94
|
+
let failed = false;
|
|
95
|
+
for (const locale of locales) {
|
|
96
|
+
const target = loaded.set.get(locale);
|
|
97
|
+
if (target === void 0 || target.catalog === null) continue;
|
|
98
|
+
const file = rootRelative(config.root, target.path);
|
|
99
|
+
const old = await readOld(ctx, project, {
|
|
100
|
+
rev: flags.from,
|
|
101
|
+
locale,
|
|
102
|
+
path: target.path,
|
|
103
|
+
file
|
|
104
|
+
});
|
|
105
|
+
if (old === "missing") continue;
|
|
106
|
+
if (old === "unreadable") {
|
|
107
|
+
failed = true;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const oldContext = oldSourceContext(project, old, oldSource);
|
|
111
|
+
const outcome = await applyToCatalog(project, {
|
|
112
|
+
...target,
|
|
113
|
+
catalog: target.catalog
|
|
114
|
+
}, {
|
|
115
|
+
locale,
|
|
116
|
+
dryRun: flags.dryRun
|
|
117
|
+
}, (catalog) => applyRestore({
|
|
118
|
+
catalog,
|
|
119
|
+
context,
|
|
120
|
+
old,
|
|
121
|
+
oldContext,
|
|
122
|
+
locale
|
|
123
|
+
}));
|
|
124
|
+
if (outcome === null) {
|
|
125
|
+
failed = true;
|
|
126
|
+
ctx.out.report([{
|
|
127
|
+
code: "restore-git",
|
|
128
|
+
severity: "error",
|
|
129
|
+
message: `${locale}'s catalog disappeared while restoring; run vi18n extract`,
|
|
130
|
+
file: target.path,
|
|
131
|
+
locale
|
|
132
|
+
}]);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
ctx.out.report(outcome.diagnostics.map((diagnostic) => ({
|
|
136
|
+
...diagnostic,
|
|
137
|
+
file: target.path
|
|
138
|
+
})));
|
|
139
|
+
data.locales.push({
|
|
140
|
+
locale,
|
|
141
|
+
file,
|
|
142
|
+
restored: outcome.restored.length
|
|
143
|
+
});
|
|
144
|
+
for (const entry of outcome.restored) data.restored.push({
|
|
145
|
+
locale,
|
|
146
|
+
...entry
|
|
147
|
+
});
|
|
148
|
+
ctx.out.result(`${locale}: ${plural(outcome.restored.length, "entry", "entries")} ${flags.dryRun ? "to restore" : "restored"} from ${flags.from}`);
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
exitCode: failed ? EXIT.failure : EXIT.ok,
|
|
152
|
+
data
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** The source locale's catalog at the revision; null when it was missing or unreadable there. */
|
|
156
|
+
async function oldSourceCatalog(project, rev, path) {
|
|
157
|
+
const bytes = await showAt(rev, path);
|
|
158
|
+
if (bytes === null) return null;
|
|
159
|
+
const read = oldCatalog(bytes, {
|
|
160
|
+
locale: project.config.sourceLocale,
|
|
161
|
+
layout: project.layout,
|
|
162
|
+
isSourceLocale: true
|
|
163
|
+
});
|
|
164
|
+
return read.error === null ? read.catalog : null;
|
|
165
|
+
}
|
|
166
|
+
/** A target catalog at the revision, or why there is none (a missing file is noted, an unreadable one reported). */
|
|
167
|
+
async function readOld(ctx, project, at) {
|
|
168
|
+
const bytes = await showAt(at.rev, at.path);
|
|
169
|
+
if (bytes === null) {
|
|
170
|
+
ctx.out.note(`vi18n restore: ${at.file} did not exist at ${at.rev}`);
|
|
171
|
+
return "missing";
|
|
172
|
+
}
|
|
173
|
+
const old = oldCatalog(bytes, {
|
|
174
|
+
locale: at.locale,
|
|
175
|
+
layout: project.layout,
|
|
176
|
+
isSourceLocale: false
|
|
177
|
+
});
|
|
178
|
+
if (old.error === null) return old.catalog;
|
|
179
|
+
ctx.out.report([{
|
|
180
|
+
...old.error,
|
|
181
|
+
code: "restore-git",
|
|
182
|
+
file: at.path,
|
|
183
|
+
locale: at.locale,
|
|
184
|
+
message: `${at.file} at ${at.rev} cannot be read: ${old.error.message}`
|
|
185
|
+
}]);
|
|
186
|
+
return "unreadable";
|
|
187
|
+
}
|
|
188
|
+
/** The old file may predate an `ids.length` change: its labels are read at its own length. */
|
|
189
|
+
function oldSourceContext(project, old, oldSource) {
|
|
190
|
+
const { codec, layout } = project;
|
|
191
|
+
const length = parseIdScheme(old.header.idScheme ?? "")?.length ?? codec.idLength;
|
|
192
|
+
const oldCodec = length === codec.idLength ? codec : createCodec(length);
|
|
193
|
+
return {
|
|
194
|
+
layout,
|
|
195
|
+
codec: oldCodec,
|
|
196
|
+
sourceIndex: sourceIndexOf(oldSource, layout, oldCodec)
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
export { restore };
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import { L as lineRange, O as isConsumed, R as listItemRange, k as isNameFree, o as importLineEdit } from "./migrate-D7TNl6SD.js";
|
|
2
|
+
// src/cli/commands/migrate/rewrite.ts
|
|
3
|
+
/**
|
|
4
|
+
* Planned file edits for the library codemods (INTERNALS §2.14). An edit
|
|
5
|
+
* replaces a range with text rendered only once every message has its id, so
|
|
6
|
+
* a call site prints `t("Text")` or `t({ id, message })` as the id
|
|
7
|
+
* assignment decides; edits nest (a `<Trans>` inside another one's values),
|
|
8
|
+
* and an outer edit reads its inner ranges through `src()` with the inner
|
|
9
|
+
* edits applied. `ModuleBuilder` adds what every module rewrite needs:
|
|
10
|
+
* removing the old library's hook bindings and imports once the rewrites
|
|
11
|
+
* consumed every use, and choosing local names for the vi18n imports.
|
|
12
|
+
*/
|
|
13
|
+
const RUNTIME_MODULE = "@menapo/vi18n";
|
|
14
|
+
function contains(outer, inner) {
|
|
15
|
+
if (inner.start < outer.start || inner.end > outer.end) return false;
|
|
16
|
+
if (inner.start === inner.end) return inner.start > outer.start && inner.start < outer.end;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
function sortEdits(edits) {
|
|
20
|
+
return [...edits].sort((a, b) => {
|
|
21
|
+
if (a.start !== b.start) return a.start - b.start;
|
|
22
|
+
const aEmpty = a.start === a.end;
|
|
23
|
+
if (aEmpty !== (b.start === b.end)) return aEmpty ? -1 : 1;
|
|
24
|
+
return b.end - a.end;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The file with every edit applied, and the top-level edits as the plan
|
|
29
|
+
* lists them (`from` as written, `to` as rendered).
|
|
30
|
+
*/
|
|
31
|
+
function renderPlan(plan, describe) {
|
|
32
|
+
const text = plan.text;
|
|
33
|
+
const listed = [];
|
|
34
|
+
const used = /* @__PURE__ */ new Set();
|
|
35
|
+
let rewritten = 0;
|
|
36
|
+
const renderRange = (edits, start, end, top) => {
|
|
37
|
+
let out = "";
|
|
38
|
+
let pos = start;
|
|
39
|
+
for (let i = 0; i < edits.length;) {
|
|
40
|
+
const edit = edits[i];
|
|
41
|
+
const nested = [];
|
|
42
|
+
let j = i + 1;
|
|
43
|
+
while (j < edits.length && contains(edit, edits[j])) nested.push(edits[j++]);
|
|
44
|
+
if (edit.start < pos) throw new Error(`vi18n migrate: overlapping edits at ${edit.start} in ${plan.file}`);
|
|
45
|
+
const rendered = renderEdit(edit, nested);
|
|
46
|
+
out += text.slice(pos, edit.start) + rendered;
|
|
47
|
+
if (top && edit.listed !== false) listed.push({
|
|
48
|
+
start: edit.start,
|
|
49
|
+
from: text.slice(edit.start, edit.end),
|
|
50
|
+
to: rendered
|
|
51
|
+
});
|
|
52
|
+
pos = edit.end;
|
|
53
|
+
i = j;
|
|
54
|
+
}
|
|
55
|
+
return out + text.slice(pos, end);
|
|
56
|
+
};
|
|
57
|
+
const renderEdit = (edit, nested) => {
|
|
58
|
+
if (edit.keys !== void 0) {
|
|
59
|
+
rewritten++;
|
|
60
|
+
for (const key of edit.keys) used.add(key);
|
|
61
|
+
}
|
|
62
|
+
if (typeof edit.text === "string") return edit.text;
|
|
63
|
+
const markup = edit.markup ?? null;
|
|
64
|
+
const renderer = {
|
|
65
|
+
src: (start, end) => renderRange(nested.filter((e) => e.start >= start && e.end <= end), start, end, false),
|
|
66
|
+
message: (key) => describe(key, markup === null || markup.quote !== "\"" ? "\"" : "'", markup),
|
|
67
|
+
fn: plan.locals,
|
|
68
|
+
types: plan.types
|
|
69
|
+
};
|
|
70
|
+
return edit.text(renderer);
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
text: renderRange(sortEdits(plan.edits), 0, text.length, true),
|
|
74
|
+
listed,
|
|
75
|
+
used,
|
|
76
|
+
rewritten
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const ALIASES = {
|
|
80
|
+
t: [
|
|
81
|
+
"t",
|
|
82
|
+
"vt",
|
|
83
|
+
"vi18nT"
|
|
84
|
+
],
|
|
85
|
+
msg: [
|
|
86
|
+
"msg",
|
|
87
|
+
"vmsg",
|
|
88
|
+
"vi18nMsg"
|
|
89
|
+
],
|
|
90
|
+
rich: [
|
|
91
|
+
"rich",
|
|
92
|
+
"vrich",
|
|
93
|
+
"vi18nRich"
|
|
94
|
+
]
|
|
95
|
+
};
|
|
96
|
+
var ModuleBuilder = class {
|
|
97
|
+
edits = [];
|
|
98
|
+
consumed = /* @__PURE__ */ new Set();
|
|
99
|
+
used = /* @__PURE__ */ new Set();
|
|
100
|
+
manual = [];
|
|
101
|
+
/** Offsets where a vi18n function is called. */
|
|
102
|
+
sites = [];
|
|
103
|
+
fns = /* @__PURE__ */ new Set();
|
|
104
|
+
/** `source` → type names to import (`react` → `ReactNode`). */
|
|
105
|
+
typeImports = /* @__PURE__ */ new Map();
|
|
106
|
+
/** Bindings of the old library's hooks (`const { t } = useTranslation()`), removed once consumed. */
|
|
107
|
+
hooks = [];
|
|
108
|
+
/** Call sites rewritten. */
|
|
109
|
+
rewritten = 0;
|
|
110
|
+
options;
|
|
111
|
+
constructor(options) {
|
|
112
|
+
this.options = options;
|
|
113
|
+
}
|
|
114
|
+
get text() {
|
|
115
|
+
return this.options.text;
|
|
116
|
+
}
|
|
117
|
+
get info() {
|
|
118
|
+
return this.options.info;
|
|
119
|
+
}
|
|
120
|
+
/** Marks reference nodes (identifiers, component names) as gone after the rewrite. */
|
|
121
|
+
consume(nodes) {
|
|
122
|
+
for (const node of nodes) this.consumed.add(node.start);
|
|
123
|
+
}
|
|
124
|
+
/** Every reference inside a range, e.g. a removed element's attributes. */
|
|
125
|
+
consumeRange(range) {
|
|
126
|
+
for (const refs of this.info.references.values()) for (const ref of refs) if (ref.start >= range.start && ref.end <= range.end) this.consumed.add(ref.start);
|
|
127
|
+
}
|
|
128
|
+
/** A rewritten call site. */
|
|
129
|
+
site(range, render, options) {
|
|
130
|
+
for (const key of options.keys) this.used.add(key);
|
|
131
|
+
for (const fn of options.uses) this.fns.add(fn);
|
|
132
|
+
this.consume(options.consume ?? []);
|
|
133
|
+
this.sites.push(range.start);
|
|
134
|
+
this.rewritten++;
|
|
135
|
+
this.edits.push({
|
|
136
|
+
start: range.start,
|
|
137
|
+
end: range.end,
|
|
138
|
+
text: render,
|
|
139
|
+
markup: options.markup ?? null,
|
|
140
|
+
keys: [...options.keys]
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/** A mechanical replacement that is not a message (an unwrapped provider). */
|
|
144
|
+
replace(range, text, consume = []) {
|
|
145
|
+
this.consume(consume);
|
|
146
|
+
this.edits.push({
|
|
147
|
+
start: range.start,
|
|
148
|
+
end: range.end,
|
|
149
|
+
text
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
note(offset, message, keys = []) {
|
|
153
|
+
this.manual.push({
|
|
154
|
+
offset,
|
|
155
|
+
message,
|
|
156
|
+
keys: [...keys]
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
typeImport(source, name) {
|
|
160
|
+
for (const declaration of this.info.imports) {
|
|
161
|
+
if (declaration.source !== source) continue;
|
|
162
|
+
for (const specifier of declaration.specifiers) if (specifier.imported === name) return specifier.local;
|
|
163
|
+
}
|
|
164
|
+
let names = this.typeImports.get(source);
|
|
165
|
+
if (names === void 0) this.typeImports.set(source, names = /* @__PURE__ */ new Set());
|
|
166
|
+
names.add(name);
|
|
167
|
+
return name;
|
|
168
|
+
}
|
|
169
|
+
finish() {
|
|
170
|
+
const { info, text } = this.options;
|
|
171
|
+
const removed = /* @__PURE__ */ new Set();
|
|
172
|
+
this.removeHooks(removed);
|
|
173
|
+
this.reportUnread();
|
|
174
|
+
const { replaceable, removedImports } = this.removeOldImports(removed);
|
|
175
|
+
const { locals, needed } = this.localNames(removed);
|
|
176
|
+
const lines = [];
|
|
177
|
+
if (needed.length > 0) {
|
|
178
|
+
const specifiers = needed.map(([fn, local]) => fn === local ? fn : `${fn} as ${local}`);
|
|
179
|
+
lines.push(`import { ${specifiers.join(", ")} } from "${RUNTIME_MODULE}";`);
|
|
180
|
+
}
|
|
181
|
+
for (const [source, names] of this.typeImports) lines.push(`import type { ${[...names].sort().join(", ")} } from "${source}";`);
|
|
182
|
+
if (lines.length > 0) this.placeImports(lines.join("\n"), replaceable, removedImports);
|
|
183
|
+
return {
|
|
184
|
+
file: this.options.file,
|
|
185
|
+
text,
|
|
186
|
+
edits: this.edits,
|
|
187
|
+
used: this.used,
|
|
188
|
+
manual: this.manual,
|
|
189
|
+
rewritten: this.rewritten,
|
|
190
|
+
locals,
|
|
191
|
+
types: this.typeNames(info)
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/** Whether every use of `binding` was rewritten. */
|
|
195
|
+
allConsumed(binding) {
|
|
196
|
+
return isConsumed(this.options.info, binding, this.consumed, this.outsideOf(binding.name));
|
|
197
|
+
}
|
|
198
|
+
/** Removes the hook bindings whose every use was rewritten, adding their ids to `removed`. */
|
|
199
|
+
removeHooks(removed) {
|
|
200
|
+
const byDeclarator = /* @__PURE__ */ new Map();
|
|
201
|
+
for (const binding of this.hooks) {
|
|
202
|
+
if (!this.allConsumed(binding)) continue;
|
|
203
|
+
removed.add(binding.id);
|
|
204
|
+
const declarator = binding.declarator;
|
|
205
|
+
let list = byDeclarator.get(declarator);
|
|
206
|
+
if (list === void 0) byDeclarator.set(declarator, list = []);
|
|
207
|
+
list.push(binding);
|
|
208
|
+
}
|
|
209
|
+
for (const [declarator, bindings] of byDeclarator) {
|
|
210
|
+
if (bindings.some((b) => this.touched(b))) {
|
|
211
|
+
this.removeBindings(declarator, bindings, removed);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
for (const b of bindings) removed.delete(b.id);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Whether a rewrite consumed a use of `b` inside its scope. */
|
|
218
|
+
touched(b) {
|
|
219
|
+
return [...(this.options.info.references.get(b.name) ?? []).map((r) => r.start), ...this.outsideOf(b.name)].some((offset) => this.consumed.has(offset) && b.scope.start <= offset && offset < b.scope.end);
|
|
220
|
+
}
|
|
221
|
+
/** Other imports the rewrites left unread (the instance a provider took): reported, not removed. */
|
|
222
|
+
reportUnread() {
|
|
223
|
+
const { info } = this.options;
|
|
224
|
+
for (const declaration of info.imports) {
|
|
225
|
+
if (this.options.isOldSource(declaration.source)) continue;
|
|
226
|
+
for (const specifier of declaration.specifiers) {
|
|
227
|
+
const binding = info.bindings.find((b) => b.slot === specifier.node);
|
|
228
|
+
const refs = info.references.get(specifier.local) ?? [];
|
|
229
|
+
if (binding === void 0 || refs.length === 0 || !this.allConsumed(binding)) continue;
|
|
230
|
+
this.note(declaration.node.start, `${specifier.local} (from ${declaration.source}) is no longer used after the rewrite: remove the import once nothing else needs that module`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Removes the old-library imports nothing reads any more, or the specifiers of
|
|
236
|
+
* them nothing reads. Returns the first removed line, which the new imports can
|
|
237
|
+
* take, and the import declarations removed whole.
|
|
238
|
+
*/
|
|
239
|
+
removeOldImports(removed) {
|
|
240
|
+
const { info, text } = this.options;
|
|
241
|
+
let replaceable = null;
|
|
242
|
+
const removedImports = /* @__PURE__ */ new Set();
|
|
243
|
+
for (const declaration of info.imports) {
|
|
244
|
+
if (!this.options.isOldSource(declaration.source) || declaration.specifiers.length === 0) continue;
|
|
245
|
+
const kept = declaration.specifiers.filter((specifier) => {
|
|
246
|
+
const binding = info.bindings.find((b) => b.slot === specifier.node);
|
|
247
|
+
return binding === void 0 || !this.allConsumed(binding);
|
|
248
|
+
});
|
|
249
|
+
if (kept.length === declaration.specifiers.length) continue;
|
|
250
|
+
for (const specifier of declaration.specifiers) {
|
|
251
|
+
const binding = info.bindings.find((b) => b.slot === specifier.node);
|
|
252
|
+
if (!kept.includes(specifier) && binding !== void 0) removed.add(binding.id);
|
|
253
|
+
}
|
|
254
|
+
if (kept.length > 0) {
|
|
255
|
+
const { start, end } = declaration.node;
|
|
256
|
+
const rebuilt = rebuildImport(text, declaration.node, kept.map((s) => s.node));
|
|
257
|
+
this.edits.push({
|
|
258
|
+
start,
|
|
259
|
+
end,
|
|
260
|
+
text: rebuilt
|
|
261
|
+
});
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
removedImports.add(declaration.node);
|
|
265
|
+
const edit = {
|
|
266
|
+
...lineRange(text, declaration.node.start, declaration.node.end),
|
|
267
|
+
text: ""
|
|
268
|
+
};
|
|
269
|
+
this.edits.push(edit);
|
|
270
|
+
replaceable ??= edit;
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
replaceable,
|
|
274
|
+
removedImports
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/** Local names for the vi18n imports, and the imports the module still needs. */
|
|
278
|
+
localNames(removed) {
|
|
279
|
+
const locals = {
|
|
280
|
+
t: "t",
|
|
281
|
+
msg: "msg",
|
|
282
|
+
rich: "rich"
|
|
283
|
+
};
|
|
284
|
+
const needed = [];
|
|
285
|
+
const taken = /* @__PURE__ */ new Set();
|
|
286
|
+
for (const fn of [
|
|
287
|
+
"msg",
|
|
288
|
+
"rich",
|
|
289
|
+
"t"
|
|
290
|
+
]) {
|
|
291
|
+
if (!this.fns.has(fn)) continue;
|
|
292
|
+
const existing = this.existingImport(fn);
|
|
293
|
+
if (existing !== null) {
|
|
294
|
+
locals[fn] = existing;
|
|
295
|
+
taken.add(existing);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const name = this.freeName(fn, taken, removed);
|
|
299
|
+
locals[fn] = name;
|
|
300
|
+
taken.add(name);
|
|
301
|
+
needed.push([fn, name]);
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
locals,
|
|
305
|
+
needed
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/** The first of `fn`'s aliases free in the module, else `fn2_`, `fn3_`… */
|
|
309
|
+
freeName(fn, taken, removed) {
|
|
310
|
+
const { info } = this.options;
|
|
311
|
+
const free = (name) => !taken.has(name) && isNameFree(info, name, this.sites, removed, this.consumed) && (this.options.outside?.get(name) ?? []).every((offset) => this.consumedOutside(offset));
|
|
312
|
+
const alias = ALIASES[fn].find(free);
|
|
313
|
+
if (alias !== void 0) return alias;
|
|
314
|
+
let n = 2;
|
|
315
|
+
while (!free(`${fn}${n}_`)) n++;
|
|
316
|
+
return `${fn}${n}_`;
|
|
317
|
+
}
|
|
318
|
+
/** Places the new import `lines`: on a removed import's line when there is one, else where the module's imports go. */
|
|
319
|
+
placeImports(lines, replaceable, removedImports) {
|
|
320
|
+
if (replaceable !== null && this.options.placeImport === void 0) {
|
|
321
|
+
replaceable.text = lines + "\n";
|
|
322
|
+
replaceable.listed = false;
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const { info, text, file } = this.options;
|
|
326
|
+
const edits = this.options.placeImport?.(lines, removedImports) ?? [importLineEdit(file, text, lines, info.program)];
|
|
327
|
+
for (const edit of edits) this.edits.push({
|
|
328
|
+
...edit,
|
|
329
|
+
listed: false
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
/** Each type name's local in the module: the imported types, else the module's own imports of it. */
|
|
333
|
+
typeNames(info) {
|
|
334
|
+
const types = {};
|
|
335
|
+
for (const names of this.typeImports.values()) for (const name of names) types[name] = name;
|
|
336
|
+
for (const declaration of info.imports) for (const specifier of declaration.specifiers) if (!(specifier.imported in types)) types[specifier.imported] = specifier.local;
|
|
337
|
+
return types;
|
|
338
|
+
}
|
|
339
|
+
/** Offsets of outside references (template calls) to `name`. */
|
|
340
|
+
outsideOf(name) {
|
|
341
|
+
return [...this.options.outside?.get(name) ?? []];
|
|
342
|
+
}
|
|
343
|
+
consumedOutside(offset) {
|
|
344
|
+
return this.consumed.has(offset);
|
|
345
|
+
}
|
|
346
|
+
existingImport(fn) {
|
|
347
|
+
for (const declaration of this.info.imports) {
|
|
348
|
+
if (declaration.source !== "@menapo/vi18n" || declaration.typeOnly) continue;
|
|
349
|
+
for (const specifier of declaration.specifiers) if (specifier.imported === fn && !specifier.typeOnly) return specifier.local;
|
|
350
|
+
}
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Removes destructured hook bindings: the whole declaration when nothing
|
|
355
|
+
* of it stays (the hook's own references are then consumed too), else the
|
|
356
|
+
* properties or array slots.
|
|
357
|
+
*/
|
|
358
|
+
removeBindings(declarator, bindings, removed) {
|
|
359
|
+
const { info, text } = this.options;
|
|
360
|
+
const pattern = declarator.id;
|
|
361
|
+
if (pattern.type === "Identifier" || pattern.type === "ObjectPattern" && pattern.properties.every((p) => bindings.some((b) => b.slot === p)) || pattern.type === "ArrayPattern" && pattern.elements.every((e) => e === null || bindings.some((b) => b.slot === e))) {
|
|
362
|
+
const declaration = info.parentOf(declarator)?.node;
|
|
363
|
+
if (declaration === void 0) return;
|
|
364
|
+
const declarators = declaration.declarations;
|
|
365
|
+
const init = declarator.init;
|
|
366
|
+
if (init !== null) this.consumeRange(init);
|
|
367
|
+
if (declarators.length === 1) this.edits.push({
|
|
368
|
+
...lineRange(text, declaration.start, declaration.end),
|
|
369
|
+
text: ""
|
|
370
|
+
});
|
|
371
|
+
else this.edits.push({
|
|
372
|
+
...listItemRange(declarators, declarators.indexOf(declarator)),
|
|
373
|
+
text: ""
|
|
374
|
+
});
|
|
375
|
+
for (const b of info.bindings) if (b.declarator === declarator) removed.add(b.id);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (pattern.type === "ObjectPattern") {
|
|
379
|
+
const properties = pattern.properties;
|
|
380
|
+
const kept = properties.filter((p) => !bindings.some((b) => b.slot === p));
|
|
381
|
+
const first = properties[0];
|
|
382
|
+
const last = properties[properties.length - 1];
|
|
383
|
+
this.edits.push({
|
|
384
|
+
start: first.start,
|
|
385
|
+
end: last.end,
|
|
386
|
+
text: kept.map((p) => text.slice(p.start, p.end)).join(", ")
|
|
387
|
+
});
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (pattern.type === "ArrayPattern") for (const binding of bindings) {
|
|
391
|
+
const slot = binding.slot;
|
|
392
|
+
this.edits.push({
|
|
393
|
+
start: slot.start,
|
|
394
|
+
end: slot.end,
|
|
395
|
+
text: ""
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
/** An import declaration printed with only `specifiers` (default, namespace, then named). */
|
|
401
|
+
function rebuildImport(text, declaration, specifiers) {
|
|
402
|
+
const source = text.slice(declaration.source.start, declaration.source.end);
|
|
403
|
+
const head = [];
|
|
404
|
+
const named = [];
|
|
405
|
+
for (const specifier of specifiers) {
|
|
406
|
+
const code = text.slice(specifier.start, specifier.end);
|
|
407
|
+
if (specifier.type === "ImportSpecifier") named.push(code);
|
|
408
|
+
else head.push(code);
|
|
409
|
+
}
|
|
410
|
+
if (named.length > 0) head.push(`{ ${named.join(", ")} }`);
|
|
411
|
+
const kind = declaration.importKind === "type" ? "type " : "";
|
|
412
|
+
const end = text.slice(declaration.start, declaration.end).trimEnd().endsWith(";") ? ";" : "";
|
|
413
|
+
return `import ${kind}${head.join(", ")} from ${source}${end}`;
|
|
414
|
+
}
|
|
415
|
+
export { RUNTIME_MODULE as n, renderPlan as r, ModuleBuilder as t };
|