@verbaly/compiler 0.28.0 → 0.29.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/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { basename, dirname, join, relative, resolve } from "node:path";
3
2
  import { parseArgs } from "node:util";
4
3
  import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
4
+ import { basename, dirname, join, relative, resolve } from "node:path";
5
5
  import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
6
6
  import { pathToFileURL } from "node:url";
7
7
  import { glob } from "tinyglobby";
@@ -769,6 +769,8 @@ const COMMENT_RE = /<!--[\s\S]*?-->/g;
769
769
  const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
770
770
  const CANDIDATE_SVELTE = /(?<![\w$])\$?t(?=[`(]|\.id\()/g;
771
771
  const CANDIDATE_VUE = /(?<![\w$])t(?=[`(]|\.id\()/g;
772
+ const MUSTACHE_RE = /\{\{[\s\S]*?\}\}/g;
773
+ const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
772
774
  function analyzeSfc(code, file) {
773
775
  const svelte = file.endsWith(".svelte");
774
776
  const options = svelte ? { tNames: ["t", "$t"] } : void 0;
@@ -783,10 +785,14 @@ function analyzeSfc(code, file) {
783
785
  }
784
786
  const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
785
787
  const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
788
+ const ranges = file.endsWith(".vue") ? vueExpressionRanges(markup) : braceRanges(markup);
789
+ let range = 0;
786
790
  let consumedTo = 0;
787
791
  for (const match of markup.matchAll(candidates)) {
788
792
  const start = match.index;
789
793
  if (start < consumedTo) continue;
794
+ while (range < ranges.length && ranges[range][1] <= start) range += 1;
795
+ if (range >= ranges.length || start < ranges[range][0]) continue;
790
796
  const end = expressionExtent(markup, start);
791
797
  if (end === void 0) continue;
792
798
  merge(analyzeSegment(code.slice(start, end), file, options), start, true);
@@ -824,6 +830,31 @@ function analyzeSegment(code, file, options) {
824
830
  function blank(source, re) {
825
831
  return source.replace(re, (m) => " ".repeat(m.length));
826
832
  }
833
+ function braceRanges(markup) {
834
+ const ranges = [];
835
+ let i = 0;
836
+ while (i < markup.length) {
837
+ if (markup[i] === "{") {
838
+ const end = balancedEnd(markup, i);
839
+ if (end !== void 0) {
840
+ ranges.push([i + 1, end - 1]);
841
+ i = end;
842
+ continue;
843
+ }
844
+ }
845
+ i += 1;
846
+ }
847
+ return ranges;
848
+ }
849
+ function vueExpressionRanges(markup) {
850
+ const ranges = [];
851
+ for (const match of markup.matchAll(MUSTACHE_RE)) ranges.push([match.index + 2, match.index + match[0].length - 2]);
852
+ for (const match of markup.matchAll(DIRECTIVE_RE)) {
853
+ const valueStart = match.index + match[0].length - match[1].length + 1;
854
+ ranges.push([valueStart, valueStart + match[1].length - 2]);
855
+ }
856
+ return ranges.sort((a, b) => a[0] - b[0]);
857
+ }
827
858
  function expressionExtent(code, start) {
828
859
  let i = start;
829
860
  if (code[i] === "$") i += 1;
@@ -877,6 +908,12 @@ async function extractProject(cfg) {
877
908
  for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
878
909
  return registry;
879
910
  }
911
+ async function collectOrigins(cfg) {
912
+ const registry = await extractProject(cfg);
913
+ const origins = {};
914
+ for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
915
+ return origins;
916
+ }
880
917
  function syncCatalogs(cfg, catalogs, registry) {
881
918
  const added = {};
882
919
  const source = catalogs[cfg.sourceLocale] ??= {};
@@ -1528,6 +1565,12 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
1528
1565
  }
1529
1566
  return result;
1530
1567
  }
1568
+ async function resolveProvider(cfg, model) {
1569
+ const configured = cfg.translate.provider;
1570
+ if (typeof configured === "function") return configured;
1571
+ const { claudeProvider } = await import("./claude-h9WA0ppg.js");
1572
+ return claudeProvider({ model: model ?? cfg.translate.model });
1573
+ }
1531
1574
  function structureMatches(source, translated) {
1532
1575
  return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
1533
1576
  }
@@ -2727,18 +2770,6 @@ function rejectStrayFlags(command, values) {
2727
2770
  process.exitCode = 1;
2728
2771
  return true;
2729
2772
  }
2730
- async function collectOrigins(cfg) {
2731
- const registry = await extractProject(cfg);
2732
- const origins = {};
2733
- for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
2734
- return origins;
2735
- }
2736
- async function resolveProvider(cfg, model) {
2737
- const configured = cfg.translate.provider;
2738
- if (typeof configured === "function") return configured;
2739
- const { claudeProvider } = await import("./claude-h9WA0ppg.js");
2740
- return claudeProvider({ model: model ?? cfg.translate.model });
2741
- }
2742
2773
  //#endregion
2743
2774
  //#region src/cli.ts
2744
2775
  runCli().catch((error) => {