@parlajs/x18n-plugin 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +23 -2
- package/build/ast.d.ts +29 -0
- package/build/ast.d.ts.map +1 -0
- package/build/ast.js +64 -0
- package/build/bindings.d.ts +31 -0
- package/build/bindings.d.ts.map +1 -0
- package/build/bindings.js +177 -0
- package/build/configs.d.ts +7 -0
- package/build/configs.d.ts.map +1 -0
- package/build/configs.js +46 -0
- package/build/declare.d.ts +8 -0
- package/build/declare.d.ts.map +1 -0
- package/build/declare.js +69 -0
- package/build/index.d.ts +10 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +23 -0
- package/build/namespaces.d.ts +11 -0
- package/build/namespaces.d.ts.map +1 -0
- package/build/namespaces.js +72 -0
- package/build/paths.d.ts +3 -0
- package/build/paths.d.ts.map +1 -0
- package/build/paths.js +7 -0
- package/build/placeholders.d.ts +3 -0
- package/build/placeholders.d.ts.map +1 -0
- package/build/placeholders.js +58 -0
- package/build/scanner.d.ts +4 -0
- package/build/scanner.d.ts.map +1 -0
- package/build/scanner.js +154 -0
- package/build/templates.d.ts +8 -0
- package/build/templates.d.ts.map +1 -0
- package/build/templates.js +107 -0
- package/build/types.d.ts +21 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +1 -0
- package/build/values.d.ts +4 -0
- package/build/values.d.ts.map +1 -0
- package/build/values.js +29 -0
- package/package.json +64 -3
- package/src/ast.ts +90 -0
- package/src/bindings.ts +226 -0
- package/src/configs.ts +58 -0
- package/src/declare.ts +94 -0
- package/src/index.ts +34 -0
- package/src/namespaces.ts +93 -0
- package/src/paths.ts +9 -0
- package/src/placeholders.ts +63 -0
- package/src/scanner.ts +175 -0
- package/src/templates.ts +115 -0
- package/src/types.ts +27 -0
- package/src/values.ts +33 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"placeholders.d.ts","sourceRoot":"","sources":["../src/placeholders.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAe,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAoC3E,eAAO,MAAM,YAAY,aAAc,QAAQ,KAAG,iBAwBhD,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { compileMessage } from '@parlajs/compiler';
|
|
2
|
+
// A namespace's format is in parla's config, which a placeholder syntax never sees: only MF2
|
|
3
|
+
// writes `{$name}` or starts with a declaration, and ICU is tried first. Other formats are known for
|
|
4
|
+
// the messages `declare` read.
|
|
5
|
+
const MF2_ONLY = /\{\s*\$|^\s*\.(?:input|local|match)\b/;
|
|
6
|
+
/** Params and markup a message reads, through parla's own parsers. Throws on invalid syntax. */
|
|
7
|
+
function messageNames(value, declared) {
|
|
8
|
+
const own = declared.syntaxes.get(value);
|
|
9
|
+
const builtIn = (MF2_ONLY.test(value) ? ['mf2'] : ['icu', 'mf2']).flatMap((id) => declared.formats.get(id) ?? []);
|
|
10
|
+
let firstError;
|
|
11
|
+
for (const format of own ? [own] : builtIn) {
|
|
12
|
+
try {
|
|
13
|
+
const { info } = compileMessage(format.parse(value), { custom: declared.functions });
|
|
14
|
+
return [...new Set([...info.params.keys(), ...info.markup])];
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
firstError ??= error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// No config reads ICU or MF2, so this message has no syntax to check.
|
|
21
|
+
if (firstError === undefined) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
throw firstError;
|
|
25
|
+
}
|
|
26
|
+
/** Where `name` opens a placeholder or tag (`{name`, `{$name`, `{#name`, `<name`), not inside text. */
|
|
27
|
+
function nameAt(value, name) {
|
|
28
|
+
const escaped = name.replaceAll(/[$()*+.?[\\\]^{|}-]/g, String.raw `\$&`);
|
|
29
|
+
const match = new RegExp(String.raw `[{<]\s*[$#/]?\s*(?:${escaped})(?![\w.-])`).exec(value);
|
|
30
|
+
return match ? match.index + match[0].length - name.length : value.indexOf(name);
|
|
31
|
+
}
|
|
32
|
+
export const placeholders = (declared) => ({
|
|
33
|
+
id: 'parla',
|
|
34
|
+
example: '{name}',
|
|
35
|
+
implicit: declared.markup,
|
|
36
|
+
find(value) {
|
|
37
|
+
let names;
|
|
38
|
+
try {
|
|
39
|
+
names = messageNames(value, declared);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
return names.map((name) => {
|
|
45
|
+
const start = Math.max(nameAt(value, name), 0);
|
|
46
|
+
return { syntax: 'parla', text: name, name, range: { start, end: start + name.length } };
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
validate(value) {
|
|
50
|
+
try {
|
|
51
|
+
messageNames(value, declared);
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
return [error.message];
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanner.d.ts","sourceRoot":"","sources":["../src/scanner.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAS1C,OAAO,KAAK,EAAE,QAAQ,EAAS,MAAM,YAAY,CAAC;AAyElD,wBAAgB,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CA2FzF"}
|
package/build/scanner.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { parseSync, Visitor } from 'oxc-parser';
|
|
2
|
+
import { isNode, keyOf, memberChain, staticKeys } from './ast.js';
|
|
3
|
+
import { collectBindings, resolve } from './bindings.js';
|
|
4
|
+
import { namespaceImports } from './namespaces.js';
|
|
5
|
+
import { scriptView, viewName } from './templates.js';
|
|
6
|
+
import { valuesIn } from './values.js';
|
|
7
|
+
/** The keys a call passes: none without an argument, unknown when they are not static. */
|
|
8
|
+
const paramKeys = (params) => params ? staticKeys(params) : [];
|
|
9
|
+
// Offsets in the view are the file's, so usages point into the template itself.
|
|
10
|
+
const TEMPLATE = /\.(?:vue|svelte|astro)$/;
|
|
11
|
+
/** `t.ns.…` on a bound `t`, with the key split from its namespace. */
|
|
12
|
+
function keyChainOf(node, bindings) {
|
|
13
|
+
const chain = memberChain(node);
|
|
14
|
+
const binding = chain && resolve(bindings, { name: chain.root['name'], position: node.start });
|
|
15
|
+
if (!binding) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if ('namespace' in binding) {
|
|
19
|
+
return { ...chain, namespace: binding.namespace, key: chain.segments };
|
|
20
|
+
}
|
|
21
|
+
if ('promise' in binding) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
let { segments } = chain;
|
|
25
|
+
if ('hook' in binding) {
|
|
26
|
+
// A hook's result not destructured: `messages.t.checkout.title()`.
|
|
27
|
+
if (segments[0]?.name !== 't') {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
segments = segments.slice(1);
|
|
31
|
+
}
|
|
32
|
+
const namespaces = 'hook' in binding ? binding.hook : binding.namespaces;
|
|
33
|
+
// Vue's ref: `t.value.checkout.title()`.
|
|
34
|
+
if (segments[0]?.name === 'value' && !namespaces.includes('value')) {
|
|
35
|
+
segments = segments.slice(1);
|
|
36
|
+
}
|
|
37
|
+
// `t` is keyed by namespace: `t.checkout.title()`.
|
|
38
|
+
const [namespace, ...key] = segments;
|
|
39
|
+
return namespace && namespaces.includes(namespace.name)
|
|
40
|
+
? { ...chain, namespace: namespace.name, key }
|
|
41
|
+
: undefined;
|
|
42
|
+
}
|
|
43
|
+
/** A namespace taken out of a load, then passed on: any key may be used. */
|
|
44
|
+
function passedOn(root, { bindings, read }) {
|
|
45
|
+
const usages = [];
|
|
46
|
+
const values = [];
|
|
47
|
+
if (bindings.some((binding) => 'namespace' in binding)) {
|
|
48
|
+
valuesIn(root, values);
|
|
49
|
+
}
|
|
50
|
+
for (const node of values) {
|
|
51
|
+
const binding = read.has(node)
|
|
52
|
+
? undefined
|
|
53
|
+
: resolve(bindings, { name: node['name'], position: node.start });
|
|
54
|
+
if (binding && 'namespace' in binding) {
|
|
55
|
+
usages.push({
|
|
56
|
+
kind: 'accessor',
|
|
57
|
+
namespace: binding.namespace,
|
|
58
|
+
raw: '',
|
|
59
|
+
range: { start: node.start, end: node.end },
|
|
60
|
+
dynamic: 'prefix',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return usages;
|
|
65
|
+
}
|
|
66
|
+
export function accessorScanner(callees, declared) {
|
|
67
|
+
return {
|
|
68
|
+
id: 'parla:accessor',
|
|
69
|
+
extensions: ['js', 'jsx', 'mjs', 'ts', 'tsx', 'mts', 'vue', 'svelte', 'astro'],
|
|
70
|
+
scan(file) {
|
|
71
|
+
const template = TEMPLATE.test(file.path);
|
|
72
|
+
const { program } = template
|
|
73
|
+
? parseSync(viewName(file.path), scriptView(file.path, file.text))
|
|
74
|
+
: parseSync(file.path, file.text);
|
|
75
|
+
const bindings = [];
|
|
76
|
+
const root = program;
|
|
77
|
+
collectBindings(root, {
|
|
78
|
+
scope: root,
|
|
79
|
+
callees,
|
|
80
|
+
imports: namespaceImports(root),
|
|
81
|
+
out: bindings,
|
|
82
|
+
});
|
|
83
|
+
const usages = [];
|
|
84
|
+
// A chain is read whole, from its outermost member.
|
|
85
|
+
const read = new Set();
|
|
86
|
+
const keyChain = (node) => keyChainOf(node, bindings);
|
|
87
|
+
const markRead = ({ root: name, members }) => {
|
|
88
|
+
read.add(name);
|
|
89
|
+
for (const member of members) {
|
|
90
|
+
read.add(member);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
new Visitor({
|
|
94
|
+
CallExpression(expression) {
|
|
95
|
+
const call = expression;
|
|
96
|
+
const { callee } = call;
|
|
97
|
+
const chain = isNode(callee) ? keyChain(callee) : undefined;
|
|
98
|
+
const [first] = chain?.key ?? [];
|
|
99
|
+
const last = chain?.key.at(-1);
|
|
100
|
+
if (!chain?.isStatic || !first || !last) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
markRead(chain);
|
|
104
|
+
const { namespace } = chain;
|
|
105
|
+
const [params] = call['arguments'];
|
|
106
|
+
const raw = keyOf(chain.key);
|
|
107
|
+
usages.push({
|
|
108
|
+
raw,
|
|
109
|
+
range: { start: first.start, end: last.end },
|
|
110
|
+
expression: { start: call.start, end: call.end },
|
|
111
|
+
// TypeScript checks such a select's params per key; x18n compares one flat list.
|
|
112
|
+
params: declared.branchy.has(`${namespace}:${raw}`) ? undefined : paramKeys(params),
|
|
113
|
+
kind: 'accessor',
|
|
114
|
+
dynamic: false,
|
|
115
|
+
namespace,
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
// Not called: a group, a message passed on, or a key chosen at run time.
|
|
119
|
+
MemberExpression(expression) {
|
|
120
|
+
const node = expression;
|
|
121
|
+
const chain = read.has(node) ? undefined : keyChain(node);
|
|
122
|
+
if (!chain) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
markRead(chain);
|
|
126
|
+
const { namespace, key, isStatic } = chain;
|
|
127
|
+
const raw = keyOf(key);
|
|
128
|
+
const usage = { kind: 'accessor', namespace };
|
|
129
|
+
const [first] = key;
|
|
130
|
+
const last = key.at(-1);
|
|
131
|
+
if (isStatic && first && last) {
|
|
132
|
+
usages.push({
|
|
133
|
+
...usage,
|
|
134
|
+
raw,
|
|
135
|
+
range: { start: first.start, end: last.end },
|
|
136
|
+
dynamic: false,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
usages.push({
|
|
140
|
+
...usage,
|
|
141
|
+
// A `keyPrefix`, as x18n would write `raw` over the range on a rename, dot included; the
|
|
142
|
+
// dot keeps `status` from reaching `statusText`.
|
|
143
|
+
raw: '',
|
|
144
|
+
keyPrefix: raw ? `${raw}.` : undefined,
|
|
145
|
+
// Not the key's range: x18n keeps one usage per range.
|
|
146
|
+
range: { start: node.start, end: node.end },
|
|
147
|
+
dynamic: 'prefix',
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
}).visit(program);
|
|
151
|
+
return { usages: [...usages, ...passedOn(root, { bindings, read })] };
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The parser's file name: Astro's markup is JSX, and a Vue or Svelte script is TypeScript. */
|
|
2
|
+
export declare const viewName: (path: string) => string;
|
|
3
|
+
/**
|
|
4
|
+
* A Vue, Svelte or Astro file as one script, every character at its own offset: the scripts and
|
|
5
|
+
* the template expressions that parse, joined by `;`, and blanks for the rest.
|
|
6
|
+
*/
|
|
7
|
+
export declare function scriptView(path: string, text: string): string;
|
|
8
|
+
//# sourceMappingURL=templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AA2EA,+FAA+F;AAC/F,eAAO,MAAM,QAAQ,SAAU,MAAM,KAAG,MACa,CAAC;AAEtD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA+B7D"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
/** Where a comment that starts at `j` ends, or `j` when none does. */
|
|
3
|
+
function pastComment(text, j) {
|
|
4
|
+
const close = { '//': '\n', '/*': '*/' }[text.slice(j, j + 2)];
|
|
5
|
+
if (close === undefined) {
|
|
6
|
+
return j;
|
|
7
|
+
}
|
|
8
|
+
const end = text.indexOf(close, j + 2);
|
|
9
|
+
return end === -1 ? text.length : end + close.length - 1;
|
|
10
|
+
}
|
|
11
|
+
/** `{expr}` in Svelte and Astro markup, past a block's keyword: `{#if expr}`, `{@html expr}`. */
|
|
12
|
+
function* braceExpressions(text, blocks) {
|
|
13
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
14
|
+
if (text[i] !== '{') {
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
// A script's `don't` would open a string that never closes.
|
|
18
|
+
const block = blocks.find(([from, to]) => from <= i && i < to);
|
|
19
|
+
if (block) {
|
|
20
|
+
i = block[1] - 1;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
let depth = 0;
|
|
24
|
+
let quote;
|
|
25
|
+
let j = i;
|
|
26
|
+
for (; j < text.length; j += 1) {
|
|
27
|
+
const c = text[j];
|
|
28
|
+
if (quote) {
|
|
29
|
+
if (c === '\\') {
|
|
30
|
+
j += 1;
|
|
31
|
+
}
|
|
32
|
+
else if (c === quote) {
|
|
33
|
+
quote = undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (c === '"' || c === "'" || c === '`') {
|
|
37
|
+
quote = c;
|
|
38
|
+
}
|
|
39
|
+
else if (c === '/') {
|
|
40
|
+
j = pastComment(text, j);
|
|
41
|
+
}
|
|
42
|
+
else if (c === '{') {
|
|
43
|
+
depth += 1;
|
|
44
|
+
}
|
|
45
|
+
else if (c === '}') {
|
|
46
|
+
depth -= 1;
|
|
47
|
+
if (depth === 0) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const keyword = /^\s*(?:[#:@/]\w+(?:\s+if)?)?/.exec(text.slice(i + 1, j))?.[0].length ?? 0;
|
|
53
|
+
yield [i + 1 + keyword, j];
|
|
54
|
+
i = j;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const MUSTACHE = /\{\{(?<expr>[\s\S]*?)\}\}/g;
|
|
58
|
+
// `v-for` holds `item in items`, which is not an expression.
|
|
59
|
+
const DIRECTIVE = /\s(?::|@|v-bind:|v-on:|v-(?:if|else-if|show|html|text|model)\b)[\w.:-]*="(?<expr>[^"]*)"/g;
|
|
60
|
+
/** `{{ expr }}` and directive values in a Vue template. */
|
|
61
|
+
function* vueExpressions(text) {
|
|
62
|
+
for (const pattern of [MUSTACHE, DIRECTIVE]) {
|
|
63
|
+
for (const match of text.matchAll(pattern)) {
|
|
64
|
+
const expr = match.groups?.['expr'] ?? '';
|
|
65
|
+
const start = match.index + match[0].lastIndexOf(expr);
|
|
66
|
+
yield [start, start + expr.length];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const BLOCK = /<(?<tag>script|style)\b[^>]*>(?<body>[\s\S]*?)<\/\k<tag>>|<!--[\s\S]*?-->/g;
|
|
71
|
+
const FRONTMATTER = /^---\r?\n(?<body>[\s\S]*?)\r?\n---/;
|
|
72
|
+
/** The parser's file name: Astro's markup is JSX, and a Vue or Svelte script is TypeScript. */
|
|
73
|
+
export const viewName = (path) => `${path}.${path.endsWith('.astro') ? 'tsx' : 'ts'}`;
|
|
74
|
+
/**
|
|
75
|
+
* A Vue, Svelte or Astro file as one script, every character at its own offset: the scripts and
|
|
76
|
+
* the template expressions that parse, joined by `;`, and blanks for the rest.
|
|
77
|
+
*/
|
|
78
|
+
export function scriptView(path, text) {
|
|
79
|
+
// By UTF-16 unit, as offsets count: a spread would split by code point.
|
|
80
|
+
const view = Array.from({ length: text.length }, (_, i) => text[i] === '\n' ? '\n' : ' ');
|
|
81
|
+
const copy = (start, end) => {
|
|
82
|
+
for (let i = start; i < end; i += 1) {
|
|
83
|
+
view[i] = text[i] ?? ' ';
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const blocks = [];
|
|
87
|
+
const frontmatter = path.endsWith('.astro') ? FRONTMATTER.exec(text) : null;
|
|
88
|
+
for (const match of [...(frontmatter ? [frontmatter] : []), ...text.matchAll(BLOCK)]) {
|
|
89
|
+
blocks.push([match.index, match.index + match[0].length]);
|
|
90
|
+
const body = match.groups?.['body'];
|
|
91
|
+
if (body !== undefined && match.groups?.['tag'] !== 'style') {
|
|
92
|
+
const start = match.index + match[0].indexOf(body);
|
|
93
|
+
copy(start, start + body.length);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const name = viewName(path);
|
|
97
|
+
const expressions = path.endsWith('.vue') ? vueExpressions(text) : braceExpressions(text, blocks);
|
|
98
|
+
for (const [start, end] of expressions) {
|
|
99
|
+
const inBlock = blocks.some(([from, to]) => from <= start && start < to);
|
|
100
|
+
if (!inBlock && parseSync(name, `(${text.slice(start, end)})`).errors.length === 0) {
|
|
101
|
+
copy(start, end);
|
|
102
|
+
view[start - 1] = ';';
|
|
103
|
+
view[end] = ';';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return view.join('');
|
|
107
|
+
}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CustomFunctions, FormatAdapter } from '@parlajs/compiler';
|
|
2
|
+
import type { Plugin, Scanner } from '@x18n/core';
|
|
3
|
+
export type Usage = Awaited<ReturnType<Scanner['scan']>>['usages'][number];
|
|
4
|
+
export type PlaceholderSyntax = NonNullable<Plugin['placeholders']>[number];
|
|
5
|
+
export type Placeholder = ReturnType<PlaceholderSyntax['find']>[number];
|
|
6
|
+
export type LocaleFinder = NonNullable<Plugin['localeFinders']>[number];
|
|
7
|
+
export type Host = Parameters<LocaleFinder['find']>[0]['host'];
|
|
8
|
+
export type ReadFile = (path: string) => Promise<string | undefined>;
|
|
9
|
+
/** What the configs declare, filled once `configFinder` has loaded them. */
|
|
10
|
+
export interface Declared {
|
|
11
|
+
readonly functions: Record<string, CustomFunctions[string]>;
|
|
12
|
+
/** The configs' `formats` by name, the first config's winning. */
|
|
13
|
+
readonly formats: Map<string, FormatAdapter>;
|
|
14
|
+
/** Each message of a namespace in a format other than `icu` and `mf2`, to its parser. */
|
|
15
|
+
readonly syntaxes: Map<string, FormatAdapter>;
|
|
16
|
+
/** The configs' `markup`, which a call may leave out. */
|
|
17
|
+
readonly markup: Array<string>;
|
|
18
|
+
/** `ns:key` of each `:string` select whose keys need different params. */
|
|
19
|
+
readonly branchy: Set<string>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,MAAM,KAAK,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAE3E,MAAM,MAAM,iBAAiB,GAAG,WAAW,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5E,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,MAAM,MAAM,IAAI,GAAG,UAAU,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/D,MAAM,MAAM,QAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AAErE,4EAA4E;AAC5E,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5D,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC7C,yFAAyF;IACzF,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC9C,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC/B,0EAA0E;IAC1E,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC/B"}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"values.d.ts","sourceRoot":"","sources":["../src/values.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAarC,qEAAqE;AACrE,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAiB3D"}
|
package/build/values.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isNode } from './ast.js';
|
|
2
|
+
/** Children that name something, rather than read a value. */
|
|
3
|
+
const NAMES = {
|
|
4
|
+
VariableDeclarator: 'id',
|
|
5
|
+
FunctionDeclaration: 'params',
|
|
6
|
+
FunctionExpression: 'params',
|
|
7
|
+
ArrowFunctionExpression: 'params',
|
|
8
|
+
CatchClause: 'param',
|
|
9
|
+
};
|
|
10
|
+
const KEYED = new Set(['MemberExpression', 'Property', 'PropertyDefinition', 'MethodDefinition']);
|
|
11
|
+
/** Every identifier read as a value: no declaration, key or type. */
|
|
12
|
+
export function valuesIn(node, out) {
|
|
13
|
+
if (node.type === 'Identifier') {
|
|
14
|
+
out.push(node);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const isType = node.type.startsWith('TS');
|
|
18
|
+
for (const [name, value] of Object.entries(node)) {
|
|
19
|
+
const isKey = KEYED.has(node.type) && !node['computed'] && /^(?:key|property)$/.test(name);
|
|
20
|
+
// Of TypeScript's nodes, only `x as T` and its like hold a value.
|
|
21
|
+
if (NAMES[node.type] !== name && !isKey && (!isType || name === 'expression')) {
|
|
22
|
+
for (const child of Array.isArray(value) ? value : [value]) {
|
|
23
|
+
if (isNode(child)) {
|
|
24
|
+
valuesIn(child, out);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parlajs/x18n-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "x18n plugin for Parla: catalogs from parla.config.ts, t.ns.key usages, ICU and MF2 params.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"i18n",
|
|
7
|
+
"icu",
|
|
8
|
+
"internationalization",
|
|
9
|
+
"l10n",
|
|
10
|
+
"localization",
|
|
11
|
+
"messageformat",
|
|
12
|
+
"messageformat2",
|
|
13
|
+
"mf2",
|
|
14
|
+
"parla",
|
|
15
|
+
"x18n"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/parlajs/parla",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/parlajs/parla/issues"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Marco Pasqualetti",
|
|
24
|
+
"url": "https://github.com/marcalexiei"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/parlajs/parla.git",
|
|
29
|
+
"directory": "packages/x18n-plugin"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"build",
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"type": "module",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"@parlajs/source": "./src/index.ts",
|
|
40
|
+
"default": "./build/index.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@parlajs/compiler": "^0.1.0",
|
|
48
|
+
"oxc-parser": "^0.160.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "26.6.6",
|
|
52
|
+
"@x18n/core": "0.1.14",
|
|
53
|
+
"vitest": "5.1.1"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@x18n/core": "^0.1.14"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": "^22.18 || ^24 || >=26"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsc -p tsconfig.build.json",
|
|
63
|
+
"lint:publish": "attw --pack . --profile esm-only && publint --strict",
|
|
64
|
+
"test": "vitest",
|
|
65
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
66
|
+
}
|
|
6
67
|
}
|
package/src/ast.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export type Node = { type: string; start: number; end: number } & Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
export const isNode = (value: unknown): value is Node =>
|
|
4
|
+
typeof value === 'object' && value !== null && typeof (value as Node).type === 'string';
|
|
5
|
+
|
|
6
|
+
export const stringValue = (node: unknown): string | undefined =>
|
|
7
|
+
isNode(node) && node.type === 'Literal' && typeof node['value'] === 'string'
|
|
8
|
+
? node['value']
|
|
9
|
+
: undefined;
|
|
10
|
+
|
|
11
|
+
export interface Segment {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly start: number;
|
|
14
|
+
readonly end: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Chain {
|
|
18
|
+
readonly root: Node;
|
|
19
|
+
/** Up to the first key chosen at run time. */
|
|
20
|
+
readonly segments: ReadonlyArray<Segment>;
|
|
21
|
+
/** Whether no key is chosen at run time: `t.cart[key]` is not static. */
|
|
22
|
+
readonly isStatic: boolean;
|
|
23
|
+
/** Every member expression of the chain. */
|
|
24
|
+
readonly members: ReadonlyArray<Node>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A member's name, as `.items` or `['items']`; undefined for `[key]`. */
|
|
28
|
+
export function segmentOf({ computed, property }: Node): Segment | undefined {
|
|
29
|
+
if (!isNode(property)) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const { start, end } = property;
|
|
33
|
+
if (!computed) {
|
|
34
|
+
return { name: property['name'] as string, start, end };
|
|
35
|
+
}
|
|
36
|
+
const name = stringValue(property);
|
|
37
|
+
return name === undefined ? undefined : { name, start: start + 1, end: end - 1 };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** `t.cart.items` → root `t`, segments `cart` and `items`; undefined unless its root is a name or `name()`. */
|
|
41
|
+
export function memberChain(node: Node): Chain | undefined {
|
|
42
|
+
const members: Array<Node> = [];
|
|
43
|
+
let current: unknown = node;
|
|
44
|
+
while (isNode(current) && current.type === 'MemberExpression') {
|
|
45
|
+
members.unshift(current);
|
|
46
|
+
current = current['object'];
|
|
47
|
+
}
|
|
48
|
+
// Solid's accessor: `t().checkout.title()`.
|
|
49
|
+
if (
|
|
50
|
+
isNode(current) &&
|
|
51
|
+
current.type === 'CallExpression' &&
|
|
52
|
+
(current['arguments'] as Array<unknown>).length === 0
|
|
53
|
+
) {
|
|
54
|
+
current = current['callee'];
|
|
55
|
+
}
|
|
56
|
+
if (!isNode(current) || current.type !== 'Identifier' || members.length === 0) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const segments: Array<Segment> = [];
|
|
60
|
+
for (const member of members) {
|
|
61
|
+
const segment = segmentOf(member);
|
|
62
|
+
if (!segment) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
segments.push(segment);
|
|
66
|
+
}
|
|
67
|
+
return { root: current, segments, isStatic: segments.length === members.length, members };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const keyOf = (key: ReadonlyArray<Segment>): string => key.map(({ name }) => name).join('.');
|
|
71
|
+
|
|
72
|
+
/** Names of an object literal's keys, or undefined when a spread or computed key hides some. */
|
|
73
|
+
export function staticKeys(node: unknown): Array<string> | undefined {
|
|
74
|
+
if (!isNode(node) || node.type !== 'ObjectExpression') {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const keys: Array<string> = [];
|
|
78
|
+
for (const property of node['properties'] as Array<Node>) {
|
|
79
|
+
const { key } = property;
|
|
80
|
+
if (property.type !== 'Property' || property['computed'] || !isNode(key)) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
if (key.type === 'Identifier') {
|
|
84
|
+
keys.push(key['name'] as string);
|
|
85
|
+
} else if (key.type === 'Literal') {
|
|
86
|
+
keys.push(String(key['value']));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return keys;
|
|
90
|
+
}
|