@yannosay/sinth 0.13.7 → 0.14.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 CHANGED
@@ -153,7 +153,7 @@ if done == true {
153
153
  remove "myElement"
154
154
  }
155
155
  ```
156
- Delete elements for good. No virtual DOM diffing.
156
+ Delete elements. No virtual DOM diffing!
157
157
 
158
158
  Compiles to pure HTML.
159
159
 
@@ -176,6 +176,7 @@ Main {
176
176
  ```
177
177
 
178
178
 
179
+
179
180
  ```ts
180
181
  page
181
182
  title = "Counter Demo"
@@ -199,7 +200,27 @@ Main {
199
200
  Input(bind: stepSize, step: 1)
200
201
  }
201
202
  ```
203
+ ##### NEW! (0.14.0):
204
+ Sinth now supports Reactivity in Functions!
205
+ ```ts
206
+ page
207
+ title = "Reactive Counter"
202
208
 
209
+ var int apples = 1
210
+ var int bananas = 2
211
+ var int oranges = -6
212
+
213
+ function Counter(int value) -> ui {
214
+ Heading(level: 1) {"Count is at: " + (value) + "!"}
215
+ Button(onClick: value = value + 1) {"Increment"}
216
+ }
217
+
218
+ Main {
219
+ (Counter(apples))
220
+ (Counter(bananas))
221
+ (Counter(oranges))
222
+ }
223
+ ```
203
224
 
204
225
 
205
226
  # Install
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.13.7",
3
+ "version": "0.14.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",
@@ -73,7 +73,7 @@ export function registerExpr(ctx: CompileCtx, expr: Expression): number {
73
73
  }
74
74
 
75
75
 
76
- export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx?: CompileCtx): string {
76
+ export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx: CompileCtx): string {
77
77
  const { name, value } = attr;
78
78
  if (value === null) return name;
79
79
  if (value.kind === "null") return "";
@@ -287,19 +287,35 @@ export function renderChild(
287
287
  localParams.set(fnDef.params[i].name, "__LIT__" + arg.value.value);
288
288
  } else if (arg.kind === "variable" && arg.name) {
289
289
  localParams.set(fnDef.params[i].name, "__VAR__" + arg.name);
290
+ } else {
291
+ const exprId = registerExpr(ctx, arg);
292
+ localParams.set(fnDef.params[i].name, "__EXPRID__" + exprId);
290
293
  }
291
294
  }
292
295
  const substituteExpr = (expr: Expression, pm: Map<string, string>): Expression => {
293
296
  if (expr.kind === "variable" && pm.has(expr.name!)) {
294
297
  const pv = pm.get(expr.name!)!;
295
298
  if (pv.startsWith("__LIT__")) return { kind: "literal", value: { kind: "str", value: pv.slice(7) } };
299
+ if (pv.startsWith("__EXPRID__")) {
300
+ const id = parseInt(pv.slice(10), 10);
301
+ return { kind: "expr_ref", exprId: id };
302
+ }
296
303
  return { kind: "variable", name: pv.slice(7) };
297
304
  }
298
305
  if (expr.kind === "binary") return { ...expr, left: substituteExpr(expr.left!, pm), right: substituteExpr(expr.right!, pm) };
299
306
  if (expr.kind === "unary") return { ...expr, operand: substituteExpr(expr.operand!, pm) };
300
307
  if (expr.kind === "call") return { ...expr, callee: substituteExpr(expr.callee!, pm), args: expr.args?.map(a => substituteExpr(a, pm)) };
301
308
  if (expr.kind === "index") return { ...expr, object: substituteExpr(expr.object!, pm), key: substituteExpr(expr.key!, pm) };
302
- if (expr.kind === "assign") return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
309
+ if (expr.kind === "assign") {
310
+ const target = expr.target;
311
+ if (target && pm.has(target)) {
312
+ const pv = pm.get(target)!;
313
+ if (pv.startsWith("__VAR__")) {
314
+ return { ...expr, target: pv.slice(7), right: expr.right ? substituteExpr(expr.right, pm) : undefined };
315
+ }
316
+ }
317
+ return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
318
+ }
303
319
  return expr;
304
320
  };
