@yannosay/sinth 1.8.1 → 1.8.2
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/LICENSE +660 -660
- package/README.md +548 -548
- package/package.json +45 -45
- package/src/cli.ts +338 -338
- package/src/core/builtins.ts +116 -116
- package/src/core/compiler.ts +1706 -1706
- package/src/core/expr.ts +88 -88
- package/src/core/head-builder.ts +86 -86
- package/src/core/lexer.ts +275 -275
- package/src/core/parser.ts +1523 -1523
- package/src/core/runtime/custom-element.ts +134 -134
- package/src/core/runtime/functions.ts +46 -46
- package/src/core/runtime/helpers.ts +332 -332
- package/src/core/runtime/render.ts +136 -136
- package/src/core/style-processor.ts +299 -299
- package/src/core/types.ts +192 -192
- package/src/resolver.ts +149 -149
- package/src/server.ts +128 -128
- package/src/utils.ts +76 -76
- package/tsconfig.json +26 -26
package/src/core/expr.ts
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool, CompileCtx } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
export function compileExprToJS(expr: Expression, loopVars?: Set<string>): string {
|
|
4
|
-
switch (expr.kind) {
|
|
5
|
-
case "literal":
|
|
6
|
-
if (!expr.value) return "null";
|
|
7
|
-
if (expr.value.kind === "str") {
|
|
8
|
-
const sv = (expr.value as LitStr).value;
|
|
9
|
-
if (sv.startsWith("__ARR__")) return sv.slice(7);
|
|
10
|
-
if (sv.startsWith("__VAR__")) return sv.slice(7);
|
|
11
|
-
return JSON.stringify(sv);
|
|
12
|
-
}
|
|
13
|
-
if (expr.value.kind === "num") return String((expr.value as LitNum).value);
|
|
14
|
-
if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
|
|
15
|
-
return "null";
|
|
16
|
-
case "variable":
|
|
17
|
-
if (loopVars && expr.name) {
|
|
18
|
-
const dotIdx = expr.name.indexOf('.');
|
|
19
|
-
const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
|
|
20
|
-
if (loopVars.has(root)) {
|
|
21
|
-
return `_ctx.${expr.name}`;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return expr.name ?? "undefined";
|
|
25
|
-
case "binary": {
|
|
26
|
-
const l = compileExprToJS(expr.left!, loopVars);
|
|
27
|
-
const r = compileExprToJS(expr.right!, loopVars);
|
|
28
|
-
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
|
|
29
|
-
return `(${l} ${o} ${r})`;
|
|
30
|
-
}
|
|
31
|
-
case "unary": {
|
|
32
|
-
const o = expr.op === "not" ? "!" : expr.op!;
|
|
33
|
-
return `${o}(${compileExprToJS(expr.operand!, loopVars)})`;
|
|
34
|
-
}
|
|
35
|
-
case "assign": {
|
|
36
|
-
const v = expr.right ? compileExprToJS(expr.right, loopVars) : "null";
|
|
37
|
-
return `${expr.target} ${expr.op} ${v}`;
|
|
38
|
-
}
|
|
39
|
-
case "index":
|
|
40
|
-
return `${compileExprToJS(expr.object!, loopVars)}[${compileExprToJS(expr.key!, loopVars)}]`;
|
|
41
|
-
case "postfix":
|
|
42
|
-
return `${expr.target}${expr.op}`;
|
|
43
|
-
case "call": {
|
|
44
|
-
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
45
|
-
if (callee === "remove") {
|
|
46
|
-
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
47
|
-
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
48
|
-
}
|
|
49
|
-
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
50
|
-
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
51
|
-
return callee;
|
|
52
|
-
}
|
|
53
|
-
return `${callee}(${args})`;
|
|
54
|
-
}
|
|
55
|
-
default:
|
|
56
|
-
return "";
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
export function compileIfToJS(ifBlock: IfBlock): string {
|
|
62
|
-
const cond = compileExprToJS(ifBlock.condition);
|
|
63
|
-
const ifJS = bodyToJS(ifBlock.body);
|
|
64
|
-
const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody) : "";
|
|
65
|
-
let js = `if (${cond}) {\n${ifJS}}\n`;
|
|
66
|
-
if (elseJS) js += `else {\n${elseJS}}\n`;
|
|
67
|
-
return js;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
export function bodyToJS(children: Child[]): string {
|
|
73
|
-
return children
|
|
74
|
-
.filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
|
|
75
|
-
.map(c => {
|
|
76
|
-
if (c.kind === "assign_stmt") return ` ${compileExprToJS((c as AssignStmt).expression)};\n`;
|
|
77
|
-
if (c.kind === "return") return ` return ${(c as ReturnStmt).expression ? compileExprToJS((c as ReturnStmt).expression!) : ""};\n`;
|
|
78
|
-
if (c.kind === "if") return compileIfToJS(c as IfBlock).replace(/^/gm, " ") + "\n";
|
|
79
|
-
if (c.kind === "expr") return ` ${compileExprToJS((c as any).expression)};\n`;
|
|
80
|
-
return "";
|
|
81
|
-
})
|
|
82
|
-
.join("");
|
|
83
|
-
}
|
|
84
|
-
export function scopeVariableName(name: string, ctx?: CompileCtx): string {
|
|
85
|
-
if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
|
|
86
|
-
return `${ctx.scopePrefix}${name}`;
|
|
87
|
-
}
|
|
88
|
-
return name;
|
|
1
|
+
import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool, CompileCtx } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
export function compileExprToJS(expr: Expression, loopVars?: Set<string>): string {
|
|
4
|
+
switch (expr.kind) {
|
|
5
|
+
case "literal":
|
|
6
|
+
if (!expr.value) return "null";
|
|
7
|
+
if (expr.value.kind === "str") {
|
|
8
|
+
const sv = (expr.value as LitStr).value;
|
|
9
|
+
if (sv.startsWith("__ARR__")) return sv.slice(7);
|
|
10
|
+
if (sv.startsWith("__VAR__")) return sv.slice(7);
|
|
11
|
+
return JSON.stringify(sv);
|
|
12
|
+
}
|
|
13
|
+
if (expr.value.kind === "num") return String((expr.value as LitNum).value);
|
|
14
|
+
if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
|
|
15
|
+
return "null";
|
|
16
|
+
case "variable":
|
|
17
|
+
if (loopVars && expr.name) {
|
|
18
|
+
const dotIdx = expr.name.indexOf('.');
|
|
19
|
+
const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
|
|
20
|
+
if (loopVars.has(root)) {
|
|
21
|
+
return `_ctx.${expr.name}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return expr.name ?? "undefined";
|
|
25
|
+
case "binary": {
|
|
26
|
+
const l = compileExprToJS(expr.left!, loopVars);
|
|
27
|
+
const r = compileExprToJS(expr.right!, loopVars);
|
|
28
|
+
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
|
|
29
|
+
return `(${l} ${o} ${r})`;
|
|
30
|
+
}
|
|
31
|
+
case "unary": {
|
|
32
|
+
const o = expr.op === "not" ? "!" : expr.op!;
|
|
33
|
+
return `${o}(${compileExprToJS(expr.operand!, loopVars)})`;
|
|
34
|
+
}
|
|
35
|
+
case "assign": {
|
|
36
|
+
const v = expr.right ? compileExprToJS(expr.right, loopVars) : "null";
|
|
37
|
+
return `${expr.target} ${expr.op} ${v}`;
|
|
38
|
+
}
|
|
39
|
+
case "index":
|
|
40
|
+
return `${compileExprToJS(expr.object!, loopVars)}[${compileExprToJS(expr.key!, loopVars)}]`;
|
|
41
|
+
case "postfix":
|
|
42
|
+
return `${expr.target}${expr.op}`;
|
|
43
|
+
case "call": {
|
|
44
|
+
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
45
|
+
if (callee === "remove") {
|
|
46
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
47
|
+
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
48
|
+
}
|
|
49
|
+
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
50
|
+
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
51
|
+
return callee;
|
|
52
|
+
}
|
|
53
|
+
return `${callee}(${args})`;
|
|
54
|
+
}
|
|
55
|
+
default:
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
export function compileIfToJS(ifBlock: IfBlock): string {
|
|
62
|
+
const cond = compileExprToJS(ifBlock.condition);
|
|
63
|
+
const ifJS = bodyToJS(ifBlock.body);
|
|
64
|
+
const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody) : "";
|
|
65
|
+
let js = `if (${cond}) {\n${ifJS}}\n`;
|
|
66
|
+
if (elseJS) js += `else {\n${elseJS}}\n`;
|
|
67
|
+
return js;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
export function bodyToJS(children: Child[]): string {
|
|
73
|
+
return children
|
|
74
|
+
.filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
|
|
75
|
+
.map(c => {
|
|
76
|
+
if (c.kind === "assign_stmt") return ` ${compileExprToJS((c as AssignStmt).expression)};\n`;
|
|
77
|
+
if (c.kind === "return") return ` return ${(c as ReturnStmt).expression ? compileExprToJS((c as ReturnStmt).expression!) : ""};\n`;
|
|
78
|
+
if (c.kind === "if") return compileIfToJS(c as IfBlock).replace(/^/gm, " ") + "\n";
|
|
79
|
+
if (c.kind === "expr") return ` ${compileExprToJS((c as any).expression)};\n`;
|
|
80
|
+
return "";
|
|
81
|
+
})
|
|
82
|
+
.join("");
|
|
83
|
+
}
|
|
84
|
+
export function scopeVariableName(name: string, ctx?: CompileCtx): string {
|
|
85
|
+
if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
|
|
86
|
+
return `${ctx.scopePrefix}${name}`;
|
|
87
|
+
}
|
|
88
|
+
return name;
|
|
89
89
|
}
|
package/src/core/head-builder.ts
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import { MetaEntry, SinthWarning } from "./types.ts";
|
|
3
|
-
import { esc, escAttr, litToString, camelToKebab } from "../utils.ts";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface HeadData {
|
|
8
|
-
title?: string; fav?: string;
|
|
9
|
-
lang: string; charset: string; viewport: string;
|
|
10
|
-
metaTags: { name: string; content: string }[];
|
|
11
|
-
metaProps: { property: string; content: string }[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function buildHeadData(meta: MetaEntry[]): HeadData {
|
|
15
|
-
let title: string | undefined, fav: string | undefined;
|
|
16
|
-
let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
|
|
17
|
-
const metaTags: { name: string; content: string }[] = [];
|
|
18
|
-
const metaProps: { property: string; content: string }[] = [];
|
|
19
|
-
|
|
20
|
-
for (const m of meta) {
|
|
21
|
-
const val = litToString(m.value);
|
|
22
|
-
switch (m.key) {
|
|
23
|
-
case "title": title = val; break;
|
|
24
|
-
case "fav": fav = val; break;
|
|
25
|
-
case "lang": lang = val; break;
|
|
26
|
-
case "charset": charset = val; break;
|
|
27
|
-
case "viewport": viewport = val; break;
|
|
28
|
-
case "descr": metaTags.push({ name: "description", content: val }); break;
|
|
29
|
-
case "author": metaTags.push({ name: "author", content: val }); break;
|
|
30
|
-
case "keywords": metaTags.push({ name: "keywords", content: val }); break;
|
|
31
|
-
case "robots": metaTags.push({ name: "robots", content: val }); break;
|
|
32
|
-
default:
|
|
33
|
-
if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
|
|
34
|
-
const prop = m.key
|
|
35
|
-
.replace(/^og([A-Z])/, (_: string, c: string) => `og:${c.toLowerCase()}`)
|
|
36
|
-
.replace(/^twitter([A-Z])/, (_: string, c: string) => `twitter:${c.toLowerCase()}`);
|
|
37
|
-
metaProps.push({ property: prop, content: val });
|
|
38
|
-
} else {
|
|
39
|
-
metaTags.push({ name: camelToKebab(m.key), content: val });
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function faviconType(p: string): string {
|
|
48
|
-
switch (path.extname(p).toLowerCase()) {
|
|
49
|
-
case ".ico": return "image/x-icon";
|
|
50
|
-
case ".png": return "image/png";
|
|
51
|
-
case ".svg": return "image/svg+xml";
|
|
52
|
-
case ".gif": return "image/gif";
|
|
53
|
-
case ".webp": return "image/webp";
|
|
54
|
-
case ".jpg":
|
|
55
|
-
case ".jpeg": return "image/jpeg";
|
|
56
|
-
default:
|
|
57
|
-
SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
|
|
58
|
-
return "image/x-icon";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function renderHead(
|
|
63
|
-
hd: HeadData,
|
|
64
|
-
cssLinks: string[],
|
|
65
|
-
jsLinks: { src: string; attrs: Record<string, string> }[],
|
|
66
|
-
scopedCSS: string,
|
|
67
|
-
companionJS: string | undefined,
|
|
68
|
-
): string {
|
|
69
|
-
const lines: string[] = [];
|
|
70
|
-
lines.push(` <meta charset="${escAttr(hd.charset)}">`);
|
|
71
|
-
lines.push(` <meta name="viewport" content="${escAttr(hd.viewport)}">`);
|
|
72
|
-
if (hd.title) lines.push(` <title>${esc(hd.title)}</title>`);
|
|
73
|
-
if (hd.fav) lines.push(` <link rel="icon" href="${escAttr(hd.fav)}" type="${faviconType(hd.fav)}">`);
|
|
74
|
-
for (const m of hd.metaTags) lines.push(` <meta name="${escAttr(m.name)}" content="${escAttr(m.content)}">`);
|
|
75
|
-
for (const m of hd.metaProps) lines.push(` <meta property="${escAttr(m.property)}" content="${escAttr(m.content)}">`);
|
|
76
|
-
for (const css of cssLinks) lines.push(` <link rel="stylesheet" href="${escAttr(css)}">`);
|
|
77
|
-
if (scopedCSS.trim()) lines.push(` <style>\n${scopedCSS}\n </style>`);
|
|
78
|
-
for (const js of jsLinks) {
|
|
79
|
-
const extra = Object.entries(js.attrs)
|
|
80
|
-
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
81
|
-
.join(" ");
|
|
82
|
-
lines.push(` <script src="${escAttr(js.src)}"${extra ? " " + extra : ""}></script>`);
|
|
83
|
-
}
|
|
84
|
-
if (companionJS) lines.push(` <script src="${escAttr(companionJS)}"></script>`);
|
|
85
|
-
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
86
|
-
}
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { MetaEntry, SinthWarning } from "./types.ts";
|
|
3
|
+
import { esc, escAttr, litToString, camelToKebab } from "../utils.ts";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export interface HeadData {
|
|
8
|
+
title?: string; fav?: string;
|
|
9
|
+
lang: string; charset: string; viewport: string;
|
|
10
|
+
metaTags: { name: string; content: string }[];
|
|
11
|
+
metaProps: { property: string; content: string }[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function buildHeadData(meta: MetaEntry[]): HeadData {
|
|
15
|
+
let title: string | undefined, fav: string | undefined;
|
|
16
|
+
let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
|
|
17
|
+
const metaTags: { name: string; content: string }[] = [];
|
|
18
|
+
const metaProps: { property: string; content: string }[] = [];
|
|
19
|
+
|
|
20
|
+
for (const m of meta) {
|
|
21
|
+
const val = litToString(m.value);
|
|
22
|
+
switch (m.key) {
|
|
23
|
+
case "title": title = val; break;
|
|
24
|
+
case "fav": fav = val; break;
|
|
25
|
+
case "lang": lang = val; break;
|
|
26
|
+
case "charset": charset = val; break;
|
|
27
|
+
case "viewport": viewport = val; break;
|
|
28
|
+
case "descr": metaTags.push({ name: "description", content: val }); break;
|
|
29
|
+
case "author": metaTags.push({ name: "author", content: val }); break;
|
|
30
|
+
case "keywords": metaTags.push({ name: "keywords", content: val }); break;
|
|
31
|
+
case "robots": metaTags.push({ name: "robots", content: val }); break;
|
|
32
|
+
default:
|
|
33
|
+
if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
|
|
34
|
+
const prop = m.key
|
|
35
|
+
.replace(/^og([A-Z])/, (_: string, c: string) => `og:${c.toLowerCase()}`)
|
|
36
|
+
.replace(/^twitter([A-Z])/, (_: string, c: string) => `twitter:${c.toLowerCase()}`);
|
|
37
|
+
metaProps.push({ property: prop, content: val });
|
|
38
|
+
} else {
|
|
39
|
+
metaTags.push({ name: camelToKebab(m.key), content: val });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function faviconType(p: string): string {
|
|
48
|
+
switch (path.extname(p).toLowerCase()) {
|
|
49
|
+
case ".ico": return "image/x-icon";
|
|
50
|
+
case ".png": return "image/png";
|
|
51
|
+
case ".svg": return "image/svg+xml";
|
|
52
|
+
case ".gif": return "image/gif";
|
|
53
|
+
case ".webp": return "image/webp";
|
|
54
|
+
case ".jpg":
|
|
55
|
+
case ".jpeg": return "image/jpeg";
|
|
56
|
+
default:
|
|
57
|
+
SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
|
|
58
|
+
return "image/x-icon";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function renderHead(
|
|
63
|
+
hd: HeadData,
|
|
64
|
+
cssLinks: string[],
|
|
65
|
+
jsLinks: { src: string; attrs: Record<string, string> }[],
|
|
66
|
+
scopedCSS: string,
|
|
67
|
+
companionJS: string | undefined,
|
|
68
|
+
): string {
|
|
69
|
+
const lines: string[] = [];
|
|
70
|
+
lines.push(` <meta charset="${escAttr(hd.charset)}">`);
|
|
71
|
+
lines.push(` <meta name="viewport" content="${escAttr(hd.viewport)}">`);
|
|
72
|
+
if (hd.title) lines.push(` <title>${esc(hd.title)}</title>`);
|
|
73
|
+
if (hd.fav) lines.push(` <link rel="icon" href="${escAttr(hd.fav)}" type="${faviconType(hd.fav)}">`);
|
|
74
|
+
for (const m of hd.metaTags) lines.push(` <meta name="${escAttr(m.name)}" content="${escAttr(m.content)}">`);
|
|
75
|
+
for (const m of hd.metaProps) lines.push(` <meta property="${escAttr(m.property)}" content="${escAttr(m.content)}">`);
|
|
76
|
+
for (const css of cssLinks) lines.push(` <link rel="stylesheet" href="${escAttr(css)}">`);
|
|
77
|
+
if (scopedCSS.trim()) lines.push(` <style>\n${scopedCSS}\n </style>`);
|
|
78
|
+
for (const js of jsLinks) {
|
|
79
|
+
const extra = Object.entries(js.attrs)
|
|
80
|
+
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
81
|
+
.join(" ");
|
|
82
|
+
lines.push(` <script src="${escAttr(js.src)}"${extra ? " " + extra : ""}></script>`);
|
|
83
|
+
}
|
|
84
|
+
if (companionJS) lines.push(` <script src="${escAttr(companionJS)}"></script>`);
|
|
85
|
+
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
86
|
+
}
|