@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.
Files changed (119) hide show
  1. package/README.md +14 -14
  2. package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
  3. package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
  4. package/dist/{check-DyrAbv73.js → check-DwFwOB1d.js} +66 -84
  5. package/dist/cli.js +2 -3
  6. package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
  7. package/dist/collisions-uMTuq36M.js +215 -0
  8. package/dist/compile-CEvH-6sY.js +240 -0
  9. package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
  10. package/dist/concurrency-BCY_4zcr.js +1 -0
  11. package/dist/concurrency-Dy2ny50V.js +107 -0
  12. package/dist/config.d.ts +1 -5
  13. package/dist/config.js +4 -5
  14. package/dist/{types-D3QGM8XO.js → diagnostics-1onDF6Ua.js} +26 -26
  15. package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
  16. package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
  17. package/dist/engine-Bf7nVigL.js +556 -0
  18. package/dist/engine.d.ts +1 -5
  19. package/dist/engine.js +297 -168
  20. package/dist/exchange-DYG9RZoT.js +41 -0
  21. package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
  22. package/dist/{export-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
  23. package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
  24. package/dist/{formats-KPIg-08V.js → formats-ChV1LVH_.js} +352 -276
  25. package/dist/fs-ezCMZeDV.js +52 -0
  26. package/dist/i18next-1waHziMy.js +1 -0
  27. package/dist/i18next-D66Icz3L.js +1907 -0
  28. package/dist/{import-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
  29. package/dist/index.d.ts +1 -13
  30. package/dist/index.js +14 -16
  31. package/dist/{init-BecCTvKi.js → init-J7AwwA5e.js} +466 -501
  32. package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
  33. package/dist/lingui-Ee2farVo.js +902 -0
  34. package/dist/lint.d.ts +0 -4
  35. package/dist/lint.js +69 -82
  36. package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
  37. package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
  38. package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
  39. package/dist/{lsp-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
  40. package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
  41. package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
  42. package/dist/{merge-driver-CpiiSVIc.js → merge-driver-B04Apm29.js} +138 -122
  43. package/dist/migrate-D7TNl6SD.js +2169 -0
  44. package/dist/migrate-khCY8TE0.js +1 -0
  45. package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
  46. package/dist/{output-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
  47. package/dist/parse.d.ts +1 -3
  48. package/dist/parse.js +7 -8
  49. package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
  50. package/dist/{pipeline-C7g11THL.js → pipeline-sgl_q_op.js} +387 -359
  51. package/dist/project-DSzWKU3C.js +544 -0
  52. package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
  53. package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
  54. package/dist/react-intl-tzGIk6G3.js +634 -0
  55. package/dist/restore-BG_zMpSj.js +199 -0
  56. package/dist/rewrite-hAcRJwiZ.js +415 -0
  57. package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
  58. package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
  59. package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
  60. package/dist/run-CFzR3V93.js +647 -0
  61. package/dist/scan-worker.d.ts +2 -4
  62. package/dist/scan-worker.js +1 -1
  63. package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
  64. package/dist/{server-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
  65. package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
  66. package/dist/server.d.ts +1 -17
  67. package/dist/server.js +415 -381
  68. package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
  69. package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
  70. package/dist/{status-DZArW5aE.js → status-CTH3qI-0.js} +30 -30
  71. package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
  72. package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
  73. package/dist/sync-CpL0Qsgw.js +1352 -0
  74. package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
  75. package/dist/test.d.ts +5 -16
  76. package/dist/test.js +77 -48
  77. package/dist/translate-BrfVRkjl.js +336 -0
  78. package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
  79. package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
  80. package/dist/{types-DsdWBG0b.d.ts → types-BaG1LboH.d.ts} +2 -6
  81. package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
  82. package/dist/types-Ds_vebja.js +41 -0
  83. package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.js} +7 -16
  84. package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
  85. package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
  86. package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
  87. package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
  88. package/dist/{version-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
  89. package/dist/{vite-scan-B_YW4X_a.js → vite-scan-DFKSGVE9.js} +87 -92
  90. package/dist/vite.d.ts +2 -6
  91. package/dist/vite.js +6482 -6018
  92. package/dist/vue-i18n-CBmZqHbG.js +806 -0
  93. package/dist/{which-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
  94. package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
  95. package/package.json +8 -8
  96. package/dist/check-D34P-S--.js +0 -1
  97. package/dist/codec-DuvU2VQy.js +0 -207
  98. package/dist/collisions-CfpZU8e4.js +0 -217
  99. package/dist/compile-B5Mz6WJm.js +0 -245
  100. package/dist/concurrency-C9UUWuDC.js +0 -96
  101. package/dist/concurrency-DONL7t4V.js +0 -1
  102. package/dist/engine-B7p40M__.js +0 -531
  103. package/dist/i18next-CrUnKFfh.js +0 -1596
  104. package/dist/i18next-Rgo-v9l0.js +0 -1
  105. package/dist/lingui--PcOOYry.js +0 -833
  106. package/dist/messages-Cxa-5tR0.js +0 -166
  107. package/dist/migrate-CntqvrW4.js +0 -1
  108. package/dist/migrate-dPgq9uzy.js +0 -1484
  109. package/dist/project-Dwx7iPm6.js +0 -381
  110. package/dist/project-pXipT8iG.js +0 -1046
  111. package/dist/react-intl-CaBVwJ9u.js +0 -519
  112. package/dist/restore-Btq66Tid.js +0 -175
  113. package/dist/rewrite-CiTRAICl.js +0 -803
  114. package/dist/run-DYD_VXKy.js +0 -694
  115. package/dist/runtime-DTMmliT1.js +0 -64
  116. package/dist/sync-CcVzger_.js +0 -658
  117. package/dist/translate-DZR3MkSA.js +0 -299
  118. package/dist/types-wSh9fCBY.js +0 -11
  119. package/dist/vue-i18n-CEMnpv7k.js +0 -651
@@ -1,12 +1,9 @@
1
- import { o as isDiagnosticError } from "./types-D3QGM8XO.js";
2
- import { r as validateTranslation } from "./validate-BxpupdAf.js";
3
- import { s as catalogPath } from "./library-D5CWvTQs.js";
4
- import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-yuRljQ6h.js";
5
- import { t as findCollisions } from "./collisions-CfpZU8e4.js";
6
- import { i as rootRelative, r as plural, t as countBySeverity } from "./output-DsaHKHEt.js";
7
- import { t as EXIT } from "./types-wSh9fCBY.js";
8
- import { a as loadCatalogs, c as ownStatus, d as referencedMessages, f as scan, o as loadProject, s as occurrencesOf } from "./project-Dwx7iPm6.js";
9
- //#region src/cli/commands/check.ts
1
+ import { t as findCollisions } from "./collisions-uMTuq36M.js";
2
+ import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-CsUXRX2J.js";
3
+ import { C as idSchemeMismatch, S as checkTranslation, c as occurrencesOf, l as ownStatus, m as scan, n as catalogFile, o as loadCatalogs, p as referencedMessages, s as loadProject, w as syntaxOfEntry } from "./project-DSzWKU3C.js";
4
+ import { r as plural, t as countBySeverity } from "./output-CF9uvk_-.js";
5
+ import { r as EXIT, t as CHECK_CATEGORIES } from "./types-Ds_vebja.js";
6
+ // src/cli/commands/check.ts
10
7
  /**
11
8
  * `vi18n check [missing] [placeholders] [syntax] [--ci] [--format …]`
12
9
  * (INTERNALS §2.14, PLAN §4.14): validation of the catalogs against the
@@ -21,11 +18,6 @@ import { a as loadCatalogs, c as ownStatus, d as referencedMessages, f as scan,
21
18
  * - missing: referenced messages without a usable own translation per target
22
19
  * locale, fuzzy and invalid ones included.
23
20
  */
24
- const ALL = [
25
- "missing",
26
- "placeholders",
27
- "syntax"
28
- ];
29
21
  /** Missing ids named in each locale's summary; the count says how many more there are. */
30
22
  const MISSING_LISTED = 20;
31
23
  /** Scan findings that belong to `check syntax`: invalid source messages, and anything the build would fail on. */
@@ -33,9 +25,9 @@ function isSyntaxFinding(diagnostic) {
33
25
  return diagnostic.severity === "error" || diagnostic.code.startsWith("icu-");
34
26
  }
35
27
  async function check(ctx, flags) {
36
- const categories = flags.categories.length === 0 ? ALL.slice() : flags.categories;
28
+ const categories = flags.categories.length === 0 ? CHECK_CATEGORIES.slice() : flags.categories;
29
+ const selected = new Set(categories);
37
30
  const project = await loadProject(ctx);
38
- const { config } = project;
39
31
  const found = {
40
32
  missing: [],
41
33
  placeholders: [],
@@ -44,74 +36,73 @@ async function check(ctx, flags) {
44
36
  const scanned = await scan(project);
45
37
  const loaded = await loadCatalogs(project);
46
38
  const referenced = referencedMessages(project, scanned.messages, loaded.set);
47
- if (categories.includes("syntax")) {
48
- found.syntax.push(...scanned.diagnostics.filter(isSyntaxFinding));
49
- found.syntax.push(...loaded.diagnostics);
50
- found.syntax.push(...referenced.diagnostics);
51
- found.syntax.push(...findCollisions(occurrencesOf(scanned), loaded.set, project.codec, { referenced: new Set(config.extraction.keep) }));
52
- }
53
- for (const locale of config.locales) {
54
- const catalog = loaded.set.get(locale);
55
- const file = rootRelative(config.root, catalog?.path ?? catalogPath(config, locale));
56
- const read = catalog?.catalog ?? null;
57
- if (categories.includes("syntax") && read !== null) {
58
- for (const entry of read.entries) syntaxOfEntry(found.syntax, entry, file, locale);
59
- found.syntax.push(...duplicateDiagnostics(resolveDuplicates(read, project.codec).duplicates, file));
60
- if (read.header.idScheme !== null && read.header.idScheme !== project.codec.idScheme) found.syntax.push({
61
- code: "id-scheme-mismatch",
62
- severity: "error",
63
- message: `${file} records ${read.header.idScheme}, but ids.length ${config.ids.length} gives ${project.codec.idScheme}; run vi18n extract to re-key it`,
64
- file,
65
- locale
66
- });
67
- }
68
- if (locale === config.sourceLocale) continue;
69
- const status = ownStatus(project, locale, catalog, referenced.identities);
70
- const findings = translationFindings(project, status, referenced.identities, file);
71
- if (categories.includes("syntax")) found.syntax.push(...findings.syntax);
72
- if (categories.includes("placeholders")) found.placeholders.push(...findings.placeholders);
73
- if (categories.includes("missing")) found.missing.push(...missing(project, status, file));
39
+ if (selected.has("syntax")) found.syntax.push(...scanned.diagnostics.filter(isSyntaxFinding), ...loaded.diagnostics, ...referenced.diagnostics, ...findCollisions(occurrencesOf(scanned), loaded.set, project.codec, { referenced: new Set(project.config.extraction.keep) }));
40
+ const sources = new SourceCache(project.codec, referenced.identities);
41
+ for (const locale of project.config.locales) checkLocale(project, locale, loaded.set.get(locale), {
42
+ identities: referenced.identities,
43
+ sources,
44
+ selected,
45
+ found
46
+ });
47
+ return finish(ctx, flags, categories, found);
48
+ }
49
+ /** One locale's findings in the selected categories. */
50
+ function checkLocale(project, locale, catalog, { identities, sources, selected, found }) {
51
+ const { config } = project;
52
+ const file = catalogFile(config, locale, catalog);
53
+ const read = catalog?.catalog ?? null;
54
+ if (selected.has("syntax") && read !== null) {
55
+ for (const entry of read.entries) syntaxOfEntry(found.syntax, entry, file, locale);
56
+ found.syntax.push(...duplicateDiagnostics(resolveDuplicates(read, project.codec).duplicates, file));
57
+ const scheme = idSchemeMismatch(project, file, locale, read.header.idScheme);
58
+ if (scheme !== null) found.syntax.push(scheme);
74
59
  }
60
+ if (locale === config.sourceLocale) return;
61
+ const status = ownStatus(project, locale, catalog, identities);
62
+ const findings = translationFindings(project.codec, status, sources, file);
63
+ if (selected.has("syntax")) found.syntax.push(...findings.syntax);
64
+ if (selected.has("placeholders")) found.placeholders.push(...findings.placeholders);
65
+ if (selected.has("missing")) found.missing.push(...missing(project, status, file));
66
+ }
67
+ function finish(ctx, flags, categories, found) {
75
68
  const data = {
76
69
  categories,
77
70
  counts: {}
78
71
  };
79
72
  let failed = false;
73
+ const summary = [];
80
74
  for (const category of categories) {
81
75
  const list = found[category];
82
76
  ctx.out.report(list);
83
77
  const counts = countBySeverity(list);
84
78
  data.counts[category] = counts;
85
79
  if (counts.error > 0 || flags.ci && counts.warning > 0) failed = true;
80
+ summary.push(`${category}: ${plural(counts.error, "error")}, ${plural(counts.warning, "warning")}`);
86
81
  }
87
- ctx.out.result(categories.map((category) => {
88
- const counts = data.counts[category];
89
- return `${category}: ${plural(counts.error, "error")}, ${plural(counts.warning, "warning")}`;
90
- }).join("; "));
82
+ ctx.out.result(summary.join("; "));
91
83
  return {
92
84
  exitCode: failed ? EXIT.failure : EXIT.ok,
93
85
  data
94
86
  };
95
87
  }
96
- /** Wrapped lines and foreign contexts; the language server reports them on open catalogs too. */
97
- function syntaxOfEntry(into, entry, file, locale) {
98
- if (entry.wrapped) into.push({
99
- code: "catalog-wrapped",
100
- severity: "warning",
101
- message: "a string is wrapped over several lines; vi18n extract rewrites it on one line",
102
- file,
103
- line: entry.line,
104
- locale
105
- });
106
- if (entry.foreignContext !== null) into.push({
107
- code: "catalog-foreign-context",
108
- severity: "warning",
109
- message: `msgctxt "${entry.foreignContext}" is not a manual id (no "."), so no message matches this entry; give it a manual id with a "." or remove it`,
110
- file,
111
- line: entry.line,
112
- locale
113
- });
114
- }
88
+ /** Each referenced source parsed once, however many locales check a translation against it. */
89
+ var SourceCache = class {
90
+ parsed = /* @__PURE__ */ new Map();
91
+ codec;
92
+ identities;
93
+ constructor(codec, identities) {
94
+ this.codec = codec;
95
+ this.identities = identities;
96
+ }
97
+ get(id) {
98
+ let source = this.parsed.get(id);
99
+ if (source === void 0) {
100
+ source = this.codec.parse(this.identities.get(id).canonical);
101
+ this.parsed.set(id, source);
102
+ }
103
+ return source;
104
+ }
105
+ };
115
106
  /**
116
107
  * Every referenced entry with a translation whose source still matches:
117
108
  * a translation that does not parse is a syntax finding; otherwise every
@@ -120,8 +111,7 @@ function syntaxOfEntry(into, entry, file, locale) {
120
111
  * plural categories, `maxLength`, apostrophes). Located at the entry's line,
121
112
  * since positions inside the text mean nothing in a catalog.
122
113
  */
123
- function translationFindings(project, status, identities, file) {
124
- const { codec } = project;
114
+ function translationFindings(codec, status, sources, file) {
125
115
  const out = {
126
116
  syntax: [],
127
117
  placeholders: []
@@ -142,21 +132,14 @@ function translationFindings(project, status, identities, file) {
142
132
  if (entry.translation === "") continue;
143
133
  const reason = status.reasons.get(id);
144
134
  if (reason === "source-mismatch" || reason === "scheme-mismatch") continue;
145
- const identity = identities.get(id);
146
- let parsed;
147
- try {
148
- parsed = codec.parse(entry.translation);
149
- } catch (error) {
150
- if (!isDiagnosticError(error)) throw error;
151
- out.syntax.push(at(error.diagnostic, id, entry));
152
- continue;
153
- }
154
- const options = {
135
+ const checked = checkTranslation(codec, {
136
+ translation: entry.translation,
137
+ source: sources.get(id),
155
138
  locale: status.locale,
156
- text: entry.translation
157
- };
158
- if (entry.maxLength !== null) options.maxLength = entry.maxLength;
159
- for (const diagnostic of validateTranslation(parsed, codec.parse(identity.canonical), options)) out.placeholders.push(at(diagnostic, id, entry));
139
+ maxLength: entry.maxLength
140
+ });
141
+ if (checked.parseError !== null) out.syntax.push(at(checked.parseError, id, entry));
142
+ for (const diagnostic of checked.findings) out.placeholders.push(at(diagnostic, id, entry));
160
143
  }
161
144
  return out;
162
145
  }
@@ -197,5 +180,4 @@ function missing(project, status, file) {
197
180
  });
198
181
  return out;
199
182
  }
200
- //#endregion
201
- export { syntaxOfEntry as n, check as t };
183
+ export { check };
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { t as run } from "./run-DYD_VXKy.js";
2
+ import { t as run } from "./run-CFzR3V93.js";
3
3
  import { createInterface } from "node:readline/promises";
4
- //#region src/cli/bin.ts
4
+ // src/cli/bin.ts
5
5
  /**
6
6
  * The `vi18n` binary (docs/INTERNALS.md §3.5). In the workspace this `.ts`
7
7
  * file runs with Node's type stripping; published, `dist/cli.js`. Everything
@@ -37,5 +37,4 @@ process.exitCode = await run(process.argv.slice(2), {
37
37
  }
38
38
  } } : {}
39
39
  });
40
- //#endregion
41
40
  export {};
@@ -1,4 +1,4 @@
1
- //#region src/core/extract/collect.ts
1
+ // src/core/extract/collect.ts
2
2
  const KEY = Symbol.for("@menapo/vi18n:extract-collector");
3
3
  /** The collector `vi18n extract --vite` set, or null outside it. */
4
4
  function activeCollector() {
@@ -9,5 +9,4 @@ function setCollector(collector) {
9
9
  if (collector === null) delete globalThis[KEY];
10
10
  else globalThis[KEY] = collector;
11
11
  }
12
- //#endregion
13
12
  export { setCollector as n, activeCollector as t };
@@ -0,0 +1,215 @@
1
+ import { r as resolveDuplicates } from "./duplicates-CsUXRX2J.js";
2
+ import { E as entryKey, T as entryCanonical, b as compareText } from "./library-BfSOBeBd.js";
3
+ // src/core/catalog/collisions.ts
4
+ const DELETED_HINT = "; if that message was deleted, run vi18n extract first";
5
+ function posix(path) {
6
+ return path.replaceAll("\\", "/");
7
+ }
8
+ function where(file, line) {
9
+ return line > 0 ? `${file}:${line}` : file;
10
+ }
11
+ function messagePlace(message) {
12
+ const location = message.locations?.[0] ?? (message.file === void 0 ? void 0 : {
13
+ file: message.file,
14
+ line: message.line ?? 0
15
+ });
16
+ if (location === void 0) return { text: "" };
17
+ return {
18
+ file: location.file,
19
+ line: location.line,
20
+ text: ` (${where(location.file, location.line)})`
21
+ };
22
+ }
23
+ /**
24
+ * Canonical text → where it was first seen, per generated key. Only entries
25
+ * whose source hashes to their key take part: in the `"id"` layout and JSON a
26
+ * hand-edited or stripped source note is a stale entry (reported as a source
27
+ * mismatch elsewhere), not a second text behind the id, and a file still on
28
+ * another id length would match nothing anyway. PLAN §5.5 makes a collision
29
+ * two live texts with one id, and §5.6 says an edited `#. source:` never
30
+ * changes what ships, which a build-stopping collision would; this narrows
31
+ * the wording of INTERNALS §2.6.7 to that intent.
32
+ */
33
+ function indexEntries(set, codec, ignoreKeys) {
34
+ const byKey = /* @__PURE__ */ new Map();
35
+ for (const loaded of set.values()) {
36
+ if (loaded.catalog === null) continue;
37
+ const { catalog } = resolveDuplicates(loaded.catalog, codec);
38
+ const file = posix(loaded.path);
39
+ for (const entry of catalog.entries) {
40
+ if (entry.manualId !== null || entry.foreignContext !== null || entry.source === null) continue;
41
+ const key = entryKey(entry, catalog.layout, codec);
42
+ if (key === null || ignoreKeys !== void 0 && ignoreKeys.has(key)) continue;
43
+ const canonical = entryCanonical(entry, codec);
44
+ if (canonical === null || codec.label(canonical) !== key) continue;
45
+ let texts = byKey.get(key);
46
+ if (texts === void 0) byKey.set(key, texts = /* @__PURE__ */ new Map());
47
+ const known = texts.get(canonical);
48
+ if (known === void 0) texts.set(canonical, {
49
+ locale: loaded.locale,
50
+ file,
51
+ line: entry.line,
52
+ translated: isTranslated(entry)
53
+ });
54
+ else if (!known.translated && isTranslated(entry)) known.translated = true;
55
+ }
56
+ }
57
+ return byKey;
58
+ }
59
+ function isTranslated(entry) {
60
+ return entry.translation !== "" && entry.state !== "untranslated";
61
+ }
62
+ function fixes(a, aTranslated, b, bTranslated, codec) {
63
+ const longer = `use a longer ids.length (now ${codec.idLength})`;
64
+ if (aTranslated !== bTranslated) return `give ${JSON.stringify(aTranslated ? b : a)} a manual id (it has no translations yet), or ${longer}`;
65
+ return `give the message that has no translations yet a manual id, or ${longer}`;
66
+ }
67
+ /** An `id-collision` error placed where the first text is (when known), the other text in `related`. */
68
+ function collision(id, message, place, related, locale) {
69
+ const diagnostic = {
70
+ code: "id-collision",
71
+ severity: "error",
72
+ message,
73
+ id,
74
+ ...locale === void 0 ? {} : { locale },
75
+ related: [related]
76
+ };
77
+ if (place.file !== void 0) {
78
+ diagnostic.file = place.file;
79
+ diagnostic.line = place.line;
80
+ }
81
+ return diagnostic;
82
+ }
83
+ /** The second text of a message collision, at the message's place when it has one. */
84
+ function relatedText(place, message) {
85
+ return {
86
+ ...place.file === void 0 ? {} : {
87
+ file: place.file,
88
+ line: place.line
89
+ },
90
+ message
91
+ };
92
+ }
93
+ /** Two messages with one id and different canonical text; returns the messages by id and their source literals. */
94
+ function messageCollisions(messages, passes) {
95
+ const byId = /* @__PURE__ */ new Map();
96
+ const literals = [];
97
+ for (const message of messages) {
98
+ if (!message.manual) for (const literal of message.sources ?? (message.source === void 0 ? [] : [message.source])) literals.push([message, literal]);
99
+ const first = byId.get(message.id);
100
+ if (first === void 0) {
101
+ byId.set(message.id, message);
102
+ continue;
103
+ }
104
+ if (first.canonical === message.canonical) continue;
105
+ if (!passes.report(message.id, first.canonical, message.canonical)) continue;
106
+ const a = messagePlace(first);
107
+ const b = messagePlace(message);
108
+ const texts = passes.entries.get(message.id);
109
+ const fix = message.manual ? "give each text its own manual id" : fixes(first.canonical, texts?.get(first.canonical)?.translated === true, message.canonical, texts?.get(message.canonical)?.translated === true, passes.codec);
110
+ passes.diagnostics.push(collision(message.id, `${message.id} is the id of two texts, ${JSON.stringify(first.canonical)}${a.text} and ${JSON.stringify(message.canonical)}${b.text}: ${fix}`, a, relatedText(b, message.canonical)));
111
+ }
112
+ return {
113
+ byId,
114
+ literals
115
+ };
116
+ }
117
+ /**
118
+ * A source literal that spells another message's id. `/server` resolves a
119
+ * string key through `sources.json` before taking it as an id (§2.11.2), so
120
+ * it would render this message on the server for every `t("<that id>")` the
121
+ * build emits, while the client renders the right one: a hydration mismatch.
122
+ */
123
+ function literalCollisions(literals, byId, passes) {
124
+ for (const [owner, literal] of literals) {
125
+ const other = byId.get(literal);
126
+ if (other === void 0 || other.id === owner.id) continue;
127
+ if (!passes.report(literal, `\0source:${owner.id}`, other.canonical)) continue;
128
+ const a = messagePlace(owner);
129
+ const b = messagePlace(other);
130
+ passes.diagnostics.push(collision(literal, `${JSON.stringify(literal)} is the text of ${owner.id}${a.text} and the id of ${JSON.stringify(other.canonical)}${b.text}, so the server would render one for the other: ${other.manual ? "rename the manual id" : `give ${JSON.stringify(owner.canonical)} a manual id`}`, a, relatedText(b, other.canonical)));
131
+ }
132
+ }
133
+ /** A generated message and an entry of any locale with its id but a different canonical source. */
134
+ function entryCollisions(byId, passes) {
135
+ for (const message of byId.values()) {
136
+ if (message.manual) continue;
137
+ const texts = passes.entries.get(message.id);
138
+ if (texts === void 0) continue;
139
+ const own = texts.get(message.canonical);
140
+ for (const [canonical, entry] of texts) {
141
+ if (canonical === message.canonical) continue;
142
+ if (!passes.report(message.id, message.canonical, canonical)) continue;
143
+ const place = messagePlace(message);
144
+ const live = passes.referenced?.has(message.id) === true;
145
+ passes.diagnostics.push(collision(message.id, `${message.id} is the id of ${JSON.stringify(message.canonical)}${place.text} and of ${JSON.stringify(canonical)} in the ${entry.locale} catalog (${where(entry.file, entry.line)}): ` + fixes(message.canonical, own?.translated === true, canonical, entry.translated, passes.codec) + (live ? "" : DELETED_HINT), place, {
146
+ file: entry.file,
147
+ line: entry.line,
148
+ message: canonical
149
+ }, entry.locale));
150
+ }
151
+ }
152
+ }
153
+ /** Two generated entries of the set with one key, no message, and different sources. */
154
+ function catalogCollisions(byId, passes) {
155
+ const colliding = [];
156
+ for (const [key, texts] of passes.entries) if (!byId.has(key) && texts.size > 1) colliding.push([key, texts]);
157
+ colliding.sort(([a], [b]) => compareText(a, b));
158
+ for (const [key, entryTexts] of colliding) {
159
+ const [first, ...others] = entryTexts;
160
+ if (first === void 0) continue;
161
+ const [firstText, firstEntry] = first;
162
+ const live = passes.referenced?.has(key) === true;
163
+ for (const [canonical, entry] of others) {
164
+ if (!passes.report(key, firstText, canonical)) continue;
165
+ passes.diagnostics.push({
166
+ code: "id-collision",
167
+ severity: "error",
168
+ message: `${key} is the id of ${JSON.stringify(firstText)} in the ${firstEntry.locale} catalog (${where(firstEntry.file, firstEntry.line)}) and of ${JSON.stringify(canonical)} in the ${entry.locale} catalog (${where(entry.file, entry.line)}): ` + fixes(firstText, firstEntry.translated, canonical, entry.translated, passes.codec) + (live ? "" : DELETED_HINT),
169
+ id: key,
170
+ locale: firstEntry.locale,
171
+ file: firstEntry.file,
172
+ line: firstEntry.line,
173
+ related: [{
174
+ file: entry.file,
175
+ line: entry.line,
176
+ message: canonical
177
+ }]
178
+ });
179
+ }
180
+ }
181
+ }
182
+ /**
183
+ * `id-collision` errors for: two messages with one id and different
184
+ * canonical text; a generated message and an entry of any locale with its id
185
+ * but a different canonical source; two generated entries of the set with
186
+ * one key and different sources.
187
+ *
188
+ * `referenced`: ids that count as live beyond `messages` (`extraction.keep`,
189
+ * apps sharing the set that this scan does not cover). A colliding entry
190
+ * whose text no message uses and whose id is not in `referenced` gets the
191
+ * "run vi18n extract first" hint, since extract would delete it.
192
+ * `ignoreKeys`: entries extract is about to delete, which therefore never
193
+ * collide.
194
+ */
195
+ function findCollisions(messages, set, codec, options = {}) {
196
+ const reported = /* @__PURE__ */ new Set();
197
+ const passes = {
198
+ codec,
199
+ entries: indexEntries(set, codec, options.ignoreKeys),
200
+ referenced: options.referenced,
201
+ report: (id, a, b) => {
202
+ const pair = a < b ? `${id}\0${a}\0${b}` : `${id}\0${b}\0${a}`;
203
+ if (reported.has(pair)) return false;
204
+ reported.add(pair);
205
+ return true;
206
+ },
207
+ diagnostics: []
208
+ };
209
+ const { byId, literals } = messageCollisions(messages, passes);
210
+ literalCollisions(literals, byId, passes);
211
+ entryCollisions(byId, passes);
212
+ catalogCollisions(byId, passes);
213
+ return passes.diagnostics;
214
+ }
215
+ export { findCollisions as t };