@yannosay/sinth 1.6.1 → 1.7.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 +3 -3
- package/package.json +1 -1
- package/src/core/compiler.ts +26 -4
- package/src/core/parser.ts +12 -4
- package/src/core/runtime/render.ts +5 -1
package/README.md
CHANGED
|
@@ -15,8 +15,8 @@ The language that makes HTML feel SO easy. Declarative. Reactive. Compiles to pu
|
|
|
15
15
|
#### Try out Sinth directly in Web! (Beta)
|
|
16
16
|
|
|
17
17
|
```html
|
|
18
|
-
<div id="
|
|
19
|
-
<script src="sinth.js"></script>
|
|
18
|
+
<div id="app"></div>
|
|
19
|
+
<script src="https://lib.yannosay.com/sinth.js/sinth.js"></script>
|
|
20
20
|
<script type="text/sinth">
|
|
21
21
|
page
|
|
22
22
|
title = "Counter"
|
|
@@ -27,7 +27,7 @@ Main {
|
|
|
27
27
|
Button(onClick: count = count + 1) {"Increment"}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
$entry-point("
|
|
30
|
+
$entry-point("app")
|
|
31
31
|
</script>
|
|
32
32
|
```
|
|
33
33
|
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -1104,6 +1104,15 @@ export function buildRuntime(opts: {
|
|
|
1104
1104
|
const val = litToString(v.value);
|
|
1105
1105
|
if (val.startsWith("__VAR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
1106
1106
|
if (val.startsWith("__ARR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
1107
|
+
if (val.startsWith("__EXPR__")) {
|
|
1108
|
+
try {
|
|
1109
|
+
const expr: Expression = JSON.parse(val.substring(8));
|
|
1110
|
+
const js = compileExprToJS(expr);
|
|
1111
|
+
const id = exprRegistry.length;
|
|
1112
|
+
exprRegistry.push(js);
|
|
1113
|
+
return `let ${v.name} = __X[${id}]({});`;
|
|
1114
|
+
} catch { return `let ${v.name} = ${val};`; }
|
|
1115
|
+
}
|
|
1107
1116
|
if (v.varType === "obj") return `let ${v.name} = ${val};`;
|
|
1108
1117
|
if (v.varType === "str") return `let ${v.name} = ${JSON.stringify(val)};`;
|
|
1109
1118
|
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
@@ -1122,9 +1131,23 @@ export function buildRuntime(opts: {
|
|
|
1122
1131
|
|
|
1123
1132
|
const helpers = generateHelpers({ needsExpr, needsIf, needsFor, needsDelay, needsMixed });
|
|
1124
1133
|
|
|
1134
|
+
const exprVarUpdates = varDecls
|
|
1135
|
+
.filter(v => v.value && litToString(v.value).startsWith("__EXPR__"))
|
|
1136
|
+
.map(v => {
|
|
1137
|
+
try {
|
|
1138
|
+
const expr: Expression = JSON.parse(litToString(v.value!).substring(8));
|
|
1139
|
+
const js = compileExprToJS(expr);
|
|
1140
|
+
const idx = exprRegistry.indexOf(js);
|
|
1141
|
+
return `${v.name} = __X[${idx}]({});`;
|
|
1142
|
+
} catch { return ""; }
|
|
1143
|
+
})
|
|
1144
|
+
.filter(Boolean)
|
|
1145
|
+
.join("\n ");
|
|
1146
|
+
|
|
1125
1147
|
const renderBody = buildRenderBody({
|
|
1126
1148
|
bodyHTML, logicBlocks, mixedBlocks,
|
|
1127
|
-
needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay
|
|
1149
|
+
needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay,
|
|
1150
|
+
exprVarUpdates: exprVarUpdates || undefined
|
|
1128
1151
|
});
|
|
1129
1152
|
|
|
1130
1153
|
let forDataJS = "";
|
|
@@ -1172,11 +1195,10 @@ export function buildRuntime(opts: {
|
|
|
1172
1195
|
}
|
|
1173
1196
|
|
|
1174
1197
|
const pageCode = `// Sinth Page Runtime
|
|
1175
|
-
${varLines}
|
|
1176
1198
|
${forDataJS}
|
|
1177
1199
|
${exprArrayJS}
|
|
1200
|
+
${varLines}
|
|
1178
1201
|
${renderFunc}`;
|
|
1179
|
-
|
|
1180
1202
|
if (opts.sharedRuntime && helpers.trim()) {
|
|
1181
1203
|
const sharedCode = `// Sinth shared runtime
|
|
1182
1204
|
${helpers}`;
|
|
@@ -1184,10 +1206,10 @@ ${helpers}`;
|
|
|
1184
1206
|
}
|
|
1185
1207
|
|
|
1186
1208
|
return `// Sinth compiled runtime
|
|
1187
|
-
${varLines}
|
|
1188
1209
|
${forDataJS}
|
|
1189
1210
|
${functionsJS ? functionsJS + "\n" : ""}${helpers}
|
|
1190
1211
|
${exprArrayJS}
|
|
1212
|
+
${varLines}
|
|
1191
1213
|
${renderFunc}`;
|
|
1192
1214
|
}
|
|
1193
1215
|
|
package/src/core/parser.ts
CHANGED
|
@@ -188,7 +188,7 @@ if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
|
|
|
188
188
|
|
|
189
189
|
private parseVarDeclaration(): VarDeclaration | null {
|
|
190
190
|
const loc = this.peek().loc;
|
|
191
|
-
this.consume(TT.IDENT);
|
|
191
|
+
this.consume(TT.IDENT);
|
|
192
192
|
|
|
193
193
|
const typeTok = this.consume(TT.IDENT);
|
|
194
194
|
let typeStr = typeTok.value;
|
|
@@ -207,9 +207,17 @@ if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
|
|
|
207
207
|
this.consume(TT.EQUALS);
|
|
208
208
|
if (this.check(TT.LBRACKET)) {
|
|
209
209
|
val = this.parseArrayLiteral();
|
|
210
|
-
} else if (this.check(TT.IDENT)) {
|
|
211
|
-
const
|
|
212
|
-
|
|
210
|
+
} else if (this.check(TT.IDENT) || this.check(TT.LPAREN)) {
|
|
211
|
+
const savedPos = this.pos;
|
|
212
|
+
const expr = this.parseExpression();
|
|
213
|
+
if (expr && (expr.kind !== "literal" || (expr.kind === "literal" && expr.value?.kind === "str" && expr.value.value.startsWith("__VAR__")))) {
|
|
214
|
+
val = { kind: "str", value: `__EXPR__${JSON.stringify(expr)}` };
|
|
215
|
+
} else {
|
|
216
|
+
this.pos = savedPos;
|
|
217
|
+
val = this.parseLiteral();
|
|
218
|
+
}
|
|
219
|
+
} else if (this.check(TT.NUMBER)) {
|
|
220
|
+
val = this.parseLiteral();
|
|
213
221
|
} else {
|
|
214
222
|
val = this.parseLiteral();
|
|
215
223
|
}
|
|
@@ -10,8 +10,9 @@ export function buildRenderBody(opts: {
|
|
|
10
10
|
needsFor: boolean;
|
|
11
11
|
needsExpr: boolean;
|
|
12
12
|
needsDelay: boolean;
|
|
13
|
+
exprVarUpdates?: string;
|
|
13
14
|
}): string {
|
|
14
|
-
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay } = opts;
|
|
15
|
+
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates } = opts;
|
|
15
16
|
let renderBody = "";
|
|
16
17
|
renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
17
18
|
|
|
@@ -63,6 +64,9 @@ export function buildRenderBody(opts: {
|
|
|
63
64
|
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
64
65
|
try { let c=el.dataset.sinthChecked; el.checked = __X[c] ? !!__X[c]({}) : false; } catch(e) {}
|
|
65
66
|
});\n`;
|
|
67
|
+
if (exprVarUpdates) {
|
|
68
|
+
renderBody += ` ${exprVarUpdates}\n`;
|
|
69
|
+
}
|
|
66
70
|
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
67
71
|
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|
|
68
72
|
try {
|