@verbaly/compiler 0.72.0 → 0.74.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/cli.js +112 -14
- package/dist/cli.js.map +1 -1
- package/dist/index.js +113 -15
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -147,7 +147,7 @@ function wantsNesting(cfg, locale, existing, previous) {
|
|
|
147
147
|
if (locale === cfg.sourceLocale) return false;
|
|
148
148
|
return isNested(readTree(cfg, cfg.sourceLocale));
|
|
149
149
|
}
|
|
150
|
-
const ICU_ARG =
|
|
150
|
+
const ICU_ARG = /(^|[^{])\{\s*[a-zA-Z0-9_]+\s*,\s*(?:plural|selectordinal|select|number|date|time)\b/;
|
|
151
151
|
function needsIcu(catalogs) {
|
|
152
152
|
for (const catalog of Object.values(catalogs)) for (const message of Object.values(flatten(catalog))) if (ICU_ARG.test(message)) return true;
|
|
153
153
|
return false;
|
|
@@ -698,13 +698,19 @@ function isTReference(node, names) {
|
|
|
698
698
|
}
|
|
699
699
|
return false;
|
|
700
700
|
}
|
|
701
|
+
const BLOCK_TAIL = /^(?:\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
|
|
701
702
|
function buildMessage(code, quasi, names) {
|
|
702
703
|
const quasis = quasi.quasis;
|
|
703
704
|
const expressions = quasi.expressions;
|
|
705
|
+
const texts = quasis.map(cookedValue);
|
|
704
706
|
const params = [];
|
|
705
707
|
const taken = /* @__PURE__ */ new Map();
|
|
706
|
-
|
|
707
|
-
for (let i = 0; i <
|
|
708
|
+
const tokens = [];
|
|
709
|
+
for (let i = 0; i < texts.length; i++) {
|
|
710
|
+
const chunk = texts[i];
|
|
711
|
+
const head = i < expressions.length && chunk.endsWith("{") && BLOCK_TAIL.test(texts[i + 1]);
|
|
712
|
+
for (const char of head ? chunk.slice(0, -1) : chunk) tokens.push(char);
|
|
713
|
+
if (i === expressions.length) break;
|
|
708
714
|
const expr = expressions[i];
|
|
709
715
|
if (!expr) return void 0;
|
|
710
716
|
if (containsTaggedT(expr, names)) return void 0;
|
|
@@ -715,13 +721,50 @@ function buildMessage(code, quasi, names) {
|
|
|
715
721
|
start: expr.start,
|
|
716
722
|
end: expr.end
|
|
717
723
|
});
|
|
718
|
-
|
|
724
|
+
tokens.push({
|
|
725
|
+
name,
|
|
726
|
+
head
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
let text = "";
|
|
730
|
+
for (let k = 0; k < tokens.length; k++) {
|
|
731
|
+
const token = tokens[k];
|
|
732
|
+
if (typeof token === "string") text += token === "{" || token === "}" ? token + token : token;
|
|
733
|
+
else if (!token.head) text += `{${token.name}}`;
|
|
734
|
+
else {
|
|
735
|
+
const end = blockEnd(tokens, k, false) ?? blockEnd(tokens, k, true);
|
|
736
|
+
if (end === void 0) return {
|
|
737
|
+
text: texts.map((chunk, i) => escapeText(chunk) + (params[i] ? `{${params[i].name}}` : "")).join(""),
|
|
738
|
+
params
|
|
739
|
+
};
|
|
740
|
+
for (let m = k; m <= end; m++) text += tokenText(tokens[m]);
|
|
741
|
+
k = end;
|
|
742
|
+
}
|
|
719
743
|
}
|
|
720
744
|
return {
|
|
721
745
|
text,
|
|
722
746
|
params
|
|
723
747
|
};
|
|
724
748
|
}
|
|
749
|
+
function tokenText(token) {
|
|
750
|
+
if (typeof token === "string") return token;
|
|
751
|
+
return token.head ? `{${token.name}` : `{${token.name}}`;
|
|
752
|
+
}
|
|
753
|
+
function blockEnd(tokens, start, pairs) {
|
|
754
|
+
let depth = 0;
|
|
755
|
+
for (let k = start; k < tokens.length; k++) {
|
|
756
|
+
const token = tokens[k];
|
|
757
|
+
if (typeof token !== "string") {
|
|
758
|
+
if (token.head) depth += 1;
|
|
759
|
+
} else if (token === "{" || token === "}") {
|
|
760
|
+
if (tokens[k + 1] === token && (token === "{" || !(pairs && depth > 1))) k += 1;
|
|
761
|
+
else {
|
|
762
|
+
depth += token === "{" ? 1 : -1;
|
|
763
|
+
if (depth === 0) return k;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
725
768
|
function cookedValue(element) {
|
|
726
769
|
if (!element) return "";
|
|
727
770
|
const value = element.value;
|
|
@@ -1040,7 +1083,26 @@ function pruneCatalogs(cfg, catalogs, registry) {
|
|
|
1040
1083
|
}
|
|
1041
1084
|
//#endregion
|
|
1042
1085
|
//#region src/validate.ts
|
|
1043
|
-
|
|
1086
|
+
let remembered = /* @__PURE__ */ new Map();
|
|
1087
|
+
let reading;
|
|
1088
|
+
function read(message) {
|
|
1089
|
+
if (!reading) return parse(message, parseIcu);
|
|
1090
|
+
let tree = reading.get(message);
|
|
1091
|
+
if (!tree) {
|
|
1092
|
+
tree = remembered.get(message) ?? parse(message, parseIcu);
|
|
1093
|
+
reading.set(message, tree);
|
|
1094
|
+
}
|
|
1095
|
+
return tree;
|
|
1096
|
+
}
|
|
1097
|
+
function gatePass(run) {
|
|
1098
|
+
reading = /* @__PURE__ */ new Map();
|
|
1099
|
+
try {
|
|
1100
|
+
return run();
|
|
1101
|
+
} finally {
|
|
1102
|
+
remembered = reading;
|
|
1103
|
+
reading = void 0;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1044
1106
|
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
1045
1107
|
"zero",
|
|
1046
1108
|
"one",
|
|
@@ -1107,10 +1169,35 @@ function surplus(a, b) {
|
|
|
1107
1169
|
function list(items) {
|
|
1108
1170
|
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
1109
1171
|
}
|
|
1110
|
-
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*[\w.]
|
|
1172
|
+
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*([\w.]+)(\s*[|:][^{}]*)\}\}/;
|
|
1173
|
+
const ESCAPED_ICU = /(?<!\{)\{\{\s*([\w.]+)\s*,\s*(plural|selectordinal|select|number|date|time)\b/;
|
|
1174
|
+
const ICU_SLICE = /^\{\{([\w.]+), (\w+), …\}\}$/;
|
|
1175
|
+
const ESCAPED_OPEN = /(?<!\{)\{\{\{([\w.]+)\}(\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
|
|
1111
1176
|
function escapedSyntax(message) {
|
|
1177
|
+
const icu = ESCAPED_ICU.exec(message);
|
|
1178
|
+
if (icu) return `{{${icu[1]}, ${icu[2]}, …}}`;
|
|
1179
|
+
const open = ESCAPED_OPEN.exec(message);
|
|
1180
|
+
if (open) return `{\${${open[1]}}${open[2]}…`;
|
|
1112
1181
|
return ESCAPED_SYNTAX.exec(message)?.[0];
|
|
1113
1182
|
}
|
|
1183
|
+
function shownSlice(slice) {
|
|
1184
|
+
return slice.startsWith("{{") ? slice.slice(1, -1) : slice;
|
|
1185
|
+
}
|
|
1186
|
+
function inlineBlock(slice) {
|
|
1187
|
+
if (slice.startsWith("{${")) return "the same block with its closing }";
|
|
1188
|
+
const icu = ICU_SLICE.exec(slice);
|
|
1189
|
+
if (icu) {
|
|
1190
|
+
const [, name = "", type = ""] = icu;
|
|
1191
|
+
if (type === "plural") return `{\${${name}} | one: … | other: …}`;
|
|
1192
|
+
if (type === "select") return `{\${${name}} | …: … | other: …}`;
|
|
1193
|
+
if (type === "selectordinal") return "t(key, params), with the ICU message in the catalog";
|
|
1194
|
+
return `{\${${name}}:${type}}`;
|
|
1195
|
+
}
|
|
1196
|
+
const match = ESCAPED_SYNTAX.exec(slice);
|
|
1197
|
+
if (!match) return slice;
|
|
1198
|
+
const [, name = "", tail = ""] = match;
|
|
1199
|
+
return `{\${${name}}${tail.trimStart().startsWith(":") ? `:${tail.trimStart().slice(1).trimStart()}` : tail}}`;
|
|
1200
|
+
}
|
|
1114
1201
|
function validateMessage(message, locale) {
|
|
1115
1202
|
const issues = [];
|
|
1116
1203
|
for (const [name, shape] of shapes(message)) {
|
|
@@ -1655,7 +1742,7 @@ function formatFinding(finding, root) {
|
|
|
1655
1742
|
const names = finding.params.map((name) => `{${name}}`).join(", ");
|
|
1656
1743
|
return `${sourcePlace(finding.site, root)}: ${names} in "${truncate(finding.site.message, 60)}" reaches the translator without a name, put the value in a named variable first`;
|
|
1657
1744
|
}
|
|
1658
|
-
case "escaped": return `${sourcePlace(finding.site, root)}: ${finding.slice} renders as literal text
|
|
1745
|
+
case "escaped": return `${sourcePlace(finding.site, root)}: ${shownSlice(finding.slice)} renders as literal text: write ${inlineBlock(finding.slice)}`;
|
|
1659
1746
|
case "unparsed": return `${sourcePlace(finding, root)}: could not be parsed (${finding.message}), its messages were not extracted`;
|
|
1660
1747
|
case "outdated": return `[${finding.locale}] ${finding.key}: translated from an older source text, update it or keep it with \`npx verbaly review --approve\``;
|
|
1661
1748
|
case "lock": {
|
|
@@ -2248,6 +2335,9 @@ function warningCount(result) {
|
|
|
2248
2335
|
return result.broken.filter((entry) => entry.severity === "warning").length + result.extra.length + result.divergent.length + result.outdated.length;
|
|
2249
2336
|
}
|
|
2250
2337
|
function check(cfg, catalogs, registry, fingerprints = {}) {
|
|
2338
|
+
return gatePass(() => checkPass(cfg, catalogs, registry, fingerprints));
|
|
2339
|
+
}
|
|
2340
|
+
function checkPass(cfg, catalogs, registry, fingerprints) {
|
|
2251
2341
|
const extracted = registry.messages();
|
|
2252
2342
|
const flat = {};
|
|
2253
2343
|
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
@@ -2537,14 +2627,20 @@ function suggestedPattern(rel) {
|
|
|
2537
2627
|
const slash = rel.indexOf("/");
|
|
2538
2628
|
return slash < 0 ? rel : `${rel.slice(0, slash)}/**/*.${SOURCE_EXTENSIONS}`;
|
|
2539
2629
|
}
|
|
2540
|
-
function
|
|
2541
|
-
if (cfg.include.length === 0) return () => void 0;
|
|
2630
|
+
function outsidePath(cfg) {
|
|
2542
2631
|
const included = picomatch(cfg.include);
|
|
2543
2632
|
const excluded = picomatch(cfg.exclude);
|
|
2544
|
-
return (
|
|
2633
|
+
return (id) => {
|
|
2545
2634
|
const rel = relativeId(cfg, id);
|
|
2546
|
-
|
|
2547
|
-
|
|
2635
|
+
return rel.startsWith("..") || included(rel) || excluded(rel) ? void 0 : rel;
|
|
2636
|
+
};
|
|
2637
|
+
}
|
|
2638
|
+
function createOutsideCheck(cfg) {
|
|
2639
|
+
if (cfg.include.length === 0) return () => void 0;
|
|
2640
|
+
const outside = outsidePath(cfg);
|
|
2641
|
+
return (code, id) => {
|
|
2642
|
+
const rel = outside(id);
|
|
2643
|
+
if (rel === void 0 || !TAGGED_HINT.test(code)) return void 0;
|
|
2548
2644
|
if (analyzeFile(code, id).tagged.length === 0) return void 0;
|
|
2549
2645
|
return {
|
|
2550
2646
|
file: id,
|
|
@@ -2561,6 +2657,7 @@ async function reportOutsideInclude(cfg) {
|
|
|
2561
2657
|
async function filesOutsideInclude(cfg) {
|
|
2562
2658
|
if (cfg.include.length === 0) return [];
|
|
2563
2659
|
const check = createOutsideCheck(cfg);
|
|
2660
|
+
const outside = outsidePath(cfg);
|
|
2564
2661
|
const files = await glob([`**/*.${SOURCE_EXTENSIONS}`], {
|
|
2565
2662
|
cwd: cfg.root,
|
|
2566
2663
|
ignore: [...NOT_SOURCE, ...cfg.exclude],
|
|
@@ -2568,14 +2665,15 @@ async function filesOutsideInclude(cfg) {
|
|
|
2568
2665
|
});
|
|
2569
2666
|
const found = [];
|
|
2570
2667
|
for (const file of files.sort()) {
|
|
2668
|
+
if (outside(file) === void 0) continue;
|
|
2571
2669
|
let code;
|
|
2572
2670
|
try {
|
|
2573
2671
|
code = readFileSync(file, "utf8");
|
|
2574
2672
|
} catch {
|
|
2575
2673
|
continue;
|
|
2576
2674
|
}
|
|
2577
|
-
const
|
|
2578
|
-
if (
|
|
2675
|
+
const entry = check(code, file);
|
|
2676
|
+
if (entry) found.push(entry);
|
|
2579
2677
|
}
|
|
2580
2678
|
return found;
|
|
2581
2679
|
}
|
|
@@ -2808,7 +2906,7 @@ async function doctor(cfg) {
|
|
|
2808
2906
|
file: rel(msg.file),
|
|
2809
2907
|
slice: escapedSyntax(msg.message)
|
|
2810
2908
|
})).filter((entry) => entry.slice !== void 0);
|
|
2811
|
-
if (escaped.length > 0) warn("messages", `a block ships as literal text in ${counted(escaped.length, "extracted message")} (${escaped[0].file}: ${escaped[0].slice})`,
|
|
2909
|
+
if (escaped.length > 0) warn("messages", `a block ships as literal text in ${counted(escaped.length, "extracted message")} (${escaped[0].file}: ${shownSlice(escaped[0].slice)})`, `in a tagged template, write ${inlineBlock(escaped[0].slice)}`);
|
|
2812
2910
|
}
|
|
2813
2911
|
if (source && scanning) {
|
|
2814
2912
|
const extracted = registry.messages();
|