@yannosay/sinth 0.19.0 → 1.0.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 +23 -3
- package/package.json +1 -1
- package/src/core/compiler.ts +6 -0
- package/src/core/expr.ts +4 -0
- package/src/core/parser.ts +14 -3
package/README.md
CHANGED
|
@@ -169,11 +169,31 @@ Chain statements with ;. No wrapper functions. No script blocks.
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
```ts
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
page
|
|
173
|
+
|
|
174
|
+
title = "Remove Test"
|
|
175
|
+
|
|
176
|
+
var bool show = true
|
|
177
|
+
var bool deleted = false
|
|
178
|
+
|
|
179
|
+
Main {
|
|
180
|
+
Button(onClick: show = not show) {
|
|
181
|
+
if show { "Hide Box" } else { "Show Box" }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
Button(onClick: remove("my-box"); deleted = true) {
|
|
185
|
+
"Delete Box Forever"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if show and not deleted {
|
|
189
|
+
Div(id: "my-box") {
|
|
190
|
+
Paragraph { "I'm a box!" }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
174
193
|
}
|
|
194
|
+
|
|
175
195
|
```
|
|
176
|
-
Delete elements.
|
|
196
|
+
Delete elements. Easy.
|
|
177
197
|
|
|
178
198
|
Compiles to pure HTML.
|
|
179
199
|
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -385,6 +385,12 @@ export function renderChild(
|
|
|
385
385
|
}
|
|
386
386
|
if (child.expression.kind === "call" && child.expression.callee?.kind === "variable") {
|
|
387
387
|
const fnName = child.expression.callee.name;
|
|
388
|
+
if (fnName === "remove" && child.expression.args && child.expression.args.length === 1) {
|
|
389
|
+
const arg = child.expression.args[0];
|
|
390
|
+
if (arg.kind === "literal" && arg.value?.kind === "str") {
|
|
391
|
+
return `<span data-sinth-remove="${esc(arg.value.value)}"></span>`;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
388
394
|
const fnDef = fnName ? ctx.functionDefs.find(f => f.name === fnName) : undefined;
|
|
389
395
|
if (fnDef && fnDef.returnType === "ui") {
|
|
390
396
|
const callArgs = child.expression.args ?? [];
|
package/src/core/expr.ts
CHANGED
|
@@ -38,6 +38,10 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
38
38
|
return `${expr.target}${expr.op}`;
|
|
39
39
|
case "call": {
|
|
40
40
|
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
41
|
+
if (callee === "remove") {
|
|
42
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
43
|
+
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
44
|
+
}
|
|
41
45
|
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
42
46
|
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
43
47
|
return callee;
|
package/src/core/parser.ts
CHANGED
|
@@ -274,7 +274,8 @@ private parseArrayLiteral(): Literal {
|
|
|
274
274
|
parseExpression(skipBinary: boolean = false): Expression | null {
|
|
275
275
|
const tok = this.peek();
|
|
276
276
|
|
|
277
|
-
|
|
277
|
+
|
|
278
|
+
// unary 'not'
|
|
278
279
|
if ((tok.type === TT.IDENT && tok.value === "not") || tok.type === TT.OP_NOT) {
|
|
279
280
|
if (tok.type === TT.IDENT) this.consume(TT.IDENT); else this.consume(TT.OP_NOT);
|
|
280
281
|
const operand = this.parseExpression();
|
|
@@ -300,7 +301,17 @@ private parseArrayLiteral(): Literal {
|
|
|
300
301
|
return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
|
|
301
302
|
}
|
|
302
303
|
|
|
304
|
+
// remove as function
|
|
305
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
306
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
307
|
+
return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
|
|
308
|
+
}
|
|
309
|
+
|
|
303
310
|
// var or assignment
|
|
311
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
312
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
313
|
+
return { kind: "variable", name };
|
|
314
|
+
}
|
|
304
315
|
if (tok.type === TT.IDENT) {
|
|
305
316
|
const name = this.consume(TT.IDENT).value;
|
|
306
317
|
let varExpr: Expression;
|
|
@@ -1145,8 +1156,8 @@ private parseArrayLiteral(): Literal {
|
|
|
1145
1156
|
continue;
|
|
1146
1157
|
}
|
|
1147
1158
|
else if (this.check(TT.KW_RETURN)) { children.push(this.parseReturnStmt()); continue; }
|
|
1148
|
-
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); }
|
|
1149
|
-
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); }
|
|
1159
|
+
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
|
|
1160
|
+
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
|
|
1150
1161
|
else if (this.check(TT.IDENT)) {
|
|
1151
1162
|
const loc = this.peek().loc;
|
|
1152
1163
|
const rawName = this.peek().value;
|