@verbaly/compiler 0.31.0 → 0.32.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 +60 -7
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +43 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
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";
|
|
2
3
|
import { parseArgs } from "node:util";
|
|
3
4
|
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, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { glob } from "tinyglobby";
|
|
@@ -113,6 +113,10 @@ function surplus(a, b) {
|
|
|
113
113
|
function list(items) {
|
|
114
114
|
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
115
115
|
}
|
|
116
|
+
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*[\w.]+\s*[|:][^{}]*\}\}/;
|
|
117
|
+
function escapedSyntax(message) {
|
|
118
|
+
return ESCAPED_SYNTAX.exec(message)?.[0];
|
|
119
|
+
}
|
|
116
120
|
function validateMessage(message, locale) {
|
|
117
121
|
const issues = [];
|
|
118
122
|
for (const [name, shape] of shapes(message)) {
|
|
@@ -691,6 +695,9 @@ var MessageRegistry = class {
|
|
|
691
695
|
}
|
|
692
696
|
return out;
|
|
693
697
|
}
|
|
698
|
+
strayImports() {
|
|
699
|
+
return [...this.files.values()].flatMap((analysis) => analysis.strayImports);
|
|
700
|
+
}
|
|
694
701
|
origins() {
|
|
695
702
|
const out = this.usedKeys();
|
|
696
703
|
for (const analysis of this.files.values()) for (const msg of analysis.tagged) {
|
|
@@ -725,8 +732,10 @@ function analyze(code, file, options = {}) {
|
|
|
725
732
|
});
|
|
726
733
|
const tagged = [];
|
|
727
734
|
const usedKeys = [];
|
|
735
|
+
const strayImports = [];
|
|
728
736
|
walk(ast.program, (node) => {
|
|
729
|
-
if (node.type === "
|
|
737
|
+
if (node.type === "ImportDeclaration") collectStrayImports(node, file, strayImports);
|
|
738
|
+
else if (node.type === "TaggedTemplateExpression") {
|
|
730
739
|
const tag = node.tag;
|
|
731
740
|
const explicit = explicitId(tag, names);
|
|
732
741
|
if (!explicit && !isTReference(tag, names)) return;
|
|
@@ -755,9 +764,24 @@ function analyze(code, file, options = {}) {
|
|
|
755
764
|
});
|
|
756
765
|
return {
|
|
757
766
|
tagged,
|
|
758
|
-
usedKeys
|
|
767
|
+
usedKeys,
|
|
768
|
+
strayImports
|
|
759
769
|
};
|
|
760
770
|
}
|
|
771
|
+
const VERBALY_PACKAGE = /^(?:verbaly|@verbaly\/[\w-]+)$/;
|
|
772
|
+
function collectStrayImports(node, file, out) {
|
|
773
|
+
const source = node.source;
|
|
774
|
+
if (typeof source.value !== "string" || !VERBALY_PACKAGE.test(source.value)) return;
|
|
775
|
+
for (const spec of node.specifiers) {
|
|
776
|
+
if (spec.type !== "ImportSpecifier") continue;
|
|
777
|
+
const imported = spec.imported;
|
|
778
|
+
if (imported.type === "Identifier" && imported.name === "t") out.push({
|
|
779
|
+
name: "t",
|
|
780
|
+
source: source.value,
|
|
781
|
+
file
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}
|
|
761
785
|
function handleTrans(code, node, file, tagged, usedKeys, names) {
|
|
762
786
|
const opening = node.openingElement;
|
|
763
787
|
const nameNode = opening.name;
|
|
@@ -986,6 +1010,7 @@ function analyzeSfc(code, file) {
|
|
|
986
1010
|
const options = svelte ? { tNames: ["t", "$t"] } : void 0;
|
|
987
1011
|
const tagged = [];
|
|
988
1012
|
const usedKeys = [];
|
|
1013
|
+
const strayImports = [];
|
|
989
1014
|
const frontmatter = file.endsWith(".astro") ? FRONTMATTER_RE.exec(code) : null;
|
|
990
1015
|
if (frontmatter?.[2]) merge(analyzeSegment(frontmatter[2], file, options), frontmatter[1].length, false);
|
|
991
1016
|
for (const match of code.matchAll(SCRIPT_RE)) {
|
|
@@ -1010,7 +1035,8 @@ function analyzeSfc(code, file) {
|
|
|
1010
1035
|
}
|
|
1011
1036
|
return {
|
|
1012
1037
|
tagged,
|
|
1013
|
-
usedKeys
|
|
1038
|
+
usedKeys,
|
|
1039
|
+
strayImports
|
|
1014
1040
|
};
|
|
1015
1041
|
function merge(analysis, offset, markupSegment) {
|
|
1016
1042
|
if (!analysis) return;
|
|
@@ -1028,6 +1054,7 @@ function analyzeSfc(code, file) {
|
|
|
1028
1054
|
...markupSegment && { singleQuote: true }
|
|
1029
1055
|
});
|
|
1030
1056
|
usedKeys.push(...analysis.usedKeys);
|
|
1057
|
+
strayImports.push(...analysis.strayImports);
|
|
1031
1058
|
}
|
|
1032
1059
|
}
|
|
1033
1060
|
function analyzeSegment(code, file, options) {
|
|
@@ -1330,7 +1357,7 @@ async function doctor(cfg) {
|
|
|
1330
1357
|
const deps = readDependencies(cfg.root);
|
|
1331
1358
|
const host = detectHost(cfg.root);
|
|
1332
1359
|
const installed = WIRING_PACKAGES.find((pkg) => deps[pkg]);
|
|
1333
|
-
if (!host) ok("plugin", "no bundler detected, the CLI flow (extract/check) applies");
|
|
1360
|
+
if (!host) ok("plugin", "no framework or bundler detected, the CLI flow (extract/check) applies");
|
|
1334
1361
|
else if (installed) ok("plugin", `${installed} installed for ${host.name}`);
|
|
1335
1362
|
else warn("plugin", `${host.name} detected but ${host.pkg} is not installed`, `pnpm add -D ${host.pkg} and ${host.wire}`);
|
|
1336
1363
|
const scanning = cfg.include.length > 0;
|
|
@@ -1342,6 +1369,18 @@ async function doctor(cfg) {
|
|
|
1342
1369
|
else ok("types", "verbaly.d.ts is up to date");
|
|
1343
1370
|
}
|
|
1344
1371
|
const registry = await extractProject(cfg);
|
|
1372
|
+
if (scanning) {
|
|
1373
|
+
const stray = registry.strayImports();
|
|
1374
|
+
if (stray.length > 0) {
|
|
1375
|
+
const files = [...new Set(stray.map((entry) => rel(entry.file)))];
|
|
1376
|
+
error("imports", `${files.length} ${files.length === 1 ? "file imports" : "files import"} t from a verbaly package, which never exports it (${preview(files)})`, "t comes from your instance (React: const t = useT()) or from virtual:verbaly");
|
|
1377
|
+
}
|
|
1378
|
+
const escaped = [...registry.messages().values()].map((msg) => ({
|
|
1379
|
+
file: rel(msg.file),
|
|
1380
|
+
slice: escapedSyntax(msg.message)
|
|
1381
|
+
})).filter((entry) => entry.slice !== void 0);
|
|
1382
|
+
if (escaped.length > 0) warn("messages", `${escaped.length} extracted ${escaped.length === 1 ? "message keeps" : "messages keep"} a block as literal text (${escaped[0].file}: ${escaped[0].slice})`, "a tagged template takes its values from ${…}: use t(key, params) for a plural or format block");
|
|
1383
|
+
}
|
|
1345
1384
|
if (source && scanning) {
|
|
1346
1385
|
const extracted = registry.messages();
|
|
1347
1386
|
const used = registry.usedKeys();
|
|
@@ -2635,7 +2674,7 @@ function line(node) {
|
|
|
2635
2674
|
const HELP = `verbaly · i18n compiler
|
|
2636
2675
|
|
|
2637
2676
|
Usage:
|
|
2638
|
-
verbaly init scaffold config + locale catalogs (detects your
|
|
2677
|
+
verbaly init scaffold config + locale catalogs (detects your framework)
|
|
2639
2678
|
verbaly doctor diagnose the setup (config, catalogs, plugin, types, keys)
|
|
2640
2679
|
verbaly wrap find hardcoded JSX text and wrap it in t\`…\` (report; --write applies)
|
|
2641
2680
|
verbaly extract scan sources, update catalogs and types
|
|
@@ -2779,6 +2818,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2779
2818
|
const total = registry.messages().size;
|
|
2780
2819
|
console.log(`[verbaly] ${total} messages · locales: ${cfg.locales.join(", ")}${dryRun ? " (dry run, nothing written)" : ""}`);
|
|
2781
2820
|
for (const [locale, keys] of Object.entries(added)) console.log(` ${locale}: ${dryRun ? `would add ${keys.length}` : `+${keys.length}`}`);
|
|
2821
|
+
reportEscapedSyntax(cfg, registry);
|
|
2782
2822
|
}
|
|
2783
2823
|
await runExtract();
|
|
2784
2824
|
if (values.watch) {
|
|
@@ -2989,6 +3029,19 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2989
3029
|
console.error(`[verbaly] unknown command "${command}"\n${HELP}`);
|
|
2990
3030
|
process.exitCode = 1;
|
|
2991
3031
|
}
|
|
3032
|
+
function formatCliError(error) {
|
|
3033
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3034
|
+
return message.startsWith("[verbaly]") ? message : `[verbaly] ${message}`;
|
|
3035
|
+
}
|
|
3036
|
+
function reportEscapedSyntax(cfg, registry) {
|
|
3037
|
+
for (const msg of registry.messages().values()) {
|
|
3038
|
+
const slice = escapedSyntax(msg.message);
|
|
3039
|
+
if (!slice) continue;
|
|
3040
|
+
const file = relative(cfg.root, msg.file).replaceAll("\\", "/");
|
|
3041
|
+
console.warn(` ${file}: ${slice} renders as literal text, a tagged template has no params`);
|
|
3042
|
+
console.warn(" use t(key, params) for a plural or format block, or pass a ${…} value");
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
2992
3045
|
const COMMON_FLAGS = /* @__PURE__ */ new Set([
|
|
2993
3046
|
"root",
|
|
2994
3047
|
"dir",
|
|
@@ -3041,7 +3094,7 @@ function rejectStrayFlags(command, values) {
|
|
|
3041
3094
|
//#endregion
|
|
3042
3095
|
//#region src/cli.ts
|
|
3043
3096
|
runCli().catch((error) => {
|
|
3044
|
-
console.error(
|
|
3097
|
+
console.error(formatCliError(error));
|
|
3045
3098
|
process.exitCode = 1;
|
|
3046
3099
|
});
|
|
3047
3100
|
//#endregion
|