Remove jit.literal and handle literal embedding automatically
This commit is contained in:
parent
328cb0c655
commit
dff22f2b41
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "@luaneko/lstd",
|
||||
"version": "0.1.6",
|
||||
"version": "0.2.0",
|
||||
"exports": "./mod.ts"
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ export class Emitter<F extends (...args: any) => void> {
|
||||
${jit.map(" ", ls, ({ f }) => {
|
||||
return jit`${f}(${jit.fragment(", ", ...ps.slice(0, f.length))});`;
|
||||
})}
|
||||
return ${jit.literal(ls.length !== 0)};
|
||||
return ${ls.length !== 0};
|
||||
}`;
|
||||
}
|
||||
}
|
||||
|
20
jit.ts
20
jit.ts
@ -18,6 +18,21 @@ export class JitCompiler {
|
||||
}
|
||||
|
||||
enclose(x: unknown) {
|
||||
switch (typeof x) {
|
||||
case "boolean":
|
||||
case "number":
|
||||
return this.write(`${x}`);
|
||||
case "bigint":
|
||||
return this.write(`${x}n`);
|
||||
case "string":
|
||||
return this.write(JSON.stringify(x));
|
||||
case "object":
|
||||
if (x === null) return this.write(`null`);
|
||||
break;
|
||||
case "undefined":
|
||||
return this.write(`undefined`);
|
||||
}
|
||||
|
||||
let name = this.#args.get(x);
|
||||
if (!name) this.#args.set(x, (name = `__x${this.#args.size}`));
|
||||
this.write(name);
|
||||
@ -52,7 +67,6 @@ export function jit(
|
||||
jit.compiled = compiled;
|
||||
jit.raw = raw;
|
||||
jit.fragment = fragment;
|
||||
jit.literal = literal;
|
||||
jit.map = map;
|
||||
jit.if = condition;
|
||||
|
||||
@ -92,10 +106,6 @@ export function fragment(
|
||||
};
|
||||
}
|
||||
|
||||
export function literal(x: unknown) {
|
||||
return raw(typeof x === "undefined" ? "undefined" : JSON.stringify(x));
|
||||
}
|
||||
|
||||
export function map<T>(
|
||||
sep: string | JitFragment,
|
||||
xs: Iterable<T>,
|
||||
|
Loading…
Reference in New Issue
Block a user