langsys-js-react 0.6.7 → 0.7.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/CHANGELOG.md +35 -0
- package/README-SSR.md +144 -170
- package/README.md +339 -34
- package/dist/app-zQKL7-vo.d.mts +61 -0
- package/dist/app-zQKL7-vo.d.ts +61 -0
- package/dist/babel.d.mts +122 -0
- package/dist/babel.d.ts +122 -0
- package/dist/babel.js +547 -0
- package/dist/babel.js.map +1 -0
- package/dist/babel.mjs +545 -0
- package/dist/babel.mjs.map +1 -0
- package/dist/index.d.mts +140 -76
- package/dist/index.d.ts +140 -76
- package/dist/index.js +502 -97
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +423 -89
- package/dist/index.mjs.map +1 -1
- package/dist/next-loader.d.mts +8 -0
- package/dist/next-loader.d.ts +8 -0
- package/dist/next-loader.js +623 -0
- package/dist/next-loader.js.map +1 -0
- package/dist/next-loader.mjs +621 -0
- package/dist/next-loader.mjs.map +1 -0
- package/dist/next.d.mts +15 -0
- package/dist/next.d.ts +15 -0
- package/dist/next.js +41 -0
- package/dist/next.js.map +1 -0
- package/dist/next.mjs +37 -0
- package/dist/next.mjs.map +1 -0
- package/dist/server.d.mts +36 -0
- package/dist/server.d.ts +36 -0
- package/dist/server.js +57 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +25 -0
- package/dist/server.mjs.map +1 -0
- package/dist/vite.d.mts +14 -0
- package/dist/vite.d.ts +14 -0
- package/dist/vite.js +623 -0
- package/dist/vite.js.map +1 -0
- package/dist/vite.mjs +621 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +38 -3
package/dist/babel.mjs
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { derivePlaceholderNames, stripC0Controls, NON_TRANSLATABLE_ELEMENTS, TRANSLATABLE_ATTRIBUTES } from 'langsys-js-typescript/pure';
|
|
2
|
+
|
|
3
|
+
// src/transform/babel.ts
|
|
4
|
+
function unwrap(e) {
|
|
5
|
+
let n = e;
|
|
6
|
+
while (n.type === "TSAsExpression" || n.type === "TSNonNullExpression" || n.type === "TSSatisfiesExpression" || n.type === "TSTypeAssertion" || n.type === "ParenthesizedExpression") {
|
|
7
|
+
n = n.expression;
|
|
8
|
+
}
|
|
9
|
+
return n;
|
|
10
|
+
}
|
|
11
|
+
function segments(e) {
|
|
12
|
+
const n = unwrap(e);
|
|
13
|
+
if (n.type === "Identifier") return [n.name];
|
|
14
|
+
if (n.type === "ThisExpression") return [];
|
|
15
|
+
if (n.type === "MemberExpression" || n.type === "OptionalMemberExpression") {
|
|
16
|
+
if (n.computed || n.property.type !== "Identifier") return null;
|
|
17
|
+
const head = segments(n.object);
|
|
18
|
+
return head ? [...head, n.property.name] : null;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
function shapeOf(e) {
|
|
23
|
+
const n = unwrap(e);
|
|
24
|
+
if (n.type === "Identifier") return { identifier: n.name };
|
|
25
|
+
if (n.type === "MemberExpression" || n.type === "OptionalMemberExpression") {
|
|
26
|
+
const segs = segments(n);
|
|
27
|
+
return segs && segs.length ? { member: segs } : { other: "computed" };
|
|
28
|
+
}
|
|
29
|
+
if (n.type === "CallExpression" || n.type === "OptionalCallExpression") {
|
|
30
|
+
const args = n.arguments;
|
|
31
|
+
if (args.some((x) => x.type === "SpreadElement" || x.type === "ArgumentPlaceholder")) return { other: "call-multi" };
|
|
32
|
+
const callee = segments(n.callee);
|
|
33
|
+
return { call: { callee: callee ? callee.join(".") : "", args: args.map((x) => shapeOf(x)) } };
|
|
34
|
+
}
|
|
35
|
+
if (n.type === "BinaryExpression" || n.type === "LogicalExpression") return { other: "binary" };
|
|
36
|
+
if (n.type === "ConditionalExpression") return { other: "conditional" };
|
|
37
|
+
if (n.type === "TemplateLiteral") return { other: "template" };
|
|
38
|
+
return { other: n.type };
|
|
39
|
+
}
|
|
40
|
+
function derivable(shape) {
|
|
41
|
+
if ("identifier" in shape || "member" in shape) return true;
|
|
42
|
+
if ("call" in shape) return shape.call.args.length === 1 && derivable(shape.call.args[0]);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
function nameAll(expressions, taken = []) {
|
|
46
|
+
const explicit = [...taken];
|
|
47
|
+
const shapes = expressions.map((x) => shapeOf(x.expression));
|
|
48
|
+
const names = derivePlaceholderNames([
|
|
49
|
+
...explicit.map((name) => ({ shape: { other: "explicit" }, explicit: name })),
|
|
50
|
+
...shapes.map((shape) => ({ shape }))
|
|
51
|
+
]).slice(explicit.length);
|
|
52
|
+
return expressions.map((x, i) => ({ ...x, name: names[i], unnameable: !derivable(shapes[i]) }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/transform/babel.ts
|
|
56
|
+
var SOURCE = "langsys-js-react";
|
|
57
|
+
var TARGETS = /* @__PURE__ */ new Set(["Translate", "Phrase"]);
|
|
58
|
+
var VERBATIM = "DontTranslate";
|
|
59
|
+
var RAW_TEXT = /* @__PURE__ */ new Set(["script", "style", "plaintext"]);
|
|
60
|
+
var TEXT_ONLY = /* @__PURE__ */ new Set(["textarea", "title", "option"]);
|
|
61
|
+
var VALUE_ELEMENTS = /* @__PURE__ */ new Set(["button"]);
|
|
62
|
+
var VALUE_INPUT_TYPES = /* @__PURE__ */ new Set(["submit", "button"]);
|
|
63
|
+
var UNNAMED = "data-ls-unnamed";
|
|
64
|
+
var EXPLICIT = /%([A-Za-z_][A-Za-z0-9_]*)%/g;
|
|
65
|
+
var BRACED = /\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*[,}]/g;
|
|
66
|
+
function langsysPlaceholders({ types: T }) {
|
|
67
|
+
const sourceOf = (state, node) => state.file.code.slice(node.start ?? 0, node.end ?? 0);
|
|
68
|
+
const warn = (state, n) => {
|
|
69
|
+
if (!n.unnameable) return;
|
|
70
|
+
const loc = n.expression.loc?.start;
|
|
71
|
+
state.meta.warnings.push({ line: loc?.line ?? null, column: loc?.column ?? null, source: n.source, name: n.name });
|
|
72
|
+
};
|
|
73
|
+
const explicitNames = (texts, params, written = EXPLICIT) => {
|
|
74
|
+
const out = /* @__PURE__ */ new Set();
|
|
75
|
+
for (const s of texts) for (const m of s.matchAll(written)) out.add(m[1]);
|
|
76
|
+
if (params && params.type === "ObjectExpression") {
|
|
77
|
+
for (const p of params.properties) {
|
|
78
|
+
if (p.type === "ObjectProperty" && !p.computed) {
|
|
79
|
+
if (p.key.type === "Identifier") out.add(p.key.name);
|
|
80
|
+
else if (p.key.type === "StringLiteral") out.add(p.key.value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
};
|
|
86
|
+
const optOut = (tag, opening) => {
|
|
87
|
+
if (NON_TRANSLATABLE_ELEMENTS.includes(tag)) return "shown";
|
|
88
|
+
let unknown = false;
|
|
89
|
+
for (const a of opening.attributes) {
|
|
90
|
+
if (a.type !== "JSXAttribute" || a.name.type !== "JSXIdentifier") continue;
|
|
91
|
+
if (a.name.name !== "translate" && a.name.name !== "data-notrans") continue;
|
|
92
|
+
const text = attributeText(a);
|
|
93
|
+
if (text === void 0) unknown = true;
|
|
94
|
+
else if (a.name.name === "translate" ? text.toLowerCase() === "no" : !["false", "0"].includes(text.trim().toLowerCase())) {
|
|
95
|
+
return "shown";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return unknown ? "unknown" : null;
|
|
99
|
+
};
|
|
100
|
+
const readsAsText = (tag, opening, name) => {
|
|
101
|
+
if (TRANSLATABLE_ATTRIBUTES.includes(name)) return true;
|
|
102
|
+
if (name !== "value") return false;
|
|
103
|
+
if (VALUE_ELEMENTS.has(tag)) return true;
|
|
104
|
+
if (tag !== "input") return false;
|
|
105
|
+
let type = "";
|
|
106
|
+
for (const a of opening.attributes) {
|
|
107
|
+
if (a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "type") type = attributeText(a);
|
|
108
|
+
}
|
|
109
|
+
return type === void 0 ? void 0 : VALUE_INPUT_TYPES.has(type.toLowerCase());
|
|
110
|
+
};
|
|
111
|
+
const isChildren = (e) => {
|
|
112
|
+
while (e.type === "TSAsExpression" || e.type === "TSSatisfiesExpression" || e.type === "TSNonNullExpression" || e.type === "TSTypeAssertion" || e.type === "ParenthesizedExpression") {
|
|
113
|
+
e = e.expression;
|
|
114
|
+
}
|
|
115
|
+
return e.type === "Identifier" && e.name === "children" || (e.type === "MemberExpression" || e.type === "OptionalMemberExpression") && !e.computed && e.property.type === "Identifier" && e.property.name === "children";
|
|
116
|
+
};
|
|
117
|
+
const holdsJsx = (e) => {
|
|
118
|
+
let jsx = false;
|
|
119
|
+
T.traverseFast(e, (n) => {
|
|
120
|
+
if (n.type === "JSXElement" || n.type === "JSXFragment") jsx = true;
|
|
121
|
+
});
|
|
122
|
+
return jsx;
|
|
123
|
+
};
|
|
124
|
+
const literalOf = (e) => e.type === "StringLiteral" || e.type === "NumericLiteral" ? String(e.value) : e.type === "TemplateLiteral" && !e.expressions.length ? e.quasis[0].value.cooked ?? void 0 : void 0;
|
|
125
|
+
const isFragment = (state, name) => name.type === "JSXIdentifier" && state.fragments.has(name.name) || name.type === "JSXMemberExpression" && name.object.type === "JSXIdentifier" && state.reactModules.has(name.object.name) && name.property.name === "Fragment";
|
|
126
|
+
const staticTexts = (unit) => {
|
|
127
|
+
const out = [];
|
|
128
|
+
for (const child of unit.children) {
|
|
129
|
+
T.traverseFast(child, (n) => {
|
|
130
|
+
if (n.type === "JSXText" || n.type === "StringLiteral") out.push(n.value);
|
|
131
|
+
else if (n.type === "TemplateElement") out.push(n.value.cooked ?? n.value.raw);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
};
|
|
136
|
+
const holdsMarker = (node) => {
|
|
137
|
+
let found = false;
|
|
138
|
+
T.traverseFast(node, (n) => {
|
|
139
|
+
if (n.type === "JSXOpeningElement" && marks(n)) found = true;
|
|
140
|
+
});
|
|
141
|
+
return found;
|
|
142
|
+
};
|
|
143
|
+
const markedNames = (unit) => {
|
|
144
|
+
const out = /* @__PURE__ */ new Set();
|
|
145
|
+
for (const child of unit.children) {
|
|
146
|
+
T.traverseFast(child, (n) => {
|
|
147
|
+
if (n.type !== "JSXAttribute" || n.name.type !== "JSXIdentifier" || n.name.name !== "data-ls-param") return;
|
|
148
|
+
const name = n.value ? attributeText(n) : void 0;
|
|
149
|
+
if (name) out.add(name.trim());
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
};
|
|
154
|
+
const marks = (opening) => opening.attributes.some(
|
|
155
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "data-ls-param"
|
|
156
|
+
);
|
|
157
|
+
const attributeText = (a) => {
|
|
158
|
+
const v = a.value;
|
|
159
|
+
if (!v) return "";
|
|
160
|
+
if (v.type === "StringLiteral") return v.value;
|
|
161
|
+
if (v.type === "JSXExpressionContainer" && v.expression.type === "StringLiteral") return v.expression.value;
|
|
162
|
+
return void 0;
|
|
163
|
+
};
|
|
164
|
+
const marker = (name, expression) => T.jsxElement(
|
|
165
|
+
T.jsxOpeningElement(T.jsxIdentifier("span"), [
|
|
166
|
+
T.jsxAttribute(T.jsxIdentifier("data-ls-param"), T.stringLiteral(name))
|
|
167
|
+
]),
|
|
168
|
+
T.jsxClosingElement(T.jsxIdentifier("span")),
|
|
169
|
+
[T.jsxExpressionContainer(expression)]
|
|
170
|
+
);
|
|
171
|
+
const asRendered = (text) => {
|
|
172
|
+
const [s] = T.react.buildChildren(
|
|
173
|
+
T.jsxElement(T.jsxOpeningElement(T.jsxIdentifier("x"), []), T.jsxClosingElement(T.jsxIdentifier("x")), [text])
|
|
174
|
+
);
|
|
175
|
+
return s?.type === "StringLiteral" ? s.value : "";
|
|
176
|
+
};
|
|
177
|
+
const placeholderAfter = (before, name) => {
|
|
178
|
+
const open = (text) => /%[A-Za-z_][A-Za-z0-9_]*$/.test(text.replace(EXPLICIT, "\0"));
|
|
179
|
+
return open(before) || open(stripC0Controls(before)) ? `{${name}}` : `%${name}%`;
|
|
180
|
+
};
|
|
181
|
+
const textNode = (value) => {
|
|
182
|
+
if (/[\r\n\t\u2028\u2029]/.test(value)) return T.jsxExpressionContainer(T.stringLiteral(value));
|
|
183
|
+
const node = T.jsxText(value);
|
|
184
|
+
node.extra = { raw: value.replace(/[&<>{}]/g, (c) => `&#${c.charCodeAt(0)};`), rawValue: value };
|
|
185
|
+
return node;
|
|
186
|
+
};
|
|
187
|
+
const placeholders = (named) => {
|
|
188
|
+
const seen = /* @__PURE__ */ new Set();
|
|
189
|
+
return named.filter((n) => !seen.has(n.name) && seen.add(n.name));
|
|
190
|
+
};
|
|
191
|
+
const mergeParams = (existing, named) => {
|
|
192
|
+
const props = named.map((n) => T.objectProperty(T.identifier(n.name), n.expression));
|
|
193
|
+
if (!existing) return T.objectExpression(props);
|
|
194
|
+
if (existing.type === "ObjectExpression") return T.objectExpression([...existing.properties, ...props]);
|
|
195
|
+
return T.objectExpression([T.spreadElement(existing), ...props]);
|
|
196
|
+
};
|
|
197
|
+
function rewriteTarget(path, state) {
|
|
198
|
+
const found = [];
|
|
199
|
+
const tag = path.node.openingElement.name;
|
|
200
|
+
const isPhrase = tag.type === "JSXIdentifier" && state.phrases.has(tag.name);
|
|
201
|
+
let unnamed = false;
|
|
202
|
+
let markAll = false;
|
|
203
|
+
const asWritten = /* @__PURE__ */ new WeakSet();
|
|
204
|
+
const placeholderNames = /* @__PURE__ */ new WeakMap();
|
|
205
|
+
const written = (value) => {
|
|
206
|
+
const node = T.jsxText(value);
|
|
207
|
+
asWritten.add(node);
|
|
208
|
+
return node;
|
|
209
|
+
};
|
|
210
|
+
const touched = /* @__PURE__ */ new Set();
|
|
211
|
+
const settle = (children) => {
|
|
212
|
+
const out = [];
|
|
213
|
+
let run = "";
|
|
214
|
+
for (const c of children) {
|
|
215
|
+
if (c.type === "JSXText") {
|
|
216
|
+
const name = placeholderNames.get(c);
|
|
217
|
+
run += name !== void 0 ? placeholderAfter(run, name) : asWritten.has(c) ? c.value : asRendered(c);
|
|
218
|
+
} else if (c.type === "JSXExpressionContainer" && c.expression.type === "JSXEmptyExpression") continue;
|
|
219
|
+
else {
|
|
220
|
+
if (run) out.push(textNode(run));
|
|
221
|
+
run = "";
|
|
222
|
+
out.push(c);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (run) out.push(textNode(run));
|
|
226
|
+
children.splice(0, children.length, ...out);
|
|
227
|
+
};
|
|
228
|
+
const unwrap2 = (children) => {
|
|
229
|
+
for (let i = 0; i < children.length; i++) {
|
|
230
|
+
const c = children[i];
|
|
231
|
+
if (c.type === "JSXFragment" || c.type === "JSXElement" && isFragment(state, c.openingElement.name)) {
|
|
232
|
+
children.splice(i--, 1, ...c.children);
|
|
233
|
+
touched.add(children);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const aloneInRun = (children, i) => {
|
|
238
|
+
for (const step of [-1, 1]) {
|
|
239
|
+
for (let j = i + step; j >= 0 && j < children.length; j += step) {
|
|
240
|
+
const d = children[j];
|
|
241
|
+
if (d.type === "JSXElement" || d.type === "JSXFragment") break;
|
|
242
|
+
if (d.type === "JSXText") {
|
|
243
|
+
if ((asWritten.has(d) ? d.value : asRendered(d)).trim()) return false;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (d.type === "JSXSpreadChild") return false;
|
|
247
|
+
const x = d.expression;
|
|
248
|
+
if (x.type === "JSXEmptyExpression") continue;
|
|
249
|
+
const literal = literalOf(x);
|
|
250
|
+
if (literal !== void 0) {
|
|
251
|
+
if (literal.trim()) return false;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (holdsJsx(x)) break;
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
};
|
|
260
|
+
const attributes = (opening, tag2, mode) => {
|
|
261
|
+
if (mode === "shown") return;
|
|
262
|
+
for (const a of opening.attributes) {
|
|
263
|
+
if (a.type !== "JSXAttribute" || a.name.type !== "JSXIdentifier" || a.value?.type !== "JSXExpressionContainer") continue;
|
|
264
|
+
const e = a.value.expression;
|
|
265
|
+
if (e.type === "JSXEmptyExpression" || literalOf(e) !== void 0) continue;
|
|
266
|
+
if (e.type === "LogicalExpression" || e.type === "ConditionalExpression" || holdsJsx(e)) continue;
|
|
267
|
+
const reads = readsAsText(tag2, opening, a.name.name);
|
|
268
|
+
if (reads === false) continue;
|
|
269
|
+
if (reads === void 0 || mode === "unknown") {
|
|
270
|
+
unnamed = true;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const parts = pieces(e) ?? [e];
|
|
274
|
+
const inTemplate = /* @__PURE__ */ new WeakSet();
|
|
275
|
+
T.traverseFast(e, (n) => {
|
|
276
|
+
if (n.type === "TemplateLiteral") n.expressions.forEach((x) => inTemplate.add(x));
|
|
277
|
+
});
|
|
278
|
+
const names = [];
|
|
279
|
+
const attr = a;
|
|
280
|
+
parts.forEach((p, k) => {
|
|
281
|
+
if (typeof p === "string") return;
|
|
282
|
+
found.push({
|
|
283
|
+
source: sourceOf(state, p),
|
|
284
|
+
expression: p,
|
|
285
|
+
param: true,
|
|
286
|
+
bare: false,
|
|
287
|
+
text: p === e ? void 0 : inTemplate.has(p) ? T.templateLiteral([T.templateElement({ raw: "" }), T.templateElement({ raw: "" }, true)], [p]) : T.binaryExpression("+", T.stringLiteral(""), p),
|
|
288
|
+
replace: (name) => {
|
|
289
|
+
names[k] = name;
|
|
290
|
+
const value = parts.reduce((out, q, j) => out + (typeof q === "string" ? q : placeholderAfter(out, names[j] ?? "")), "");
|
|
291
|
+
attr.value = T.jsxExpressionContainer(T.stringLiteral(value));
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const walk = (children, where) => {
|
|
298
|
+
unwrap2(children);
|
|
299
|
+
children.forEach((c, i) => {
|
|
300
|
+
if (c.type === "JSXExpressionContainer") {
|
|
301
|
+
const e = c.expression;
|
|
302
|
+
if (e.type === "JSXEmptyExpression") {
|
|
303
|
+
touched.add(children);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const literal = literalOf(e);
|
|
307
|
+
if (literal !== void 0) {
|
|
308
|
+
children[i] = written(literal);
|
|
309
|
+
touched.add(children);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (e.type === "JSXElement" || e.type === "JSXFragment" || e.type === "LogicalExpression" || e.type === "ConditionalExpression" || holdsJsx(e) || where.mode === "text" && isChildren(e) && aloneInRun(children, i)) {
|
|
313
|
+
if (isPhrase && where.mode === "text" && holdsMarker(e)) markAll = true;
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (where.mode !== "text" && where.markerless) {
|
|
317
|
+
if (!isPhrase) unnamed = true;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
touched.add(children);
|
|
321
|
+
found.push({
|
|
322
|
+
source: sourceOf(state, e),
|
|
323
|
+
expression: e,
|
|
324
|
+
param: where.mode !== "shown",
|
|
325
|
+
bare: where.mode === "text" && where.markerless,
|
|
326
|
+
replace: (name) => {
|
|
327
|
+
if (where.mode !== "text" || markAll) {
|
|
328
|
+
children[i] = marker(name, e);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const text = written(`%${name}%`);
|
|
332
|
+
placeholderNames.set(text, name);
|
|
333
|
+
children[i] = text;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
} else if (c.type === "JSXElement") {
|
|
337
|
+
const n = c.openingElement.name;
|
|
338
|
+
if (n.type !== "JSXIdentifier" || /^[A-Z]/.test(n.name) && !state.verbatim.has(n.name)) {
|
|
339
|
+
if (isPhrase && where.mode === "text" && holdsMarker(c)) markAll = true;
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (/^[a-z]/.test(n.name)) {
|
|
343
|
+
if (RAW_TEXT.has(n.name)) {
|
|
344
|
+
const value = (d) => d.type === "JSXExpressionContainer" && d.expression.type !== "JSXEmptyExpression" && literalOf(d.expression) === void 0;
|
|
345
|
+
if (!isPhrase && c.children.some(value)) unnamed = true;
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (marks(c.openingElement)) {
|
|
349
|
+
if (isPhrase && where.mode === "text") markAll = true;
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const opt = optOut(n.name, c.openingElement);
|
|
353
|
+
const mode = where.mode === "shown" || opt === "shown" ? "shown" : !isPhrase && (where.mode === "unknown" || opt === "unknown") ? "unknown" : "text";
|
|
354
|
+
const foreign = n.name === "math" || n.name === "svg" ? n.name : where.foreign === "svg" && n.name === "foreignObject" ? null : where.foreign;
|
|
355
|
+
if (!isPhrase) attributes(c.openingElement, n.name, mode);
|
|
356
|
+
walk(c.children, { mode, foreign, markerless: foreign !== null || TEXT_ONLY.has(n.name) });
|
|
357
|
+
} else if (state.verbatim.has(n.name)) walk(c.children, { ...where, mode: "shown" });
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
walk(path.node.children, { mode: "text", foreign: null, markerless: false });
|
|
362
|
+
if (markAll && found.some((f) => f.bare)) {
|
|
363
|
+
unnamed = true;
|
|
364
|
+
for (let i = found.length - 1; i >= 0; i--) if (found[i].bare) found.splice(i, 1);
|
|
365
|
+
}
|
|
366
|
+
const attrs = path.node.openingElement.attributes;
|
|
367
|
+
if (unnamed && !attrs.some((a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === UNNAMED)) {
|
|
368
|
+
attrs.push(T.jsxAttribute(T.jsxIdentifier(UNNAMED), T.stringLiteral("")));
|
|
369
|
+
}
|
|
370
|
+
if (!found.length) {
|
|
371
|
+
touched.forEach(settle);
|
|
372
|
+
if (unnamed || touched.size) state.meta.rewritten++;
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const existing = attrs.find(
|
|
376
|
+
(a) => a.type === "JSXAttribute" && a.name.type === "JSXIdentifier" && a.name.name === "params"
|
|
377
|
+
);
|
|
378
|
+
const existingExpr = existing?.value?.type === "JSXExpressionContainer" && existing.value.expression.type !== "JSXEmptyExpression" ? existing.value.expression : null;
|
|
379
|
+
const named = nameAll(found, /* @__PURE__ */ new Set([...explicitNames(staticTexts(path.node), existingExpr), ...markedNames(path.node)]));
|
|
380
|
+
found.forEach((f, i) => f.replace(named[i].name));
|
|
381
|
+
placeholders(named).forEach((n) => warn(state, n));
|
|
382
|
+
const params = placeholders(named.filter((_, i) => found[i].param)).map((n) => {
|
|
383
|
+
const uses = found.filter((f, i) => f.param && named[i].name === n.name);
|
|
384
|
+
const given = uses.find((f) => !f.text);
|
|
385
|
+
return { ...n, expression: given ? given.expression : uses[0].text };
|
|
386
|
+
});
|
|
387
|
+
touched.forEach(settle);
|
|
388
|
+
if (params.length) {
|
|
389
|
+
const value = T.jsxExpressionContainer(mergeParams(existingExpr, params));
|
|
390
|
+
if (existing) existing.value = value;
|
|
391
|
+
else attrs.push(T.jsxAttribute(T.jsxIdentifier("params"), value));
|
|
392
|
+
}
|
|
393
|
+
state.meta.rewritten++;
|
|
394
|
+
}
|
|
395
|
+
function pieces(node) {
|
|
396
|
+
if (node.type === "TemplateLiteral") {
|
|
397
|
+
const out = [];
|
|
398
|
+
node.quasis.forEach((q, i) => {
|
|
399
|
+
if (q.value.cooked) out.push(q.value.cooked);
|
|
400
|
+
if (i < node.expressions.length) out.push(node.expressions[i]);
|
|
401
|
+
});
|
|
402
|
+
return node.expressions.length ? out : null;
|
|
403
|
+
}
|
|
404
|
+
if (node.type !== "BinaryExpression" || node.operator !== "+") return null;
|
|
405
|
+
const spine = [];
|
|
406
|
+
for (let n = node; n.type === "BinaryExpression" && n.operator === "+"; n = n.left) spine.push(n);
|
|
407
|
+
const operands = [spine[spine.length - 1].left, ...spine.map((s) => s.right).reverse()];
|
|
408
|
+
const first = operands.findIndex((o) => o.type === "StringLiteral" || o.type === "TemplateLiteral");
|
|
409
|
+
if (first < 0) return null;
|
|
410
|
+
const flat = [];
|
|
411
|
+
if (first === 1) flat.push(operands[0]);
|
|
412
|
+
else if (first > 1) flat.push(spine[spine.length - (first - 1)]);
|
|
413
|
+
for (const o of operands.slice(first)) {
|
|
414
|
+
if (o.type === "StringLiteral") flat.push(o.value);
|
|
415
|
+
else if (o.type === "TemplateLiteral") flat.push(...pieces(o) ?? [o.quasis[0].value.cooked ?? ""]);
|
|
416
|
+
else flat.push(o);
|
|
417
|
+
}
|
|
418
|
+
return flat.some((p) => typeof p !== "string") ? flat : null;
|
|
419
|
+
}
|
|
420
|
+
const isKeyShaped = (parts) => parts.every((p) => typeof p !== "string" || /^[A-Za-z0-9_.:-]*$/.test(p));
|
|
421
|
+
const secondArgument = (second, third) => {
|
|
422
|
+
if (!second) return "category";
|
|
423
|
+
if (second.type === "ObjectExpression" || second.type === "NullLiteral") return "params";
|
|
424
|
+
if (second.type === "StringLiteral" || second.type === "TemplateLiteral") return "category";
|
|
425
|
+
if (second.type === "Identifier" && second.name === "undefined") return "category";
|
|
426
|
+
if (second.type === "CallExpression" && second.callee.type === "Identifier" && second.callee.name === "inCategory") return "category";
|
|
427
|
+
return third ? "category" : "unknown";
|
|
428
|
+
};
|
|
429
|
+
const decidedAtRuntime = (params, second) => {
|
|
430
|
+
const a = T.identifier("a");
|
|
431
|
+
const p = T.identifier("p");
|
|
432
|
+
const isRef = T.logicalExpression(
|
|
433
|
+
"&&",
|
|
434
|
+
T.binaryExpression("!==", a, T.nullLiteral()),
|
|
435
|
+
T.binaryExpression(
|
|
436
|
+
"===",
|
|
437
|
+
T.memberExpression(a, T.callExpression(T.memberExpression(T.identifier("Symbol"), T.identifier("for")), [T.stringLiteral("langsys.category")]), true),
|
|
438
|
+
T.booleanLiteral(true)
|
|
439
|
+
)
|
|
440
|
+
);
|
|
441
|
+
const isParams = T.logicalExpression("&&", T.binaryExpression("===", T.unaryExpression("typeof", a), T.stringLiteral("object")), T.unaryExpression("!", isRef));
|
|
442
|
+
const pick = T.arrowFunctionExpression(
|
|
443
|
+
[p, a],
|
|
444
|
+
T.conditionalExpression(isParams, T.arrayExpression([T.objectExpression([T.spreadElement(a), T.spreadElement(p)])]), T.arrayExpression([a, p]))
|
|
445
|
+
);
|
|
446
|
+
return T.spreadElement(T.callExpression(pick, [params, second]));
|
|
447
|
+
};
|
|
448
|
+
function rewriteCall(path, state) {
|
|
449
|
+
const [phrase, second, third] = path.node.arguments;
|
|
450
|
+
if (!phrase || phrase.type === "SpreadElement" || phrase.type === "ArgumentPlaceholder") return;
|
|
451
|
+
const parts = pieces(phrase);
|
|
452
|
+
if (!parts || isKeyShaped(parts)) return;
|
|
453
|
+
if (second?.type === "SpreadElement" || second?.type === "ArgumentPlaceholder") return;
|
|
454
|
+
if (third?.type === "SpreadElement" || third?.type === "ArgumentPlaceholder") return;
|
|
455
|
+
const values = parts.filter((p) => typeof p !== "string").map((e) => ({ source: sourceOf(state, e), expression: e }));
|
|
456
|
+
const kind = secondArgument(second, third);
|
|
457
|
+
const paramsExpr = kind === "category" ? third ?? null : second.type === "NullLiteral" ? null : second;
|
|
458
|
+
const unread = !!paramsExpr && (paramsExpr.type !== "ObjectExpression" || paramsExpr.properties.some((p) => p.type !== "ObjectProperty" || p.computed));
|
|
459
|
+
const written = unread ? parts.filter((p) => typeof p === "string") : [];
|
|
460
|
+
const named = nameAll(values, explicitNames(written, paramsExpr, BRACED));
|
|
461
|
+
let next = 0;
|
|
462
|
+
const text = parts.map((p) => typeof p === "string" ? p : `{${named[next++].name}}`).join("");
|
|
463
|
+
const unique = placeholders(named);
|
|
464
|
+
unique.forEach((n) => warn(state, n));
|
|
465
|
+
const rest = path.node.arguments.slice(kind === "category" ? 3 : 2);
|
|
466
|
+
path.node.arguments = [
|
|
467
|
+
T.stringLiteral(text),
|
|
468
|
+
...kind === "category" ? [second ?? T.identifier("undefined"), mergeParams(paramsExpr, unique)] : kind === "params" ? [mergeParams(paramsExpr, unique)] : [decidedAtRuntime(mergeParams(null, unique), second)],
|
|
469
|
+
...rest
|
|
470
|
+
];
|
|
471
|
+
state.meta.rewritten++;
|
|
472
|
+
}
|
|
473
|
+
const isBound = (path, name, bindings) => {
|
|
474
|
+
const b = path.scope.getBinding(name);
|
|
475
|
+
return !!b && bindings.has(b.identifier);
|
|
476
|
+
};
|
|
477
|
+
return {
|
|
478
|
+
name: "langsys-placeholders",
|
|
479
|
+
pre(file) {
|
|
480
|
+
this.meta = { warnings: [], rewritten: 0 };
|
|
481
|
+
file.metadata.langsys = this.meta;
|
|
482
|
+
this.targets = /* @__PURE__ */ new Set();
|
|
483
|
+
this.phrases = /* @__PURE__ */ new Set();
|
|
484
|
+
this.hooks = /* @__PURE__ */ new Set();
|
|
485
|
+
this.targetBindings = /* @__PURE__ */ new Set();
|
|
486
|
+
this.hookBindings = /* @__PURE__ */ new Set();
|
|
487
|
+
this.tBindings = /* @__PURE__ */ new Set();
|
|
488
|
+
this.verbatim = /* @__PURE__ */ new Set();
|
|
489
|
+
this.fragments = /* @__PURE__ */ new Set();
|
|
490
|
+
this.reactModules = /* @__PURE__ */ new Set();
|
|
491
|
+
},
|
|
492
|
+
visitor: {
|
|
493
|
+
Program(program, state) {
|
|
494
|
+
for (const stmt of program.node.body) {
|
|
495
|
+
if (stmt.type === "ImportDeclaration" && stmt.source.value === "react") {
|
|
496
|
+
for (const s of stmt.specifiers) {
|
|
497
|
+
if (s.type !== "ImportSpecifier") state.reactModules.add(s.local.name);
|
|
498
|
+
else if ((s.imported.type === "Identifier" ? s.imported.name : s.imported.value) === "Fragment") {
|
|
499
|
+
state.fragments.add(s.local.name);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (stmt.type !== "ImportDeclaration" || stmt.source.value !== SOURCE) continue;
|
|
504
|
+
for (const s of stmt.specifiers) {
|
|
505
|
+
if (s.type !== "ImportSpecifier") continue;
|
|
506
|
+
const imported = s.imported.type === "Identifier" ? s.imported.name : s.imported.value;
|
|
507
|
+
if (TARGETS.has(imported)) state.targets.add(s.local.name);
|
|
508
|
+
if (imported === "Phrase") state.phrases.add(s.local.name);
|
|
509
|
+
if (imported === "useT") state.hooks.add(s.local.name);
|
|
510
|
+
if (imported === VERBATIM) state.verbatim.add(s.local.name);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (!state.targets.size && !state.hooks.size) return;
|
|
514
|
+
const bind = (names, into) => names.forEach((name) => {
|
|
515
|
+
const b = program.scope.getBinding(name);
|
|
516
|
+
if (b) into.add(b.identifier);
|
|
517
|
+
});
|
|
518
|
+
bind(state.targets, state.targetBindings);
|
|
519
|
+
bind(state.hooks, state.hookBindings);
|
|
520
|
+
program.traverse({
|
|
521
|
+
VariableDeclarator(d) {
|
|
522
|
+
const init = d.node.init;
|
|
523
|
+
if (d.node.id.type === "Identifier" && init?.type === "CallExpression" && init.callee.type === "Identifier" && isBound(d, init.callee.name, state.hookBindings)) {
|
|
524
|
+
const b = d.scope.getBinding(d.node.id.name);
|
|
525
|
+
if (b) state.tBindings.add(b.identifier);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
},
|
|
530
|
+
JSXElement(path, state) {
|
|
531
|
+
const n = path.node.openingElement.name;
|
|
532
|
+
if (n.type === "JSXIdentifier" && isBound(path, n.name, state.targetBindings)) rewriteTarget(path, state);
|
|
533
|
+
},
|
|
534
|
+
CallExpression(path, state) {
|
|
535
|
+
const callee = path.node.callee;
|
|
536
|
+
const isT = callee.type === "Identifier" && isBound(path, callee.name, state.tBindings) || callee.type === "CallExpression" && callee.callee.type === "Identifier" && isBound(path, callee.callee.name, state.hookBindings);
|
|
537
|
+
if (isT) rewriteCall(path, state);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export { langsysPlaceholders as default };
|
|
544
|
+
//# sourceMappingURL=babel.mjs.map
|
|
545
|
+
//# sourceMappingURL=babel.mjs.map
|