@yannosay/sinth 0.12.0 → 0.13.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/package.json +1 -1
- package/src/core/compiler.ts +70 -34
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import * as fs from "fs";
|
|
3
|
-
import { Loc, Literal, Expression, Child, Attr, CompUse, IfBlock, ForLoop, RemoveStmt, StyleBlock, CompDef, ParamDecl, VarDeclaration, SinthFile, CompileCtx, MixedBlockEntry, SinthError, SinthWarning, TT, AssignStmt, MetaEntry } from "./types.ts";
|
|
3
|
+
import { Loc, Literal, Expression, Child, Attr, CompUse, IfBlock, ForLoop, RemoveStmt, ReturnStmt, StyleBlock, CompDef, ParamDecl, VarDeclaration, SinthFile, CompileCtx, MixedBlockEntry, SinthError, SinthWarning, TT, AssignStmt, MetaEntry } from "./types.ts";
|
|
4
4
|
import { Parser } from "./parser.ts";
|
|
5
5
|
import { fnv1a, camelToKebab, esc, escAttr, litToString, tagNameToPascal } from "../utils.ts";
|
|
6
6
|
import { compileExprToJS, compileIfToJS, bodyToJS } from "./expr.ts";
|
|
@@ -637,15 +637,49 @@ export function renderChild(
|
|
|
637
637
|
localParams.set(fnDef.params[i].name, "__VAR__" + arg.name);
|
|
638
638
|
}
|
|
639
639
|
}
|
|
640
|
-
|
|
641
|
-
if (
|
|
642
|
-
|
|
640
|
+
const substituteExpr = (expr: Expression, pm: Map<string, string>): Expression => {
|
|
641
|
+
if (expr.kind === "variable" && pm.has(expr.name!)) {
|
|
642
|
+
const pv = pm.get(expr.name!)!;
|
|
643
|
+
if (pv.startsWith("__LIT__")) return { kind: "literal", value: { kind: "str", value: pv.slice(7) } };
|
|
644
|
+
return { kind: "variable", name: pv.slice(7) };
|
|
645
|
+
}
|
|
646
|
+
if (expr.kind === "binary") return { ...expr, left: substituteExpr(expr.left!, pm), right: substituteExpr(expr.right!, pm) };
|
|
647
|
+
if (expr.kind === "unary") return { ...expr, operand: substituteExpr(expr.operand!, pm) };
|
|
648
|
+
if (expr.kind === "call") return { ...expr, callee: substituteExpr(expr.callee!, pm), args: expr.args?.map(a => substituteExpr(a, pm)) };
|
|
649
|
+
if (expr.kind === "index") return { ...expr, object: substituteExpr(expr.object!, pm), key: substituteExpr(expr.key!, pm) };
|
|
650
|
+
if (expr.kind === "assign") return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
|
|
651
|
+
return expr;
|
|
652
|
+
};
|
|
653
|
+
const substituteChild = (c: Child, pm: Map<string, string>): Child => {
|
|
654
|
+
if (c.kind === "expr") return { ...c, expression: substituteExpr(c.expression, pm) };
|
|
655
|
+
if (c.kind === "assign_stmt") return { ...c, expression: substituteExpr(c.expression, pm) as Expression };
|
|
656
|
+
if (c.kind === "return" && (c as ReturnStmt).expression) return { ...c, expression: substituteExpr((c as ReturnStmt).expression!, pm) } as Child;
|
|
657
|
+
if (c.kind === "if") {
|
|
658
|
+
const ib = c as IfBlock;
|
|
659
|
+
return { ...ib, condition: substituteExpr(ib.condition, pm), body: ib.body.map(bc => substituteChild(bc, pm)), elseBody: ib.elseBody?.map(bc => substituteChild(bc, pm)) };
|
|
660
|
+
}
|
|
661
|
+
if (c.kind === "for") {
|
|
662
|
+
const fl = c as ForLoop;
|
|
663
|
+
return { ...fl, body: fl.body.map(bc => substituteChild(bc, pm)) };
|
|
643
664
|
}
|
|
644
665
|
if (c.kind === "use") {
|
|
645
|
-
|
|
666
|
+
const u = c as CompUse;
|
|
667
|
+
const sa: Attr[] = u.attrs.map(a => {
|
|
668
|
+
if (a.value?.kind === "str") {
|
|
669
|
+
let raw = a.value.value;
|
|
670
|
+
for (const [pn, pv] of pm) {
|
|
671
|
+
if (pv.startsWith("__LIT__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
|
|
672
|
+
if (pv.startsWith("__VAR__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
|
|
673
|
+
}
|
|
674
|
+
return { ...a, value: { kind: "str" as const, value: raw } };
|
|
675
|
+
}
|
|
676
|
+
return a;
|
|
677
|
+
});
|
|
678
|
+
return { ...u, attrs: sa, children: u.children.map(cc => substituteChild(cc, pm)) };
|
|
646
679
|
}
|
|
647
|
-
return
|
|
648
|
-
}
|
|
680
|
+
return c;
|
|
681
|
+
};
|
|
682
|
+
return fnDef.body.map(c => renderChild(substituteChild(c, localParams), ctx, localParams, depth + 1)).join("");
|
|
649
683
|
}
|
|
650
684
|
}
|
|
651
685
|
if (child.expression.kind === "literal" && child.expression.value) {
|
|
@@ -1124,8 +1158,7 @@ function compileFunctionDef(fn: FunctionDef, ctx: CompileCtx): string {
|
|
|
1124
1158
|
const paramsJS = fn.params.map(p => p.name).join(", ");
|
|
1125
1159
|
|
|
1126
1160
|
if (fn.returnType === "ui") {
|
|
1127
|
-
|
|
1128
|
-
return `function ${fn.name}(${paramsJS}) {\n return [${childHTMLs.map(h => JSON.stringify(h)).join(", ")}];\n}`;
|
|
1161
|
+
return "";
|
|
1129
1162
|
}
|
|
1130
1163
|
|
|
1131
1164
|
const bodyStatements: string[] = [];
|
|
@@ -1243,7 +1276,7 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
|
1243
1276
|
frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
1244
1277
|
if (${needsDelay}) {
|
|
1245
1278
|
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
1246
|
-
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
1279
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
1247
1280
|
}
|
|
1248
1281
|
var fragFirst = frag.firstChild;
|
|
1249
1282
|
var fragLast = frag.lastChild;
|
|
@@ -1270,15 +1303,24 @@ function sinthDelay(el) {
|
|
|
1270
1303
|
el.dataset.sinthDelayDone = '1';
|
|
1271
1304
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
1272
1305
|
el.style.display = 'none';
|
|
1273
|
-
|
|
1274
|
-
|
|
1306
|
+
var show = function() {
|
|
1307
|
+
el.style.display = '';
|
|
1308
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
1309
|
+
};
|
|
1310
|
+
if (ms > 0) setTimeout(show, ms);
|
|
1311
|
+
else show();
|
|
1275
1312
|
}
|
|
1276
|
-
function sinthDelayExpr(el) {
|
|
1313
|
+
function sinthDelayExpr(el, _ctx) {
|
|
1314
|
+
_ctx = _ctx || {};
|
|
1277
1315
|
try {
|
|
1278
1316
|
var fn = __X[el.dataset.sinthDelayExprId];
|
|
1279
|
-
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
1280
|
-
|
|
1281
|
-
|
|
1317
|
+
var ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
|
|
1318
|
+
var show = function() {
|
|
1319
|
+
el.style.display = '';
|
|
1320
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
1321
|
+
};
|
|
1322
|
+
if (ms > 0) setTimeout(show, ms);
|
|
1323
|
+
else show();
|
|
1282
1324
|
} catch(e) {}
|
|
1283
1325
|
}
|
|
1284
1326
|
`;
|
|
@@ -1337,7 +1379,7 @@ function sinthIfBlock(t) {
|
|
|
1337
1379
|
ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
1338
1380
|
if (${needsDelay}) {
|
|
1339
1381
|
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
1340
|
-
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
1382
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
1341
1383
|
}
|
|
1342
1384
|
t.parentNode.insertBefore(ef, t);
|
|
1343
1385
|
} else {
|
|
@@ -1403,33 +1445,28 @@ function sinthForBlock(t) {
|
|
|
1403
1445
|
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
1404
1446
|
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
1405
1447
|
}
|
|
1448
|
+
var loopCtx = {};
|
|
1449
|
+
if (_item) loopCtx[_item] = _v;
|
|
1450
|
+
if (_key) loopCtx[_key] = _k;
|
|
1451
|
+
if (_idx) loopCtx[_idx] = _loopIdx - 1;
|
|
1406
1452
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
1407
1453
|
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
1408
1454
|
try {
|
|
1409
1455
|
var exprFn = __X[el.dataset.exprId];
|
|
1410
|
-
if (
|
|
1411
|
-
var _ctx = {};
|
|
1412
|
-
if (_item) _ctx[_item] = _v;
|
|
1413
|
-
if (_key) _ctx[_key] = _k;
|
|
1414
|
-
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
1415
|
-
el.textContent = exprFn(_ctx);
|
|
1416
|
-
el.classList.remove('sinth-expr');
|
|
1456
|
+
if (exprFn) el.textContent = exprFn(loopCtx);
|
|
1417
1457
|
} catch(e) {}
|
|
1458
|
+
el.classList.remove('sinth-expr');
|
|
1418
1459
|
});
|
|
1419
1460
|
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
1420
1461
|
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
1421
|
-
var _ctx = {};
|
|
1422
|
-
if (_item) _ctx[_item] = _v;
|
|
1423
|
-
if (_key) _ctx[_key] = _k;
|
|
1424
|
-
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
1425
1462
|
var cond = false;
|
|
1426
|
-
try { if (condFn) cond = condFn(
|
|
1463
|
+
try { if (condFn) cond = condFn(loopCtx); } catch(e) {}
|
|
1427
1464
|
if (cond) {
|
|
1428
1465
|
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
1429
1466
|
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
1430
1467
|
try {
|
|
1431
1468
|
var exprFn2 = __X[el2.dataset.exprId];
|
|
1432
|
-
if (exprFn2) el2.textContent = exprFn2(
|
|
1469
|
+
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
1433
1470
|
} catch(e) {}
|
|
1434
1471
|
});
|
|
1435
1472
|
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
@@ -1440,7 +1477,7 @@ function sinthForBlock(t) {
|
|
|
1440
1477
|
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
1441
1478
|
try {
|
|
1442
1479
|
var exprFn2 = __X[el2.dataset.exprId];
|
|
1443
|
-
if (exprFn2) el2.textContent = exprFn2(
|
|
1480
|
+
if (exprFn2) el2.textContent = exprFn2(loopCtx);
|
|
1444
1481
|
} catch(e) {}
|
|
1445
1482
|
});
|
|
1446
1483
|
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
@@ -1449,7 +1486,7 @@ function sinthForBlock(t) {
|
|
|
1449
1486
|
});
|
|
1450
1487
|
if (${needsDelay}) {
|
|
1451
1488
|
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
1452
|
-
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
1489
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, loopCtx); });
|
|
1453
1490
|
}
|
|
1454
1491
|
t.parentNode && t.parentNode.insertBefore(frag, t);
|
|
1455
1492
|
});
|
|
@@ -1488,7 +1525,7 @@ function sinthForBlock(t) {
|
|
|
1488
1525
|
__el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
1489
1526
|
__el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
|
|
1490
1527
|
${needsDelay ? `__el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
1491
|
-
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);` : ""}
|
|
1528
|
+
__el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
|
|
1492
1529
|
}
|
|
1493
1530
|
})();\n`;
|
|
1494
1531
|
}
|
|
@@ -1525,7 +1562,6 @@ function sinthForBlock(t) {
|
|
|
1525
1562
|
if (needsDelay) {
|
|
1526
1563
|
renderBody += ` setTimeout(function() {
|
|
1527
1564
|
document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
1528
|
-
document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
1529
1565
|
}, 0);\n`;
|
|
1530
1566
|
}
|
|
1531
1567
|
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|