meowjsx 0.3.0-alpha.0 → 0.3.0-alpha.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/README.md +171 -0
- package/dist/src/context.d.ts +14 -0
- package/dist/src/context.js +36 -0
- package/dist/src/i18n-audit.d.ts +127 -0
- package/dist/src/i18n-audit.js +906 -0
- package/dist/src/i18n-message.d.ts +42 -0
- package/dist/src/i18n-message.js +252 -0
- package/dist/src/i18n.d.ts +151 -0
- package/dist/src/i18n.js +346 -0
- package/dist/src/keyed.js +7 -1
- package/dist/src/reactive.js +7 -6
- package/dist/src/registry.d.ts +2 -1
- package/dist/src/registry.js +2 -2
- package/dist/src/runtime.d.ts +1 -1
- package/dist/src/runtime.js +51 -0
- package/dist/src/types.d.ts +21 -0
- package/dist/web/render.js +26 -2
- package/package.json +9 -1
|
@@ -0,0 +1,906 @@
|
|
|
1
|
+
import { describeNodes, parseTemplate } from "./i18n-message.js";
|
|
2
|
+
class Abort extends Error {
|
|
3
|
+
}
|
|
4
|
+
/** After these words an expression starts, so `/` is a regex and `<` may open JSX. */
|
|
5
|
+
const EXPRESSION_KEYWORDS = new Set([
|
|
6
|
+
"return",
|
|
7
|
+
"typeof",
|
|
8
|
+
"instanceof",
|
|
9
|
+
"in",
|
|
10
|
+
"of",
|
|
11
|
+
"new",
|
|
12
|
+
"delete",
|
|
13
|
+
"void",
|
|
14
|
+
"throw",
|
|
15
|
+
"case",
|
|
16
|
+
"do",
|
|
17
|
+
"else",
|
|
18
|
+
"yield",
|
|
19
|
+
"await",
|
|
20
|
+
"default",
|
|
21
|
+
]);
|
|
22
|
+
const isIdentifierStart = (char) => /[\p{ID_Start}$_]/u.test(char);
|
|
23
|
+
const isIdentifierPart = (char) => /[\p{ID_Continue}$]/u.test(char);
|
|
24
|
+
const isSpace = (char) => /\s/.test(char);
|
|
25
|
+
const isDigit = (char) => char >= "0" && char <= "9";
|
|
26
|
+
function tokenize(code) {
|
|
27
|
+
const tokens = [];
|
|
28
|
+
const length = code.length;
|
|
29
|
+
let pos = 0;
|
|
30
|
+
const push = (token) => {
|
|
31
|
+
tokens.push(token);
|
|
32
|
+
return token;
|
|
33
|
+
};
|
|
34
|
+
const punct = (text, at) => {
|
|
35
|
+
push({ type: "punct", text, start: at, end: at + text.length });
|
|
36
|
+
};
|
|
37
|
+
/** Consumes one escape sequence starting at the backslash and returns its value. */
|
|
38
|
+
const readEscape = () => {
|
|
39
|
+
pos += 1;
|
|
40
|
+
const char = code[pos];
|
|
41
|
+
if (char === undefined)
|
|
42
|
+
return "";
|
|
43
|
+
pos += 1;
|
|
44
|
+
switch (char) {
|
|
45
|
+
case "n":
|
|
46
|
+
return "\n";
|
|
47
|
+
case "r":
|
|
48
|
+
return "\r";
|
|
49
|
+
case "t":
|
|
50
|
+
return "\t";
|
|
51
|
+
case "b":
|
|
52
|
+
return "\b";
|
|
53
|
+
case "f":
|
|
54
|
+
return "\f";
|
|
55
|
+
case "v":
|
|
56
|
+
return "\v";
|
|
57
|
+
case "0":
|
|
58
|
+
return isDigit(code[pos] ?? "") ? "0" : "\0";
|
|
59
|
+
case "x": {
|
|
60
|
+
const digits = code.slice(pos, pos + 2);
|
|
61
|
+
if (/^[0-9a-fA-F]{2}$/.test(digits)) {
|
|
62
|
+
pos += 2;
|
|
63
|
+
return String.fromCharCode(Number.parseInt(digits, 16));
|
|
64
|
+
}
|
|
65
|
+
return "x";
|
|
66
|
+
}
|
|
67
|
+
case "u": {
|
|
68
|
+
if (code[pos] === "{") {
|
|
69
|
+
const close = code.indexOf("}", pos);
|
|
70
|
+
const digits = close < 0 ? "" : code.slice(pos + 1, close);
|
|
71
|
+
if (/^[0-9a-fA-F]{1,6}$/.test(digits) && Number.parseInt(digits, 16) <= 0x10ffff) {
|
|
72
|
+
pos = close + 1;
|
|
73
|
+
return String.fromCodePoint(Number.parseInt(digits, 16));
|
|
74
|
+
}
|
|
75
|
+
return "u";
|
|
76
|
+
}
|
|
77
|
+
const digits = code.slice(pos, pos + 4);
|
|
78
|
+
if (/^[0-9a-fA-F]{4}$/.test(digits)) {
|
|
79
|
+
pos += 4;
|
|
80
|
+
return String.fromCharCode(Number.parseInt(digits, 16));
|
|
81
|
+
}
|
|
82
|
+
return "u";
|
|
83
|
+
}
|
|
84
|
+
case "\r":
|
|
85
|
+
if (code[pos] === "\n")
|
|
86
|
+
pos += 1;
|
|
87
|
+
return "";
|
|
88
|
+
case "\n":
|
|
89
|
+
case "
":
|
|
90
|
+
case "
":
|
|
91
|
+
return "";
|
|
92
|
+
default:
|
|
93
|
+
return char;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const skipComment = () => {
|
|
97
|
+
if (code[pos] !== "/")
|
|
98
|
+
return false;
|
|
99
|
+
if (code[pos + 1] === "/") {
|
|
100
|
+
const end = code.indexOf("\n", pos);
|
|
101
|
+
pos = end < 0 ? length : end;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (code[pos + 1] === "*") {
|
|
105
|
+
const end = code.indexOf("*/", pos + 2);
|
|
106
|
+
pos = end < 0 ? length : end + 2;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
};
|
|
111
|
+
const skipTrivia = () => {
|
|
112
|
+
while (pos < length) {
|
|
113
|
+
if (isSpace(code[pos]))
|
|
114
|
+
pos += 1;
|
|
115
|
+
else if (!skipComment())
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const expressionEnded = () => {
|
|
120
|
+
const previous = tokens[tokens.length - 1];
|
|
121
|
+
if (!previous)
|
|
122
|
+
return false;
|
|
123
|
+
if (previous.type === "ident")
|
|
124
|
+
return !EXPRESSION_KEYWORDS.has(previous.text);
|
|
125
|
+
if (previous.type === "punct")
|
|
126
|
+
return previous.text === ")" || previous.text === "]" || previous.text === "}";
|
|
127
|
+
return true;
|
|
128
|
+
};
|
|
129
|
+
function scanString(quote) {
|
|
130
|
+
const start = pos;
|
|
131
|
+
pos += 1;
|
|
132
|
+
let value = "";
|
|
133
|
+
while (pos < length) {
|
|
134
|
+
const char = code[pos];
|
|
135
|
+
if (char === quote) {
|
|
136
|
+
pos += 1;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
if (char === "\\")
|
|
140
|
+
value += readEscape();
|
|
141
|
+
else if (char === "\n")
|
|
142
|
+
break;
|
|
143
|
+
else {
|
|
144
|
+
value += char;
|
|
145
|
+
pos += 1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
push({ type: "string", text: code.slice(start, pos), start, end: pos, value });
|
|
149
|
+
}
|
|
150
|
+
/** `{` ... `}` with virtual brace tokens so commas inside never split an argument. */
|
|
151
|
+
function scanContainer() {
|
|
152
|
+
punct("{", pos);
|
|
153
|
+
pos += 1;
|
|
154
|
+
scanCode(true);
|
|
155
|
+
if (pos >= length)
|
|
156
|
+
throw new Abort();
|
|
157
|
+
punct("}", pos);
|
|
158
|
+
pos += 1;
|
|
159
|
+
}
|
|
160
|
+
function scanTemplate() {
|
|
161
|
+
const start = pos;
|
|
162
|
+
pos += 1;
|
|
163
|
+
let value = "";
|
|
164
|
+
let simple = true;
|
|
165
|
+
const token = push({ type: "template", text: "", start, end: start });
|
|
166
|
+
while (pos < length) {
|
|
167
|
+
const char = code[pos];
|
|
168
|
+
if (char === "`") {
|
|
169
|
+
pos += 1;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
if (char === "\\")
|
|
173
|
+
value += readEscape();
|
|
174
|
+
else if (char === "$" && code[pos + 1] === "{") {
|
|
175
|
+
simple = false;
|
|
176
|
+
pos += 1;
|
|
177
|
+
scanContainer();
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
value += char;
|
|
181
|
+
pos += 1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
token.end = pos;
|
|
185
|
+
token.text = code.slice(start, pos);
|
|
186
|
+
token.simple = simple;
|
|
187
|
+
if (simple)
|
|
188
|
+
token.value = value;
|
|
189
|
+
}
|
|
190
|
+
function scanRegex() {
|
|
191
|
+
const start = pos;
|
|
192
|
+
let cursor = pos + 1;
|
|
193
|
+
let inClass = false;
|
|
194
|
+
while (cursor < length) {
|
|
195
|
+
const char = code[cursor];
|
|
196
|
+
if (char === "\n")
|
|
197
|
+
return false;
|
|
198
|
+
if (char === "\\")
|
|
199
|
+
cursor += 2;
|
|
200
|
+
else if (char === "[") {
|
|
201
|
+
inClass = true;
|
|
202
|
+
cursor += 1;
|
|
203
|
+
}
|
|
204
|
+
else if (char === "]") {
|
|
205
|
+
inClass = false;
|
|
206
|
+
cursor += 1;
|
|
207
|
+
}
|
|
208
|
+
else if (char === "/" && !inClass)
|
|
209
|
+
break;
|
|
210
|
+
else
|
|
211
|
+
cursor += 1;
|
|
212
|
+
}
|
|
213
|
+
if (cursor >= length)
|
|
214
|
+
return false;
|
|
215
|
+
cursor += 1;
|
|
216
|
+
while (cursor < length && /[a-z]/i.test(code[cursor]))
|
|
217
|
+
cursor += 1;
|
|
218
|
+
pos = cursor;
|
|
219
|
+
push({ type: "regex", text: code.slice(start, pos), start, end: pos });
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
const startsJsx = () => {
|
|
223
|
+
if (expressionEnded())
|
|
224
|
+
return false;
|
|
225
|
+
const next = code[pos + 1];
|
|
226
|
+
if (next === undefined || (next !== ">" && !isIdentifierStart(next)))
|
|
227
|
+
return false;
|
|
228
|
+
// `<T,>(x) => x` and `<T extends U>(x) => x` are generic arrow functions, not elements.
|
|
229
|
+
return !/^<[\p{ID_Start}$_][\w$.:-]*\s*(,|extends\b)/u.test(code.slice(pos, pos + 80));
|
|
230
|
+
};
|
|
231
|
+
function skipJsxSpace() {
|
|
232
|
+
skipTrivia();
|
|
233
|
+
}
|
|
234
|
+
function scanJsxChildren() {
|
|
235
|
+
for (;;) {
|
|
236
|
+
while (pos < length && code[pos] !== "<" && code[pos] !== "{")
|
|
237
|
+
pos += 1;
|
|
238
|
+
if (pos >= length)
|
|
239
|
+
throw new Abort();
|
|
240
|
+
if (code[pos] === "{")
|
|
241
|
+
scanContainer();
|
|
242
|
+
else if (code[pos + 1] === "/") {
|
|
243
|
+
const close = code.indexOf(">", pos);
|
|
244
|
+
if (close < 0)
|
|
245
|
+
throw new Abort();
|
|
246
|
+
pos = close + 1;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
else
|
|
250
|
+
scanJsxElement(false);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function scanJsxElement(top) {
|
|
254
|
+
const start = pos;
|
|
255
|
+
const marker = top ? push({ type: "jsx", text: "<", start, end: start }) : undefined;
|
|
256
|
+
pos += 1;
|
|
257
|
+
if (code[pos] === ">") {
|
|
258
|
+
pos += 1;
|
|
259
|
+
scanJsxChildren();
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
while (pos < length && /[\w$.:-]/.test(code[pos]))
|
|
263
|
+
pos += 1;
|
|
264
|
+
for (;;) {
|
|
265
|
+
skipJsxSpace();
|
|
266
|
+
const char = code[pos];
|
|
267
|
+
if (char === undefined)
|
|
268
|
+
throw new Abort();
|
|
269
|
+
if (char === "/" && code[pos + 1] === ">") {
|
|
270
|
+
pos += 2;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
if (char === ">") {
|
|
274
|
+
pos += 1;
|
|
275
|
+
scanJsxChildren();
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
if (char === "{")
|
|
279
|
+
scanContainer();
|
|
280
|
+
else if (/[\w$]/.test(char)) {
|
|
281
|
+
while (pos < length && /[\w$:.-]/.test(code[pos]))
|
|
282
|
+
pos += 1;
|
|
283
|
+
skipJsxSpace();
|
|
284
|
+
if (code[pos] !== "=")
|
|
285
|
+
continue;
|
|
286
|
+
pos += 1;
|
|
287
|
+
skipJsxSpace();
|
|
288
|
+
const value = code[pos];
|
|
289
|
+
if (value === '"' || value === "'") {
|
|
290
|
+
const close = code.indexOf(value, pos + 1);
|
|
291
|
+
if (close < 0)
|
|
292
|
+
throw new Abort();
|
|
293
|
+
pos = close + 1;
|
|
294
|
+
}
|
|
295
|
+
else if (value === "{")
|
|
296
|
+
scanContainer();
|
|
297
|
+
else if (value === "<")
|
|
298
|
+
scanJsxElement(false);
|
|
299
|
+
else
|
|
300
|
+
throw new Abort();
|
|
301
|
+
}
|
|
302
|
+
else
|
|
303
|
+
throw new Abort();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (marker)
|
|
307
|
+
marker.end = pos;
|
|
308
|
+
}
|
|
309
|
+
function scanCode(untilBrace) {
|
|
310
|
+
let depth = 0;
|
|
311
|
+
while (pos < length) {
|
|
312
|
+
const char = code[pos];
|
|
313
|
+
if (isSpace(char)) {
|
|
314
|
+
pos += 1;
|
|
315
|
+
}
|
|
316
|
+
else if (char === "/" && (code[pos + 1] === "/" || code[pos + 1] === "*")) {
|
|
317
|
+
skipComment();
|
|
318
|
+
}
|
|
319
|
+
else if (char === '"' || char === "'") {
|
|
320
|
+
scanString(char);
|
|
321
|
+
}
|
|
322
|
+
else if (char === "`") {
|
|
323
|
+
scanTemplate();
|
|
324
|
+
}
|
|
325
|
+
else if (isIdentifierStart(char)) {
|
|
326
|
+
const start = pos;
|
|
327
|
+
pos += 1;
|
|
328
|
+
while (pos < length && isIdentifierPart(code[pos]))
|
|
329
|
+
pos += 1;
|
|
330
|
+
push({ type: "ident", text: code.slice(start, pos), start, end: pos });
|
|
331
|
+
}
|
|
332
|
+
else if (isDigit(char)) {
|
|
333
|
+
const start = pos;
|
|
334
|
+
while (pos < length && /[\w.]/.test(code[pos]))
|
|
335
|
+
pos += 1;
|
|
336
|
+
push({ type: "number", text: code.slice(start, pos), start, end: pos });
|
|
337
|
+
}
|
|
338
|
+
else if (char === "{") {
|
|
339
|
+
depth += 1;
|
|
340
|
+
punct("{", pos);
|
|
341
|
+
pos += 1;
|
|
342
|
+
}
|
|
343
|
+
else if (char === "}") {
|
|
344
|
+
if (depth === 0 && untilBrace)
|
|
345
|
+
return;
|
|
346
|
+
depth = Math.max(0, depth - 1);
|
|
347
|
+
punct("}", pos);
|
|
348
|
+
pos += 1;
|
|
349
|
+
}
|
|
350
|
+
else if (char === "<" && startsJsx()) {
|
|
351
|
+
const snapshot = { pos, count: tokens.length };
|
|
352
|
+
try {
|
|
353
|
+
scanJsxElement(true);
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
if (!(error instanceof Abort))
|
|
357
|
+
throw error;
|
|
358
|
+
pos = snapshot.pos;
|
|
359
|
+
tokens.length = snapshot.count;
|
|
360
|
+
punct("<", pos);
|
|
361
|
+
pos += 1;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else if (char === "/" && !expressionEnded() && scanRegex()) {
|
|
365
|
+
// The regex token is pushed by scanRegex.
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
punct(char, pos);
|
|
369
|
+
pos += 1;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (untilBrace)
|
|
373
|
+
throw new Abort();
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
scanCode(false);
|
|
377
|
+
}
|
|
378
|
+
catch (error) {
|
|
379
|
+
if (!(error instanceof Abort))
|
|
380
|
+
throw error;
|
|
381
|
+
}
|
|
382
|
+
return tokens;
|
|
383
|
+
}
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
// Token helpers
|
|
386
|
+
// ---------------------------------------------------------------------------
|
|
387
|
+
const isPunct = (token, text) => token?.type === "punct" && token.text === text;
|
|
388
|
+
const isIdent = (token, text) => token?.type === "ident" && (text === undefined || token.text === text);
|
|
389
|
+
/** The value of a single string literal or substitution-free template, or `undefined`. */
|
|
390
|
+
function literalValue(tokens) {
|
|
391
|
+
let list = tokens;
|
|
392
|
+
// `"text" as const` is still a literal.
|
|
393
|
+
if (list.length === 3 && isIdent(list[1], "as") && isIdent(list[2], "const"))
|
|
394
|
+
list = list.slice(0, 1);
|
|
395
|
+
if (list.length !== 1)
|
|
396
|
+
return undefined;
|
|
397
|
+
const token = list[0];
|
|
398
|
+
if (token.type === "string")
|
|
399
|
+
return token.value;
|
|
400
|
+
if (token.type === "template" && token.simple)
|
|
401
|
+
return token.value;
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
/** Splits at top-level commas. */
|
|
405
|
+
function splitTop(tokens) {
|
|
406
|
+
const parts = [[]];
|
|
407
|
+
let depth = 0;
|
|
408
|
+
for (const token of tokens) {
|
|
409
|
+
if (token.type === "punct") {
|
|
410
|
+
if (token.text === "(" || token.text === "[" || token.text === "{")
|
|
411
|
+
depth += 1;
|
|
412
|
+
else if (token.text === ")" || token.text === "]" || token.text === "}")
|
|
413
|
+
depth -= 1;
|
|
414
|
+
else if (token.text === "," && depth === 0) {
|
|
415
|
+
parts.push([]);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
parts[parts.length - 1].push(token);
|
|
420
|
+
}
|
|
421
|
+
if (parts[parts.length - 1].length === 0)
|
|
422
|
+
parts.pop();
|
|
423
|
+
return parts;
|
|
424
|
+
}
|
|
425
|
+
/** The tokens inside the object literal that starts `tokens`, or `undefined`. */
|
|
426
|
+
function objectBody(tokens) {
|
|
427
|
+
if (!isPunct(tokens[0], "{"))
|
|
428
|
+
return undefined;
|
|
429
|
+
let depth = 0;
|
|
430
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
431
|
+
const token = tokens[index];
|
|
432
|
+
if (token.type !== "punct")
|
|
433
|
+
continue;
|
|
434
|
+
if (token.text === "(" || token.text === "[" || token.text === "{")
|
|
435
|
+
depth += 1;
|
|
436
|
+
else if (token.text === ")" || token.text === "]" || token.text === "}") {
|
|
437
|
+
depth -= 1;
|
|
438
|
+
if (depth === 0)
|
|
439
|
+
return tokens.slice(1, index);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
function memberKey(member) {
|
|
445
|
+
const key = member[0];
|
|
446
|
+
if (!key || !isPunct(member[1], ":"))
|
|
447
|
+
return undefined;
|
|
448
|
+
if (key.type === "ident")
|
|
449
|
+
return key.text;
|
|
450
|
+
if (key.type === "string")
|
|
451
|
+
return key.value;
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
/** Reads `context` and `note` from an options object literal argument. */
|
|
455
|
+
function readOptions(argument) {
|
|
456
|
+
if (!argument || argument.length === 0)
|
|
457
|
+
return {};
|
|
458
|
+
if (argument.length === 1 &&
|
|
459
|
+
isIdent(argument[0]) &&
|
|
460
|
+
["undefined", "null"].includes(argument[0].text)) {
|
|
461
|
+
return {};
|
|
462
|
+
}
|
|
463
|
+
const body = objectBody(argument);
|
|
464
|
+
if (!body)
|
|
465
|
+
return { unreadable: "options" };
|
|
466
|
+
const result = {};
|
|
467
|
+
for (const member of splitTop(body)) {
|
|
468
|
+
if (isPunct(member[0], "."))
|
|
469
|
+
return { unreadable: "options" };
|
|
470
|
+
const shorthand = member.length === 1 && isIdent(member[0]) ? member[0].text : undefined;
|
|
471
|
+
const key = memberKey(member) ?? shorthand;
|
|
472
|
+
if (key !== "context" && key !== "note")
|
|
473
|
+
continue;
|
|
474
|
+
const value = shorthand ? undefined : literalValue(member.slice(2));
|
|
475
|
+
if (value === undefined) {
|
|
476
|
+
if (key === "context")
|
|
477
|
+
return { unreadable: "context" };
|
|
478
|
+
}
|
|
479
|
+
else if (key === "context")
|
|
480
|
+
result.context = value;
|
|
481
|
+
else
|
|
482
|
+
result.note = value;
|
|
483
|
+
}
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
function excerpt(code, tokens) {
|
|
487
|
+
if (tokens.length === 0)
|
|
488
|
+
return "";
|
|
489
|
+
// A template ends after its last substitution token, so the end is the furthest token end.
|
|
490
|
+
const end = Math.max(...tokens.map((token) => token.end));
|
|
491
|
+
const text = code.slice(tokens[0].start, end).replace(/\s+/g, " ").trim();
|
|
492
|
+
return text.length > 60 ? `${text.slice(0, 57)}...` : text;
|
|
493
|
+
}
|
|
494
|
+
function dynamicReason(tokens) {
|
|
495
|
+
if (tokens.length === 0)
|
|
496
|
+
return "missing-argument";
|
|
497
|
+
let depth = 0;
|
|
498
|
+
for (const token of tokens) {
|
|
499
|
+
if (token.type !== "punct")
|
|
500
|
+
continue;
|
|
501
|
+
if (token.text === "(" || token.text === "[" || token.text === "{")
|
|
502
|
+
depth += 1;
|
|
503
|
+
else if (token.text === ")" || token.text === "]" || token.text === "}")
|
|
504
|
+
depth -= 1;
|
|
505
|
+
else if (token.text === "+" && depth === 0)
|
|
506
|
+
return "concatenation";
|
|
507
|
+
}
|
|
508
|
+
if (tokens[0].type === "template")
|
|
509
|
+
return "template-literal";
|
|
510
|
+
if (tokens.length === 1 && isIdent(tokens[0]))
|
|
511
|
+
return "identifier";
|
|
512
|
+
if (tokens.every((token) => isIdent(token) || isPunct(token, ".") || isPunct(token, "?"))) {
|
|
513
|
+
return "member-access";
|
|
514
|
+
}
|
|
515
|
+
if (isPunct(tokens[tokens.length - 1], ")"))
|
|
516
|
+
return "call";
|
|
517
|
+
return "expression";
|
|
518
|
+
}
|
|
519
|
+
// ---------------------------------------------------------------------------
|
|
520
|
+
// extractMessages
|
|
521
|
+
// ---------------------------------------------------------------------------
|
|
522
|
+
const I18N_MODULE = /(^|\/)i18n(\.[cm]?[jt]sx?)?$/;
|
|
523
|
+
const ALIASED = new Set(["t", "defineMessages"]);
|
|
524
|
+
/** Parses a source for its argument and tag names; empty when it does not parse. */
|
|
525
|
+
function describeSource(source, rich) {
|
|
526
|
+
try {
|
|
527
|
+
return describeNodes(parseTemplate(source, { rich }));
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
return { names: [], tags: [] };
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
export function extractMessages(code, file) {
|
|
534
|
+
const tokens = tokenize(code);
|
|
535
|
+
const lineStarts = [0];
|
|
536
|
+
for (let index = 0; index < code.length; index += 1) {
|
|
537
|
+
if (code[index] === "\n")
|
|
538
|
+
lineStarts.push(index + 1);
|
|
539
|
+
}
|
|
540
|
+
const position = (offset) => {
|
|
541
|
+
let low = 0;
|
|
542
|
+
let high = lineStarts.length - 1;
|
|
543
|
+
while (low < high) {
|
|
544
|
+
const middle = (low + high + 1) >> 1;
|
|
545
|
+
if (lineStarts[middle] <= offset)
|
|
546
|
+
low = middle;
|
|
547
|
+
else
|
|
548
|
+
high = middle - 1;
|
|
549
|
+
}
|
|
550
|
+
return { line: low + 1, column: offset - lineStarts[low] + 1 };
|
|
551
|
+
};
|
|
552
|
+
const messages = [];
|
|
553
|
+
const dynamic = [];
|
|
554
|
+
const aliases = [];
|
|
555
|
+
const aliasOf = new Map();
|
|
556
|
+
const addDynamic = (at, callee, argument, reason) => {
|
|
557
|
+
dynamic.push({ file, ...position(at.start), callee, argument, reason });
|
|
558
|
+
};
|
|
559
|
+
const addMessage = (at, kind, source, read, extra) => {
|
|
560
|
+
const described = extra.literal
|
|
561
|
+
? { names: [], tags: [] }
|
|
562
|
+
: describeSource(source, kind === "rich");
|
|
563
|
+
messages.push({
|
|
564
|
+
source,
|
|
565
|
+
...(read.context === undefined ? {} : { context: read.context }),
|
|
566
|
+
...(read.note === undefined ? {} : { note: read.note }),
|
|
567
|
+
kind,
|
|
568
|
+
...(extra.key === undefined ? {} : { key: extra.key }),
|
|
569
|
+
...(extra.literal ? { literal: true } : {}),
|
|
570
|
+
file,
|
|
571
|
+
...position(at.start),
|
|
572
|
+
placeholders: described.names,
|
|
573
|
+
tags: kind === "rich" ? described.tags : [],
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
/** The argument token lists of the call whose `(` is at `open`, or `undefined`. */
|
|
577
|
+
const callArguments = (open) => {
|
|
578
|
+
let depth = 0;
|
|
579
|
+
for (let index = open + 1; index < tokens.length; index += 1) {
|
|
580
|
+
const token = tokens[index];
|
|
581
|
+
if (token.type !== "punct")
|
|
582
|
+
continue;
|
|
583
|
+
if (token.text === "(" || token.text === "[" || token.text === "{")
|
|
584
|
+
depth += 1;
|
|
585
|
+
else if (token.text === ")" || token.text === "]" || token.text === "}") {
|
|
586
|
+
if (depth === 0) {
|
|
587
|
+
if (token.text !== ")")
|
|
588
|
+
return undefined;
|
|
589
|
+
return { args: splitTop(tokens.slice(open + 1, index)), close: index };
|
|
590
|
+
}
|
|
591
|
+
depth -= 1;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return undefined;
|
|
595
|
+
};
|
|
596
|
+
const readAlias = (importIndex) => {
|
|
597
|
+
let cursor = importIndex + 1;
|
|
598
|
+
if (isIdent(tokens[cursor], "type"))
|
|
599
|
+
return;
|
|
600
|
+
while (cursor < importIndex + 8 && !isPunct(tokens[cursor], "{")) {
|
|
601
|
+
if (tokens[cursor]?.type === "string")
|
|
602
|
+
return;
|
|
603
|
+
cursor += 1;
|
|
604
|
+
}
|
|
605
|
+
if (!isPunct(tokens[cursor], "{"))
|
|
606
|
+
return;
|
|
607
|
+
const end = tokens.findIndex((token, index) => index > cursor && isPunct(token, "}"));
|
|
608
|
+
if (end < 0 || !isIdent(tokens[end + 1], "from") || tokens[end + 2]?.type !== "string")
|
|
609
|
+
return;
|
|
610
|
+
const module = tokens[end + 2].value;
|
|
611
|
+
if (!I18N_MODULE.test(module))
|
|
612
|
+
return;
|
|
613
|
+
for (const specifier of splitTop(tokens.slice(cursor + 1, end))) {
|
|
614
|
+
const names = specifier.filter((token) => isIdent(token));
|
|
615
|
+
const asIndex = names.findIndex((token) => token.text === "as");
|
|
616
|
+
if (asIndex <= 0 || asIndex !== names.length - 2)
|
|
617
|
+
continue;
|
|
618
|
+
const imported = names[asIndex - 1];
|
|
619
|
+
const local = names[asIndex + 1];
|
|
620
|
+
if (!ALIASED.has(imported.text) || local.text === imported.text)
|
|
621
|
+
continue;
|
|
622
|
+
aliasOf.set(local.text, imported.text);
|
|
623
|
+
aliases.push({
|
|
624
|
+
file,
|
|
625
|
+
...position(imported.start),
|
|
626
|
+
imported: imported.text,
|
|
627
|
+
local: local.text,
|
|
628
|
+
module,
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
const readMessageCall = (at, callee, kind, args) => {
|
|
633
|
+
const first = args[0] ?? [];
|
|
634
|
+
const optionsIndex = kind === "rich" ? 3 : kind === "lookup" ? 1 : 2;
|
|
635
|
+
const source = literalValue(first);
|
|
636
|
+
if (source === undefined) {
|
|
637
|
+
addDynamic(at, callee, excerpt(code, first), kind === "lookup" ? "lookup" : dynamicReason(first));
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
const read = readOptions(args[optionsIndex]);
|
|
641
|
+
if (read.unreadable) {
|
|
642
|
+
addDynamic(at, callee, excerpt(code, args[optionsIndex] ?? first), read.unreadable);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (kind === "lookup")
|
|
646
|
+
addMessage(at, "t", source, read, { literal: true });
|
|
647
|
+
else
|
|
648
|
+
addMessage(at, kind, source, read, {});
|
|
649
|
+
};
|
|
650
|
+
const readDefineMessages = (at, callee, args) => {
|
|
651
|
+
const first = args[0] ?? [];
|
|
652
|
+
const body = objectBody(first);
|
|
653
|
+
if (!body) {
|
|
654
|
+
addDynamic(at, callee, excerpt(code, first), first.length === 0 ? "missing-argument" : "not-an-object-literal");
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
for (const member of splitTop(body)) {
|
|
658
|
+
const keyToken = member[0];
|
|
659
|
+
const key = memberKey(member);
|
|
660
|
+
const value = member.slice(2);
|
|
661
|
+
if (key === undefined) {
|
|
662
|
+
addDynamic(keyToken, callee, excerpt(code, member), "unreadable-member");
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
const direct = literalValue(value);
|
|
666
|
+
if (direct !== undefined) {
|
|
667
|
+
addMessage(keyToken, "defined", direct, {}, { key });
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const inner = objectBody(value);
|
|
671
|
+
const fields = inner ? splitTop(inner) : [];
|
|
672
|
+
const sourceField = fields.find((field) => memberKey(field) === "source");
|
|
673
|
+
const source = sourceField ? literalValue(sourceField.slice(2)) : undefined;
|
|
674
|
+
if (!inner || source === undefined) {
|
|
675
|
+
addDynamic(keyToken, callee, excerpt(code, member), "unreadable-member");
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
const read = readOptions(value);
|
|
679
|
+
if (read.unreadable) {
|
|
680
|
+
addDynamic(keyToken, callee, excerpt(code, member), read.unreadable);
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
addMessage(keyToken, "defined", source, read, { key });
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
687
|
+
const token = tokens[index];
|
|
688
|
+
if (token.type !== "ident") {
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
if (token.text === "import") {
|
|
692
|
+
readAlias(index);
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
const previous = tokens[index - 1];
|
|
696
|
+
if (isIdent(previous, "function"))
|
|
697
|
+
continue;
|
|
698
|
+
const spread = isPunct(previous, ".") &&
|
|
699
|
+
isPunct(tokens[index - 2], ".") &&
|
|
700
|
+
tokens[index - 2].end === previous.start;
|
|
701
|
+
const member = isPunct(previous, ".") && !spread;
|
|
702
|
+
const prefix = member ? "." : "";
|
|
703
|
+
const imported = member ? undefined : aliasOf.get(token.text);
|
|
704
|
+
const name = imported ?? token.text;
|
|
705
|
+
const next = tokens[index + 1];
|
|
706
|
+
let run;
|
|
707
|
+
let open = -1;
|
|
708
|
+
if (name === "t") {
|
|
709
|
+
if (isPunct(next, "(")) {
|
|
710
|
+
open = index + 1;
|
|
711
|
+
const callee = prefix + token.text;
|
|
712
|
+
run = (args) => readMessageCall(token, callee, "t", args);
|
|
713
|
+
}
|
|
714
|
+
else if (isPunct(next, ".") &&
|
|
715
|
+
isIdent(tokens[index + 2], "rich") &&
|
|
716
|
+
isPunct(tokens[index + 3], "(")) {
|
|
717
|
+
open = index + 3;
|
|
718
|
+
const callee = `${prefix}${token.text}.rich`;
|
|
719
|
+
run = (args) => readMessageCall(token, callee, "rich", args);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
else if (name === "lookup" && member && isPunct(next, "(")) {
|
|
723
|
+
open = index + 1;
|
|
724
|
+
run = (args) => readMessageCall(token, ".lookup", "lookup", args);
|
|
725
|
+
}
|
|
726
|
+
else if (name === "defineMessages" && isPunct(next, "(")) {
|
|
727
|
+
open = index + 1;
|
|
728
|
+
const callee = prefix + token.text;
|
|
729
|
+
run = (args) => readDefineMessages(token, callee, args);
|
|
730
|
+
}
|
|
731
|
+
if (!run)
|
|
732
|
+
continue;
|
|
733
|
+
const call = callArguments(open);
|
|
734
|
+
if (!call)
|
|
735
|
+
continue;
|
|
736
|
+
// `t(source) { ... }` is a method or function definition, not a call.
|
|
737
|
+
if (isPunct(tokens[call.close + 1], "{"))
|
|
738
|
+
continue;
|
|
739
|
+
run(call.args);
|
|
740
|
+
}
|
|
741
|
+
return { messages, dynamic, aliases };
|
|
742
|
+
}
|
|
743
|
+
// ---------------------------------------------------------------------------
|
|
744
|
+
// messageArguments
|
|
745
|
+
// ---------------------------------------------------------------------------
|
|
746
|
+
/**
|
|
747
|
+
* Argument names and rich tag names of a message, from the parser the run time uses.
|
|
748
|
+
* `rich: true` reads `<name>...</name>` tags. Throws an `Error` when the template does not
|
|
749
|
+
* parse.
|
|
750
|
+
*/
|
|
751
|
+
export function messageArguments(source, options = {}) {
|
|
752
|
+
try {
|
|
753
|
+
return describeNodes(parseTemplate(source, { rich: options.rich === true }));
|
|
754
|
+
}
|
|
755
|
+
catch (error) {
|
|
756
|
+
throw new Error(error instanceof Error ? error.message : String(error));
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
/** `closest` needs at least this normalized edit-distance similarity (1 is identical). */
|
|
760
|
+
export const CLOSEST_SIMILARITY = 0.6;
|
|
761
|
+
function similarity(a, b) {
|
|
762
|
+
const left = a.toLowerCase();
|
|
763
|
+
const right = b.toLowerCase();
|
|
764
|
+
const longest = Math.max(left.length, right.length);
|
|
765
|
+
if (longest === 0)
|
|
766
|
+
return 1;
|
|
767
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
768
|
+
for (let row = 1; row <= left.length; row += 1) {
|
|
769
|
+
const current = [row];
|
|
770
|
+
for (let column = 1; column <= right.length; column += 1) {
|
|
771
|
+
current[column] = Math.min(previous[column] + 1, current[column - 1] + 1, previous[column - 1] + (left[row - 1] === right[column - 1] ? 0 : 1));
|
|
772
|
+
}
|
|
773
|
+
previous = current;
|
|
774
|
+
}
|
|
775
|
+
return 1 - previous[right.length] / longest;
|
|
776
|
+
}
|
|
777
|
+
const identity = (source, context) => JSON.stringify([context ?? "", source]);
|
|
778
|
+
function translationOf(catalog, source, context) {
|
|
779
|
+
if (!Object.hasOwn(catalog, source))
|
|
780
|
+
return undefined;
|
|
781
|
+
const entry = catalog[source];
|
|
782
|
+
let found;
|
|
783
|
+
if (typeof entry === "string")
|
|
784
|
+
found = context === undefined || context === "" ? entry : undefined;
|
|
785
|
+
else if (typeof entry === "object" && entry !== null) {
|
|
786
|
+
found = Object.hasOwn(entry, context ?? "") ? entry[context ?? ""] : undefined;
|
|
787
|
+
}
|
|
788
|
+
return typeof found === "string" && found !== "" ? found : undefined;
|
|
789
|
+
}
|
|
790
|
+
function difference(label, expected, actual) {
|
|
791
|
+
const missing = expected.filter((name) => !actual.includes(name));
|
|
792
|
+
const unexpected = actual.filter((name) => !expected.includes(name));
|
|
793
|
+
return (`${label} differ from the source` +
|
|
794
|
+
(missing.length ? `; missing: ${missing.join(", ")}` : "") +
|
|
795
|
+
(unexpected.length ? `; unexpected: ${unexpected.join(", ")}` : ""));
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* Compares the messages the code uses with one locale's catalog and returns the work list.
|
|
799
|
+
* `catalog` maps a source to a translation, or to a map from context to translation.
|
|
800
|
+
*/
|
|
801
|
+
export function auditCatalog(messages, catalog, locale, options = {}) {
|
|
802
|
+
const extraLiteral = options.extraLiteral ?? true;
|
|
803
|
+
const wanted = new Map();
|
|
804
|
+
const add = (message, literal) => {
|
|
805
|
+
const key = identity(message.source, message.context);
|
|
806
|
+
const known = wanted.get(key);
|
|
807
|
+
const rich = message.kind === "rich";
|
|
808
|
+
if (known) {
|
|
809
|
+
if (rich)
|
|
810
|
+
known.rich = true;
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
wanted.set(key, { ...message, literal: literal || message.literal === true, rich });
|
|
814
|
+
};
|
|
815
|
+
for (const message of messages)
|
|
816
|
+
add(message, false);
|
|
817
|
+
for (const message of options.extra ?? [])
|
|
818
|
+
add(message, extraLiteral);
|
|
819
|
+
const result = { locale, missing: [], unused: [], invalid: [] };
|
|
820
|
+
for (const message of wanted.values()) {
|
|
821
|
+
const { source, context } = message;
|
|
822
|
+
const translation = translationOf(catalog, source, context);
|
|
823
|
+
if (translation === undefined) {
|
|
824
|
+
result.missing.push({
|
|
825
|
+
source,
|
|
826
|
+
...(context === undefined || context === "" ? {} : { context }),
|
|
827
|
+
...(message.note === undefined ? {} : { note: message.note }),
|
|
828
|
+
...(message.file === undefined ? {} : { file: message.file }),
|
|
829
|
+
...(message.line === undefined ? {} : { line: message.line }),
|
|
830
|
+
});
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
if (message.literal)
|
|
834
|
+
continue;
|
|
835
|
+
const invalid = (reason) => {
|
|
836
|
+
result.invalid.push({
|
|
837
|
+
source,
|
|
838
|
+
...(context === undefined || context === "" ? {} : { context }),
|
|
839
|
+
translation,
|
|
840
|
+
reason,
|
|
841
|
+
});
|
|
842
|
+
};
|
|
843
|
+
let expected;
|
|
844
|
+
try {
|
|
845
|
+
expected = messageArguments(source, { rich: message.rich });
|
|
846
|
+
}
|
|
847
|
+
catch (error) {
|
|
848
|
+
invalid(`the source does not parse: ${error.message}`);
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
let actual;
|
|
852
|
+
try {
|
|
853
|
+
actual = messageArguments(translation, { rich: message.rich });
|
|
854
|
+
}
|
|
855
|
+
catch (error) {
|
|
856
|
+
invalid(`the translation does not parse: ${error.message}`);
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
if (expected.names.join("\u0000") !== actual.names.join("\u0000")) {
|
|
860
|
+
invalid(difference("argument names", expected.names, actual.names));
|
|
861
|
+
}
|
|
862
|
+
else if (message.rich && expected.tags.join("\u0000") !== actual.tags.join("\u0000")) {
|
|
863
|
+
invalid(difference("rich tags", expected.tags, actual.tags));
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
const entries = [];
|
|
867
|
+
for (const source of Object.keys(catalog)) {
|
|
868
|
+
const entry = catalog[source];
|
|
869
|
+
if (typeof entry === "string")
|
|
870
|
+
entries.push({ source, context: "", translation: entry });
|
|
871
|
+
else if (typeof entry === "object" && entry !== null) {
|
|
872
|
+
for (const context of Object.keys(entry)) {
|
|
873
|
+
entries.push({ source, context, translation: entry[context] });
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
for (const entry of entries) {
|
|
878
|
+
if (typeof entry.translation !== "string" || entry.translation === "")
|
|
879
|
+
continue;
|
|
880
|
+
if (wanted.has(identity(entry.source, entry.context)))
|
|
881
|
+
continue;
|
|
882
|
+
let closest;
|
|
883
|
+
let best = CLOSEST_SIMILARITY;
|
|
884
|
+
for (const candidate of result.missing) {
|
|
885
|
+
const score = similarity(entry.source, candidate.source);
|
|
886
|
+
if (score >= best && (closest === undefined || score > best)) {
|
|
887
|
+
closest = candidate;
|
|
888
|
+
best = score;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
result.unused.push({
|
|
892
|
+
source: entry.source,
|
|
893
|
+
...(entry.context === "" ? {} : { context: entry.context }),
|
|
894
|
+
translation: entry.translation,
|
|
895
|
+
...(closest
|
|
896
|
+
? {
|
|
897
|
+
closest: {
|
|
898
|
+
source: closest.source,
|
|
899
|
+
...(closest.context === undefined ? {} : { context: closest.context }),
|
|
900
|
+
},
|
|
901
|
+
}
|
|
902
|
+
: {}),
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
return result;
|
|
906
|
+
}
|