@yannosay/sinth 0.6.0 → 0.6.1
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/sinth.ts +58 -8
package/package.json
CHANGED
package/src/sinth.ts
CHANGED
|
@@ -127,6 +127,7 @@ interface CompileCtx {
|
|
|
127
127
|
mixedBlocks: MixedBlockEntry[];
|
|
128
128
|
mixedCounter: number;
|
|
129
129
|
logicBlocks: string[];
|
|
130
|
+
ifIdCounter: number;
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
class SinthError extends Error {
|
|
@@ -1030,6 +1031,30 @@ private parseArrayLiteral(): Literal {
|
|
|
1030
1031
|
`Unexpected token '${this.peek().value}' in if/else body`,
|
|
1031
1032
|
this.peek().loc,
|
|
1032
1033
|
);
|
|
1034
|
+
|
|
1035
|
+
// reject implicit concatenation only after expression children
|
|
1036
|
+
if (children.length > 0) {
|
|
1037
|
+
const lastChild = children[children.length - 1];
|
|
1038
|
+
if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
|
|
1039
|
+
const nextTT = this.tokens[this.pos]?.type;
|
|
1040
|
+
if (
|
|
1041
|
+
nextTT === TT.STRING || nextTT === TT.NUMBER ||
|
|
1042
|
+
nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
|
|
1043
|
+
(nextTT === TT.IDENT &&
|
|
1044
|
+
this.tokens[this.pos]?.value !== "var" &&
|
|
1045
|
+
this.tokens[this.pos]?.value !== "if" &&
|
|
1046
|
+
this.tokens[this.pos]?.value !== "for" &&
|
|
1047
|
+
this.tokens[this.pos]?.value !== "else" &&
|
|
1048
|
+
this.tokens[this.pos]?.value !== "and" &&
|
|
1049
|
+
this.tokens[this.pos]?.value !== "or")
|
|
1050
|
+
) {
|
|
1051
|
+
throw new SinthError(
|
|
1052
|
+
`Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
|
|
1053
|
+
this.peek().loc,
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1033
1058
|
}
|
|
1034
1059
|
|
|
1035
1060
|
return children;
|
|
@@ -1299,6 +1324,29 @@ private parseArrayLiteral(): Literal {
|
|
|
1299
1324
|
}
|
|
1300
1325
|
}
|
|
1301
1326
|
else throw new SinthError(`Unexpected token '${this.peek().value}' in children`, this.peek().loc);
|
|
1327
|
+
// reject implicit concatenation only after expression children
|
|
1328
|
+
if (children.length > 0) {
|
|
1329
|
+
const lastChild = children[children.length - 1];
|
|
1330
|
+
if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
|
|
1331
|
+
const nextTT = this.tokens[this.pos]?.type;
|
|
1332
|
+
if (
|
|
1333
|
+
nextTT === TT.STRING || nextTT === TT.NUMBER ||
|
|
1334
|
+
nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
|
|
1335
|
+
(nextTT === TT.IDENT &&
|
|
1336
|
+
this.tokens[this.pos]?.value !== "var" &&
|
|
1337
|
+
this.tokens[this.pos]?.value !== "if" &&
|
|
1338
|
+
this.tokens[this.pos]?.value !== "for" &&
|
|
1339
|
+
this.tokens[this.pos]?.value !== "else" &&
|
|
1340
|
+
this.tokens[this.pos]?.value !== "and" &&
|
|
1341
|
+
this.tokens[this.pos]?.value !== "or")
|
|
1342
|
+
) {
|
|
1343
|
+
throw new SinthError(
|
|
1344
|
+
`Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
|
|
1345
|
+
this.peek().loc,
|
|
1346
|
+
);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1302
1350
|
}
|
|
1303
1351
|
return children;
|
|
1304
1352
|
}
|
|
@@ -2194,11 +2242,12 @@ depth: number,
|
|
|
2194
2242
|
|
|
2195
2243
|
// pure DOM
|
|
2196
2244
|
if (!hasAssign && hasComp) {
|
|
2245
|
+
const tplId = ctx.ifIdCounter++;
|
|
2197
2246
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2198
2247
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2199
2248
|
return (
|
|
2200
|
-
`<template data-sinth-if="${escAttr(condJS)}">${bodyHTML}</template>` +
|
|
2201
|
-
(elseHTML ? `<template data-sinth-else>${elseHTML}</template>` : "")
|
|
2249
|
+
`<template data-sinth-if="${escAttr(condJS)}" data-sinth-if-id="${tplId}">${bodyHTML}</template>` +
|
|
2250
|
+
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
|
|
2202
2251
|
);
|
|
2203
2252
|
}
|
|
2204
2253
|
|
|
@@ -2694,7 +2743,8 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2694
2743
|
if (needsIf) {
|
|
2695
2744
|
renderBody += `
|
|
2696
2745
|
document.querySelectorAll('template[data-sinth-if]').forEach(function(t) {
|
|
2697
|
-
var
|
|
2746
|
+
var ifId = t.dataset.sinthIfId;
|
|
2747
|
+
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
2698
2748
|
var cond;
|
|
2699
2749
|
try { cond = eval(t.dataset.sinthIf); } catch(e) { cond = false; }
|
|
2700
2750
|
if (cond) {
|
|
@@ -2704,7 +2754,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2704
2754
|
} else {
|
|
2705
2755
|
anchor = document.createElement('span');
|
|
2706
2756
|
anchor.style.display = 'none';
|
|
2707
|
-
anchor.dataset.sinthIfAnchor =
|
|
2757
|
+
anchor.dataset.sinthIfAnchor = ifId;
|
|
2708
2758
|
t.parentNode.insertBefore(anchor, t);
|
|
2709
2759
|
}
|
|
2710
2760
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
@@ -2735,14 +2785,14 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2735
2785
|
}
|
|
2736
2786
|
var elseT = t.nextElementSibling;
|
|
2737
2787
|
if (elseT && elseT.dataset && elseT.hasAttribute('data-sinth-else')) {
|
|
2738
|
-
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' +
|
|
2788
|
+
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
2739
2789
|
if (ea) {
|
|
2740
2790
|
var cur3 = ea.nextSibling;
|
|
2741
2791
|
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
2742
2792
|
} else {
|
|
2743
2793
|
ea = document.createElement('span');
|
|
2744
2794
|
ea.style.display = 'none';
|
|
2745
|
-
ea.dataset.sinthIfAnchor = '__else__' +
|
|
2795
|
+
ea.dataset.sinthIfAnchor = '__else__' + ifId;
|
|
2746
2796
|
t.parentNode.insertBefore(ea, t);
|
|
2747
2797
|
}
|
|
2748
2798
|
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
@@ -2764,7 +2814,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2764
2814
|
});
|
|
2765
2815
|
t.parentNode.insertBefore(ef, t);
|
|
2766
2816
|
} else {
|
|
2767
|
-
var ea2 = t.parentNode ? t.parentNode.querySelector('[data-sinth-if-anchor="__else__' +
|
|
2817
|
+
var ea2 = t.parentNode ? t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]') : null;
|
|
2768
2818
|
if (ea2) {
|
|
2769
2819
|
var ec = ea2.nextSibling;
|
|
2770
2820
|
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
@@ -2776,7 +2826,6 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2776
2826
|
`;
|
|
2777
2827
|
}
|
|
2778
2828
|
|
|
2779
|
-
|
|
2780
2829
|
// mixed if/else blocks
|
|
2781
2830
|
if (needsMixed) {
|
|
2782
2831
|
for (const mb of mixedBlocks) {
|
|
@@ -2924,6 +2973,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2924
2973
|
mixedBlocks: [],
|
|
2925
2974
|
mixedCounter: 0,
|
|
2926
2975
|
logicBlocks: [],
|
|
2976
|
+
ifIdCounter: 0,
|
|
2927
2977
|
};
|
|
2928
2978
|
|
|
2929
2979
|
if (!file.isPage) {
|