fuma-translate 0.0.0 → 0.0.2
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/dist/compiler.d.mts.map +1 -1
- package/dist/compiler.mjs +30 -14
- package/dist/compiler.mjs.map +1 -1
- package/dist/react.d.mts +3 -1
- package/dist/react.d.mts.map +1 -1
- package/dist/react.mjs +7 -4
- package/dist/react.mjs.map +1 -1
- package/dist/shared-CAg_QhcN.mjs +8 -0
- package/dist/shared-CAg_QhcN.mjs.map +1 -0
- package/package.json +1 -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/dist/compiler.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,iBAyWE,OAAA,CAAQ,OAAA,EAAS,cAAA,GAAiB,OAAA,CAAQ,aAAA;AAAA,iBAkBhD,OAAA,CAAQ,MAAqB,EAAb,aAAa"}
|
package/dist/compiler.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as encodeKey } from "./shared-
|
|
1
|
+
import { t as encodeKey } from "./shared-CAg_QhcN.mjs";
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { Visitor, parseSync } from "oxc-parser";
|
|
@@ -23,8 +23,14 @@ function formatLocation(source, offset) {
|
|
|
23
23
|
} else column++;
|
|
24
24
|
return `${line}:${column}`;
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
function parseUseTranslationsCall(expr, source, file) {
|
|
27
|
+
if (expr.type !== "CallExpression") return null;
|
|
28
|
+
if (expr.callee.type !== "Identifier" || expr.callee.name !== "useTranslations") return null;
|
|
29
|
+
if (expr.arguments.length === 0) return [void 0];
|
|
30
|
+
if (expr.arguments.length > 1) fail(source, file, expr, "useTranslations accepts at most one options argument");
|
|
31
|
+
const arg = expr.arguments[0];
|
|
32
|
+
if (!arg || arg.type === "SpreadElement") fail(source, file, arg ?? expr, "useTranslations options must be a static object");
|
|
33
|
+
return collectNotes(arg, source, file);
|
|
28
34
|
}
|
|
29
35
|
function unwrapExpression(expr) {
|
|
30
36
|
while (expr.type === "ParenthesizedExpression" || expr.type === "TSAsExpression" || expr.type === "TSSatisfiesExpression" || expr.type === "TSTypeAssertion") expr = expr.expression;
|
|
@@ -71,10 +77,10 @@ function currentScope(scopes) {
|
|
|
71
77
|
if (!scope) throw new Error("scope stack is empty");
|
|
72
78
|
return scope;
|
|
73
79
|
}
|
|
74
|
-
function registerBinding(pattern,
|
|
80
|
+
function registerBinding(pattern, hookNotes, scopes) {
|
|
75
81
|
switch (pattern.type) {
|
|
76
82
|
case "Identifier":
|
|
77
|
-
currentScope(scopes).set(pattern.name,
|
|
83
|
+
currentScope(scopes).set(pattern.name, hookNotes);
|
|
78
84
|
return;
|
|
79
85
|
case "ObjectPattern":
|
|
80
86
|
for (const prop of pattern.properties) if (prop.type === "RestElement") registerBinding(prop.argument, false, scopes);
|
|
@@ -88,7 +94,7 @@ function registerBinding(pattern, isHook, scopes) {
|
|
|
88
94
|
}
|
|
89
95
|
return;
|
|
90
96
|
case "AssignmentPattern":
|
|
91
|
-
registerBinding(pattern.left,
|
|
97
|
+
registerBinding(pattern.left, hookNotes, scopes);
|
|
92
98
|
return;
|
|
93
99
|
}
|
|
94
100
|
}
|
|
@@ -97,7 +103,7 @@ function registerParams(params, scopes) {
|
|
|
97
103
|
else if (param.type === "RestElement") registerBinding(param.argument, false, scopes);
|
|
98
104
|
else registerBinding(param, false, scopes);
|
|
99
105
|
}
|
|
100
|
-
function
|
|
106
|
+
function getTranslationHookNotes(name, scopes) {
|
|
101
107
|
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
102
108
|
const scope = scopes[i];
|
|
103
109
|
if (!scope) continue;
|
|
@@ -105,21 +111,32 @@ function isTranslationHook(name, scopes) {
|
|
|
105
111
|
}
|
|
106
112
|
return false;
|
|
107
113
|
}
|
|
114
|
+
function encodeTranslationKey(text, hookNotes, callNotes) {
|
|
115
|
+
const keys = [];
|
|
116
|
+
for (const hookNote of hookNotes) for (const callNote of callNotes) {
|
|
117
|
+
const notes = [];
|
|
118
|
+
if (hookNote) notes.push(hookNote);
|
|
119
|
+
if (callNote) notes.push(callNote);
|
|
120
|
+
keys.push(encodeKey(text, notes));
|
|
121
|
+
}
|
|
122
|
+
return keys;
|
|
123
|
+
}
|
|
108
124
|
function analyzeCall(call, source, file, keys, scopes) {
|
|
109
125
|
const callee = unwrapExpression(call.callee);
|
|
110
126
|
if (callee.type !== "Identifier") return;
|
|
111
|
-
|
|
127
|
+
const hookNotes = getTranslationHookNotes(callee.name, scopes);
|
|
128
|
+
if (hookNotes === false) return;
|
|
112
129
|
if (call.arguments.length === 0) fail(source, file, call, "translation call requires a static string argument");
|
|
113
130
|
const firstArg = call.arguments[0];
|
|
114
131
|
if (!firstArg || firstArg.type === "SpreadElement") fail(source, file, firstArg ?? call, "translation key must be a static string");
|
|
115
132
|
const texts = collectStaticStrings(firstArg, source, file);
|
|
116
|
-
let
|
|
133
|
+
let callNotes = [void 0];
|
|
117
134
|
if (call.arguments.length > 1) {
|
|
118
135
|
const secondArg = call.arguments[1];
|
|
119
136
|
if (!secondArg || secondArg.type === "SpreadElement") fail(source, file, secondArg ?? call, "translation options must be a static object");
|
|
120
|
-
|
|
137
|
+
callNotes = collectNotes(secondArg, source, file);
|
|
121
138
|
}
|
|
122
|
-
for (const text of texts) for (const
|
|
139
|
+
for (const text of texts) for (const key of encodeTranslationKey(text, hookNotes, callNotes)) keys.add(key);
|
|
123
140
|
}
|
|
124
141
|
function analyzeSource(file, lang, source) {
|
|
125
142
|
const result = parseSync(file, source, {
|
|
@@ -157,9 +174,8 @@ function analyzeSource(file, lang, source) {
|
|
|
157
174
|
"ArrowFunctionExpression:exit": popScope,
|
|
158
175
|
VariableDeclarator(decl) {
|
|
159
176
|
if (!decl.init) return;
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
registerBinding(decl.id, isHook, scopes);
|
|
177
|
+
const hookNotes = parseUseTranslationsCall(unwrapExpression(decl.init), source, file);
|
|
178
|
+
registerBinding(decl.id, hookNotes ?? false, scopes);
|
|
163
179
|
},
|
|
164
180
|
CallExpression(call) {
|
|
165
181
|
analyzeCall(call, source, file, keys, scopes);
|
package/dist/compiler.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,q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KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO;EACT,KAAK,QACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,OACH,OAAO;CACX;AACF;AAEA,eAAsB,QAAQ,SAAiD;CAC7E,MAAM,QAAQ,MAAM,KAAK,QAAQ,OAAO,EAAE,UAAU,KAAK,CAAC;CAC1D,MAAM,uBAAO,IAAI,IAAY;CAE7B,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,OAAO,QAAQ,IAAI;EACzB,IAAI,CAAC,MAAM;EAEX,MAAM,SAAS,MAAM,GAAG,SAAS,MAAM,MAAM;EAC7C,KAAK,MAAM,OAAO,cAAc,MAAM,MAAM,MAAM,GAChD,KAAK,IAAI,GAAG;CAEhB;CAGA,OAAO,EAAE,iBADe,CAAC,GAAG,IAAI,CAAC,CAAC,KACX,EAAE;AAC3B;AAEA,SAAgB,QAAQ,QAA+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"}
|
|
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\ntype HookNoteBranches = (string | undefined)[];\n\nfunction parseUseTranslationsCall(\n expr: Expression,\n source: string,\n file: string,\n): HookNoteBranches | null {\n if (expr.type !== \"CallExpression\") return null;\n if (expr.callee.type !== \"Identifier\" || expr.callee.name !== \"useTranslations\") {\n return null;\n }\n\n if (expr.arguments.length === 0) return [undefined];\n\n if (expr.arguments.length > 1) {\n fail(source, file, expr, \"useTranslations accepts at most one options argument\");\n }\n\n const arg = expr.arguments[0];\n if (!arg || arg.type === \"SpreadElement\") {\n fail(source, file, arg ?? expr, \"useTranslations options must be a static object\");\n }\n\n return collectNotes(arg, source, file);\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(\n scopes: Map<string, HookNoteBranches | false>[],\n): Map<string, HookNoteBranches | false> {\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 hookNotes: HookNoteBranches | false,\n scopes: Map<string, HookNoteBranches | false>[],\n): void {\n switch (pattern.type) {\n case \"Identifier\":\n currentScope(scopes).set(pattern.name, hookNotes);\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, hookNotes, scopes);\n return;\n }\n}\n\nfunction registerParams(\n params: ParamPattern[],\n scopes: Map<string, HookNoteBranches | false>[],\n): 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 getTranslationHookNotes(\n name: string,\n scopes: Map<string, HookNoteBranches | false>[],\n): HookNoteBranches | false {\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 encodeTranslationKey(\n text: string,\n hookNotes: HookNoteBranches,\n callNotes: HookNoteBranches,\n): string[] {\n const keys: string[] = [];\n\n for (const hookNote of hookNotes) {\n for (const callNote of callNotes) {\n const notes: string[] = [];\n if (hookNote) notes.push(hookNote);\n if (callNote) notes.push(callNote);\n keys.push(encodeKey(text, notes));\n }\n }\n\n return keys;\n}\n\nfunction analyzeCall(\n call: CallExpression,\n source: string,\n file: string,\n keys: Set<string>,\n scopes: Map<string, HookNoteBranches | false>[],\n): void {\n const callee = unwrapExpression(call.callee);\n if (callee.type !== \"Identifier\") return;\n\n const hookNotes = getTranslationHookNotes(callee.name, scopes);\n if (hookNotes === false) 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 callNotes: HookNoteBranches = [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 callNotes = collectNotes(secondArg, source, file);\n }\n\n for (const text of texts) {\n for (const key of encodeTranslationKey(text, hookNotes, callNotes)) {\n keys.add(key);\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, HookNoteBranches | false>[] = [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 hookNotes = parseUseTranslationsCall(init, source, file);\n registerBinding(decl.id, hookNotes ?? false, 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;AAIA,SAAS,yBACP,MACA,QACA,MACyB;CACzB,IAAI,KAAK,SAAS,kBAAkB,OAAO;CAC3C,IAAI,KAAK,OAAO,SAAS,gBAAgB,KAAK,OAAO,SAAS,mBAC5D,OAAO;CAGT,IAAI,KAAK,UAAU,WAAW,GAAG,OAAO,CAAC,KAAA,CAAS;CAElD,IAAI,KAAK,UAAU,SAAS,GAC1B,KAAK,QAAQ,MAAM,MAAM,sDAAsD;CAGjF,MAAM,MAAM,KAAK,UAAU;CAC3B,IAAI,CAAC,OAAO,IAAI,SAAS,iBACvB,KAAK,QAAQ,MAAM,OAAO,MAAM,iDAAiD;CAGnF,OAAO,aAAa,KAAK,QAAQ,IAAI;AACvC;AAEA,SAAS,iBAAiB,MAA8B;CACtD,OACE,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MAAM,MAAM;EAC9C;CACF,CAEM,CAAC,CAAC,MAAM,OAAO,OAAO;CAC5B,OAAO,CAAC,GAAG,IAAI;AACjB;AAEA,SAAS,QAAQ,MAAyC;CACxD,QAAQ,KAAK,QAAQ,IAAI,GAAzB;EACE,KAAK,QACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO;EACT,KAAK,QACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,OACH,OAAO;CACX;AACF;AAEA,eAAsB,QAAQ,SAAiD;CAC7E,MAAM,QAAQ,MAAM,KAAK,QAAQ,OAAO,EAAE,UAAU,KAAK,CAAC;CAC1D,MAAM,uBAAO,IAAI,IAAY;CAE7B,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,OAAO,QAAQ,IAAI;EACzB,IAAI,CAAC,MAAM;EAEX,MAAM,SAAS,MAAM,GAAG,SAAS,MAAM,MAAM;EAC7C,KAAK,MAAM,OAAO,cAAc,MAAM,MAAM,MAAM,GAChD,KAAK,IAAI,GAAG;CAEhB;CAGA,OAAO,EAAE,iBADe,CAAC,GAAG,IAAI,CAAC,CAAC,KACX,EAAE;AAC3B;AAEA,SAAgB,QAAQ,QAA+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
CHANGED
|
@@ -21,7 +21,9 @@ interface TranslationsHook {
|
|
|
21
21
|
variables?: Record<GetVariables<Text>, string>;
|
|
22
22
|
}): string;
|
|
23
23
|
}
|
|
24
|
-
declare function useTranslations(
|
|
24
|
+
declare function useTranslations(hookOptions?: {
|
|
25
|
+
/** provide additional context to all t() calls */note?: string;
|
|
26
|
+
}): TranslationsHook;
|
|
25
27
|
//#endregion
|
|
26
28
|
export { TranslationProvider, TranslationsHook, useTranslations };
|
|
27
29
|
//# sourceMappingURL=react.d.mts.map
|
package/dist/react.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,CAAgB,WAAA;EA/B9B,kDAiCA,IAAA;AAAA,IACE,gBAAgB"}
|
package/dist/react.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { t as encodeKey } from "./shared-
|
|
2
|
+
import { t as encodeKey } from "./shared-CAg_QhcN.mjs";
|
|
3
3
|
import { createContext, use, useMemo } from "react";
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
//#region src/react.tsx
|
|
@@ -15,12 +15,15 @@ function TranslationProvider({ translations, children }) {
|
|
|
15
15
|
children
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
|
-
function useTranslations() {
|
|
18
|
+
function useTranslations(hookOptions) {
|
|
19
19
|
const translations = use(Context);
|
|
20
20
|
return (rawText, opts = {}) => {
|
|
21
21
|
const { note, variables } = opts;
|
|
22
|
-
|
|
23
|
-
if (
|
|
22
|
+
const notes = [];
|
|
23
|
+
if (hookOptions?.note) notes.push(hookOptions.note);
|
|
24
|
+
if (note) notes.push(note);
|
|
25
|
+
let text = translations[encodeKey(rawText, notes)] ?? rawText;
|
|
26
|
+
if (variables) for (const k in variables) text = text.replaceAll(`{${k}}`, variables[k]);
|
|
24
27
|
return text;
|
|
25
28
|
};
|
|
26
29
|
}
|
package/dist/react.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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(hookOptions?: {\n /** provide additional context to all t() calls */\n note?: string;\n}): TranslationsHook {\n const translations = use(Context);\n\n return (rawText, opts = {}) => {\n const { note, variables } = opts;\n const notes: string[] = [];\n if (hookOptions?.note) notes.push(hookOptions.note);\n if (note) notes.push(note);\n const k = encodeKey(rawText, notes);\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,gBAAgB,aAGX;CACnB,MAAM,eAAe,IAAI,OAAO;CAEhC,QAAQ,SAAS,OAAO,CAAC,MAAM;EAC7B,MAAM,EAAE,MAAM,cAAc;EAC5B,MAAM,QAAkB,CAAC;EACzB,IAAI,aAAa,MAAM,MAAM,KAAK,YAAY,IAAI;EAClD,IAAI,MAAM,MAAM,KAAK,IAAI;EAEzB,IAAI,OAAO,aADD,UAAU,SAAS,KACL,MAAM;EAE9B,IAAI,WACF,KAAK,MAAM,KAAK,WAAW,OAAO,KAAK,WAAW,IAAI,EAAE,IAAI,UAAU,EAAW;EAGnF,OAAO;CACT;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared-CAg_QhcN.mjs","names":[],"sources":["../src/shared.ts"],"sourcesContent":["export function encodeKey(text: string, notes: string[]): string {\n return text + notes.map((n) => `(${n})`).join(\"\");\n}\n"],"mappings":";AAAA,SAAgB,UAAU,MAAc,OAAyB;CAC/D,OAAO,OAAO,MAAM,KAAK,MAAM,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE;AAClD"}
|
package/package.json
CHANGED
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"}
|