305
321
  const substituteChild = (c: Child, pm: Map<string, string>): Child => {
@@ -319,6 +335,20 @@ export function renderChild(
319
335
  const sa: Attr[] = u.attrs.map(a => {
320
336
  if (a.value?.kind === "str") {
321
337
  let raw = a.value.value;
338
+ if (raw.startsWith("__EXPR__")) {
339
+ try {
340
+ const expr: Expression = JSON.parse(raw.substring(8));
341
+ const substituted = substituteExpr(expr, pm);
342
+ return { ...a, value: { kind: "str" as const, value: "__EXPR__" + JSON.stringify(substituted) } };
343
+ } catch {}
344
+ }
345
+ if (raw.startsWith("__MULTI_EXPR__")) {
346
+ try {
347
+ const exprs: Expression[] = JSON.parse(raw.substring("__MULTI_EXPR__".length));
348
+ const substituted = exprs.map(e => substituteExpr(e, pm));
349
+ return { ...a, value: { kind: "str" as const, value: "__MULTI_EXPR__" + JSON.stringify(substituted) } };
350
+ } catch {}
351
+ }
322
352
  for (const [pn, pv] of pm) {
323
353
  if (pv.startsWith("__LIT__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
324
354
  if (pv.startsWith("__VAR__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
@@ -334,6 +364,9 @@ export function renderChild(
334
364
  return fnDef.body.map(c => renderChild(substituteChild(c, localParams), ctx, localParams, depth + 1)).join("");
335
365
  }
336
366
  }
367
+ if (child.expression.kind === "expr_ref" && child.expression.exprId !== undefined) {
368
+ return `<span class="sinth-expr" data-expr-id="${child.expression.exprId}"></span>`;
369
+ }
337
370
  if (child.expression.kind === "literal" && child.expression.value) {
338
371
  return esc(litToString(child.expression.value));
339
372
  }
@@ -880,7 +913,6 @@ ${exprArrayJS}
880
913
  ${renderFunc}`;
881
914
  }
882
915
 
883
- // main compilation pipeline
884
916
 
885
917
  export interface CompileOptions {
886
918
  projectRoot: string;
@@ -894,14 +926,77 @@ export interface CompileOptions {
894
926
  export function compileFile(filePath: string, opts: CompileOptions): { html: string; shared?: string } | null {
895
927
  const absPath = path.resolve(filePath);
896
928
  const file = parseFile(absPath);
897
-
898
929
  const cfg: ResolverConfig = { projectRoot: opts.projectRoot, libraryPaths: opts.libraryPaths };
899
930
  const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
900
931
  const hash = fnv1a(absPath);
901
-
902
932
  const allVarDecls: VarDeclaration[] = file.varDecls;
903
933
  const functionDefs: FunctionDef[] = file.functions;
934
+
935
+ const declaredVars = new Set(allVarDecls.map(v => v.name));
936
+ const declaredFuncs = new Set(file.functions.map(f => f.name));
937
+
938
+ function collectExprVars(expr: Expression | undefined, vars: Set<string>): void {
939
+ if (!expr) return;
940
+ if (expr.kind === "variable" && expr.name && !declaredFuncs.has(expr.name)) vars.add(expr.name);
941
+ if (expr.kind === "assign" && expr.target) vars.add(expr.target);
942
+ if (expr.left) collectExprVars(expr.left, vars);
943
+ if (expr.right) collectExprVars(expr.right, vars);
944
+ if (expr.operand) collectExprVars(expr.operand, vars);
945
+ if (expr.object) collectExprVars(expr.object, vars);
946
+ if (expr.key) collectExprVars(expr.key, vars);
947
+ if (expr.args) expr.args.forEach(a => collectExprVars(a, vars));
948
+ }
904
949
 
950
+ function collectChildVars(child: Child, vars: Set<string>): void {
951
+ if (child.kind === "expr" || child.kind === "assign_stmt" || child.kind === "return") {
952
+ collectExprVars(child.expression, vars);
953
+ }
954
+ if (child.kind === "if") {
955
+ collectExprVars((child as IfBlock).condition, vars);
956
+ (child as IfBlock).body.forEach(c => collectChildVars(c, vars));
957
+ (child as IfBlock).elseBody?.forEach(c => collectChildVars(c, vars));
958
+ }
959
+ if (child.kind === "for") {
960
+ (child as ForLoop).body.forEach(c => collectChildVars(c, vars));
961
+ }
962
+ if (child.kind === "use") {
963
+ (child as CompUse).attrs.forEach(a => {
964
+ if (a.value?.kind === "str") {
965
+ const v = a.value.value;
966
+ if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
967
+ try {
968
+ const exprs: Expression[] = v.startsWith("__MULTI_EXPR__")
969
+ ? JSON.parse(v.substring("__MULTI_EXPR__".length))
970
+ : [JSON.parse(v.substring(8))];
971
+ exprs.forEach(e => collectExprVars(e, vars));
972
+ } catch {}
973
+ }
974
+ }
975
+ });
976
+ (child as CompUse).children.forEach(c => collectChildVars(c, vars));
977
+ }
978
+ }
979
+ const pageVars = new Set<string>();
980
+ file.uses.forEach(u => collectChildVars(u, pageVars));
981
+ for (const v of pageVars) {
982
+ if (!declaredVars.has(v)) {
983
+ throw new SinthError(
984
+ `Variable '${v}' is used but never declared. Add 'var str ${v}' or 'var int ${v}' before using it.`
985
+ );
986
+ }
987
+ }
988
+ for (const fn of file.functions) {
989
+ const fnParamNames = new Set(fn.params.map(p => p.name));
990
+ const fnBodyVars = new Set<string>();
991
+ fn.body.forEach(c => collectChildVars(c, fnBodyVars));
992
+ for (const v of fnBodyVars) {
993
+ if (!fnParamNames.has(v) && !declaredVars.has(v) && !declaredFuncs.has(v)) {
994
+ throw new SinthError(
995
+ `Variable '${v}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`
996
+ );
997
+ }
998
+ }
999
+ }
905
1000
  const ctx: CompileCtx = {
906
1001
  allDefs, functionDefs, customEls, cssLinks, jsLinks,
907
1002
  scopeHash: hash,
@@ -43,14 +43,13 @@ export function buildRenderBody(opts: {
43
43
  }
44
44
  }
45
45
 
46
+ if (needsIf) {
47
+ renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
48
+ }
46
49
  renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
47
50
  var target = document.getElementById(el.dataset.sinthRemove);
48
51
  if (target) target.remove();
49
52
  });\n`;
50
-
51
- if (needsIf) {
52
- renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
53
- }
54
53
  if (needsFor) {
55
54
  renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
56
55
  }
package/src/core/types.ts CHANGED
@@ -41,7 +41,7 @@ export type AssignOp = "=" | "+=" | "-=";
41
41
  export type PostfixOp = "++" | "--";
42
42
 
43
43
  export interface Expression {
44
- kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call";
44
+ kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call" | "expr_ref";
45
45
  value?: Literal;
46
46
  name?: string;
47
47
  op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;
@@ -53,6 +53,7 @@ export interface Expression {
53
53
  key?: Expression;
54
54
  callee?: Expression;
55
55
  args?: Expression[];
56
+ exprId?: number; // for "expr_ref" kind
56
57
  }
57
58
 
58
59
  export interface Attr { name: string; value: Literal | null; loc: Loc }
package/tsconfig.json CHANGED
@@ -5,11 +5,11 @@
5
5
  "lib": ["ES2020"],
6
6
  "outDir": "./dist",
7
7
  "rootDir": "./src",
8
- "strict": false,
8
+ "strict": true,
9
9
  "esModuleInterop": true,
10
10
  "skipLibCheck": true,
11
- "noImplicitAny": false,
12
- "strictNullChecks": false,
11
+ "noImplicitAny": true,
12
+ "strictNullChecks": true,
13
13
  "allowImportingTsExtensions": true,
14
14
  "noEmit": true,
15
15
  "types": ["node"],