orvyn-lang 0.2.0 → 0.3.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/GUIDE.md +149 -0
- package/LANGUAGE.md +744 -27
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +186 -10
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +102 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +428 -57
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +43 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +154 -7
- package/src/lang/native/android/OrvynRenderer.kt +518 -44
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/build.js +130 -60
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +266 -4
- package/src/lang/native/ios/OrvynViews.swift +429 -34
- package/src/lang/native/ios.js +75 -2
- package/src/lang/native-runtime.js +406 -56
- package/src/lang/page.js +52 -0
- package/src/lang/parser.js +243 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +11 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +792 -10
- package/src/lang/runtime.js +362 -52
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +312 -0
- package/src/lang/vite-plugin.js +82 -3
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/mount.js +116 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +11 -1
- package/src/styling/fluid.js +91 -4
- package/src/styling/spec.js +306 -0
package/src/lang/codegen.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OrvynSyntaxError } from "./lexer.js";
|
|
2
|
+
import { LIST_WORDS } from "./words.js";
|
|
2
3
|
const LIST_METHODS = /* @__PURE__ */ new Set(["add", "remove", "removeAt", "clear", "update"]);
|
|
3
4
|
const BUILTINS = {
|
|
4
5
|
Column: { tag: "div", props: { flex: true, direction: "column" } },
|
|
@@ -10,26 +11,52 @@ const BUILTINS = {
|
|
|
10
11
|
Form: { tag: "form", props: { flex: true, direction: "column", noValidate: true } },
|
|
11
12
|
SubmitButton: { tag: "button", props: { type: "submit" }, arg: "child" },
|
|
12
13
|
Image: { tag: "img", arg: "src" },
|
|
14
|
+
DatePicker: { tag: "input", props: { type: "date" }, arg: "placeholder" },
|
|
15
|
+
TextArea: { tag: "textarea", props: { rows: 4 }, arg: "placeholder" },
|
|
13
16
|
Input: { tag: "input", arg: "placeholder" },
|
|
14
17
|
Link: { tag: "a", arg: "child" }
|
|
15
18
|
};
|
|
19
|
+
const TEST_GLOBALS = /* @__PURE__ */ new Set(["tap", "type", "expect", "text", "mock", "settle"]);
|
|
20
|
+
const TEST_WAITS = /* @__PURE__ */ new Set(["tap", "type", "settle"]);
|
|
16
21
|
const EVENTS = {
|
|
17
22
|
onTap: "onClick",
|
|
18
23
|
onClick: "onClick",
|
|
19
24
|
onChange: "onInput",
|
|
20
25
|
onSubmit: "onSubmit",
|
|
26
|
+
onError: "onError",
|
|
21
27
|
onHover: "onMouseenter",
|
|
22
28
|
onLeave: "onMouseleave"
|
|
23
29
|
};
|
|
24
30
|
const GLOBALS = {
|
|
25
31
|
print: "console.log",
|
|
26
32
|
fetch: "__o.fetchData",
|
|
33
|
+
get: "__o.get",
|
|
34
|
+
post: "__o.post",
|
|
35
|
+
put: "__o.put",
|
|
36
|
+
patch: "__o.patch",
|
|
37
|
+
load: "__o.load",
|
|
38
|
+
task: "__o.task",
|
|
27
39
|
go: "__o.go",
|
|
40
|
+
back: "__o.back",
|
|
41
|
+
canGoBack: "__o.canGoBack",
|
|
42
|
+
openUrl: "__o.openUrl",
|
|
28
43
|
sleep: "__o.sleep",
|
|
29
44
|
random: "__o.random",
|
|
30
45
|
len: "__o.len",
|
|
31
46
|
validate: "__o.validate",
|
|
47
|
+
theme: "__o.theme",
|
|
48
|
+
storage: "__o.storage",
|
|
49
|
+
secure: "__o.secure",
|
|
50
|
+
auth: "__o.auth",
|
|
51
|
+
log: "__o.log",
|
|
52
|
+
pickPhoto: "__o.pickPhoto",
|
|
53
|
+
takePhoto: "__o.takePhoto",
|
|
54
|
+
pickFile: "__o.pickFile",
|
|
55
|
+
getLocation: "__o.getLocation",
|
|
56
|
+
env: "__o.env",
|
|
32
57
|
Math: "Math",
|
|
58
|
+
Array: "Array",
|
|
59
|
+
Object: "Object",
|
|
33
60
|
JSON: "JSON",
|
|
34
61
|
Date: "Date",
|
|
35
62
|
Number: "Number",
|
|
@@ -81,9 +108,12 @@ class Scope {
|
|
|
81
108
|
vars = /* @__PURE__ */ new Map();
|
|
82
109
|
parent;
|
|
83
110
|
awaitAllowed;
|
|
84
|
-
|
|
111
|
+
fnBoundary;
|
|
112
|
+
hasAwait = false;
|
|
113
|
+
constructor(parent = null, awaitAllowed = parent?.awaitAllowed ?? false, fnBoundary = false) {
|
|
85
114
|
this.parent = parent;
|
|
86
115
|
this.awaitAllowed = awaitAllowed;
|
|
116
|
+
this.fnBoundary = fnBoundary;
|
|
87
117
|
}
|
|
88
118
|
declare(name, binding, pos) {
|
|
89
119
|
if (this.vars.has(name)) {
|
|
@@ -94,24 +124,64 @@ class Scope {
|
|
|
94
124
|
lookup(name) {
|
|
95
125
|
return this.vars.get(name) ?? this.parent?.lookup(name);
|
|
96
126
|
}
|
|
127
|
+
names() {
|
|
128
|
+
return [...this.vars.keys(), ...this.parent?.names() ?? []];
|
|
129
|
+
}
|
|
130
|
+
markAwait() {
|
|
131
|
+
let current = this;
|
|
132
|
+
while (current) {
|
|
133
|
+
if (current.fnBoundary) {
|
|
134
|
+
current.hasAwait = true;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
current = current.parent;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
97
140
|
}
|
|
98
|
-
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__forms", "screens", "mountApp"]);
|
|
141
|
+
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__F", "__e", "__forms", "__local", "screens", "mountApp"]);
|
|
99
142
|
function jsName(name) {
|
|
100
143
|
return JS_RESERVED.has(name) || INTERNAL_NAMES.has(name) || name.startsWith("__orvyn_") ? "__orvyn_" + Array.from(name, (c) => c.codePointAt(0).toString(16)).join("_") : name;
|
|
101
144
|
}
|
|
145
|
+
function suggest(name, candidates) {
|
|
146
|
+
let best = "";
|
|
147
|
+
let bestScore = Infinity;
|
|
148
|
+
const lower = name.toLowerCase();
|
|
149
|
+
for (const c of candidates) {
|
|
150
|
+
if (c.startsWith("__")) continue;
|
|
151
|
+
const d = c.toLowerCase() === lower ? 0 : editDistance(lower, c.toLowerCase());
|
|
152
|
+
if (d < bestScore) {
|
|
153
|
+
bestScore = d;
|
|
154
|
+
best = c;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const limit = name.length <= 4 ? 1 : 2;
|
|
158
|
+
return best && bestScore <= limit ? ` Did you mean "${best}"?` : "";
|
|
159
|
+
}
|
|
160
|
+
function editDistance(a, b) {
|
|
161
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
162
|
+
for (let i = 1; i <= a.length; i++) {
|
|
163
|
+
let prev = row[0];
|
|
164
|
+
row[0] = i;
|
|
165
|
+
for (let j = 1; j <= b.length; j++) {
|
|
166
|
+
const temp = row[j];
|
|
167
|
+
row[j] = a[i - 1] === b[j - 1] ? prev : 1 + Math.min(prev, row[j], row[j - 1]);
|
|
168
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) row[j] = Math.min(row[j], temp);
|
|
169
|
+
prev = temp;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return row[b.length];
|
|
173
|
+
}
|
|
102
174
|
function fail(message, pos) {
|
|
103
175
|
throw new OrvynSyntaxError(message, pos.line, pos.col);
|
|
104
176
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (Array.isArray(node)) return node.some(containsWait);
|
|
108
|
-
const value = node;
|
|
109
|
-
if (value.kind === "Lambda") return false;
|
|
110
|
-
if (value.kind === "Wait") return true;
|
|
111
|
-
return Object.values(value).some(containsWait);
|
|
112
|
-
}
|
|
177
|
+
const ASYNC_GLOBALS = /* @__PURE__ */ new Set(["fetch", "get", "post", "put", "patch", "sleep", "pickPhoto", "takePhoto", "pickFile", "getLocation"]);
|
|
178
|
+
const NEEDS = { takePhoto: "camera", getLocation: "location" };
|
|
113
179
|
class Generator {
|
|
180
|
+
currentView = "";
|
|
181
|
+
fnParams = /* @__PURE__ */ new Map();
|
|
114
182
|
formId = 0;
|
|
183
|
+
needs = /* @__PURE__ */ new Set();
|
|
184
|
+
inTest = false;
|
|
115
185
|
out = [];
|
|
116
186
|
indent = 0;
|
|
117
187
|
global = new Scope();
|
|
@@ -128,27 +198,44 @@ class Generator {
|
|
|
128
198
|
const starts = [];
|
|
129
199
|
for (const decl of this.program.body) {
|
|
130
200
|
if (decl.kind === "Use") {
|
|
131
|
-
decl.
|
|
132
|
-
|
|
133
|
-
|
|
201
|
+
const library = /\.(m?js|ts)$/.test(decl.path) || !/^[./]/.test(decl.path);
|
|
202
|
+
decl.names.forEach((name) => this.global.declare(name, library ? "const" : "imported", decl));
|
|
203
|
+
const path = library || decl.path.endsWith(".orv") ? decl.path : `${decl.path}.orv`;
|
|
204
|
+
if (library) {
|
|
205
|
+
const all = `__orvyn_lib${imports.length}`;
|
|
206
|
+
const fallback = decl.names.length === 1 ? ` ?? Reflect.get(${all}, "default")` : "";
|
|
207
|
+
imports.push([
|
|
208
|
+
`import * as ${all} from ${JSON.stringify(path)}`,
|
|
209
|
+
...decl.names.map((n) => `const ${jsName(n)} = ${all}[${JSON.stringify(n)}]${fallback}`)
|
|
210
|
+
].join("\n"));
|
|
211
|
+
} else {
|
|
212
|
+
imports.push(`import { ${decl.names.map(jsName).join(", ")} } from ${JSON.stringify(path)}`);
|
|
213
|
+
}
|
|
134
214
|
continue;
|
|
135
215
|
}
|
|
136
216
|
if (decl.kind === "Start") {
|
|
137
217
|
starts.push(decl.body);
|
|
138
218
|
continue;
|
|
139
219
|
}
|
|
220
|
+
if (decl.kind === "Test" || decl.kind === "Type") continue;
|
|
140
221
|
const binding = decl.kind === "State" ? "state" : decl.kind === "Const" ? "const" : decl.kind === "Fn" ? "fn" : decl.type;
|
|
141
222
|
this.global.declare(decl.name, binding, decl);
|
|
142
223
|
if (decl.kind === "Screen") this.components.set(decl.name, decl.params);
|
|
224
|
+
if (decl.kind === "Fn") this.fnParams.set(decl.name, decl.params.length);
|
|
143
225
|
}
|
|
144
226
|
this.line(`import * as __o from "${runtime}/lang/runtime"`);
|
|
145
227
|
imports.forEach((line) => this.line(line));
|
|
146
228
|
this.line("const __signal = __o.signal");
|
|
147
229
|
this.line("const __h = __o.createVNode");
|
|
148
230
|
this.line("const __forms = __o.formCache()");
|
|
231
|
+
this.line(`const __F = ${JSON.stringify(this.options.moduleId || "app")}`);
|
|
232
|
+
if (this.options.sourceText !== void 0) {
|
|
233
|
+
this.line(`__o.registerSource?.(__F, ${JSON.stringify(this.options.sourceText)})`);
|
|
234
|
+
}
|
|
235
|
+
if (this.options.env) this.line(`__o.setEnv(${JSON.stringify(this.options.env)})`);
|
|
149
236
|
this.line("");
|
|
150
237
|
for (const decl of this.program.body) {
|
|
151
|
-
if (decl.kind === "Use" || decl.kind === "Start") continue;
|
|
238
|
+
if (decl.kind === "Use" || decl.kind === "Start" || decl.kind === "Test") continue;
|
|
152
239
|
this.decl(decl);
|
|
153
240
|
this.line("");
|
|
154
241
|
}
|
|
@@ -156,14 +243,40 @@ class Generator {
|
|
|
156
243
|
this.line(this.fnHeader("function __start", [], starts.flat(), this.global));
|
|
157
244
|
this.line("");
|
|
158
245
|
}
|
|
246
|
+
const onErrorFn = this.program.body.find((d) => d.kind === "Fn" && d.name === "onError");
|
|
247
|
+
if (onErrorFn) {
|
|
248
|
+
this.line("__o.onError(onError)");
|
|
249
|
+
this.line("");
|
|
250
|
+
}
|
|
251
|
+
for (const need of this.needs) this.line(`globalThis.__orvyn_needs_${need} = true`);
|
|
252
|
+
const tests = this.program.body.filter((d) => d.kind === "Test");
|
|
253
|
+
if (this.options.tests && tests.length) {
|
|
254
|
+
this.line("export const __tests = [");
|
|
255
|
+
for (const test of tests) {
|
|
256
|
+
this.inTest = true;
|
|
257
|
+
const inner = new Scope(this.global, true, true);
|
|
258
|
+
const body = this.capture(() => this.stmts(test.body, inner));
|
|
259
|
+
this.inTest = false;
|
|
260
|
+
this.line(`{ name: ${JSON.stringify(test.name)}, run: async (__t) => {
|
|
261
|
+
${body.join("\n")}
|
|
262
|
+
} },`);
|
|
263
|
+
}
|
|
264
|
+
this.line("]");
|
|
265
|
+
this.line("");
|
|
266
|
+
}
|
|
159
267
|
const screens = this.program.body.filter(
|
|
160
268
|
(d) => d.kind === "Screen" && d.type === "screen"
|
|
161
269
|
);
|
|
162
|
-
const entry = screens.find((s) => s.name === "App") ?? screens[0];
|
|
270
|
+
const entry = screens.find((s) => s.name === "App") ?? screens.find((s) => s.name !== "NotFound" && s.name !== "ErrorPage") ?? screens[0];
|
|
271
|
+
if (screens.some((s) => s.name === "ErrorPage")) {
|
|
272
|
+
this.line("__o.setErrorPage(ErrorPage)");
|
|
273
|
+
this.line("");
|
|
274
|
+
}
|
|
275
|
+
if (screens.length) this.line(`__o.registerScreens?.({ ${screens.map((s) => s.name).join(", ")} })`);
|
|
163
276
|
this.line(`export const screens = { ${screens.map((s) => s.name).join(", ")} }`);
|
|
164
277
|
this.line("");
|
|
165
|
-
this.line("export function mountApp(target, screen) {");
|
|
166
|
-
this.line(` __o.startApp(target, screen ? screens[screen] : ${entry ? entry.name : "null"}, ${starts.length > 0 ?
|
|
278
|
+
this.line("export function mountApp(target, screen, params) {");
|
|
279
|
+
this.line(` return __o.startApp(target, screen ? screens[screen] : ${entry ? entry.name : "null"}, ${starts.length > 0 ? `__o.guard(__start, "start", __F, ${starts[0][0]?.line ?? 0}, "start")` : "null"}, screens, params)`);
|
|
167
280
|
this.line("}");
|
|
168
281
|
this.line("");
|
|
169
282
|
this.line(`export default ${entry ? entry.name : "null"}`);
|
|
@@ -177,11 +290,21 @@ class Generator {
|
|
|
177
290
|
decl(decl) {
|
|
178
291
|
const scope = this.global;
|
|
179
292
|
switch (decl.kind) {
|
|
180
|
-
case "State":
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
293
|
+
case "State": {
|
|
294
|
+
if (!decl.saved) {
|
|
295
|
+
this.line(`export const ${jsName(decl.name)} = __signal(${this.expr(decl.init, scope)}, "${decl.name}")`);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const opts = [];
|
|
299
|
+
if (this.options.moduleId) opts.push(`module: ${JSON.stringify(this.options.moduleId)}`);
|
|
300
|
+
if (decl.savedVersion && !(decl.savedVersion.kind === "Number" && decl.savedVersion.value === 1)) {
|
|
301
|
+
opts.push(`version: ${this.expr(decl.savedVersion, scope)}`);
|
|
302
|
+
}
|
|
303
|
+
if (decl.savedMigrate) opts.push(`migrate: ${this.expr(decl.savedMigrate, scope)}`);
|
|
304
|
+
const suffix = opts.length > 0 ? `, { ${opts.join(", ")} }` : "";
|
|
305
|
+
this.line(`export const ${jsName(decl.name)} = __o.savedSignal("${decl.name}", ${this.expr(decl.init, scope)}${suffix})`);
|
|
184
306
|
return;
|
|
307
|
+
}
|
|
185
308
|
case "Const":
|
|
186
309
|
this.line(`export const ${jsName(decl.name)} = ${this.expr(decl.init, scope)}`);
|
|
187
310
|
return;
|
|
@@ -193,22 +316,61 @@ class Generator {
|
|
|
193
316
|
decl.params.forEach((p) => inner.declare(p, "local", decl));
|
|
194
317
|
inner.declare("children", "local", decl);
|
|
195
318
|
const params = [...decl.params.map((p) => p === jsName(p) ? p : `${p}: ${jsName(p)}`), "children"];
|
|
319
|
+
this.currentView = decl.name;
|
|
196
320
|
this.line(`export function ${decl.name}({ ${params.join(", ")} } = {}) {`);
|
|
197
321
|
this.indent++;
|
|
322
|
+
this.line(`try {`);
|
|
323
|
+
this.indent++;
|
|
324
|
+
this.line(`__o.at(__F, ${decl.line})`);
|
|
325
|
+
const locals = decl.locals ?? [];
|
|
326
|
+
if (locals.some((local) => local.kind === "State")) {
|
|
327
|
+
this.line("const __local = __o.componentState()");
|
|
328
|
+
}
|
|
329
|
+
let stateSlot = 0;
|
|
330
|
+
for (const local of locals) {
|
|
331
|
+
if (local.kind === "State") {
|
|
332
|
+
const init = this.expr(local.init, inner);
|
|
333
|
+
inner.declare(local.name, "state", local);
|
|
334
|
+
this.line(`const ${jsName(local.name)} = __local(${stateSlot++}, () => ${init}, ${JSON.stringify(local.name)})`);
|
|
335
|
+
} else {
|
|
336
|
+
const init = this.expr(local.init, inner);
|
|
337
|
+
inner.declare(local.name, "const", local);
|
|
338
|
+
this.line(`const ${jsName(local.name)} = ${init}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (decl.type === "screen" && (decl.page || decl.requiresAuth)) {
|
|
342
|
+
const fields = (decl.page ?? []).map((p) => `${p.key}: ${p.value.kind === "Expr" ? this.expr(p.value.expr, inner) : "null"}`);
|
|
343
|
+
if (decl.requiresAuth) fields.push("noindex: true");
|
|
344
|
+
this.line(`__o.setPage?.({ ${fields.join(", ")} })`);
|
|
345
|
+
}
|
|
198
346
|
this.line(`return ${this.viewList(decl.body, inner)}`);
|
|
199
347
|
this.indent--;
|
|
348
|
+
this.line(`} catch (__e) {`);
|
|
349
|
+
const isScreen = decl.type === "screen" && decl.name !== "ErrorPage";
|
|
350
|
+
this.line(` return __o.renderError(__e, ${JSON.stringify(decl.name)}${isScreen ? ", true" : ""})`);
|
|
351
|
+
this.line(`}`);
|
|
352
|
+
this.indent--;
|
|
200
353
|
this.line("}");
|
|
354
|
+
if (decl.type === "screen") this.line(`${decl.name}.__name = ${JSON.stringify(decl.name)}`);
|
|
355
|
+
if (decl.type === "screen" && decl.params.length > 0) {
|
|
356
|
+
this.line(`${decl.name}.__params = ${JSON.stringify(decl.params)}`);
|
|
357
|
+
}
|
|
358
|
+
if (decl.requiresAuth) this.line(`${decl.name}.__requiresAuth = true`);
|
|
359
|
+
if (decl.prebuild) {
|
|
360
|
+
const asyncScope = new Scope(scope, true, true);
|
|
361
|
+
const values = this.expr(decl.prebuild, asyncScope);
|
|
362
|
+
this.line(`${decl.name}.__prebuild = async () => ${decl.prebuild.kind === "Object" ? `(${values})` : values}`);
|
|
363
|
+
}
|
|
201
364
|
return;
|
|
202
365
|
}
|
|
203
366
|
}
|
|
204
367
|
}
|
|
205
368
|
// `function name(params) { body }`, made async when the body uses wait
|
|
206
369
|
fnHeader(head, params, body, scope, pos = { line: 0, col: 0 }) {
|
|
207
|
-
const inner = new Scope(scope, true);
|
|
370
|
+
const inner = new Scope(scope, true, true);
|
|
208
371
|
params.forEach((p) => inner.declare(p, "local", pos));
|
|
209
372
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
210
|
-
const
|
|
211
|
-
const fullHead = isAsync ? head.replace(/^(export )?/, "$1async ") : head;
|
|
373
|
+
const fullHead = inner.hasAwait ? head.replace(/^(export )?/, "$1async ") : head;
|
|
212
374
|
const code = [`${fullHead}(${params.map(jsName).join(", ")}) {`, ...lines.map((l) => " " + l), "}"];
|
|
213
375
|
return code.join("\n" + " ".repeat(this.indent));
|
|
214
376
|
}
|
|
@@ -228,7 +390,10 @@ class Generator {
|
|
|
228
390
|
}
|
|
229
391
|
// --- statements ----------------------------------------------------------
|
|
230
392
|
stmts(body, scope) {
|
|
231
|
-
for (const stmt of body)
|
|
393
|
+
for (const stmt of body) {
|
|
394
|
+
if (stmt.line) this.line(`__o.at(__F, ${stmt.line}, ${stmt.col ?? 0})`);
|
|
395
|
+
this.stmt(stmt, scope);
|
|
396
|
+
}
|
|
232
397
|
}
|
|
233
398
|
stmt(stmt, scope) {
|
|
234
399
|
switch (stmt.kind) {
|
|
@@ -264,13 +429,35 @@ class Generator {
|
|
|
264
429
|
case "For": {
|
|
265
430
|
const inner = new Scope(scope);
|
|
266
431
|
inner.declare(stmt.name, "local", stmt);
|
|
267
|
-
this.line(`for (const ${jsName(stmt.name)} of ${this.expr(stmt.iter, scope)}) {`);
|
|
432
|
+
this.line(`for (const ${jsName(stmt.name)} of ${this.expr(stmt.iter, scope)} ?? []) {`);
|
|
268
433
|
this.indent++;
|
|
269
434
|
this.stmts(stmt.body, inner);
|
|
270
435
|
this.indent--;
|
|
271
436
|
this.line("}");
|
|
272
437
|
return;
|
|
273
438
|
}
|
|
439
|
+
case "Try": {
|
|
440
|
+
this.line("try {");
|
|
441
|
+
this.indent++;
|
|
442
|
+
this.stmts(stmt.body, new Scope(scope));
|
|
443
|
+
this.indent--;
|
|
444
|
+
if (stmt.catchName) {
|
|
445
|
+
const catchScope = new Scope(scope);
|
|
446
|
+
catchScope.declare(stmt.catchName, "const", stmt);
|
|
447
|
+
this.line("} catch (__orvyn_err) {");
|
|
448
|
+
this.indent++;
|
|
449
|
+
this.line(`const ${jsName(stmt.catchName)} = __o.errorText(__orvyn_err)`);
|
|
450
|
+
this.stmts(stmt.catchBody, catchScope);
|
|
451
|
+
this.indent--;
|
|
452
|
+
} else {
|
|
453
|
+
this.line("} catch {");
|
|
454
|
+
this.indent++;
|
|
455
|
+
this.stmts(stmt.catchBody, new Scope(scope));
|
|
456
|
+
this.indent--;
|
|
457
|
+
}
|
|
458
|
+
this.line("}");
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
274
461
|
}
|
|
275
462
|
}
|
|
276
463
|
assign(stmt, scope) {
|
|
@@ -279,7 +466,8 @@ class Generator {
|
|
|
279
466
|
if (target.kind === "Ident") {
|
|
280
467
|
const binding = scope.lookup(target.name);
|
|
281
468
|
if (binding === void 0) {
|
|
282
|
-
|
|
469
|
+
const hint = suggest(target.name, scope.names());
|
|
470
|
+
fail(hint ? `"${target.name}" is not declared.${hint}` : `"${target.name}" is not declared. Add: state ${target.name} = ...`, target);
|
|
283
471
|
}
|
|
284
472
|
if (binding === "state" || binding === "imported") {
|
|
285
473
|
const name = this.stateRef(target.name, scope);
|
|
@@ -317,11 +505,15 @@ class Generator {
|
|
|
317
505
|
return this.expr(expr, scope);
|
|
318
506
|
}
|
|
319
507
|
// todos.add(x) on state → todos.set(__o.add(todos.get(), x))
|
|
508
|
+
// todos.reset() on saved state → __o.resetState(todos)
|
|
320
509
|
stateListCall(expr, scope) {
|
|
321
510
|
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || !["state", "imported"].includes(scope.lookup(expr.callee.object.name))) {
|
|
322
511
|
return null;
|
|
323
512
|
}
|
|
324
513
|
const method = expr.callee.property;
|
|
514
|
+
if (method === "reset" && expr.args.length === 0) {
|
|
515
|
+
return `__o.resetState(${this.stateRef(expr.callee.object.name, scope)})`;
|
|
516
|
+
}
|
|
325
517
|
if (!LIST_METHODS.has(method)) return null;
|
|
326
518
|
const name = this.stateRef(expr.callee.object.name, scope);
|
|
327
519
|
const args = [`${name}.get()`, ...expr.args.map((a) => this.expr(a, scope))];
|
|
@@ -351,30 +543,60 @@ class Generator {
|
|
|
351
543
|
if (binding !== void 0) return jsName(expr.name);
|
|
352
544
|
if (Object.hasOwn(GLOBALS, expr.name)) return GLOBALS[expr.name];
|
|
353
545
|
if (bareWords) return JSON.stringify(expr.name);
|
|
354
|
-
|
|
546
|
+
{
|
|
547
|
+
const hint = suggest(expr.name, [...scope.names(), ...Object.keys(GLOBALS)]);
|
|
548
|
+
return fail(`"${expr.name}" is not declared${hint ? "." + hint : ""}`, expr);
|
|
549
|
+
}
|
|
355
550
|
}
|
|
356
551
|
case "List":
|
|
357
552
|
return `[${expr.items.map((item) => this.expr(item, scope)).join(", ")}]`;
|
|
358
553
|
// ?. so user.name is empty (not a crash) while data is loading
|
|
359
554
|
case "Member":
|
|
555
|
+
this.checkEnv(expr, scope);
|
|
360
556
|
return `${this.postfix(expr.object, scope)}?.${expr.property}`;
|
|
361
557
|
case "Index":
|
|
362
558
|
return `${this.postfix(expr.object, scope)}?.[${this.expr(expr.index, scope)}]`;
|
|
363
559
|
case "Object":
|
|
364
|
-
return `{ ${expr.entries.map((e) => `${JSON.stringify(e.key)}: ${this.expr(e.value, scope)}`).join(", ")} }`;
|
|
560
|
+
return `{ ${expr.entries.map((e) => `${JSON.stringify(e.key)}: ${this.expr(e.value, scope, bareWords)}`).join(", ")} }`;
|
|
365
561
|
case "Lambda": {
|
|
366
|
-
const inner = new Scope(scope, true);
|
|
562
|
+
const inner = new Scope(scope, true, true);
|
|
367
563
|
expr.params.forEach((p) => inner.declare(p, "local", expr));
|
|
368
564
|
const body = this.expr(expr.body, inner);
|
|
369
|
-
return `(${
|
|
565
|
+
return `(${inner.hasAwait ? "async " : ""}(${expr.params.map(jsName).join(", ")}) => ${expr.body.kind === "Object" ? `(${body})` : body})`;
|
|
370
566
|
}
|
|
371
567
|
case "Ternary":
|
|
372
568
|
return `(${this.expr(expr.cond, scope)} ? ${this.expr(expr.then, scope, bareWords)} : ${this.expr(expr.else, scope, bareWords)})`;
|
|
373
|
-
case "Wait":
|
|
569
|
+
case "Wait": {
|
|
374
570
|
if (!scope.awaitAllowed) fail("Use wait inside fn, start, an event handler, or a lambda", expr);
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
return
|
|
571
|
+
scope.markAwait();
|
|
572
|
+
const inner = this.expr(expr.expr, scope);
|
|
573
|
+
return inner.startsWith("__o.resume(await ") ? inner : `__o.resume(await __o.track(${inner}))`;
|
|
574
|
+
}
|
|
575
|
+
case "Call": {
|
|
576
|
+
if (this.inTest && expr.callee.kind === "Ident" && TEST_GLOBALS.has(expr.callee.name) && scope.lookup(expr.callee.name) === void 0) {
|
|
577
|
+
const name = expr.callee.name;
|
|
578
|
+
const args = expr.args.map((a) => this.expr(a, scope));
|
|
579
|
+
if (name === "expect") args.push(String(expr.line));
|
|
580
|
+
const call2 = `__t.${name}(${args.join(", ")})`;
|
|
581
|
+
return TEST_WAITS.has(name) ? `await ${call2}` : call2;
|
|
582
|
+
}
|
|
583
|
+
this.checkCall(expr, scope);
|
|
584
|
+
if (expr.callee.kind === "Member" && LIST_WORDS.includes(expr.callee.property)) {
|
|
585
|
+
const target = this.expr(expr.callee.object, scope);
|
|
586
|
+
return `__o.words(${target}, ${JSON.stringify(expr.callee.property)}, [${expr.args.map((a) => this.expr(a, scope)).join(", ")}])`;
|
|
587
|
+
}
|
|
588
|
+
const call = `${this.postfix(expr.callee, scope)}(${expr.args.map((a) => this.expr(a, scope)).join(", ")})`;
|
|
589
|
+
if (expr.callee.kind === "Ident" && ASYNC_GLOBALS.has(expr.callee.name) && scope.lookup(expr.callee.name) === void 0) {
|
|
590
|
+
if (NEEDS[expr.callee.name]) this.needs.add(NEEDS[expr.callee.name]);
|
|
591
|
+
if (!scope.awaitAllowed) {
|
|
592
|
+
const name = expr.callee.name;
|
|
593
|
+
fail(/^(pick|take|getLocation)/.test(name) ? `${name}() waits for the person, so use it inside fn or an event handler` : name === "sleep" ? "sleep() pauses, so use it inside fn, start, or an event handler" : `${name}() talks to a server and takes time. To show data, use load: state users = load("/api/users"). To send data, call ${name}() inside fn, start, or an event handler`, expr);
|
|
594
|
+
}
|
|
595
|
+
scope.markAwait();
|
|
596
|
+
return `__o.resume(await __o.track(${call}))`;
|
|
597
|
+
}
|
|
598
|
+
return call;
|
|
599
|
+
}
|
|
378
600
|
case "Unary":
|
|
379
601
|
return `${expr.op}${this.wrap(expr.operand, scope)}`;
|
|
380
602
|
case "Binary": {
|
|
@@ -383,6 +605,40 @@ class Generator {
|
|
|
383
605
|
}
|
|
384
606
|
}
|
|
385
607
|
}
|
|
608
|
+
// a typo like env.API_ULR fails the build instead of shipping an empty value
|
|
609
|
+
checkEnv(expr, scope) {
|
|
610
|
+
const env = this.options.env;
|
|
611
|
+
if (!env || expr.object.kind !== "Ident" || expr.object.name !== "env" || scope.lookup("env") !== void 0) return;
|
|
612
|
+
if (Object.hasOwn(env, expr.property)) return;
|
|
613
|
+
fail(`env.${expr.property} is not set for the "${env.name}" environment.${suggest(expr.property, Object.keys(env)) || " Add it under env in orvyn.config.js"}`, expr);
|
|
614
|
+
}
|
|
615
|
+
// catch wrong argument counts and go(NotAScreen) before the app runs
|
|
616
|
+
checkCall(expr, scope) {
|
|
617
|
+
if (expr.callee.kind !== "Ident") return;
|
|
618
|
+
const name = expr.callee.name;
|
|
619
|
+
const binding = scope.lookup(name);
|
|
620
|
+
if (binding === "fn" && this.fnParams.has(name)) {
|
|
621
|
+
const want = this.fnParams.get(name);
|
|
622
|
+
const got = expr.args.length;
|
|
623
|
+
if (got !== want) {
|
|
624
|
+
const values = (n) => `${n} value${n === 1 ? "" : "s"}`;
|
|
625
|
+
fail(`${name}() takes ${values(want)}, but got ${values(got)}`, expr);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (binding === void 0 && (name === "go" || name === "back")) {
|
|
629
|
+
const target = expr.args[0];
|
|
630
|
+
if (name === "go" && !target) fail("go() needs a screen, like go(Home)", expr);
|
|
631
|
+
if (target?.kind === "Ident") {
|
|
632
|
+
const kind = scope.lookup(target.name);
|
|
633
|
+
if (kind === void 0) {
|
|
634
|
+
const screens = this.program.body.flatMap((d) => d.kind === "Screen" && d.type === "screen" ? [d.name] : []);
|
|
635
|
+
fail(`There is no screen called "${target.name}".${suggest(target.name, screens) || (screens.length ? ` Screens: ${screens.join(", ")}` : "")}`, target);
|
|
636
|
+
}
|
|
637
|
+
if (kind === "component") fail(`${target.name} is a component, not a screen. Use "screen ${target.name}" to navigate to it`, target);
|
|
638
|
+
if (kind !== "screen" && kind !== "imported" && kind !== "local") fail(`${name}() needs a screen, but "${target.name}" is a ${kind}`, target);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
386
642
|
postfix(expr, scope) {
|
|
387
643
|
const code = this.expr(expr, scope);
|
|
388
644
|
return ["Binary", "Unary", "Object"].includes(expr.kind) ? `(${code})` : code;
|
|
@@ -401,6 +657,83 @@ class Generator {
|
|
|
401
657
|
return `\`${body}\``;
|
|
402
658
|
}
|
|
403
659
|
// --- view ----------------------------------------------------------------
|
|
660
|
+
// List rowHeight: 56 { for item in items key item.id { ... } }
|
|
661
|
+
list(node, scope) {
|
|
662
|
+
const loop = node.children[0];
|
|
663
|
+
if (node.arg || node.children.length !== 1 || loop?.kind !== "ViewFor") {
|
|
664
|
+
fail("List needs one loop inside, like: List { for item in items key item.id { Text item.name } }", node);
|
|
665
|
+
}
|
|
666
|
+
const inner = new Scope(scope);
|
|
667
|
+
inner.declare(loop.name, "local", loop);
|
|
668
|
+
const name = jsName(loop.name);
|
|
669
|
+
const props = node.props.map((p) => this.prop(p, "List", scope));
|
|
670
|
+
props.push(`items: ${this.expr(loop.iter, scope)}`);
|
|
671
|
+
props.push(`row: (${name}) => ${this.viewList(loop.body, inner)}`);
|
|
672
|
+
if (loop.key) props.push(`keyOf: (${name}) => ${this.expr(loop.key, inner)}`);
|
|
673
|
+
return `__h(__o.List, { ${props.join(", ")} }, [])`;
|
|
674
|
+
}
|
|
675
|
+
// bind: for Checkbox, Switch and Select: the value and the update handler
|
|
676
|
+
bound(node, scope, helper) {
|
|
677
|
+
const bind = node.props.find((p) => p.key === "bind");
|
|
678
|
+
if (!bind || bind.value.kind !== "Expr") fail(`${node.name} needs bind, like: ${node.name} "...", bind: name`, node);
|
|
679
|
+
const target = bind.value.expr;
|
|
680
|
+
const root = rootIdent(target);
|
|
681
|
+
if (!root || !["state", "imported"].includes(scope.lookup(root.name)) || target.kind !== "Ident" && !((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident")) {
|
|
682
|
+
fail("bind needs a state name or one field, like bind: user.name", bind);
|
|
683
|
+
}
|
|
684
|
+
const key = target.kind === "Member" ? JSON.stringify(target.property) : target.kind === "Index" ? this.expr(target.index, scope) : "undefined";
|
|
685
|
+
return {
|
|
686
|
+
value: this.expr(target, scope),
|
|
687
|
+
handler: `__o.${helper}(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
// Checkbox "I agree", bind: agreed
|
|
691
|
+
toggle(node, scope) {
|
|
692
|
+
if (!node.arg) fail(`${node.name} needs its words, like: ${node.name} "Send me news", bind: news`, node);
|
|
693
|
+
const { value, handler } = this.bound(node, scope, "bindCheck");
|
|
694
|
+
const said = this.expr(node.arg, scope);
|
|
695
|
+
const input = [`type: "checkbox"`, `checked: !!(${value})`, `onInput: ${handler}`];
|
|
696
|
+
if (node.name === "Switch") input.push(`role: "switch"`);
|
|
697
|
+
const outer = [`flex: true`, `direction: "row"`, `align: "center"`, `gap: 8`, `nativeRole: "toggle"`];
|
|
698
|
+
for (const prop of node.props) {
|
|
699
|
+
if (prop.key === "disabled") input.push(this.prop(prop, node.name, scope));
|
|
700
|
+
else if (prop.key !== "bind") outer.push(this.prop(prop, node.name, scope));
|
|
701
|
+
}
|
|
702
|
+
return `__h("label", { ${outer.join(", ")} }, [__h("input", { ${input.join(", ")} }, []), __h("span", {}, [${said}])])`;
|
|
703
|
+
}
|
|
704
|
+
// Select "Country", bind: country, options: ["India", "Nepal"]
|
|
705
|
+
select(node, scope) {
|
|
706
|
+
const options = node.props.find((p) => p.key === "options");
|
|
707
|
+
if (!options || options.value.kind !== "Expr") fail(`Select needs options, like: Select "Country", bind: country, options: ["India", "Nepal"]`, node);
|
|
708
|
+
const { value, handler } = this.bound(node, scope, "bindInput");
|
|
709
|
+
const placeholder = node.arg ? this.expr(node.arg, scope) : `""`;
|
|
710
|
+
const props = [`value: ${value}`, `onInput: ${handler}`, `placeholder: ${placeholder}`];
|
|
711
|
+
for (const prop of node.props) {
|
|
712
|
+
if (prop.key === "label" && prop.value.kind === "Expr") props.push(`"aria-label": ${this.expr(prop.value.expr, scope)}`);
|
|
713
|
+
else if (prop.key !== "bind" && prop.key !== "options") props.push(this.prop(prop, "Select", scope));
|
|
714
|
+
}
|
|
715
|
+
if (node.arg && !node.props.some((p) => p.key === "label")) props.push(`"aria-label": ${placeholder}`);
|
|
716
|
+
return `__o.createSelect({ ${props.join(", ")} }, ${this.expr(options.value.expr, scope)})`;
|
|
717
|
+
}
|
|
718
|
+
tabs(node, scope) {
|
|
719
|
+
const options = node.props.find((p) => p.key === "options");
|
|
720
|
+
if (!options || options.value.kind !== "Expr") fail(`Tabs needs options, like: Tabs bind: tab, options: ["Home", "Profile"]`, node);
|
|
721
|
+
const { value, handler } = this.bound(node, scope, "bindInput");
|
|
722
|
+
const props = [
|
|
723
|
+
`value: ${value}`,
|
|
724
|
+
`onInput: ${handler}`,
|
|
725
|
+
...node.props.filter((p) => p.key !== "bind" && p.key !== "options").map((p) => this.prop(p, "Tabs", scope))
|
|
726
|
+
];
|
|
727
|
+
return `__o.createTabs({ ${props.join(", ")} }, ${this.expr(options.value.expr, scope)})`;
|
|
728
|
+
}
|
|
729
|
+
modal(node, scope) {
|
|
730
|
+
for (const key of ["open", "onClose"]) {
|
|
731
|
+
if (!node.props.some((p) => p.key === key)) fail(`Modal needs ${key}, like: Modal open: help, onClose: help = false { ... }`, node);
|
|
732
|
+
}
|
|
733
|
+
const props = node.props.map((p) => p.key === "onClose" ? `onClose: ${p.value.kind === "Handler" ? this.handler(p.value.body, scope) : this.exprHandler(p.value.expr, scope)}` : this.prop(p, "Modal", scope));
|
|
734
|
+
if (node.arg) props.push(`title: ${this.expr(node.arg, scope)}`);
|
|
735
|
+
return `__o.createModal({ ${props.join(", ")} }, [${node.children.map((c) => this.view(c, scope)).join(", ")}])`;
|
|
736
|
+
}
|
|
404
737
|
viewList(nodes, scope) {
|
|
405
738
|
return `[${nodes.map((node) => this.view(node, scope)).join(", ")}]`;
|
|
406
739
|
}
|
|
@@ -415,13 +748,34 @@ class Generator {
|
|
|
415
748
|
const inner = new Scope(scope);
|
|
416
749
|
inner.declare(node.name, "local", node);
|
|
417
750
|
const name = jsName(node.name);
|
|
418
|
-
|
|
751
|
+
const body = this.viewList(node.body, inner);
|
|
752
|
+
const keyed = node.key ? `__o.keyed(${this.expr(node.key, inner)}, ${body})` : body;
|
|
753
|
+
return `Array.from(${this.expr(node.iter, scope)} ?? [], (${name}) => ${keyed})`;
|
|
419
754
|
}
|
|
420
755
|
case "Element":
|
|
421
|
-
return this.element(node, scope);
|
|
756
|
+
return node.line ? `(__o.at(__F, ${node.line}, ${node.col ?? 0}), ${this.element(node, scope)})` : this.element(node, scope);
|
|
422
757
|
}
|
|
423
758
|
}
|
|
424
759
|
element(node, scope) {
|
|
760
|
+
if (node.name === "List" && !this.components.has("List")) return this.list(node, scope);
|
|
761
|
+
if (node.name === "Question" && !this.components.has("Question")) {
|
|
762
|
+
if (!node.arg || !node.children.length) fail(`Question needs text and an answer, like: Question "Is it free?" { Text "Yes" }`, node);
|
|
763
|
+
const asked = this.expr(node.arg, scope);
|
|
764
|
+
const props2 = node.props.map((p) => this.prop(p, "Question", scope));
|
|
765
|
+
const answer = node.children.map((c) => this.view(c, scope)).join(", ");
|
|
766
|
+
return `__h("section", { ${[`flex: true`, `direction: "column"`, `gap: 8`, `"data-orvyn-question": ${asked}`, ...props2].join(", ")} }, [__h("h2", { size: 20 }, [${asked}]), ${answer}])`;
|
|
767
|
+
}
|
|
768
|
+
if ((node.name === "Checkbox" || node.name === "Switch") && !this.components.has(node.name)) return this.toggle(node, scope);
|
|
769
|
+
if (node.name === "Select" && !this.components.has("Select")) return this.select(node, scope);
|
|
770
|
+
if (node.name === "Tabs" && !this.components.has("Tabs")) return this.tabs(node, scope);
|
|
771
|
+
if (node.name === "Modal" && !this.components.has("Modal")) return this.modal(node, scope);
|
|
772
|
+
if (node.name === "Map" && !this.components.has("Map")) {
|
|
773
|
+
for (const key of ["latitude", "longitude"]) {
|
|
774
|
+
if (!node.props.some((p) => p.key === key)) fail(`Map needs ${key}, like: Map latitude: 28.61, longitude: 77.21`, node);
|
|
775
|
+
}
|
|
776
|
+
const props2 = node.props.map((p) => p.key === "label" && p.value.kind === "Expr" ? `title: ${this.expr(p.value.expr, scope)}` : this.prop(p, "Map", scope));
|
|
777
|
+
return `__o.createMap({ ${props2.join(", ")} })`;
|
|
778
|
+
}
|
|
425
779
|
const builtin = Object.hasOwn(BUILTINS, node.name) ? BUILTINS[node.name] : void 0;
|
|
426
780
|
const imported = scope.lookup(node.name) === "imported";
|
|
427
781
|
const userComponent = this.components.get(node.name) ?? (imported ? null : void 0);
|
|
@@ -449,7 +803,7 @@ class Generator {
|
|
|
449
803
|
}
|
|
450
804
|
const bind = node.props.find((p) => p.key === "bind");
|
|
451
805
|
if (bind) {
|
|
452
|
-
if (node.name !== "Input") fail("Use bind on Input,
|
|
806
|
+
if (node.name !== "Input" && node.name !== "TextArea" && node.name !== "DatePicker") fail("Use bind on Input, TextArea, Checkbox, Switch or Select", bind);
|
|
453
807
|
if (node.props.filter((p) => p.key === "bind").length > 1 || node.props.some((p) => ["value", "onChange", "onInput"].includes(p.key))) {
|
|
454
808
|
fail("bind supplies value and onChange. Remove the duplicate binding or event", bind);
|
|
455
809
|
}
|
|
@@ -467,13 +821,30 @@ class Generator {
|
|
|
467
821
|
props.push(`onInput: __o.bindInput(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`);
|
|
468
822
|
}
|
|
469
823
|
for (const prop of node.props) {
|
|
470
|
-
if (prop.key
|
|
824
|
+
if (node.name === "Input" && prop.key === "type" && prop.value.kind === "Expr") {
|
|
825
|
+
const kind = prop.value.expr;
|
|
826
|
+
const known = ["text", "email", "password", "number", "phone"];
|
|
827
|
+
const word = kind.kind === "String" && kind.parts.length === 1 ? String(kind.parts[0]) : kind.kind === "Ident" && (known.includes(kind.name) || scope.lookup(kind.name) === void 0) ? kind.name : null;
|
|
828
|
+
if (word !== null) {
|
|
829
|
+
if (!["text", "email", "password", "number", "phone"].includes(word)) {
|
|
830
|
+
fail(`Input type can be: text, email, password, number or phone`, prop);
|
|
831
|
+
}
|
|
832
|
+
props.push(`type: ${JSON.stringify(word === "phone" ? "tel" : word)}`);
|
|
833
|
+
continue;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
if (prop.key === "label" && prop.value.kind === "Expr") {
|
|
837
|
+
const said = this.expr(prop.value.expr, scope);
|
|
838
|
+
props.push(`"aria-label": ${said}`);
|
|
839
|
+
if (node.name === "Image") props.push(`alt: ${said}`);
|
|
840
|
+
} else if (prop.key !== "bind") props.push(this.prop(prop, node.name, scope));
|
|
471
841
|
}
|
|
842
|
+
if (node.name === "Image") props.push(`loading: "lazy"`, `decoding: "async"`);
|
|
472
843
|
} else {
|
|
473
844
|
if (node.arg) fail(`${node.name} needs named arguments, like ${node.name} title: "..."`, node.arg);
|
|
474
845
|
const params = userComponent;
|
|
475
846
|
for (const prop of node.props) {
|
|
476
|
-
if (params && !params.includes(prop.key)) {
|
|
847
|
+
if (params && !params.includes(prop.key) && prop.key !== "key") {
|
|
477
848
|
fail(`${node.name} has no parameter "${prop.key}". It takes: ${params.join(", ") || "nothing"}`, prop);
|
|
478
849
|
}
|
|
479
850
|
if (prop.value.kind === "Handler") {
|
|
@@ -485,7 +856,7 @@ class Generator {
|
|
|
485
856
|
}
|
|
486
857
|
for (const child of node.children) children.push(this.view(child, scope));
|
|
487
858
|
if (!builtin) {
|
|
488
|
-
return
|
|
859
|
+
return `__h(${node.name}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
489
860
|
}
|
|
490
861
|
if (node.name === "Form") {
|
|
491
862
|
return `__o.createForm(__forms, ${this.formId++}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
@@ -502,28 +873,28 @@ class Generator {
|
|
|
502
873
|
if (!event) {
|
|
503
874
|
fail(`Unknown event "${prop.key}". Available: ${Object.keys(EVENTS).join(", ")}`, prop);
|
|
504
875
|
}
|
|
876
|
+
const isCallableRef = prop.value.kind === "Expr" && (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member" || prop.value.expr.kind === "Lambda");
|
|
505
877
|
if (prop.key === "onChange") {
|
|
506
|
-
const call = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope, ["value"]) : this.expr(prop.value.expr, scope);
|
|
507
|
-
return `${event}: (e) => (${call})(e.target.value)`;
|
|
508
|
-
}
|
|
509
|
-
let handler;
|
|
510
|
-
if (prop.value.kind === "Handler") {
|
|
511
|
-
handler = this.handler(prop.value.body, scope);
|
|
512
|
-
} else if (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member") {
|
|
513
|
-
handler = this.expr(prop.value.expr, scope);
|
|
514
|
-
} else if (this.stateListCall(prop.value.expr, scope)) {
|
|
515
|
-
handler = `() => ${this.stateListCall(prop.value.expr, scope)}`;
|
|
516
|
-
} else {
|
|
517
|
-
handler = `() => ${this.expr(prop.value.expr, scope)}`;
|
|
878
|
+
const call = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope, ["value"]) : isCallableRef ? this.expr(prop.value.expr, scope) : this.exprHandler(prop.value.expr, scope, ["value"]);
|
|
879
|
+
return `${event}: __o.guard((e) => (${call})(e.target.value), ${JSON.stringify(`onChange in ${this.currentView || element}`)}, __F, ${prop.line ?? 0})`;
|
|
518
880
|
}
|
|
519
|
-
|
|
881
|
+
const handler = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope) : isCallableRef ? this.expr(prop.value.expr, scope) : this.exprHandler(prop.value.expr, scope);
|
|
882
|
+
if (element === "Form" && prop.key === "onSubmit") return `${event}: __o.submitHandler(${handler})`;
|
|
883
|
+
return `${event}: __o.guard(${handler}, ${JSON.stringify(`${prop.key} in ${this.currentView || element}`)}, __F, ${prop.line ?? 0})`;
|
|
884
|
+
}
|
|
885
|
+
// `onTap: post(...)` or `onTap: sleep(10)` runs in an event-handler scope so
|
|
886
|
+
// waiting works the same with or without `{ ... }`.
|
|
887
|
+
exprHandler(expr, scope, params = []) {
|
|
888
|
+
const inner = new Scope(scope, true, true);
|
|
889
|
+
params.forEach((p) => inner.declare(p, "local", expr));
|
|
890
|
+
const body = this.stateListCall(expr, inner) ?? this.expr(expr, inner);
|
|
891
|
+
return `${inner.hasAwait ? "async " : ""}(${params.join(", ")}) => ${expr.kind === "Object" ? `(${body})` : body}`;
|
|
520
892
|
}
|
|
521
893
|
handler(body, scope, params = []) {
|
|
522
|
-
const inner = new Scope(scope, true);
|
|
894
|
+
const inner = new Scope(scope, true, true);
|
|
523
895
|
params.forEach((p) => inner.declare(p, "local", body[0] ?? { line: 0, col: 0 }));
|
|
524
896
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
525
|
-
|
|
526
|
-
return `${isAsync ? "async " : ""}(${params.join(", ")}) => {
|
|
897
|
+
return `${inner.hasAwait ? "async " : ""}(${params.join(", ")}) => {
|
|
527
898
|
${lines.join("\n")}
|
|
528
899
|
}`;
|
|
529
900
|
}
|