fuma-translate 0.0.0 → 0.0.1
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/LICENSE +21 -0
- package/package.json +1 -1
- package/dist/compiler.d.mts +0 -21
- package/dist/compiler.d.mts.map +0 -1
- package/dist/compiler.mjs +0 -200
- package/dist/compiler.mjs.map +0 -1
- package/dist/react.d.mts +0 -27
- package/dist/react.d.mts.map +0 -1
- package/dist/react.mjs +0 -30
- package/dist/react.mjs.map +0 -1
- package/dist/shared-CAhaQI7c.mjs +0 -8
- package/dist/shared-CAhaQI7c.mjs.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fuma Nama
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
package/dist/compiler.d.mts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Span } from "oxc-parser";
|
|
2
|
-
|
|
3
|
-
//#region src/compiler.d.ts
|
|
4
|
-
interface CompileOptions {
|
|
5
|
-
/** glob patterns */
|
|
6
|
-
input: string[];
|
|
7
|
-
}
|
|
8
|
-
interface CompileOutput {
|
|
9
|
-
/** All encoded keys */
|
|
10
|
-
translationKeys: string[];
|
|
11
|
-
}
|
|
12
|
-
declare class StaticAnalysisError extends Error {
|
|
13
|
-
readonly file: string;
|
|
14
|
-
readonly span?: Span | undefined;
|
|
15
|
-
constructor(message: string, file: string, span?: Span | undefined);
|
|
16
|
-
}
|
|
17
|
-
declare function compile(options: CompileOptions): Promise<CompileOutput>;
|
|
18
|
-
declare function typegen(output: CompileOutput): string;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { CompileOptions, CompileOutput, StaticAnalysisError, compile, typegen };
|
|
21
|
-
//# sourceMappingURL=compiler.d.mts.map
|
package/dist/compiler.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.d.mts","names":[],"sources":["../src/compiler.ts"],"mappings":";;;UAiBiB,cAAA;;EAEf,KAAK;AAAA;AAAA,UAGU,aAAA;EAHV;EAKL,eAAe;AAAA;AAAA,cAGJ,mBAAA,SAA4B,KAAA;EAAA,SAG5B,IAAA;EAAA,SACA,IAAA,GAAO,IAAA;cAFhB,OAAA,UACS,IAAA,UACA,IAAA,GAAO,IAAA;AAAA;AAAA,iBA2TE,OAAA,CAAQ,OAAA,EAAS,cAAA,GAAiB,OAAA,CAAQ,aAAA;AAAA,iBAkBhD,OAAA,CAAQ,MAAqB,EAAb,aAAa"}
|
package/dist/compiler.mjs
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { t as encodeKey } from "./shared-CAhaQI7c.mjs";
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { Visitor, parseSync } from "oxc-parser";
|
|
5
|
-
import { glob } from "tinyglobby";
|
|
6
|
-
//#region src/compiler.ts
|
|
7
|
-
var StaticAnalysisError = class extends Error {
|
|
8
|
-
file;
|
|
9
|
-
span;
|
|
10
|
-
constructor(message, file, span) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.file = file;
|
|
13
|
-
this.span = span;
|
|
14
|
-
this.name = "StaticAnalysisError";
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
function formatLocation(source, offset) {
|
|
18
|
-
let line = 1;
|
|
19
|
-
let column = 1;
|
|
20
|
-
for (let i = 0; i < offset && i < source.length; i++) if (source[i] === "\n") {
|
|
21
|
-
line++;
|
|
22
|
-
column = 1;
|
|
23
|
-
} else column++;
|
|
24
|
-
return `${line}:${column}`;
|
|
25
|
-
}
|
|
26
|
-
function isUseTranslationsCall(expr) {
|
|
27
|
-
return expr.type === "CallExpression" && expr.callee.type === "Identifier" && expr.callee.name === "useTranslations" && expr.arguments.length === 0;
|
|
28
|
-
}
|
|
29
|
-
function unwrapExpression(expr) {
|
|
30
|
-
while (expr.type === "ParenthesizedExpression" || expr.type === "TSAsExpression" || expr.type === "TSSatisfiesExpression" || expr.type === "TSTypeAssertion") expr = expr.expression;
|
|
31
|
-
return expr;
|
|
32
|
-
}
|
|
33
|
-
function fail(source, file, span, message) {
|
|
34
|
-
throw new StaticAnalysisError(`${file}:${formatLocation(source, span.start)}: ${message}`, file, span);
|
|
35
|
-
}
|
|
36
|
-
function collectStaticStrings(expr, source, file) {
|
|
37
|
-
expr = unwrapExpression(expr);
|
|
38
|
-
if (expr.type === "Literal" && typeof expr.value === "string") return [expr.value];
|
|
39
|
-
if (expr.type === "TemplateLiteral") {
|
|
40
|
-
if (expr.expressions.length > 0) fail(source, file, expr, "translation key must be a static string");
|
|
41
|
-
return [expr.quasis.map((q) => q.value.cooked ?? q.value.raw).join("")];
|
|
42
|
-
}
|
|
43
|
-
if (expr.type === "ConditionalExpression") return [...collectStaticStrings(expr.consequent, source, file), ...collectStaticStrings(expr.alternate, source, file)];
|
|
44
|
-
fail(source, file, expr, "translation key must be a static string");
|
|
45
|
-
}
|
|
46
|
-
function getNoteProperty(properties) {
|
|
47
|
-
for (const prop of properties) {
|
|
48
|
-
if (prop.type !== "Property") continue;
|
|
49
|
-
if (prop.kind !== "init") continue;
|
|
50
|
-
if (prop.shorthand && prop.key.type === "Identifier") {
|
|
51
|
-
if (prop.key.name === "note") return prop;
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
if (prop.key.type === "Identifier" && prop.key.name === "note") return prop;
|
|
55
|
-
if (prop.key.type === "Literal" && typeof prop.key.value === "string" && prop.key.value === "note") return prop;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function collectNotes(expr, source, file) {
|
|
59
|
-
if (!expr) return [void 0];
|
|
60
|
-
expr = unwrapExpression(expr);
|
|
61
|
-
if (expr.type === "ConditionalExpression") return [...collectNotes(expr.consequent, source, file), ...collectNotes(expr.alternate, source, file)];
|
|
62
|
-
if (expr.type !== "ObjectExpression") fail(source, file, expr, "translation options must be a static object");
|
|
63
|
-
for (const prop of expr.properties) if (prop.type === "SpreadElement") fail(source, file, prop, "translation options cannot use spread properties");
|
|
64
|
-
const noteProp = getNoteProperty(expr.properties);
|
|
65
|
-
if (!noteProp) return [void 0];
|
|
66
|
-
if (noteProp.shorthand) fail(source, file, noteProp, "translation note must be a static string");
|
|
67
|
-
return collectStaticStrings(noteProp.value, source, file).map((note) => note);
|
|
68
|
-
}
|
|
69
|
-
function currentScope(scopes) {
|
|
70
|
-
const scope = scopes.at(-1);
|
|
71
|
-
if (!scope) throw new Error("scope stack is empty");
|
|
72
|
-
return scope;
|
|
73
|
-
}
|
|
74
|
-
function registerBinding(pattern, isHook, scopes) {
|
|
75
|
-
switch (pattern.type) {
|
|
76
|
-
case "Identifier":
|
|
77
|
-
currentScope(scopes).set(pattern.name, isHook);
|
|
78
|
-
return;
|
|
79
|
-
case "ObjectPattern":
|
|
80
|
-
for (const prop of pattern.properties) if (prop.type === "RestElement") registerBinding(prop.argument, false, scopes);
|
|
81
|
-
else registerBinding(prop.value, false, scopes);
|
|
82
|
-
return;
|
|
83
|
-
case "ArrayPattern":
|
|
84
|
-
for (const element of pattern.elements) {
|
|
85
|
-
if (!element) continue;
|
|
86
|
-
if (element.type === "RestElement") registerBinding(element.argument, false, scopes);
|
|
87
|
-
else registerBinding(element, false, scopes);
|
|
88
|
-
}
|
|
89
|
-
return;
|
|
90
|
-
case "AssignmentPattern":
|
|
91
|
-
registerBinding(pattern.left, isHook, scopes);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
function registerParams(params, scopes) {
|
|
96
|
-
for (const param of params) if (param.type === "TSParameterProperty") registerBinding(param.parameter, false, scopes);
|
|
97
|
-
else if (param.type === "RestElement") registerBinding(param.argument, false, scopes);
|
|
98
|
-
else registerBinding(param, false, scopes);
|
|
99
|
-
}
|
|
100
|
-
function isTranslationHook(name, scopes) {
|
|
101
|
-
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
102
|
-
const scope = scopes[i];
|
|
103
|
-
if (!scope) continue;
|
|
104
|
-
if (scope.has(name)) return scope.get(name);
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
function analyzeCall(call, source, file, keys, scopes) {
|
|
109
|
-
const callee = unwrapExpression(call.callee);
|
|
110
|
-
if (callee.type !== "Identifier") return;
|
|
111
|
-
if (!isTranslationHook(callee.name, scopes)) return;
|
|
112
|
-
if (call.arguments.length === 0) fail(source, file, call, "translation call requires a static string argument");
|
|
113
|
-
const firstArg = call.arguments[0];
|
|
114
|
-
if (!firstArg || firstArg.type === "SpreadElement") fail(source, file, firstArg ?? call, "translation key must be a static string");
|
|
115
|
-
const texts = collectStaticStrings(firstArg, source, file);
|
|
116
|
-
let notes = [void 0];
|
|
117
|
-
if (call.arguments.length > 1) {
|
|
118
|
-
const secondArg = call.arguments[1];
|
|
119
|
-
if (!secondArg || secondArg.type === "SpreadElement") fail(source, file, secondArg ?? call, "translation options must be a static object");
|
|
120
|
-
notes = collectNotes(secondArg, source, file);
|
|
121
|
-
}
|
|
122
|
-
for (const text of texts) for (const note of notes) keys.add(encodeKey(text, note));
|
|
123
|
-
}
|
|
124
|
-
function analyzeSource(file, lang, source) {
|
|
125
|
-
const result = parseSync(file, source, {
|
|
126
|
-
lang,
|
|
127
|
-
sourceType: "module"
|
|
128
|
-
});
|
|
129
|
-
if (result.errors.length > 0) throw new StaticAnalysisError(result.errors.map((error) => error.message).join("\n"), file);
|
|
130
|
-
const keys = /* @__PURE__ */ new Set();
|
|
131
|
-
const scopes = [/* @__PURE__ */ new Map()];
|
|
132
|
-
const pushScope = () => {
|
|
133
|
-
scopes.push(/* @__PURE__ */ new Map());
|
|
134
|
-
};
|
|
135
|
-
const popScope = () => {
|
|
136
|
-
scopes.pop();
|
|
137
|
-
};
|
|
138
|
-
new Visitor({
|
|
139
|
-
BlockStatement: pushScope,
|
|
140
|
-
"BlockStatement:exit": popScope,
|
|
141
|
-
CatchClause: pushScope,
|
|
142
|
-
"CatchClause:exit": popScope,
|
|
143
|
-
FunctionDeclaration(node) {
|
|
144
|
-
pushScope();
|
|
145
|
-
registerParams(node.params, scopes);
|
|
146
|
-
},
|
|
147
|
-
"FunctionDeclaration:exit": popScope,
|
|
148
|
-
FunctionExpression(node) {
|
|
149
|
-
pushScope();
|
|
150
|
-
registerParams(node.params, scopes);
|
|
151
|
-
},
|
|
152
|
-
"FunctionExpression:exit": popScope,
|
|
153
|
-
ArrowFunctionExpression(node) {
|
|
154
|
-
pushScope();
|
|
155
|
-
registerParams(node.params, scopes);
|
|
156
|
-
},
|
|
157
|
-
"ArrowFunctionExpression:exit": popScope,
|
|
158
|
-
VariableDeclarator(decl) {
|
|
159
|
-
if (!decl.init) return;
|
|
160
|
-
const init = unwrapExpression(decl.init);
|
|
161
|
-
const isHook = init.type === "CallExpression" && isUseTranslationsCall(init);
|
|
162
|
-
registerBinding(decl.id, isHook, scopes);
|
|
163
|
-
},
|
|
164
|
-
CallExpression(call) {
|
|
165
|
-
analyzeCall(call, source, file, keys, scopes);
|
|
166
|
-
}
|
|
167
|
-
}).visit(result.program);
|
|
168
|
-
return [...keys];
|
|
169
|
-
}
|
|
170
|
-
function getLang(file) {
|
|
171
|
-
switch (path.extname(file)) {
|
|
172
|
-
case ".tsx": return "tsx";
|
|
173
|
-
case ".ts":
|
|
174
|
-
case ".cts":
|
|
175
|
-
case ".mts": return "ts";
|
|
176
|
-
case ".jsx": return "jsx";
|
|
177
|
-
case ".cjs":
|
|
178
|
-
case ".mjs":
|
|
179
|
-
case ".js": return "js";
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
async function compile(options) {
|
|
183
|
-
const files = await glob(options.input, { absolute: true });
|
|
184
|
-
const keys = /* @__PURE__ */ new Set();
|
|
185
|
-
for (const file of files) {
|
|
186
|
-
const lang = getLang(file);
|
|
187
|
-
if (!lang) continue;
|
|
188
|
-
const source = await fs.readFile(file, "utf8");
|
|
189
|
-
for (const key of analyzeSource(file, lang, source)) keys.add(key);
|
|
190
|
-
}
|
|
191
|
-
return { translationKeys: [...keys].sort() };
|
|
192
|
-
}
|
|
193
|
-
function typegen(output) {
|
|
194
|
-
if (output.translationKeys.length === 0) return "export type Translations = {};\n";
|
|
195
|
-
return `export type Translations = {\n${output.translationKeys.map((key) => ` ${JSON.stringify(key)}: string;`).join("\n")}\n};\n`;
|
|
196
|
-
}
|
|
197
|
-
//#endregion
|
|
198
|
-
export { StaticAnalysisError, compile, typegen };
|
|
199
|
-
|
|
200
|
-
//# sourceMappingURL=compiler.mjs.map
|
package/dist/compiler.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.mjs","names":[],"sources":["../src/compiler.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport type {\n BindingPattern,\n CallExpression,\n Expression,\n ObjectProperty,\n ObjectPropertyKind,\n ParamPattern,\n Span,\n} from \"oxc-parser\";\nimport { parseSync, Visitor } from \"oxc-parser\";\nimport { glob } from \"tinyglobby\";\nimport { encodeKey } from \"./shared\";\n\ntype SupportedLang = \"js\" | \"jsx\" | \"ts\" | \"tsx\";\n\nexport interface CompileOptions {\n /** glob patterns */\n input: string[];\n}\n\nexport interface CompileOutput {\n /** All encoded keys */\n translationKeys: string[];\n}\n\nexport class StaticAnalysisError extends Error {\n constructor(\n message: string,\n readonly file: string,\n readonly span?: Span,\n ) {\n super(message);\n this.name = \"StaticAnalysisError\";\n }\n}\n\nfunction formatLocation(source: string, offset: number): string {\n let line = 1;\n let column = 1;\n\n for (let i = 0; i < offset && i < source.length; i++) {\n if (source[i] === \"\\n\") {\n line++;\n column = 1;\n } else {\n column++;\n }\n }\n\n return `${line}:${column}`;\n}\n\nfunction isUseTranslationsCall(expr: Expression): boolean {\n return (\n expr.type === \"CallExpression\" &&\n expr.callee.type === \"Identifier\" &&\n expr.callee.name === \"useTranslations\" &&\n expr.arguments.length === 0\n );\n}\n\nfunction unwrapExpression(expr: Expression): Expression {\n while (\n expr.type === \"ParenthesizedExpression\" ||\n expr.type === \"TSAsExpression\" ||\n expr.type === \"TSSatisfiesExpression\" ||\n expr.type === \"TSTypeAssertion\"\n ) {\n expr = expr.expression;\n }\n return expr;\n}\n\nfunction fail(source: string, file: string, span: Span, message: string): never {\n throw new StaticAnalysisError(\n `${file}:${formatLocation(source, span.start)}: ${message}`,\n file,\n span,\n );\n}\n\nfunction collectStaticStrings(expr: Expression, source: string, file: string): string[] {\n expr = unwrapExpression(expr);\n\n if (expr.type === \"Literal\" && typeof expr.value === \"string\") {\n return [expr.value];\n }\n\n if (expr.type === \"TemplateLiteral\") {\n if (expr.expressions.length > 0) {\n fail(source, file, expr, \"translation key must be a static string\");\n }\n return [expr.quasis.map((q) => q.value.cooked ?? q.value.raw).join(\"\")];\n }\n\n if (expr.type === \"ConditionalExpression\") {\n return [\n ...collectStaticStrings(expr.consequent, source, file),\n ...collectStaticStrings(expr.alternate, source, file),\n ];\n }\n\n fail(source, file, expr, \"translation key must be a static string\");\n}\n\nfunction getNoteProperty(properties: ObjectPropertyKind[]): ObjectProperty | undefined {\n for (const prop of properties) {\n if (prop.type !== \"Property\") continue;\n if (prop.kind !== \"init\") continue;\n\n if (prop.shorthand && prop.key.type === \"Identifier\") {\n if (prop.key.name === \"note\") return prop;\n continue;\n }\n\n if (prop.key.type === \"Identifier\" && prop.key.name === \"note\") {\n return prop;\n }\n\n if (\n prop.key.type === \"Literal\" &&\n typeof prop.key.value === \"string\" &&\n prop.key.value === \"note\"\n ) {\n return prop;\n }\n }\n return undefined;\n}\n\nfunction collectNotes(\n expr: Expression | undefined,\n source: string,\n file: string,\n): (string | undefined)[] {\n if (!expr) return [undefined];\n\n expr = unwrapExpression(expr);\n\n if (expr.type === \"ConditionalExpression\") {\n return [\n ...collectNotes(expr.consequent, source, file),\n ...collectNotes(expr.alternate, source, file),\n ];\n }\n\n if (expr.type !== \"ObjectExpression\") {\n fail(source, file, expr, \"translation options must be a static object\");\n }\n\n for (const prop of expr.properties) {\n if (prop.type === \"SpreadElement\") {\n fail(source, file, prop, \"translation options cannot use spread properties\");\n }\n }\n\n const noteProp = getNoteProperty(expr.properties);\n if (!noteProp) return [undefined];\n\n if (noteProp.shorthand) {\n fail(source, file, noteProp, \"translation note must be a static string\");\n }\n\n const noteValues = collectStaticStrings(noteProp.value, source, file);\n return noteValues.map((note) => note);\n}\n\nfunction currentScope(scopes: Map<string, boolean>[]): Map<string, boolean> {\n const scope = scopes.at(-1);\n if (!scope) throw new Error(\"scope stack is empty\");\n return scope;\n}\n\nfunction registerBinding(\n pattern: BindingPattern,\n isHook: boolean,\n scopes: Map<string, boolean>[],\n): void {\n switch (pattern.type) {\n case \"Identifier\":\n currentScope(scopes).set(pattern.name, isHook);\n return;\n case \"ObjectPattern\":\n for (const prop of pattern.properties) {\n if (prop.type === \"RestElement\") {\n registerBinding(prop.argument, false, scopes);\n } else {\n registerBinding(prop.value, false, scopes);\n }\n }\n return;\n case \"ArrayPattern\":\n for (const element of pattern.elements) {\n if (!element) continue;\n if (element.type === \"RestElement\") {\n registerBinding(element.argument, false, scopes);\n } else {\n registerBinding(element, false, scopes);\n }\n }\n return;\n case \"AssignmentPattern\":\n registerBinding(pattern.left, isHook, scopes);\n return;\n }\n}\n\nfunction registerParams(params: ParamPattern[], scopes: Map<string, boolean>[]): void {\n for (const param of params) {\n if (param.type === \"TSParameterProperty\") {\n registerBinding(param.parameter, false, scopes);\n } else if (param.type === \"RestElement\") {\n registerBinding(param.argument, false, scopes);\n } else {\n registerBinding(param, false, scopes);\n }\n }\n}\n\nfunction isTranslationHook(name: string, scopes: Map<string, boolean>[]): boolean {\n for (let i = scopes.length - 1; i >= 0; i--) {\n const scope = scopes[i];\n if (!scope) continue;\n if (scope.has(name)) return scope.get(name)!;\n }\n return false;\n}\n\nfunction analyzeCall(\n call: CallExpression,\n source: string,\n file: string,\n keys: Set<string>,\n scopes: Map<string, boolean>[],\n): void {\n const callee = unwrapExpression(call.callee);\n if (callee.type !== \"Identifier\") return;\n if (!isTranslationHook(callee.name, scopes)) return;\n\n if (call.arguments.length === 0) {\n fail(source, file, call, \"translation call requires a static string argument\");\n }\n\n const firstArg = call.arguments[0];\n if (!firstArg || firstArg.type === \"SpreadElement\") {\n fail(source, file, firstArg ?? call, \"translation key must be a static string\");\n }\n\n const texts = collectStaticStrings(firstArg, source, file);\n\n let notes: (string | undefined)[] = [undefined];\n if (call.arguments.length > 1) {\n const secondArg = call.arguments[1];\n if (!secondArg || secondArg.type === \"SpreadElement\") {\n fail(source, file, secondArg ?? call, \"translation options must be a static object\");\n }\n notes = collectNotes(secondArg, source, file);\n }\n\n for (const text of texts) {\n for (const note of notes) {\n keys.add(encodeKey(text, note));\n }\n }\n}\n\nfunction analyzeSource(file: string, lang: SupportedLang, source: string): string[] {\n const result = parseSync(file, source, { lang, sourceType: \"module\" });\n\n if (result.errors.length > 0) {\n const message = result.errors.map((error) => error.message).join(\"\\n\");\n throw new StaticAnalysisError(message, file);\n }\n\n const keys = new Set<string>();\n const scopes: Map<string, boolean>[] = [new Map()];\n\n const pushScope = () => {\n scopes.push(new Map());\n };\n\n const popScope = () => {\n scopes.pop();\n };\n\n const visitor = new Visitor({\n BlockStatement: pushScope,\n \"BlockStatement:exit\": popScope,\n\n CatchClause: pushScope,\n \"CatchClause:exit\": popScope,\n\n FunctionDeclaration(node) {\n pushScope();\n registerParams(node.params, scopes);\n },\n \"FunctionDeclaration:exit\": popScope,\n\n FunctionExpression(node) {\n pushScope();\n registerParams(node.params, scopes);\n },\n \"FunctionExpression:exit\": popScope,\n\n ArrowFunctionExpression(node) {\n pushScope();\n registerParams(node.params, scopes);\n },\n \"ArrowFunctionExpression:exit\": popScope,\n\n VariableDeclarator(decl) {\n if (!decl.init) return;\n\n const init = unwrapExpression(decl.init);\n const isHook = init.type === \"CallExpression\" && isUseTranslationsCall(init);\n registerBinding(decl.id, isHook, scopes);\n },\n\n CallExpression(call) {\n analyzeCall(call, source, file, keys, scopes);\n },\n });\n\n visitor.visit(result.program);\n return [...keys];\n}\n\nfunction getLang(file: string): SupportedLang | undefined {\n switch (path.extname(file)) {\n case \".tsx\":\n return \"tsx\";\n case \".ts\":\n case \".cts\":\n case \".mts\":\n return \"ts\";\n case \".jsx\":\n return \"jsx\";\n case \".cjs\":\n case \".mjs\":\n case \".js\":\n return \"js\";\n }\n}\n\nexport async function compile(options: CompileOptions): Promise<CompileOutput> {\n const files = await glob(options.input, { absolute: true });\n const keys = new Set<string>();\n\n for (const file of files) {\n const lang = getLang(file);\n if (!lang) continue;\n\n const source = await fs.readFile(file, \"utf8\");\n for (const key of analyzeSource(file, lang, source)) {\n keys.add(key);\n }\n }\n\n const translationKeys = [...keys].sort();\n return { translationKeys };\n}\n\nexport function typegen(output: CompileOutput): string {\n if (output.translationKeys.length === 0) {\n return \"export type Translations = {};\\n\";\n }\n\n const entries = output.translationKeys\n .map((key) => ` ${JSON.stringify(key)}: string;`)\n .join(\"\\n\");\n\n return `export type Translations = {\\n${entries}\\n};\\n`;\n}\n"],"mappings":";;;;;;AA2BA,IAAa,sBAAb,cAAyC,MAAM;CAGlC;CACA;CAHX,YACE,SACA,MACA,MACA;EACA,MAAM,OAAO;EAHJ,KAAA,OAAA;EACA,KAAA,OAAA;EAGT,KAAK,OAAO;CACd;AACF;AAEA,SAAS,eAAe,QAAgB,QAAwB;CAC9D,IAAI,OAAO;CACX,IAAI,SAAS;CAEb,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,IAAI,OAAO,QAAQ,KAC/C,IAAI,OAAO,OAAO,MAAM;EACtB;EACA,SAAS;CACX,OACE;CAIJ,OAAO,GAAG,KAAK,GAAG;AACpB;AAEA,SAAS,sBAAsB,MAA2B;CACxD,OACE,KAAK,SAAS,oBACd,KAAK,OAAO,SAAS,gBACrB,KAAK,OAAO,SAAS,qBACrB,KAAK,UAAU,WAAW;AAE9B;AAEA,SAAS,iBAAiB,MAA8B;CACtD,OACE,KAAK,SAAS,6BACd,KAAK,SAAS,oBACd,KAAK,SAAS,2BACd,KAAK,SAAS,mBAEd,OAAO,KAAK;CAEd,OAAO;AACT;AAEA,SAAS,KAAK,QAAgB,MAAc,MAAY,SAAwB;CAC9E,MAAM,IAAI,oBACR,GAAG,KAAK,GAAG,eAAe,QAAQ,KAAK,KAAK,EAAE,IAAI,WAClD,MACA,IACF;AACF;AAEA,SAAS,qBAAqB,MAAkB,QAAgB,MAAwB;CACtF,OAAO,iBAAiB,IAAI;CAE5B,IAAI,KAAK,SAAS,aAAa,OAAO,KAAK,UAAU,UACnD,OAAO,CAAC,KAAK,KAAK;CAGpB,IAAI,KAAK,SAAS,mBAAmB;EACnC,IAAI,KAAK,YAAY,SAAS,GAC5B,KAAK,QAAQ,MAAM,M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B;CACrD,IAAI,OAAO,gBAAgB,WAAW,GACpC,OAAO;CAOT,OAAO,iCAJS,OAAO,gBACpB,KAAK,QAAQ,KAAK,KAAK,UAAU,GAAG,EAAE,UAAU,CAAC,CACjD,KAAK,IAEsC,EAAE;AAClD"}
|
package/dist/react.d.mts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ReactNode } from "react";
|
|
2
|
-
|
|
3
|
-
//#region src/react.d.ts
|
|
4
|
-
type Translations = Record<string, string>;
|
|
5
|
-
/** add translations, you can stack multiple <TranslationProvider /> to override/extend translations */
|
|
6
|
-
declare function TranslationProvider({
|
|
7
|
-
translations,
|
|
8
|
-
children
|
|
9
|
-
}: {
|
|
10
|
-
translations: Translations;
|
|
11
|
-
children: ReactNode;
|
|
12
|
-
}): import("react").JSX.Element;
|
|
13
|
-
type GetVariables<T extends string> = T extends `${string}{${infer K}}${infer After}` ? K | GetVariables<After> : never;
|
|
14
|
-
interface TranslationsHook {
|
|
15
|
-
<Text extends string>(text: Text, opts?: {
|
|
16
|
-
/**
|
|
17
|
-
* add more context to `text`.
|
|
18
|
-
* @example "The aria-label of close dialog button"
|
|
19
|
-
*/
|
|
20
|
-
note?: string;
|
|
21
|
-
variables?: Record<GetVariables<Text>, string>;
|
|
22
|
-
}): string;
|
|
23
|
-
}
|
|
24
|
-
declare function useTranslations(): TranslationsHook;
|
|
25
|
-
//#endregion
|
|
26
|
-
export { TranslationProvider, TranslationsHook, useTranslations };
|
|
27
|
-
//# sourceMappingURL=react.d.mts.map
|
package/dist/react.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.mts","names":[],"sources":["../src/react.tsx"],"mappings":";;;KAIK,YAAA,GAAe,MAAM;;iBAKV,mBAAA;EACd,YAAA;EACA;AAAA;EAEA,YAAA,EAAc,YAAA;EACd,QAAA,EAAU,SAAA;AAAA,oBACX,GAAA,CAAA,OAAA;AAAA,KASI,YAAA,qBAAiC,CAAA,iDAClC,CAAA,GAAI,YAAA,CAAa,KAAA;AAAA,UAGJ,gBAAA;EAAA,sBAEb,IAAA,EAAM,IAAA,EACN,IAAA;IAtB+B;;;;IA2B7B,IAAA;IACA,SAAA,GAAY,MAAA,CAAO,YAAA,CAAa,IAAA;EAAA;AAAA;AAAA,iBAKtB,eAAA,IAAmB,gBAAgB"}
|
package/dist/react.mjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { t as encodeKey } from "./shared-CAhaQI7c.mjs";
|
|
3
|
-
import { createContext, use, useMemo } from "react";
|
|
4
|
-
import { jsx } from "react/jsx-runtime";
|
|
5
|
-
//#region src/react.tsx
|
|
6
|
-
const Context = createContext({});
|
|
7
|
-
/** add translations, you can stack multiple <TranslationProvider /> to override/extend translations */
|
|
8
|
-
function TranslationProvider({ translations, children }) {
|
|
9
|
-
const parent = use(Context);
|
|
10
|
-
return /* @__PURE__ */ jsx(Context, {
|
|
11
|
-
value: useMemo(() => ({
|
|
12
|
-
...parent,
|
|
13
|
-
...translations
|
|
14
|
-
}), [parent, translations]),
|
|
15
|
-
children
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
function useTranslations() {
|
|
19
|
-
const translations = use(Context);
|
|
20
|
-
return (rawText, opts = {}) => {
|
|
21
|
-
const { note, variables } = opts;
|
|
22
|
-
let text = translations[encodeKey(rawText, note)] ?? rawText;
|
|
23
|
-
if (variables) for (const k in variables) text = text.replaceAll(k, variables[k]);
|
|
24
|
-
return text;
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
//#endregion
|
|
28
|
-
export { TranslationProvider, useTranslations };
|
|
29
|
-
|
|
30
|
-
//# sourceMappingURL=react.mjs.map
|
package/dist/react.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react.mjs","names":[],"sources":["../src/react.tsx"],"sourcesContent":["\"use client\";\nimport { createContext, use, useMemo, type ReactNode } from \"react\";\nimport { encodeKey } from \"./shared\";\n\ntype Translations = Record<string, string>;\n\nconst Context = createContext<Translations>({});\n\n/** add translations, you can stack multiple <TranslationProvider /> to override/extend translations */\nexport function TranslationProvider({\n translations,\n children,\n}: {\n translations: Translations;\n children: ReactNode;\n}) {\n const parent = use(Context);\n return (\n <Context value={useMemo(() => ({ ...parent, ...translations }), [parent, translations])}>\n {children}\n </Context>\n );\n}\n\ntype GetVariables<T extends string> = T extends `${string}{${infer K}}${infer After}`\n ? K | GetVariables<After>\n : never;\n\nexport interface TranslationsHook {\n <Text extends string>(\n text: Text,\n opts?: {\n /**\n * add more context to `text`.\n * @example \"The aria-label of close dialog button\"\n */\n note?: string;\n variables?: Record<GetVariables<Text>, string>;\n },\n ): string;\n}\n\nexport function useTranslations(): TranslationsHook {\n const translations = use(Context);\n\n return (rawText, opts = {}) => {\n const { note, variables } = opts;\n const k = encodeKey(rawText, note);\n let text = translations[k] ?? rawText;\n\n if (variables) {\n for (const k in variables) text = text.replaceAll(k, variables[k as never]);\n }\n\n return text;\n };\n}\n"],"mappings":";;;;;AAMA,MAAM,UAAU,cAA4B,CAAC,CAAC;;AAG9C,SAAgB,oBAAoB,EAClC,cACA,YAIC;CACD,MAAM,SAAS,IAAI,OAAO;CAC1B,OACE,oBAAC,SAAD;EAAS,OAAO,eAAe;GAAE,GAAG;GAAQ,GAAG;EAAa,IAAI,CAAC,QAAQ,YAAY,CAAC;EACnF;CACM,CAAA;AAEb;AAoBA,SAAgB,kBAAoC;CAClD,MAAM,eAAe,IAAI,OAAO;CAEhC,QAAQ,SAAS,OAAO,CAAC,MAAM;EAC7B,MAAM,EAAE,MAAM,cAAc;EAE5B,IAAI,OAAO,aADD,UAAU,SAAS,IACL,MAAM;EAE9B,IAAI,WACF,KAAK,MAAM,KAAK,WAAW,OAAO,KAAK,WAAW,GAAG,UAAU,EAAW;EAG5E,OAAO;CACT;AACF"}
|
package/dist/shared-CAhaQI7c.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shared-CAhaQI7c.mjs","names":[],"sources":["../src/shared.ts"],"sourcesContent":["export function encodeKey(text: string, note?: string): string {\n return note ? `${text}(${note})` : text;\n}\n"],"mappings":";AAAA,SAAgB,UAAU,MAAc,MAAuB;CAC7D,OAAO,OAAO,GAAG,KAAK,GAAG,KAAK,KAAK;AACrC"}
|