@yannosay/sinth 1.6.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "A declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
@@ -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
 
@@ -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); // consume 'var'
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 refName = this.consume(TT.IDENT).value;
212
- val = { kind: "str", value: `__VAR__${refName}` };
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 {