@yannosay/sinth 3.1.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/browser/compile.js +3 -2
- package/dist/cli.js +62 -59
- package/dist/core/cli-compiler.js +178 -28
- package/dist/core/compiler.js +163 -68
- package/dist/core/expr.js +9 -2
- package/dist/core/lexer.js +3 -7
- package/dist/core/parser.js +191 -215
- package/dist/core/runtime/render.js +1 -1
- package/dist/server.js +9 -3
- package/dist/update-check.js +6 -2
- package/dist/utils.js +15 -8
- package/package.json +10 -3
package/dist/core/compiler.js
CHANGED
|
@@ -113,6 +113,7 @@ function inlineFunctionBody(fnDef, args, ctx) {
|
|
|
113
113
|
statements.push((0, expr_1.compileIfToJS)(substituted, ctx.loopVars, ctx.namespace, ctx.declaredVars));
|
|
114
114
|
}
|
|
115
115
|
else if (substituted.kind === "return") {
|
|
116
|
+
void 0;
|
|
116
117
|
}
|
|
117
118
|
else if (substituted.kind === "for") {
|
|
118
119
|
const fl = substituted;
|
|
@@ -186,7 +187,7 @@ function inlineFunctionBody(fnDef, args, ctx) {
|
|
|
186
187
|
}
|
|
187
188
|
}
|
|
188
189
|
else {
|
|
189
|
-
const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]", obj: "{}" };
|
|
190
|
+
const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]", obj: "{}" };
|
|
190
191
|
initJS = defaults[vd.varType] ?? "undefined";
|
|
191
192
|
}
|
|
192
193
|
statements.push(`let ${vd.name} = ${initJS};`);
|
|
@@ -230,6 +231,18 @@ function emitCEAction(ctx, eventName, handlerBody) {
|
|
|
230
231
|
ctx.ceActionHandlers.push(`function(e, host) { ${handlerBody} }`);
|
|
231
232
|
return `data-sinth-ce-${eventName}="${idx}"`;
|
|
232
233
|
}
|
|
234
|
+
function resolveExprWithParams(expr, ctx, paramMap) {
|
|
235
|
+
const paramNames = new Set(paramMap.keys());
|
|
236
|
+
const combinedVars = new Set();
|
|
237
|
+
if (ctx.declaredVars) {
|
|
238
|
+
for (const v of ctx.declaredVars)
|
|
239
|
+
combinedVars.add(v);
|
|
240
|
+
}
|
|
241
|
+
for (const p of paramNames) {
|
|
242
|
+
combinedVars.add(p);
|
|
243
|
+
}
|
|
244
|
+
return (0, expr_1.compileExprToJS)(expr, ctx.loopVars, ctx.namespace, combinedVars);
|
|
245
|
+
}
|
|
233
246
|
function renderAttr(attr, paramMap, ctx) {
|
|
234
247
|
const { name, value } = attr;
|
|
235
248
|
const renderFn = ctx.namespace ? "sinthRender_" + ctx.namespace : "sinthRender";
|
|
@@ -247,7 +260,7 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
247
260
|
if (ctx.namespace)
|
|
248
261
|
vName = ctx.namespace + "_" + vName;
|
|
249
262
|
const varDecl = ctx?.varDecls?.find(v => v.name === vName);
|
|
250
|
-
const rhs = (varDecl && varDecl.varType === "int")
|
|
263
|
+
const rhs = (varDecl && (varDecl.varType === "int" || varDecl.varType === "num"))
|
|
251
264
|
? `Number(e.target.value) || 0`
|
|
252
265
|
: `e.target.value`;
|
|
253
266
|
const renderCall = ctx.scopeVar ? `${ctx.scopeVar}._render()` : renderFn + "()";
|
|
@@ -268,7 +281,9 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
268
281
|
return `data-sinth-step="${stepExprId}"`;
|
|
269
282
|
}
|
|
270
283
|
}
|
|
271
|
-
catch {
|
|
284
|
+
catch {
|
|
285
|
+
void 0;
|
|
286
|
+
}
|
|
272
287
|
}
|
|
273
288
|
}
|
|
274
289
|
if (name === "delay") {
|
|
@@ -284,7 +299,9 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
284
299
|
const id = registerExpr(ctx, expr);
|
|
285
300
|
return `data-sinth-delay-expr-id="${id}"`;
|
|
286
301
|
}
|
|
287
|
-
catch {
|
|
302
|
+
catch {
|
|
303
|
+
void 0;
|
|
304
|
+
}
|
|
288
305
|
}
|
|
289
306
|
const id = registerExpr(ctx, { kind: "variable", name: v });
|
|
290
307
|
return `data-sinth-delay-expr-id="${id}"`;
|
|
@@ -292,9 +309,6 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
292
309
|
if (name === "hide") {
|
|
293
310
|
if (value === null)
|
|
294
311
|
return `data-sinth-hide=""`;
|
|
295
|
-
if (value && value.kind === "bool") {
|
|
296
|
-
return value.value ? `data-sinth-hide=""` : "";
|
|
297
|
-
}
|
|
298
312
|
if (value?.kind === "str") {
|
|
299
313
|
const v = value.value;
|
|
300
314
|
if (v === "true")
|
|
@@ -340,14 +354,16 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
340
354
|
return `data-sinth-fullscreen-sync="${(0, utils_1.escAttr)(expr.name)}"`;
|
|
341
355
|
}
|
|
342
356
|
}
|
|
343
|
-
catch {
|
|
357
|
+
catch {
|
|
358
|
+
void 0;
|
|
359
|
+
}
|
|
344
360
|
}
|
|
345
361
|
return `data-sinth-fullscreen-sync="${(0, utils_1.escAttr)(raw)}"`;
|
|
346
362
|
}
|
|
347
363
|
return "";
|
|
348
364
|
}
|
|
349
365
|
if (name === "checked") {
|
|
350
|
-
return
|
|
366
|
+
return "";
|
|
351
367
|
}
|
|
352
368
|
if (value.kind === "num")
|
|
353
369
|
return `${name}="${value.value}"`;
|
|
@@ -392,12 +408,17 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
392
408
|
return `${ev}="(function(){ ${jsExprs.replace(/"/g, """)}${bumps}; ${renderFn}(); })()"`;
|
|
393
409
|
return `${name}="${(0, utils_1.escAttr)(jsExprs)}"`;
|
|
394
410
|
}
|
|
395
|
-
catch {
|
|
411
|
+
catch {
|
|
412
|
+
void 0;
|
|
413
|
+
}
|
|
396
414
|
}
|
|
397
415
|
if (raw.startsWith("__EXPR__")) {
|
|
398
416
|
const exprJson = raw.substring("__EXPR__".length);
|
|
399
417
|
try {
|
|
400
|
-
|
|
418
|
+
let expr = JSON.parse(exprJson);
|
|
419
|
+
if (paramMap.size > 0) {
|
|
420
|
+
expr = substituteParamsInExpr(expr, paramMap);
|
|
421
|
+
}
|
|
401
422
|
const ev = eventAttrName(name);
|
|
402
423
|
if (ev && expr.kind === "call" && expr.callee?.kind === "variable") {
|
|
403
424
|
const fnName = expr.callee.name;
|
|
@@ -410,9 +431,17 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
410
431
|
}
|
|
411
432
|
}
|
|
412
433
|
}
|
|
413
|
-
let jsExpr = (
|
|
414
|
-
if (ctx.namespace &&
|
|
415
|
-
|
|
434
|
+
let jsExpr = resolveExprWithParams(expr, ctx, paramMap);
|
|
435
|
+
if (ctx.namespace && !ctx.scopePrefix) {
|
|
436
|
+
const combinedVars = new Set();
|
|
437
|
+
if (ctx.declaredVars) {
|
|
438
|
+
for (const v of ctx.declaredVars)
|
|
439
|
+
combinedVars.add(v);
|
|
440
|
+
}
|
|
441
|
+
for (const p of paramMap.keys()) {
|
|
442
|
+
combinedVars.add(p);
|
|
443
|
+
}
|
|
444
|
+
for (const v of combinedVars) {
|
|
416
445
|
const regex = new RegExp(`\\b${v}\\b`, 'g');
|
|
417
446
|
jsExpr = jsExpr.replace(regex, ctx.namespace + '_' + v);
|
|
418
447
|
}
|
|
@@ -442,7 +471,9 @@ function renderAttr(attr, paramMap, ctx) {
|
|
|
442
471
|
}
|
|
443
472
|
return `${name}="${(0, utils_1.escAttr)(jsExpr)}"`;
|
|
444
473
|
}
|
|
445
|
-
catch {
|
|
474
|
+
catch {
|
|
475
|
+
void 0;
|
|
476
|
+
}
|
|
446
477
|
}
|
|
447
478
|
raw = (0, utils_1.interpolateAttr)(raw, paramMap);
|
|
448
479
|
const ev = eventAttrName(name);
|
|
@@ -518,6 +549,9 @@ function renderChild(child, ctx, params, depth) {
|
|
|
518
549
|
case "expr": {
|
|
519
550
|
if (!child.expression)
|
|
520
551
|
return "";
|
|
552
|
+
if (params.size > 0) {
|
|
553
|
+
child = { ...child, expression: substituteParamsInExpr(child.expression, params) };
|
|
554
|
+
}
|
|
521
555
|
if (child.expression.kind === "variable" && child.expression.name && params.has(child.expression.name)) {
|
|
522
556
|
const val = params.get(child.expression.name);
|
|
523
557
|
if (val.startsWith("__LIT__")) {
|
|
@@ -551,6 +585,13 @@ function renderChild(child, ctx, params, depth) {
|
|
|
551
585
|
ctx.logicBlocks.push((0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars) + ";");
|
|
552
586
|
return "";
|
|
553
587
|
}
|
|
588
|
+
const fnDef = fnName ? ctx.functionDefs.find(f => f.name === fnName) : undefined;
|
|
589
|
+
if (!fnDef) {
|
|
590
|
+
if (!ctx.initBlocks)
|
|
591
|
+
ctx.initBlocks = [];
|
|
592
|
+
ctx.initBlocks.push(`if(!_memo_${fnName}_done){${(0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars)};_memo_${fnName}_done=true;}`);
|
|
593
|
+
return "";
|
|
594
|
+
}
|
|
554
595
|
const js = (0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars);
|
|
555
596
|
ctx.logicBlocks.push(`if(!_memo_${fnName}_done){_memo_${fnName}=${js};_memo_${fnName}_done=true;}`);
|
|
556
597
|
const exprId = registerExpr(ctx, { kind: "variable", name: `_memo_${fnName}` });
|
|
@@ -571,16 +612,49 @@ function renderChild(child, ctx, params, depth) {
|
|
|
571
612
|
const callArgs = child.expression.args ?? [];
|
|
572
613
|
const localParams = new Map();
|
|
573
614
|
for (let i = 0; i < fnDef.params.length && i < callArgs.length; i++) {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
615
|
+
localParams.set(fnDef.params[i].name, "__EXPR__" + JSON.stringify(callArgs[i]));
|
|
616
|
+
}
|
|
617
|
+
if (!ctx.fnCallCounters)
|
|
618
|
+
ctx.fnCallCounters = new Map();
|
|
619
|
+
const fnCallIdx = (ctx.fnCallCounters.get(fnName) ?? 0) + 1;
|
|
620
|
+
ctx.fnCallCounters.set(fnName, fnCallIdx);
|
|
621
|
+
const scopePrefix = `_fn_${fnName}_${fnCallIdx}_`;
|
|
622
|
+
for (const bodyChild of fnDef.body) {
|
|
623
|
+
if (bodyChild.kind === "var") {
|
|
624
|
+
const vd = bodyChild;
|
|
625
|
+
const scopedName = scopePrefix + vd.name;
|
|
626
|
+
localParams.set(vd.name, "__VAR__" + scopedName);
|
|
627
|
+
if (!ctx.varDecls)
|
|
628
|
+
ctx.varDecls = [];
|
|
629
|
+
ctx.varDecls.push({
|
|
630
|
+
kind: "var",
|
|
631
|
+
name: scopedName,
|
|
632
|
+
varType: vd.varType,
|
|
633
|
+
value: vd.value,
|
|
634
|
+
loc: vd.loc,
|
|
635
|
+
});
|
|
636
|
+
if (ctx.declaredVars)
|
|
637
|
+
ctx.declaredVars.add(scopedName);
|
|
580
638
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
639
|
+
}
|
|
640
|
+
for (let i = 0; i < fnDef.params.length && i < callArgs.length; i++) {
|
|
641
|
+
const param = fnDef.params[i];
|
|
642
|
+
const argExpr = callArgs[i];
|
|
643
|
+
const isSimpleVar = argExpr && argExpr.kind === "variable" && argExpr.name && !argExpr.name.includes(".");
|
|
644
|
+
if (!isSimpleVar && argExpr) {
|
|
645
|
+
const scopedName = scopePrefix + param.name;
|
|
646
|
+
localParams.set(param.name, "__VAR__" + scopedName);
|
|
647
|
+
if (!ctx.varDecls)
|
|
648
|
+
ctx.varDecls = [];
|
|
649
|
+
ctx.varDecls.push({
|
|
650
|
+
kind: "var",
|
|
651
|
+
name: scopedName,
|
|
652
|
+
varType: param.paramType || "num",
|
|
653
|
+
value: { kind: "str", value: "__EXPR__" + JSON.stringify(argExpr) },
|
|
654
|
+
loc: param.loc,
|
|
655
|
+
});
|
|
656
|
+
if (ctx.declaredVars)
|
|
657
|
+
ctx.declaredVars.add(scopedName);
|
|
584
658
|
}
|
|
585
659
|
}
|
|
586
660
|
const substituteExpr = (expr, pm) => {
|
|
@@ -592,6 +666,14 @@ function renderChild(child, ctx, params, depth) {
|
|
|
592
666
|
const id = parseInt(pv.slice(10), 10);
|
|
593
667
|
return { kind: "expr_ref", exprId: id };
|
|
594
668
|
}
|
|
669
|
+
if (pv.startsWith("__EXPR__")) {
|
|
670
|
+
try {
|
|
671
|
+
return JSON.parse(pv.substring(8));
|
|
672
|
+
}
|
|
673
|
+
catch {
|
|
674
|
+
return expr;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
595
677
|
return { kind: "variable", name: pv.slice(7) };
|
|
596
678
|
}
|
|
597
679
|
if (expr.kind === "binary")
|
|
@@ -609,6 +691,17 @@ function renderChild(child, ctx, params, depth) {
|
|
|
609
691
|
if (pv.startsWith("__VAR__")) {
|
|
610
692
|
return { ...expr, target: pv.slice(7), right: expr.right ? substituteExpr(expr.right, pm) : undefined };
|
|
611
693
|
}
|
|
694
|
+
if (pv.startsWith("__EXPR__")) {
|
|
695
|
+
try {
|
|
696
|
+
const argExpr = JSON.parse(pv.substring(8));
|
|
697
|
+
if (argExpr.kind === "variable" && argExpr.name) {
|
|
698
|
+
return { ...expr, target: argExpr.name, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
catch {
|
|
702
|
+
void 0;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
612
705
|
}
|
|
613
706
|
return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
|
|
614
707
|
}
|
|
@@ -640,7 +733,9 @@ function renderChild(child, ctx, params, depth) {
|
|
|
640
733
|
const substituted = substituteExpr(expr, pm);
|
|
641
734
|
return { ...a, value: { kind: "str", value: "__EXPR__" + JSON.stringify(substituted) } };
|
|
642
735
|
}
|
|
643
|
-
catch {
|
|
736
|
+
catch {
|
|
737
|
+
void 0;
|
|
738
|
+
}
|
|
644
739
|
}
|
|
645
740
|
if (raw.startsWith("__ACTION_JS__")) {
|
|
646
741
|
return { ...a, value: { kind: "str", value: raw } };
|
|
@@ -651,7 +746,9 @@ function renderChild(child, ctx, params, depth) {
|
|
|
651
746
|
const substituted = exprs.map(e => substituteExpr(e, pm));
|
|
652
747
|
return { ...a, value: { kind: "str", value: "__MULTI_EXPR__" + JSON.stringify(substituted) } };
|
|
653
748
|
}
|
|
654
|
-
catch {
|
|
749
|
+
catch {
|
|
750
|
+
void 0;
|
|
751
|
+
}
|
|
655
752
|
}
|
|
656
753
|
for (const [pn, pv] of pm) {
|
|
657
754
|
if (pv.startsWith("__LIT__"))
|
|
@@ -724,11 +821,15 @@ function renderChild(child, ctx, params, depth) {
|
|
|
724
821
|
function renderIfBlock(ifBlock, ctx, params, depth) {
|
|
725
822
|
if (depth > 64)
|
|
726
823
|
throw new types_1.SinthError("Maximum if-block nesting depth (64) exceeded.", ifBlock.loc);
|
|
727
|
-
const condJS = (0, expr_1.compileExprToJS)(ifBlock.condition, undefined, ctx.namespace, ctx.declaredVars);
|
|
728
824
|
const allChildren = [...ifBlock.body, ...(ifBlock.elseBody ?? [])];
|
|
729
825
|
const hasAssign = allChildren.some(c => c.kind === "assign_stmt");
|
|
826
|
+
const hasExprAssign = allChildren.some(c => c.kind === "expr" && c.expression?.kind === "assign");
|
|
730
827
|
const hasComp = allChildren.some(c => c.kind === "use");
|
|
731
|
-
if (!
|
|
828
|
+
if (!hasComp) {
|
|
829
|
+
ctx.logicBlocks.push((0, expr_1.compileIfToJS)(ifBlock, undefined, ctx.namespace, ctx.declaredVars));
|
|
830
|
+
return "";
|
|
831
|
+
}
|
|
832
|
+
if (!hasAssign && !hasExprAssign && !hasComp) {
|
|
732
833
|
const tplId = ctx.ifIdCounter++;
|
|
733
834
|
const condId = registerExpr(ctx, ifBlock.condition);
|
|
734
835
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
@@ -737,11 +838,6 @@ function renderIfBlock(ifBlock, ctx, params, depth) {
|
|
|
737
838
|
return (`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${persistAttr}>${bodyHTML}</template>` +
|
|
738
839
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : ""));
|
|
739
840
|
}
|
|
740
|
-
// pure logic
|
|
741
|
-
if (hasAssign && !hasComp) {
|
|
742
|
-
ctx.logicBlocks.push((0, expr_1.compileIfToJS)(ifBlock));
|
|
743
|
-
return "";
|
|
744
|
-
}
|
|
745
841
|
// pure DOM (components need replace/persist/delay handling)
|
|
746
842
|
if (!hasAssign && hasComp) {
|
|
747
843
|
const tplId = ctx.ifIdCounter++;
|
|
@@ -768,7 +864,7 @@ function renderIfBlock(ifBlock, ctx, params, depth) {
|
|
|
768
864
|
return true;
|
|
769
865
|
return u.attrs.some(a => a.name === "bind" || a.name === "model" || a.name === "fullscreenSync");
|
|
770
866
|
});
|
|
771
|
-
|
|
867
|
+
const persistAttr = (ifBlock.persist || hasStatefulComp) ? ` data-sinth-if-persist="true"` : "";
|
|
772
868
|
const firstComp = ifBlock.body.find(c => c.kind === "use");
|
|
773
869
|
if (firstComp) {
|
|
774
870
|
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
@@ -1041,21 +1137,23 @@ function renderCompUse(use, ctx, params, depth) {
|
|
|
1041
1137
|
if (expr.kind === "variable" && expr.name)
|
|
1042
1138
|
vName = expr.name;
|
|
1043
1139
|
}
|
|
1044
|
-
catch {
|
|
1140
|
+
catch {
|
|
1141
|
+
void 0;
|
|
1142
|
+
}
|
|
1045
1143
|
}
|
|
1046
1144
|
const varDecl = ctx.varDecls?.find(v => v.name === vName);
|
|
1047
1145
|
if (!use.attrs.some(a => a.name === "type")) {
|
|
1048
|
-
if (varDecl && varDecl.varType === "int") {
|
|
1146
|
+
if (varDecl && (varDecl.varType === "int" || varDecl.varType === "num")) {
|
|
1049
1147
|
attrParts.push(`type="number"`);
|
|
1050
1148
|
}
|
|
1051
1149
|
else {
|
|
1052
1150
|
attrParts.push(`type="text"`);
|
|
1053
1151
|
}
|
|
1054
1152
|
}
|
|
1055
|
-
if (varDecl && varDecl.varType === "int" && !use.attrs.some(a => a.name === "step")) {
|
|
1153
|
+
if (varDecl && (varDecl.varType === "int" || varDecl.varType === "num") && !use.attrs.some(a => a.name === "step")) {
|
|
1056
1154
|
attrParts.push(`step="1"`);
|
|
1057
1155
|
}
|
|
1058
|
-
const rhs = (varDecl && varDecl.varType === "int")
|
|
1156
|
+
const rhs = (varDecl && (varDecl.varType === "int" || varDecl.varType === "num"))
|
|
1059
1157
|
? `Number(e.target.value) || 0`
|
|
1060
1158
|
: `e.target.value`;
|
|
1061
1159
|
const initialVal = varDecl?.value ? (0, utils_1.litToString)(varDecl.value).replace(/"/g, '"') : "";
|
|
@@ -1086,7 +1184,7 @@ function renderCompUse(use, ctx, params, depth) {
|
|
|
1086
1184
|
// Resolve bound variable name (same pattern as Input's bind/model)
|
|
1087
1185
|
let boundVarName = null;
|
|
1088
1186
|
if (checkedAttr && checkedAttr.value?.kind === "str") {
|
|
1089
|
-
|
|
1187
|
+
const raw = checkedAttr.value.value;
|
|
1090
1188
|
if (raw.startsWith("__EXPR__")) {
|
|
1091
1189
|
try {
|
|
1092
1190
|
const expr = JSON.parse(raw.substring(8));
|
|
@@ -1094,7 +1192,9 @@ function renderCompUse(use, ctx, params, depth) {
|
|
|
1094
1192
|
boundVarName = expr.name;
|
|
1095
1193
|
}
|
|
1096
1194
|
}
|
|
1097
|
-
catch {
|
|
1195
|
+
catch {
|
|
1196
|
+
void 0;
|
|
1197
|
+
}
|
|
1098
1198
|
}
|
|
1099
1199
|
else if (raw.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
1100
1200
|
boundVarName = raw;
|
|
@@ -1117,14 +1217,16 @@ function renderCompUse(use, ctx, params, depth) {
|
|
|
1117
1217
|
}
|
|
1118
1218
|
}
|
|
1119
1219
|
if (onChangeAttr && onChangeAttr.value?.kind === "str") {
|
|
1120
|
-
|
|
1220
|
+
const raw = onChangeAttr.value.value;
|
|
1121
1221
|
if (raw.startsWith("__EXPR__")) {
|
|
1122
1222
|
try {
|
|
1123
1223
|
const expr = JSON.parse(raw.substring(8));
|
|
1124
1224
|
const jsExpr = (0, expr_1.compileExprToJS)(expr, undefined, ctx.namespace, ctx.declaredVars);
|
|
1125
1225
|
attrParts.push(`onchange="(function(){ ${jsExpr}; ${renderFn}(); })(event)"`);
|
|
1126
1226
|
}
|
|
1127
|
-
catch {
|
|
1227
|
+
catch {
|
|
1228
|
+
void 0;
|
|
1229
|
+
}
|
|
1128
1230
|
}
|
|
1129
1231
|
else {
|
|
1130
1232
|
attrParts.push(`onchange="${(0, utils_1.escAttr)(raw)};${renderFn}()"`);
|
|
@@ -1218,7 +1320,7 @@ function extractFunctionNames(js) {
|
|
|
1218
1320
|
return names;
|
|
1219
1321
|
}
|
|
1220
1322
|
function buildRuntime(opts) {
|
|
1221
|
-
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry, functionsJS } = opts;
|
|
1323
|
+
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry, functionsJS, initBlocks } = opts;
|
|
1222
1324
|
const ns = opts.namespace ? "_" + opts.namespace : "";
|
|
1223
1325
|
const renderFn = "sinthRender" + ns;
|
|
1224
1326
|
const needsMixed = mixedBlocks.length > 0;
|
|
@@ -1235,10 +1337,10 @@ function buildRuntime(opts) {
|
|
|
1235
1337
|
const vn = ns ? ns.slice(1) + "_" + v.name : v.name;
|
|
1236
1338
|
if (!v.value) {
|
|
1237
1339
|
if (!assignedVars.has(v.name)) {
|
|
1238
|
-
const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
1340
|
+
const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]" };
|
|
1239
1341
|
types_1.SinthWarning.emit(`Variable '${v.name}' is declared but never assigned. Defaulting to ${defaults[v.varType] ?? "undefined"}.`, v.loc);
|
|
1240
1342
|
}
|
|
1241
|
-
const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
1343
|
+
const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]" };
|
|
1242
1344
|
return `var ${vn} = ${defaults[v.varType] ?? "undefined"};`;
|
|
1243
1345
|
}
|
|
1244
1346
|
const val = (0, utils_1.litToString)(v.value);
|
|
@@ -1282,8 +1384,16 @@ function buildRuntime(opts) {
|
|
|
1282
1384
|
if (matches)
|
|
1283
1385
|
matches.forEach(m => memoVars.add(m));
|
|
1284
1386
|
}
|
|
1387
|
+
for (const ib of (initBlocks || [])) {
|
|
1388
|
+
let matches = ib.match(/_memo_(\w+)/g);
|
|
1389
|
+
if (matches)
|
|
1390
|
+
matches.forEach(m => memoVars.add(m));
|
|
1391
|
+
matches = ib.match(/_memo_(\w+)_done/g);
|
|
1392
|
+
if (matches)
|
|
1393
|
+
matches.forEach(m => memoVars.add(m));
|
|
1394
|
+
}
|
|
1285
1395
|
for (const e of exprRegistry) {
|
|
1286
|
-
|
|
1396
|
+
const matches = e.match(/_memo_(\w+)/g);
|
|
1287
1397
|
if (matches)
|
|
1288
1398
|
matches.forEach(m => memoVars.add(m));
|
|
1289
1399
|
}
|
|
@@ -1293,26 +1403,9 @@ function buildRuntime(opts) {
|
|
|
1293
1403
|
return out ? `// Sinth Compiled Runtime\n${out}` : "";
|
|
1294
1404
|
}
|
|
1295
1405
|
const helpers = (0, helpers_1.generateHelpers)({ needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns });
|
|
1296
|
-
const exprVarUpdates = varDecls
|
|
1297
|
-
.filter(v => v.value && (0, utils_1.litToString)(v.value).startsWith("__EXPR__"))
|
|
1298
|
-
.map(v => {
|
|
1299
|
-
try {
|
|
1300
|
-
const expr = JSON.parse((0, utils_1.litToString)(v.value).substring(8));
|
|
1301
|
-
const js = (0, expr_1.compileExprToJS)(expr, undefined, opts.namespace, opts.declaredVars);
|
|
1302
|
-
const idx = exprRegistry.indexOf(js);
|
|
1303
|
-
const vn2 = ns ? ns.slice(1) + "_" + v.name : v.name;
|
|
1304
|
-
return `${vn2} = __X${ns}[${idx}]({});`;
|
|
1305
|
-
}
|
|
1306
|
-
catch {
|
|
1307
|
-
return "";
|
|
1308
|
-
}
|
|
1309
|
-
})
|
|
1310
|
-
.filter(Boolean)
|
|
1311
|
-
.join("\n ");
|
|
1312
1406
|
const renderBody = (0, render_1.buildRenderBody)({
|
|
1313
|
-
bodyHTML, logicBlocks, mixedBlocks,
|
|
1314
|
-
needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay,
|
|
1315
|
-
exprVarUpdates: exprVarUpdates || undefined,
|
|
1407
|
+
bodyHTML, logicBlocks: [], mixedBlocks,
|
|
1408
|
+
needsLogic: false, needsMixed, needsIf, needsFor, needsExpr, needsDelay,
|
|
1316
1409
|
ns
|
|
1317
1410
|
});
|
|
1318
1411
|
let forDataJS = "";
|
|
@@ -1337,8 +1430,9 @@ function buildRuntime(opts) {
|
|
|
1337
1430
|
? `let __X${ns} = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
1338
1431
|
: "";
|
|
1339
1432
|
let initLogicJS = "";
|
|
1340
|
-
|
|
1341
|
-
|
|
1433
|
+
const allInit = [...(opts.initLogic || []), ...(initBlocks || [])];
|
|
1434
|
+
if (allInit.length > 0) {
|
|
1435
|
+
let logic = allInit.join("\n");
|
|
1342
1436
|
if (opts.namespace && opts.declaredVars) {
|
|
1343
1437
|
for (const v of opts.declaredVars) {
|
|
1344
1438
|
const re = new RegExp(`\\b${v}\\b`, 'g');
|
|
@@ -1349,7 +1443,8 @@ function buildRuntime(opts) {
|
|
|
1349
1443
|
}
|
|
1350
1444
|
let renderFunc = '';
|
|
1351
1445
|
if (needsRender) {
|
|
1352
|
-
|
|
1446
|
+
const logicAtTop = logicBlocks.length > 0 ? logicBlocks.join("\n") + "\n" : "";
|
|
1447
|
+
renderFunc = `function ${renderFn}() {\n ${logicAtTop}${forSyncBlock}${renderBody}\n}\n${initLogicJS}${renderFn}();`;
|
|
1353
1448
|
}
|
|
1354
1449
|
if (needsFullscreenSync) {
|
|
1355
1450
|
renderFunc += `
|
package/dist/core/expr.js
CHANGED
|
@@ -46,7 +46,11 @@ function compileExprToJS(expr, loopVars, namespace, declaredVars) {
|
|
|
46
46
|
}
|
|
47
47
|
case "unary": {
|
|
48
48
|
const o = expr.op === "not" ? "!" : expr.op;
|
|
49
|
-
|
|
49
|
+
const operand = expr.operand;
|
|
50
|
+
if (operand.kind === "binary" && (operand.op === "+" || operand.op === "-" || operand.op === "*" || operand.op === "/" || operand.op === "%")) {
|
|
51
|
+
return `${o}(${compileExprToJS(operand, loopVars, namespace, declaredVars)})`;
|
|
52
|
+
}
|
|
53
|
+
return `${o}${compileExprToJS(operand, loopVars, namespace, declaredVars)}`;
|
|
50
54
|
}
|
|
51
55
|
case "assign": {
|
|
52
56
|
const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
|
|
@@ -74,7 +78,10 @@ function compileExprToJS(expr, loopVars, namespace, declaredVars) {
|
|
|
74
78
|
return `${t2}${expr.op}`;
|
|
75
79
|
}
|
|
76
80
|
case "call": {
|
|
77
|
-
|
|
81
|
+
let callee = compileExprToJS(expr.callee, loopVars, namespace, declaredVars);
|
|
82
|
+
if (expr.memo && callee.startsWith('$')) {
|
|
83
|
+
callee = callee.substring(1);
|
|
84
|
+
}
|
|
78
85
|
if (callee === "remove") {
|
|
79
86
|
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
|
|
80
87
|
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
package/dist/core/lexer.js
CHANGED
|
@@ -143,7 +143,7 @@ class Lexer {
|
|
|
143
143
|
tokens.push({ type: types_1.TT.OP_ARROW, value: "->", loc });
|
|
144
144
|
continue;
|
|
145
145
|
}
|
|
146
|
-
if (ch === "-"
|
|
146
|
+
if (ch === "-") {
|
|
147
147
|
tokens.push(this.single(types_1.TT.OP_MINUS, loc));
|
|
148
148
|
continue;
|
|
149
149
|
}
|
|
@@ -161,7 +161,7 @@ class Lexer {
|
|
|
161
161
|
tokens.push(this.readString(loc));
|
|
162
162
|
continue;
|
|
163
163
|
}
|
|
164
|
-
if (this.isDigit(ch)
|
|
164
|
+
if (this.isDigit(ch)) {
|
|
165
165
|
tokens.push(this.readNumber(loc));
|
|
166
166
|
continue;
|
|
167
167
|
}
|
|
@@ -365,10 +365,6 @@ class Lexer {
|
|
|
365
365
|
}
|
|
366
366
|
readNumber(loc) {
|
|
367
367
|
let s = "";
|
|
368
|
-
if (this.src[this.pos] === "-") {
|
|
369
|
-
s += "-";
|
|
370
|
-
this.adv();
|
|
371
|
-
}
|
|
372
368
|
while (this.isDigit(this.src[this.pos] ?? "")) {
|
|
373
369
|
s += this.src[this.pos];
|
|
374
370
|
this.adv();
|
|
@@ -409,6 +405,6 @@ class Lexer {
|
|
|
409
405
|
loc() { return { file: this.file, line: this.line, col: this.col }; }
|
|
410
406
|
isDigit(c) { return c >= "0" && c <= "9"; }
|
|
411
407
|
isIdentStart(c) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
|
|
412
|
-
isIdentCont(c) { return /[a-zA-Z0-9_
|
|
408
|
+
isIdentCont(c) { return /[a-zA-Z0-9_$\u00C0-\uFFFF]/.test(c); }
|
|
413
409
|
}
|
|
414
410
|
exports.Lexer = Lexer;
|