@verbaly/compiler 0.33.0 → 0.34.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.d.ts CHANGED
@@ -106,6 +106,7 @@ interface Analysis {
106
106
  tagged: TaggedMessage[];
107
107
  usedKeys: UsedKey[];
108
108
  strayImports: StrayImport[];
109
+ parseError?: string;
109
110
  }
110
111
  //#endregion
111
112
  //#region src/registry.d.ts
@@ -116,6 +117,10 @@ declare class MessageRegistry {
116
117
  messages(): Map<string, TaggedMessage>;
117
118
  usedKeys(): Map<string, string[]>;
118
119
  strayImports(): StrayImport[];
120
+ parseErrors(): {
121
+ file: string;
122
+ message: string;
123
+ }[];
119
124
  origins(): Map<string, string[]>;
120
125
  }
121
126
  //#endregion
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { dirname, join, relative } from "node:path";
4
4
  import { glob } from "tinyglobby";
5
5
  import { parse } from "@babel/parser";
6
6
  import { createHash } from "node:crypto";
7
- import { RICH_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
7
+ import { RICH_TAGS, VOID_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
8
8
  import picomatch from "picomatch";
9
9
  import MagicString from "magic-string";
10
10
  //#region src/catalog.ts
@@ -82,6 +82,14 @@ var MessageRegistry = class {
82
82
  strayImports() {
83
83
  return [...this.files.values()].flatMap((analysis) => analysis.strayImports);
84
84
  }
85
+ parseErrors() {
86
+ const out = [];
87
+ for (const [file, analysis] of this.files) if (analysis.parseError) out.push({
88
+ file,
89
+ message: analysis.parseError
90
+ });
91
+ return out;
92
+ }
85
93
  origins() {
86
94
  const out = this.usedKeys();
87
95
  for (const analysis of this.files.values()) for (const msg of analysis.tagged) {
@@ -99,6 +107,9 @@ function stableKey(message) {
99
107
  }
100
108
  //#endregion
101
109
  //#region src/analyze.ts
110
+ function parseErrorMessage(error) {
111
+ return error instanceof Error ? error.message : String(error);
112
+ }
102
113
  const SKIP_KEYS = /* @__PURE__ */ new Set([
103
114
  "loc",
104
115
  "leadingComments",
@@ -107,12 +118,13 @@ const SKIP_KEYS = /* @__PURE__ */ new Set([
107
118
  "extra"
108
119
  ]);
109
120
  const DEFAULT_T_NAMES = ["t"];
121
+ const JSX_FILE_RE = /\.(?:[cm]?jsx?|tsx)$/;
110
122
  function analyze(code, file, options = {}) {
111
123
  const names = new Set(options.tNames ?? DEFAULT_T_NAMES);
112
124
  const ast = parse(code, {
113
125
  sourceType: "module",
114
126
  errorRecovery: true,
115
- plugins: /\.[jt]sx$/.test(file) ? ["typescript", "jsx"] : ["typescript"]
127
+ plugins: JSX_FILE_RE.test(file) ? ["typescript", "jsx"] : ["typescript"]
116
128
  });
117
129
  const tagged = [];
118
130
  const usedKeys = [];
@@ -379,7 +391,17 @@ function isNode(value) {
379
391
  //#region src/sfc.ts
380
392
  const SFC_FILE_RE = /\.(?:svelte|vue|astro)$/;
381
393
  function analyzeFile(code, file) {
382
- return SFC_FILE_RE.test(file) ? analyzeSfc(code, file) : analyze(code, file);
394
+ if (SFC_FILE_RE.test(file)) return analyzeSfc(code, file);
395
+ try {
396
+ return analyze(code, file);
397
+ } catch (error) {
398
+ return {
399
+ tagged: [],
400
+ usedKeys: [],
401
+ strayImports: [],
402
+ parseError: parseErrorMessage(error)
403
+ };
404
+ }
383
405
  }
384
406
  const SCRIPT_RE = /(<script\b[^>]*>)([\s\S]*?)(<\/script\s*>)/gi;
385
407
  const STYLE_RE = /<style\b[^>]*>[\s\S]*?<\/style\s*>/gi;
@@ -395,12 +417,17 @@ function analyzeSfc(code, file) {
395
417
  const tagged = [];
396
418
  const usedKeys = [];
397
419
  const strayImports = [];
420
+ let parseError;
421
+ const block = (segment, offset) => {
422
+ parseError ??= segment.error;
423
+ merge(segment.analysis, offset, false);
424
+ };
398
425
  const frontmatter = file.endsWith(".astro") ? FRONTMATTER_RE.exec(code) : null;
399
- if (frontmatter?.[2]) merge(analyzeSegment(frontmatter[2], file, options), frontmatter[1].length, false);
426
+ if (frontmatter?.[2]) block(analyzeSegment(frontmatter[2], file, options), frontmatter[1].length);
400
427
  for (const match of code.matchAll(SCRIPT_RE)) {
401
428
  const content = match[2];
402
429
  if (!content) continue;
403
- merge(analyzeSegment(content, file, options), match.index + match[1].length, false);
430
+ block(analyzeSegment(content, file, options), match.index + match[1].length);
404
431
  }
405
432
  const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
406
433
  const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
@@ -414,13 +441,14 @@ function analyzeSfc(code, file) {
414
441
  if (range >= ranges.length || start < ranges[range][0]) continue;
415
442
  const end = expressionExtent(markup, start);
416
443
  if (end === void 0) continue;
417
- merge(analyzeSegment(code.slice(start, end), file, options), start, true);
444
+ merge(analyzeSegment(code.slice(start, end), file, options).analysis, start, true);
418
445
  consumedTo = end;
419
446
  }
420
447
  return {
421
448
  tagged,
422
449
  usedKeys,
423
- strayImports
450
+ strayImports,
451
+ ...parseError && { parseError }
424
452
  };
425
453
  function merge(analysis, offset, markupSegment) {
426
454
  if (!analysis) return;
@@ -443,9 +471,9 @@ function analyzeSfc(code, file) {
443
471
  }
444
472
  function analyzeSegment(code, file, options) {
445
473
  try {
446
- return analyze(code, file, options);
447
- } catch {
448
- return;
474
+ return { analysis: analyze(code, file, options) };
475
+ } catch (error) {
476
+ return { error: parseErrorMessage(error) };
449
477
  }
450
478
  }
451
479
  function blank(source, re) {
@@ -985,13 +1013,24 @@ function truncate(text, max) {
985
1013
  return text.length > max ? text.slice(0, max - 1) + "…" : text;
986
1014
  }
987
1015
  //#endregion
1016
+ //#region src/warn.ts
1017
+ const warned = /* @__PURE__ */ new Set();
1018
+ function warnParseError(file, message) {
1019
+ const line = `[verbaly] ${file}: could not be parsed (${message}), its messages were not extracted`;
1020
+ if (warned.has(line)) return;
1021
+ warned.add(line);
1022
+ console.warn(line);
1023
+ }
1024
+ //#endregion
988
1025
  //#region src/transform.ts
989
1026
  function quote(text, single) {
990
1027
  if (!single) return JSON.stringify(text);
991
1028
  return `'${text.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
992
1029
  }
993
1030
  function transformCode(code, file, analysis) {
994
- const { tagged } = analysis ?? analyzeFile(code, file);
1031
+ const own = analysis ?? analyzeFile(code, file);
1032
+ if (!analysis && own.parseError) warnParseError(file, own.parseError);
1033
+ const { tagged } = own;
995
1034
  if (tagged.length === 0) return null;
996
1035
  const s = new MagicString(code);
997
1036
  for (const msg of tagged) {
@@ -1039,6 +1078,7 @@ function createSourceFilter(cfg) {
1039
1078
  function transformSource(code, id, registry) {
1040
1079
  const analysis = analyzeFile(code, id);
1041
1080
  registry.update(id, analysis);
1081
+ if (analysis.parseError) warnParseError(id, analysis.parseError);
1042
1082
  const messages = {};
1043
1083
  for (const msg of analysis.tagged) messages[msg.key] ??= msg.message;
1044
1084
  return {
@@ -1212,22 +1252,7 @@ function structureMatches(source, translated) {
1212
1252
  //#region src/render.ts
1213
1253
  const HREFLANG_OPEN = "<!--verbaly:hreflang-->";
1214
1254
  const HREFLANG_CLOSE = "<!--/verbaly:hreflang-->";
1215
- const VOID_TAGS = /* @__PURE__ */ new Set([
1216
- "area",
1217
- "base",
1218
- "br",
1219
- "col",
1220
- "embed",
1221
- "hr",
1222
- "img",
1223
- "input",
1224
- "link",
1225
- "meta",
1226
- "param",
1227
- "source",
1228
- "track",
1229
- "wbr"
1230
- ]);
1255
+ const VOID = new Set(VOID_TAGS);
1231
1256
  const START_TAG = /<([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|'[^']*'|[^"'>])*)>/g;
1232
1257
  const ATTR = /([^\s=/"'<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
1233
1258
  function renderHtml(html, options) {
@@ -1274,7 +1299,7 @@ function renderHtml(html, options) {
1274
1299
  const args = parseArgs(attrs.get(argsAttr));
1275
1300
  if (key) {
1276
1301
  if (!v.has(key)) missing.add(key);
1277
- else if (!VOID_TAGS.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
1302
+ else if (!VOID.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
1278
1303
  const close = findClose(html, tagName, openEnd, inSkip);
1279
1304
  if (close) {
1280
1305
  const text = t(key, args);
@@ -1466,7 +1491,7 @@ function richToHtml(nodes, allowed, links) {
1466
1491
  if (target) attrs += ` target="${escapeAttr(target)}"`;
1467
1492
  if (rel) attrs += ` rel="${escapeAttr(rel)}"`;
1468
1493
  out += `<a${attrs}>${richToHtml(node.children, allowed, links)}</a>`;
1469
- } else if (allowed.has(node.name)) out += `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
1494
+ } else if (allowed.has(node.name)) out += VOID.has(node.name) ? `<${node.name}>` : `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
1470
1495
  else out += richToHtml(node.children, allowed, links);
1471
1496
  return out;
1472
1497
  }