orvyn-lang 0.1.1 → 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 +877 -24
- package/README.md +227 -131
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +216 -489
- 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 +498 -64
- 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 +160 -0
- 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 +519 -40
- 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 +268 -4
- package/src/lang/native/ios/OrvynViews.swift +429 -31
- package/src/lang/native/ios.js +75 -2
- package/src/lang/native-runtime.js +423 -57
- 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 +820 -10
- package/src/lang/runtime.js +375 -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/validation.js +55 -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/core/context.js +0 -19
- package/src/core/lifecycle.js +0 -8
- package/src/core/resource.js +0 -27
- package/src/core/router.js +0 -20
- package/src/core/theme.js +0 -10
- package/src/devtools/devicePreview/PreviewFrame.js +0 -214
- package/src/devtools/devicePreview/index.js +0 -6
- package/src/devtools/overlay.js +0 -105
- package/src/runtime/createRenderer.js +0 -22
- package/src/runtime/h.js +0 -93
- package/src/runtime/hydrate.js +0 -59
- package/src/runtime/jsx-dev-runtime.js +0 -7
- package/src/runtime/jsx-runtime.js +0 -13
- package/src/runtime/partialHydration.js +0 -154
- package/src/runtime/platform.js +0 -135
- package/src/runtime/renderToStream.js +0 -127
- package/src/runtime/renderToString.js +0 -121
- package/src/runtime/resume.js +0 -62
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" } },
|
|
@@ -6,27 +7,56 @@ const BUILTINS = {
|
|
|
6
7
|
Box: { tag: "div" },
|
|
7
8
|
Text: { tag: "span", arg: "child" },
|
|
8
9
|
Heading: { tag: "h1", arg: "child" },
|
|
9
|
-
Button: { tag: "button", arg: "child" },
|
|
10
|
+
Button: { tag: "button", props: { type: "button" }, arg: "child" },
|
|
11
|
+
Form: { tag: "form", props: { flex: true, direction: "column", noValidate: true } },
|
|
12
|
+
SubmitButton: { tag: "button", props: { type: "submit" }, arg: "child" },
|
|
10
13
|
Image: { tag: "img", arg: "src" },
|
|
14
|
+
DatePicker: { tag: "input", props: { type: "date" }, arg: "placeholder" },
|
|
15
|
+
TextArea: { tag: "textarea", props: { rows: 4 }, arg: "placeholder" },
|
|
11
16
|
Input: { tag: "input", arg: "placeholder" },
|
|
12
17
|
Link: { tag: "a", arg: "child" }
|
|
13
18
|
};
|
|
19
|
+
const TEST_GLOBALS = /* @__PURE__ */ new Set(["tap", "type", "expect", "text", "mock", "settle"]);
|
|
20
|
+
const TEST_WAITS = /* @__PURE__ */ new Set(["tap", "type", "settle"]);
|
|
14
21
|
const EVENTS = {
|
|
15
22
|
onTap: "onClick",
|
|
16
23
|
onClick: "onClick",
|
|
17
24
|
onChange: "onInput",
|
|
18
25
|
onSubmit: "onSubmit",
|
|
26
|
+
onError: "onError",
|
|
19
27
|
onHover: "onMouseenter",
|
|
20
28
|
onLeave: "onMouseleave"
|
|
21
29
|
};
|
|
22
30
|
const GLOBALS = {
|
|
23
31
|
print: "console.log",
|
|
24
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",
|
|
25
39
|
go: "__o.go",
|
|
40
|
+
back: "__o.back",
|
|
41
|
+
canGoBack: "__o.canGoBack",
|
|
42
|
+
openUrl: "__o.openUrl",
|
|
26
43
|
sleep: "__o.sleep",
|
|
27
44
|
random: "__o.random",
|
|
28
45
|
len: "__o.len",
|
|
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",
|
|
29
57
|
Math: "Math",
|
|
58
|
+
Array: "Array",
|
|
59
|
+
Object: "Object",
|
|
30
60
|
JSON: "JSON",
|
|
31
61
|
Date: "Date",
|
|
32
62
|
Number: "Number",
|
|
@@ -65,13 +95,25 @@ const JS_RESERVED = /* @__PURE__ */ new Set([
|
|
|
65
95
|
"instanceof",
|
|
66
96
|
"enum",
|
|
67
97
|
"static",
|
|
68
|
-
"arguments"
|
|
98
|
+
"arguments",
|
|
99
|
+
"eval",
|
|
100
|
+
"implements",
|
|
101
|
+
"interface",
|
|
102
|
+
"package",
|
|
103
|
+
"private",
|
|
104
|
+
"protected",
|
|
105
|
+
"public"
|
|
69
106
|
]);
|
|
70
107
|
class Scope {
|
|
71
108
|
vars = /* @__PURE__ */ new Map();
|
|
72
109
|
parent;
|
|
73
|
-
|
|
110
|
+
awaitAllowed;
|
|
111
|
+
fnBoundary;
|
|
112
|
+
hasAwait = false;
|
|
113
|
+
constructor(parent = null, awaitAllowed = parent?.awaitAllowed ?? false, fnBoundary = false) {
|
|
74
114
|
this.parent = parent;
|
|
115
|
+
this.awaitAllowed = awaitAllowed;
|
|
116
|
+
this.fnBoundary = fnBoundary;
|
|
75
117
|
}
|
|
76
118
|
declare(name, binding, pos) {
|
|
77
119
|
if (this.vars.has(name)) {
|
|
@@ -82,14 +124,64 @@ class Scope {
|
|
|
82
124
|
lookup(name) {
|
|
83
125
|
return this.vars.get(name) ?? this.parent?.lookup(name);
|
|
84
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
|
+
}
|
|
85
140
|
}
|
|
141
|
+
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__F", "__e", "__forms", "__local", "screens", "mountApp"]);
|
|
86
142
|
function jsName(name) {
|
|
87
|
-
return JS_RESERVED.has(name) ?
|
|
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;
|
|
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];
|
|
88
173
|
}
|
|
89
174
|
function fail(message, pos) {
|
|
90
175
|
throw new OrvynSyntaxError(message, pos.line, pos.col);
|
|
91
176
|
}
|
|
177
|
+
const ASYNC_GLOBALS = /* @__PURE__ */ new Set(["fetch", "get", "post", "put", "patch", "sleep", "pickPhoto", "takePhoto", "pickFile", "getLocation"]);
|
|
178
|
+
const NEEDS = { takePhoto: "camera", getLocation: "location" };
|
|
92
179
|
class Generator {
|
|
180
|
+
currentView = "";
|
|
181
|
+
fnParams = /* @__PURE__ */ new Map();
|
|
182
|
+
formId = 0;
|
|
183
|
+
needs = /* @__PURE__ */ new Set();
|
|
184
|
+
inTest = false;
|
|
93
185
|
out = [];
|
|
94
186
|
indent = 0;
|
|
95
187
|
global = new Scope();
|
|
@@ -106,26 +198,44 @@ class Generator {
|
|
|
106
198
|
const starts = [];
|
|
107
199
|
for (const decl of this.program.body) {
|
|
108
200
|
if (decl.kind === "Use") {
|
|
109
|
-
decl.
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
}
|
|
112
214
|
continue;
|
|
113
215
|
}
|
|
114
216
|
if (decl.kind === "Start") {
|
|
115
217
|
starts.push(decl.body);
|
|
116
218
|
continue;
|
|
117
219
|
}
|
|
220
|
+
if (decl.kind === "Test" || decl.kind === "Type") continue;
|
|
118
221
|
const binding = decl.kind === "State" ? "state" : decl.kind === "Const" ? "const" : decl.kind === "Fn" ? "fn" : decl.type;
|
|
119
222
|
this.global.declare(decl.name, binding, decl);
|
|
120
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);
|
|
121
225
|
}
|
|
122
226
|
this.line(`import * as __o from "${runtime}/lang/runtime"`);
|
|
123
227
|
imports.forEach((line) => this.line(line));
|
|
124
228
|
this.line("const __signal = __o.signal");
|
|
125
229
|
this.line("const __h = __o.createVNode");
|
|
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)})`);
|
|
126
236
|
this.line("");
|
|
127
237
|
for (const decl of this.program.body) {
|
|
128
|
-
if (decl.kind === "Use" || decl.kind === "Start") continue;
|
|
238
|
+
if (decl.kind === "Use" || decl.kind === "Start" || decl.kind === "Test") continue;
|
|
129
239
|
this.decl(decl);
|
|
130
240
|
this.line("");
|
|
131
241
|
}
|
|
@@ -133,14 +243,40 @@ class Generator {
|
|
|
133
243
|
this.line(this.fnHeader("function __start", [], starts.flat(), this.global));
|
|
134
244
|
this.line("");
|
|
135
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
|
+
}
|
|
136
267
|
const screens = this.program.body.filter(
|
|
137
268
|
(d) => d.kind === "Screen" && d.type === "screen"
|
|
138
269
|
);
|
|
139
|
-
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(", ")} })`);
|
|
140
276
|
this.line(`export const screens = { ${screens.map((s) => s.name).join(", ")} }`);
|
|
141
277
|
this.line("");
|
|
142
|
-
this.line("export function mountApp(target, screen) {");
|
|
143
|
-
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)`);
|
|
144
280
|
this.line("}");
|
|
145
281
|
this.line("");
|
|
146
282
|
this.line(`export default ${entry ? entry.name : "null"}`);
|
|
@@ -154,11 +290,21 @@ class Generator {
|
|
|
154
290
|
decl(decl) {
|
|
155
291
|
const scope = this.global;
|
|
156
292
|
switch (decl.kind) {
|
|
157
|
-
case "State":
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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})`);
|
|
161
306
|
return;
|
|
307
|
+
}
|
|
162
308
|
case "Const":
|
|
163
309
|
this.line(`export const ${jsName(decl.name)} = ${this.expr(decl.init, scope)}`);
|
|
164
310
|
return;
|
|
@@ -169,23 +315,62 @@ class Generator {
|
|
|
169
315
|
const inner = new Scope(scope);
|
|
170
316
|
decl.params.forEach((p) => inner.declare(p, "local", decl));
|
|
171
317
|
inner.declare("children", "local", decl);
|
|
172
|
-
const params = [...decl.params.map(jsName), "children"];
|
|
318
|
+
const params = [...decl.params.map((p) => p === jsName(p) ? p : `${p}: ${jsName(p)}`), "children"];
|
|
319
|
+
this.currentView = decl.name;
|
|
173
320
|
this.line(`export function ${decl.name}({ ${params.join(", ")} } = {}) {`);
|
|
174
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
|
+
}
|
|
175
346
|
this.line(`return ${this.viewList(decl.body, inner)}`);
|
|
176
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--;
|
|
177
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
|
+
}
|
|
178
364
|
return;
|
|
179
365
|
}
|
|
180
366
|
}
|
|
181
367
|
}
|
|
182
368
|
// `function name(params) { body }`, made async when the body uses wait
|
|
183
369
|
fnHeader(head, params, body, scope, pos = { line: 0, col: 0 }) {
|
|
184
|
-
const inner = new Scope(scope);
|
|
370
|
+
const inner = new Scope(scope, true, true);
|
|
185
371
|
params.forEach((p) => inner.declare(p, "local", pos));
|
|
186
372
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
187
|
-
const
|
|
188
|
-
const fullHead = isAsync ? head.replace(/^(export )?/, "$1async ") : head;
|
|
373
|
+
const fullHead = inner.hasAwait ? head.replace(/^(export )?/, "$1async ") : head;
|
|
189
374
|
const code = [`${fullHead}(${params.map(jsName).join(", ")}) {`, ...lines.map((l) => " " + l), "}"];
|
|
190
375
|
return code.join("\n" + " ".repeat(this.indent));
|
|
191
376
|
}
|
|
@@ -205,7 +390,10 @@ class Generator {
|
|
|
205
390
|
}
|
|
206
391
|
// --- statements ----------------------------------------------------------
|
|
207
392
|
stmts(body, scope) {
|
|
208
|
-
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
|
+
}
|
|
209
397
|
}
|
|
210
398
|
stmt(stmt, scope) {
|
|
211
399
|
switch (stmt.kind) {
|
|
@@ -241,13 +429,35 @@ class Generator {
|
|
|
241
429
|
case "For": {
|
|
242
430
|
const inner = new Scope(scope);
|
|
243
431
|
inner.declare(stmt.name, "local", stmt);
|
|
244
|
-
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)} ?? []) {`);
|
|
245
433
|
this.indent++;
|
|
246
434
|
this.stmts(stmt.body, inner);
|
|
247
435
|
this.indent--;
|
|
248
436
|
this.line("}");
|
|
249
437
|
return;
|
|
250
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
|
+
}
|
|
251
461
|
}
|
|
252
462
|
}
|
|
253
463
|
assign(stmt, scope) {
|
|
@@ -256,10 +466,11 @@ class Generator {
|
|
|
256
466
|
if (target.kind === "Ident") {
|
|
257
467
|
const binding = scope.lookup(target.name);
|
|
258
468
|
if (binding === void 0) {
|
|
259
|
-
|
|
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);
|
|
260
471
|
}
|
|
261
|
-
if (binding === "state") {
|
|
262
|
-
const name =
|
|
472
|
+
if (binding === "state" || binding === "imported") {
|
|
473
|
+
const name = this.stateRef(target.name, scope);
|
|
263
474
|
if (stmt.op === "=") return `${name}.set(${value})`;
|
|
264
475
|
const op = stmt.op[0];
|
|
265
476
|
return `${name}.set(${name}.get() ${op} (${value}))`;
|
|
@@ -272,14 +483,14 @@ class Generator {
|
|
|
272
483
|
}
|
|
273
484
|
return `${jsName(target.name)} ${stmt.op} ${value}`;
|
|
274
485
|
}
|
|
275
|
-
if ((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident" && scope.lookup(target.object.name)
|
|
276
|
-
const name =
|
|
486
|
+
if ((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident" && ["state", "imported"].includes(scope.lookup(target.object.name))) {
|
|
487
|
+
const name = this.stateRef(target.object.name, scope);
|
|
277
488
|
const current = target.kind === "Member" ? `${name}.get()?.${target.property}` : `${name}.get()?.[${this.expr(target.index, scope)}]`;
|
|
278
489
|
const next = stmt.op === "=" ? value : `${current} ${stmt.op[0]} (${value})`;
|
|
279
490
|
return target.kind === "Member" ? `${name}.set(__o.setField(${name}.get(), ${JSON.stringify(target.property)}, ${next}))` : `${name}.set(__o.setAt(${name}.get(), ${this.expr(target.index, scope)}, ${next}))`;
|
|
280
491
|
}
|
|
281
492
|
const root = rootIdent(target);
|
|
282
|
-
if (root && scope.lookup(root.name)
|
|
493
|
+
if (root && ["state", "imported"].includes(scope.lookup(root.name))) {
|
|
283
494
|
fail(
|
|
284
495
|
`Can't change part of state "${root.name}" directly. Assign a new value instead, like "${root.name} = ..."`,
|
|
285
496
|
target
|
|
@@ -294,16 +505,23 @@ class Generator {
|
|
|
294
505
|
return this.expr(expr, scope);
|
|
295
506
|
}
|
|
296
507
|
// todos.add(x) on state → todos.set(__o.add(todos.get(), x))
|
|
508
|
+
// todos.reset() on saved state → __o.resetState(todos)
|
|
297
509
|
stateListCall(expr, scope) {
|
|
298
|
-
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || scope.lookup(expr.callee.object.name)
|
|
510
|
+
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || !["state", "imported"].includes(scope.lookup(expr.callee.object.name))) {
|
|
299
511
|
return null;
|
|
300
512
|
}
|
|
301
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
|
+
}
|
|
302
517
|
if (!LIST_METHODS.has(method)) return null;
|
|
303
|
-
const name =
|
|
518
|
+
const name = this.stateRef(expr.callee.object.name, scope);
|
|
304
519
|
const args = [`${name}.get()`, ...expr.args.map((a) => this.expr(a, scope))];
|
|
305
520
|
return `${name}.set(__o.${method}(${args.join(", ")}))`;
|
|
306
521
|
}
|
|
522
|
+
stateRef(name, scope) {
|
|
523
|
+
return scope.lookup(name) === "imported" ? `__o.importedState(${jsName(name)}, ${JSON.stringify(name)})` : jsName(name);
|
|
524
|
+
}
|
|
307
525
|
// --- expressions ---------------------------------------------------------
|
|
308
526
|
// `bareWords`: in style props, unknown names become strings (bg: black)
|
|
309
527
|
expr(expr, scope, bareWords = false) {
|
|
@@ -321,31 +539,64 @@ class Generator {
|
|
|
321
539
|
case "Ident": {
|
|
322
540
|
const binding = scope.lookup(expr.name);
|
|
323
541
|
if (binding === "state") return `${jsName(expr.name)}.get()`;
|
|
542
|
+
if (binding === "imported") return `__o.readImported(${jsName(expr.name)})`;
|
|
324
543
|
if (binding !== void 0) return jsName(expr.name);
|
|
325
|
-
if (expr.name
|
|
544
|
+
if (Object.hasOwn(GLOBALS, expr.name)) return GLOBALS[expr.name];
|
|
326
545
|
if (bareWords) return JSON.stringify(expr.name);
|
|
327
|
-
|
|
546
|
+
{
|
|
547
|
+
const hint = suggest(expr.name, [...scope.names(), ...Object.keys(GLOBALS)]);
|
|
548
|
+
return fail(`"${expr.name}" is not declared${hint ? "." + hint : ""}`, expr);
|
|
549
|
+
}
|
|
328
550
|
}
|
|
329
551
|
case "List":
|
|
330
552
|
return `[${expr.items.map((item) => this.expr(item, scope)).join(", ")}]`;
|
|
331
553
|
// ?. so user.name is empty (not a crash) while data is loading
|
|
332
554
|
case "Member":
|
|
333
|
-
|
|
555
|
+
this.checkEnv(expr, scope);
|
|
556
|
+
return `${this.postfix(expr.object, scope)}?.${expr.property}`;
|
|
334
557
|
case "Index":
|
|
335
|
-
return `${this.
|
|
558
|
+
return `${this.postfix(expr.object, scope)}?.[${this.expr(expr.index, scope)}]`;
|
|
336
559
|
case "Object":
|
|
337
|
-
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(", ")} }`;
|
|
338
561
|
case "Lambda": {
|
|
339
|
-
const inner = new Scope(scope);
|
|
562
|
+
const inner = new Scope(scope, true, true);
|
|
340
563
|
expr.params.forEach((p) => inner.declare(p, "local", expr));
|
|
341
|
-
|
|
564
|
+
const body = this.expr(expr.body, inner);
|
|
565
|
+
return `(${inner.hasAwait ? "async " : ""}(${expr.params.map(jsName).join(", ")}) => ${expr.body.kind === "Object" ? `(${body})` : body})`;
|
|
342
566
|
}
|
|
343
567
|
case "Ternary":
|
|
344
568
|
return `(${this.expr(expr.cond, scope)} ? ${this.expr(expr.then, scope, bareWords)} : ${this.expr(expr.else, scope, bareWords)})`;
|
|
345
|
-
case "Wait":
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
569
|
+
case "Wait": {
|
|
570
|
+
if (!scope.awaitAllowed) fail("Use wait inside fn, start, an event handler, or a lambda", expr);
|
|
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
|
+
}
|
|
349
600
|
case "Unary":
|
|
350
601
|
return `${expr.op}${this.wrap(expr.operand, scope)}`;
|
|
351
602
|
case "Binary": {
|
|
@@ -354,6 +605,44 @@ class Generator {
|
|
|
354
605
|
}
|
|
355
606
|
}
|
|
356
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
|
+
}
|
|
642
|
+
postfix(expr, scope) {
|
|
643
|
+
const code = this.expr(expr, scope);
|
|
644
|
+
return ["Binary", "Unary", "Object"].includes(expr.kind) ? `(${code})` : code;
|
|
645
|
+
}
|
|
357
646
|
wrap(expr, scope) {
|
|
358
647
|
const code = this.expr(expr, scope);
|
|
359
648
|
return expr.kind === "Binary" || expr.kind === "Ternary" ? `(${code})` : code;
|
|
@@ -368,6 +657,83 @@ class Generator {
|
|
|
368
657
|
return `\`${body}\``;
|
|
369
658
|
}
|
|
370
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
|
+
}
|
|
371
737
|
viewList(nodes, scope) {
|
|
372
738
|
return `[${nodes.map((node) => this.view(node, scope)).join(", ")}]`;
|
|
373
739
|
}
|
|
@@ -382,14 +748,35 @@ class Generator {
|
|
|
382
748
|
const inner = new Scope(scope);
|
|
383
749
|
inner.declare(node.name, "local", node);
|
|
384
750
|
const name = jsName(node.name);
|
|
385
|
-
|
|
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})`;
|
|
386
754
|
}
|
|
387
755
|
case "Element":
|
|
388
|
-
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);
|
|
389
757
|
}
|
|
390
758
|
}
|
|
391
759
|
element(node, scope) {
|
|
392
|
-
|
|
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
|
+
}
|
|
779
|
+
const builtin = Object.hasOwn(BUILTINS, node.name) ? BUILTINS[node.name] : void 0;
|
|
393
780
|
const imported = scope.lookup(node.name) === "imported";
|
|
394
781
|
const userComponent = this.components.get(node.name) ?? (imported ? null : void 0);
|
|
395
782
|
if (!builtin && userComponent === void 0) {
|
|
@@ -399,6 +786,12 @@ class Generator {
|
|
|
399
786
|
const children = [];
|
|
400
787
|
const props = [];
|
|
401
788
|
if (builtin) {
|
|
789
|
+
if (node.name === "Form" && !node.props.some((p) => p.key === "onSubmit")) {
|
|
790
|
+
fail("Form needs onSubmit, like Form onSubmit: saveProfile { ... }", node);
|
|
791
|
+
}
|
|
792
|
+
if (node.name === "SubmitButton" && node.props.some((p) => ["onTap", "onClick", "type"].includes(p.key))) {
|
|
793
|
+
fail("SubmitButton runs the Form's onSubmit. Put your action on Form onSubmit instead", node);
|
|
794
|
+
}
|
|
402
795
|
for (const [key, value] of Object.entries(builtin.props || {})) {
|
|
403
796
|
props.push(`${key}: ${JSON.stringify(value)}`);
|
|
404
797
|
}
|
|
@@ -408,24 +801,65 @@ class Generator {
|
|
|
408
801
|
else if (builtin.arg) props.push(`${builtin.arg}: ${arg}`);
|
|
409
802
|
else fail(`${node.name} doesn't take an unnamed argument. Use name: value`, node.arg);
|
|
410
803
|
}
|
|
411
|
-
|
|
804
|
+
const bind = node.props.find((p) => p.key === "bind");
|
|
805
|
+
if (bind) {
|
|
806
|
+
if (node.name !== "Input" && node.name !== "TextArea" && node.name !== "DatePicker") fail("Use bind on Input, TextArea, Checkbox, Switch or Select", bind);
|
|
807
|
+
if (node.props.filter((p) => p.key === "bind").length > 1 || node.props.some((p) => ["value", "onChange", "onInput"].includes(p.key))) {
|
|
808
|
+
fail("bind supplies value and onChange. Remove the duplicate binding or event", bind);
|
|
809
|
+
}
|
|
810
|
+
if (bind.value.kind !== "Expr") fail("bind needs state, like bind: name or bind: user.name", bind);
|
|
811
|
+
const target = bind.value.expr;
|
|
812
|
+
const root = rootIdent(target);
|
|
813
|
+
if (!root || !["state", "imported"].includes(scope.lookup(root.name))) {
|
|
814
|
+
fail("bind needs changing state. Declare the value with state first", bind);
|
|
815
|
+
}
|
|
816
|
+
if (target.kind !== "Ident" && !((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident")) {
|
|
817
|
+
fail("bind supports a state name or one field, like bind: user.name", bind);
|
|
818
|
+
}
|
|
819
|
+
const key = target.kind === "Member" ? JSON.stringify(target.property) : target.kind === "Index" ? this.expr(target.index, scope) : "undefined";
|
|
820
|
+
props.push(`value: ${this.expr(target, scope)}`);
|
|
821
|
+
props.push(`onInput: __o.bindInput(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`);
|
|
822
|
+
}
|
|
823
|
+
for (const prop of node.props) {
|
|
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));
|
|
841
|
+
}
|
|
842
|
+
if (node.name === "Image") props.push(`loading: "lazy"`, `decoding: "async"`);
|
|
412
843
|
} else {
|
|
413
844
|
if (node.arg) fail(`${node.name} needs named arguments, like ${node.name} title: "..."`, node.arg);
|
|
414
845
|
const params = userComponent;
|
|
415
846
|
for (const prop of node.props) {
|
|
416
|
-
if (params && !params.includes(prop.key)) {
|
|
847
|
+
if (params && !params.includes(prop.key) && prop.key !== "key") {
|
|
417
848
|
fail(`${node.name} has no parameter "${prop.key}". It takes: ${params.join(", ") || "nothing"}`, prop);
|
|
418
849
|
}
|
|
419
850
|
if (prop.value.kind === "Handler") {
|
|
420
|
-
props.push(`${
|
|
851
|
+
props.push(`${prop.key}: ${this.handler(prop.value.body, scope)}`);
|
|
421
852
|
} else {
|
|
422
|
-
props.push(`${
|
|
853
|
+
props.push(`${prop.key}: ${this.expr(prop.value.expr, scope)}`);
|
|
423
854
|
}
|
|
424
855
|
}
|
|
425
856
|
}
|
|
426
857
|
for (const child of node.children) children.push(this.view(child, scope));
|
|
427
858
|
if (!builtin) {
|
|
428
|
-
return
|
|
859
|
+
return `__h(${node.name}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
860
|
+
}
|
|
861
|
+
if (node.name === "Form") {
|
|
862
|
+
return `__o.createForm(__forms, ${this.formId++}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
429
863
|
}
|
|
430
864
|
return `__h(${JSON.stringify(builtin.tag)}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
431
865
|
}
|
|
@@ -439,28 +873,28 @@ class Generator {
|
|
|
439
873
|
if (!event) {
|
|
440
874
|
fail(`Unknown event "${prop.key}". Available: ${Object.keys(EVENTS).join(", ")}`, prop);
|
|
441
875
|
}
|
|
876
|
+
const isCallableRef = prop.value.kind === "Expr" && (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member" || prop.value.expr.kind === "Lambda");
|
|
442
877
|
if (prop.key === "onChange") {
|
|
443
|
-
const call = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope, ["value"]) : this.expr(prop.value.expr, scope);
|
|
444
|
-
return `${event}: (e) => (${call})(e.target.value)`;
|
|
445
|
-
}
|
|
446
|
-
let handler;
|
|
447
|
-
if (prop.value.kind === "Handler") {
|
|
448
|
-
handler = this.handler(prop.value.body, scope);
|
|
449
|
-
} else if (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member") {
|
|
450
|
-
handler = this.expr(prop.value.expr, scope);
|
|
451
|
-
} else if (this.stateListCall(prop.value.expr, scope)) {
|
|
452
|
-
handler = `() => ${this.stateListCall(prop.value.expr, scope)}`;
|
|
453
|
-
} else {
|
|
454
|
-
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})`;
|
|
455
880
|
}
|
|
456
|
-
|
|
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}`;
|
|
457
892
|
}
|
|
458
893
|
handler(body, scope, params = []) {
|
|
459
|
-
const inner = new Scope(scope);
|
|
894
|
+
const inner = new Scope(scope, true, true);
|
|
460
895
|
params.forEach((p) => inner.declare(p, "local", body[0] ?? { line: 0, col: 0 }));
|
|
461
896
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
462
|
-
|
|
463
|
-
return `${isAsync ? "async " : ""}(${params.join(", ")}) => {
|
|
897
|
+
return `${inner.hasAwait ? "async " : ""}(${params.join(", ")}) => {
|
|
464
898
|
${lines.join("\n")}
|
|
465
899
|
}`;
|
|
466
900
|
}
|