@verbaly/compiler 0.72.0 → 0.73.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/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 = /\{\s*[a-zA-Z0-9_]+\s*,\s*(?:plural|selectordinal|select|number|date|time)\b/;
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
- let text = escapeText(cookedValue(quasis[0]));
707
- for (let i = 0; i < expressions.length; 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
- text += `{${name}}` + escapeText(cookedValue(quasis[i + 1]));
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;
@@ -1107,10 +1150,35 @@ function surplus(a, b) {
1107
1150
  function list(items) {
1108
1151
  return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
1109
1152
  }
1110
- const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*[\w.]+\s*[|:][^{}]*\}\}/;
1153
+ const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*([\w.]+)(\s*[|:][^{}]*)\}\}/;
1154
+ const ESCAPED_ICU = /(?<!\{)\{\{\s*([\w.]+)\s*,\s*(plural|selectordinal|select|number|date|time)\b/;
1155
+ const ICU_SLICE = /^\{\{([\w.]+), (\w+), …\}\}$/;
1156
+ const ESCAPED_OPEN = /(?<!\{)\{\{\{([\w.]+)\}(\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
1111
1157
  function escapedSyntax(message) {
1158
+ const icu = ESCAPED_ICU.exec(message);
1159
+ if (icu) return `{{${icu[1]}, ${icu[2]}, …}}`;
1160
+ const open = ESCAPED_OPEN.exec(message);
1161
+ if (open) return `{\${${open[1]}}${open[2]}…`;
1112
1162
  return ESCAPED_SYNTAX.exec(message)?.[0];
1113
1163
  }
1164
+ function shownSlice(slice) {
1165
+ return slice.startsWith("{{") ? slice.slice(1, -1) : slice;
1166
+ }
1167
+ function inlineBlock(slice) {
1168
+ if (slice.startsWith("{${")) return "the same block with its closing }";
1169
+ const icu = ICU_SLICE.exec(slice);
1170
+ if (icu) {
1171
+ const [, name = "", type = ""] = icu;
1172
+ if (type === "plural") return `{\${${name}} | one: … | other: …}`;
1173
+ if (type === "select") return `{\${${name}} | …: … | other: …}`;
1174
+ if (type === "selectordinal") return "t(key, params), with the ICU message in the catalog";
1175
+ return `{\${${name}}:${type}}`;
1176
+ }
1177
+ const match = ESCAPED_SYNTAX.exec(slice);
1178
+ if (!match) return slice;
1179
+ const [, name = "", tail = ""] = match;
1180
+ return `{\${${name}}${tail.trimStart().startsWith(":") ? `:${tail.trimStart().slice(1).trimStart()}` : tail}}`;
1181
+ }
1114
1182
  function validateMessage(message, locale) {
1115
1183
  const issues = [];
1116
1184
  for (const [name, shape] of shapes(message)) {
@@ -1655,7 +1723,7 @@ function formatFinding(finding, root) {
1655
1723
  const names = finding.params.map((name) => `{${name}}`).join(", ");
1656
1724
  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
1725
  }
1658
- case "escaped": return `${sourcePlace(finding.site, root)}: ${finding.slice} renders as literal text, a tagged template has no params (use t(key, params) for a plural or format block, or pass a \${…} value)`;
1726
+ case "escaped": return `${sourcePlace(finding.site, root)}: ${shownSlice(finding.slice)} renders as literal text: write ${inlineBlock(finding.slice)}`;
1659
1727
  case "unparsed": return `${sourcePlace(finding, root)}: could not be parsed (${finding.message}), its messages were not extracted`;
1660
1728
  case "outdated": return `[${finding.locale}] ${finding.key}: translated from an older source text, update it or keep it with \`npx verbaly review --approve\``;
1661
1729
  case "lock": {
@@ -2808,7 +2876,7 @@ async function doctor(cfg) {
2808
2876
  file: rel(msg.file),
2809
2877
  slice: escapedSyntax(msg.message)
2810
2878
  })).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})`, "a tagged template takes its values from ${…}: use t(key, params) for a plural or format block");
2879
+ 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
2880
  }
2813
2881
  if (source && scanning) {
2814
2882
  const extracted = registry.messages();