funkscript 0.2.6 → 0.2.7
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 +1 -1
- package/dist/cli/build-watch.d.ts.map +1 -1
- package/dist/cli/build-watch.js +4 -24
- package/dist/cli/build-watch.js.map +1 -1
- package/dist/cli/build.d.ts.map +1 -1
- package/dist/cli/build.js +3 -58
- package/dist/cli/build.js.map +1 -1
- package/dist/cli/check-policy.d.ts +9 -0
- package/dist/cli/check-policy.d.ts.map +1 -0
- package/dist/cli/check-policy.js +39 -0
- package/dist/cli/check-policy.js.map +1 -0
- package/dist/cli/check.d.ts.map +1 -1
- package/dist/cli/check.js +16 -30
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +11 -0
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +2 -6
- package/dist/cli/doctor.js.map +1 -1
- package/dist/cli/index.js +3 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/query.d.ts +3 -0
- package/dist/cli/query.d.ts.map +1 -0
- package/dist/cli/query.js +152 -0
- package/dist/cli/query.js.map +1 -0
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +2 -29
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/tsc.d.ts.map +1 -1
- package/dist/cli/tsc.js +2 -6
- package/dist/cli/tsc.js.map +1 -1
- package/dist/compiler/check.d.ts.map +1 -1
- package/dist/compiler/check.js +18 -28
- package/dist/compiler/check.js.map +1 -1
- package/dist/compiler/diagnostics-json.d.ts +73 -0
- package/dist/compiler/diagnostics-json.d.ts.map +1 -0
- package/dist/compiler/diagnostics-json.js +957 -0
- package/dist/compiler/diagnostics-json.js.map +1 -0
- package/dist/compiler/escape-audit.d.ts +46 -0
- package/dist/compiler/escape-audit.d.ts.map +1 -0
- package/dist/compiler/escape-audit.js +682 -0
- package/dist/compiler/escape-audit.js.map +1 -0
- package/dist/compiler/modules/output-plan.d.ts.map +1 -1
- package/dist/compiler/modules/output-plan.js +10 -5
- package/dist/compiler/modules/output-plan.js.map +1 -1
- package/dist/compiler/modules/source-files.d.ts +2 -0
- package/dist/compiler/modules/source-files.d.ts.map +1 -1
- package/dist/compiler/modules/source-files.js +8 -0
- package/dist/compiler/modules/source-files.js.map +1 -1
- package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
- package/dist/compiler/numeric-comparisons.js +13 -7
- package/dist/compiler/numeric-comparisons.js.map +1 -1
- package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
- package/dist/compiler/pipelines/resolution.js +24 -23
- package/dist/compiler/pipelines/resolution.js.map +1 -1
- package/dist/compiler/query.d.ts +53 -0
- package/dist/compiler/query.d.ts.map +1 -0
- package/dist/compiler/query.js +781 -0
- package/dist/compiler/query.js.map +1 -0
- package/dist/compiler/source-map.d.ts.map +1 -1
- package/dist/compiler/source-map.js +2 -7
- package/dist/compiler/source-map.js.map +1 -1
- package/dist/compiler/type-view-paths.d.ts +2 -0
- package/dist/compiler/type-view-paths.d.ts.map +1 -1
- package/dist/compiler/type-view-paths.js +8 -0
- package/dist/compiler/type-view-paths.js.map +1 -1
- package/dist/compiler/typescript-context.d.ts.map +1 -1
- package/dist/compiler/typescript-context.js +5 -7
- package/dist/compiler/typescript-context.js.map +1 -1
- package/dist/emitter/assemblyscript.d.ts.map +1 -1
- package/dist/emitter/assemblyscript.js +120 -136
- package/dist/emitter/assemblyscript.js.map +1 -1
- package/dist/formatter/index.d.ts +9 -0
- package/dist/formatter/index.d.ts.map +1 -1
- package/dist/formatter/index.js +158 -2
- package/dist/formatter/index.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lsp/completion.d.ts +25 -0
- package/dist/lsp/completion.d.ts.map +1 -1
- package/dist/lsp/completion.js +1114 -51
- package/dist/lsp/completion.js.map +1 -1
- package/dist/lsp/definition.d.ts +2 -0
- package/dist/lsp/definition.d.ts.map +1 -1
- package/dist/lsp/definition.js +52 -58
- package/dist/lsp/definition.js.map +1 -1
- package/dist/lsp/hover.d.ts +5 -1
- package/dist/lsp/hover.d.ts.map +1 -1
- package/dist/lsp/hover.js +1000 -41
- package/dist/lsp/hover.js.map +1 -1
- package/dist/lsp/imports.d.ts +2 -0
- package/dist/lsp/imports.d.ts.map +1 -1
- package/dist/lsp/imports.js +35 -20
- package/dist/lsp/imports.js.map +1 -1
- package/dist/lsp/navigation.d.ts +1 -1
- package/dist/lsp/navigation.d.ts.map +1 -1
- package/dist/lsp/navigation.js +30 -12
- package/dist/lsp/navigation.js.map +1 -1
- package/dist/lsp/range-format.d.ts +4 -0
- package/dist/lsp/range-format.d.ts.map +1 -0
- package/dist/lsp/range-format.js +88 -0
- package/dist/lsp/range-format.js.map +1 -0
- package/dist/lsp/schema-import-hover.d.ts +8 -0
- package/dist/lsp/schema-import-hover.d.ts.map +1 -0
- package/dist/lsp/schema-import-hover.js +52 -0
- package/dist/lsp/schema-import-hover.js.map +1 -0
- package/dist/lsp/server.d.ts +1 -0
- package/dist/lsp/server.d.ts.map +1 -1
- package/dist/lsp/server.js +53 -48
- package/dist/lsp/server.js.map +1 -1
- package/dist/lsp/workspace.d.ts +3 -1
- package/dist/lsp/workspace.d.ts.map +1 -1
- package/dist/lsp/workspace.js +66 -10
- package/dist/lsp/workspace.js.map +1 -1
- package/dist/plugins/bun.d.ts +2 -18
- package/dist/plugins/bun.d.ts.map +1 -1
- package/dist/plugins/bun.js +5 -30
- package/dist/plugins/bun.js.map +1 -1
- package/dist/plugins/esbuild.d.ts +1 -1
- package/dist/plugins/esbuild.d.ts.map +1 -1
- package/dist/plugins/esbuild.js +10 -36
- package/dist/plugins/esbuild.js.map +1 -1
- package/dist/plugins/server-boundary.d.ts +14 -0
- package/dist/plugins/server-boundary.d.ts.map +1 -0
- package/dist/plugins/server-boundary.js +25 -0
- package/dist/plugins/server-boundary.js.map +1 -0
- package/dist/plugins/vite.d.ts +2 -18
- package/dist/plugins/vite.d.ts.map +1 -1
- package/dist/plugins/vite.js +4 -29
- package/dist/plugins/vite.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/build-watch.ts +4 -26
- package/src/cli/build.ts +9 -60
- package/src/cli/check-policy.ts +54 -0
- package/src/cli/check.ts +19 -31
- package/src/cli/commands.ts +13 -0
- package/src/cli/doctor.ts +2 -5
- package/src/cli/index.ts +3 -0
- package/src/cli/query.ts +191 -0
- package/src/cli/run.ts +5 -30
- package/src/cli/tsc.ts +2 -5
- package/src/compiler/check.ts +28 -48
- package/src/compiler/diagnostics-json.ts +1205 -0
- package/src/compiler/escape-audit.ts +913 -0
- package/src/compiler/modules/output-plan.ts +11 -9
- package/src/compiler/modules/source-files.ts +9 -0
- package/src/compiler/numeric-comparisons.ts +14 -8
- package/src/compiler/pipelines/resolution.ts +30 -26
- package/src/compiler/query.ts +1177 -0
- package/src/compiler/source-map.ts +3 -7
- package/src/compiler/type-view-paths.ts +7 -0
- package/src/compiler/typescript-context.ts +6 -7
- package/src/emitter/assemblyscript.ts +101 -147
- package/src/formatter/index.ts +229 -2
- package/src/index.ts +3 -0
- package/src/lsp/completion.ts +1452 -66
- package/src/lsp/definition.ts +50 -58
- package/src/lsp/hover.ts +1442 -40
- package/src/lsp/imports.ts +55 -21
- package/src/lsp/navigation.ts +41 -22
- package/src/lsp/range-format.ts +102 -0
- package/src/lsp/schema-import-hover.ts +76 -0
- package/src/lsp/server.ts +69 -69
- package/src/lsp/workspace.ts +63 -9
- package/src/plugins/bun.ts +14 -66
- package/src/plugins/esbuild.ts +16 -55
- package/src/plugins/server-boundary.ts +41 -0
- package/src/plugins/vite.ts +12 -62
|
@@ -2,6 +2,7 @@ import MagicString, { Bundle, type SourceMap } from "magic-string";
|
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import type { Program } from "../ast/types.js";
|
|
4
4
|
import { Emitter, type EmitterOptions } from "../emitter/emitter.js";
|
|
5
|
+
import { funSourceToTypeScriptFile } from "./modules/source-files.js";
|
|
5
6
|
|
|
6
7
|
/** Map generated TypeScript back to declarations in the original source. */
|
|
7
8
|
export function generateSourceMap(
|
|
@@ -12,13 +13,8 @@ export function generateSourceMap(
|
|
|
12
13
|
emitterOptions: EmitterOptions,
|
|
13
14
|
outputFile?: string,
|
|
14
15
|
): SourceMap {
|
|
15
|
-
const outTarget =
|
|
16
|
-
|
|
17
|
-
: filename.endsWith(".funx")
|
|
18
|
-
? `${filename.slice(0, -5)}.tsx`
|
|
19
|
-
: filename.endsWith(".fun")
|
|
20
|
-
? `${filename.slice(0, -4)}.ts`
|
|
21
|
-
: `${filename}.ts`;
|
|
16
|
+
const outTarget =
|
|
17
|
+
outputFile || funSourceToTypeScriptFile(filename) || `${filename}.ts`;
|
|
22
18
|
|
|
23
19
|
const s = new MagicString(source);
|
|
24
20
|
const movedImports: Array<{
|
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
/** Default location and manifest shared by the type-view writer and readers. */
|
|
2
2
|
export const DEFAULT_TYPE_VIEW_DIR = ".funscript/types";
|
|
3
3
|
export const TYPE_VIEW_MANIFEST = ".funscript-type-view.json";
|
|
4
|
+
|
|
5
|
+
/** Read source names from the current manifest or its legacy array format. */
|
|
6
|
+
export function typeViewManifestSources(manifest: unknown): unknown[] {
|
|
7
|
+
if (Array.isArray(manifest)) return manifest;
|
|
8
|
+
if (manifest && typeof manifest === "object") return Object.keys(manifest);
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import ts from "typescript";
|
|
4
4
|
import { isFileNotFoundError } from "../errors.js";
|
|
5
|
+
import { funSourceToTypeScriptFile } from "./modules/source-files.js";
|
|
5
6
|
|
|
6
7
|
// Declaration files are immutable for the lifetime of a process in the common
|
|
7
8
|
// case. Share parsed ASTs between compilation contexts, while checking file
|
|
@@ -174,13 +175,11 @@ export class TypeScriptCompilationContext {
|
|
|
174
175
|
}
|
|
175
176
|
|
|
176
177
|
setFilename(filename: string, reuseProgram = false): void {
|
|
177
|
-
const nextFile =
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
? filename
|
|
181
|
-
: filename.
|
|
182
|
-
? filename
|
|
183
|
-
: filename + ".ts";
|
|
178
|
+
const nextFile =
|
|
179
|
+
funSourceToTypeScriptFile(filename) ??
|
|
180
|
+
(filename.endsWith(".ts") || filename.endsWith(".tsx")
|
|
181
|
+
? filename
|
|
182
|
+
: `${filename}.ts`);
|
|
184
183
|
if (this.virtualFile && this.virtualFile !== nextFile && !reuseProgram)
|
|
185
184
|
this.program = undefined;
|
|
186
185
|
// Directory tooling can opt into using the old Program as an incremental
|
|
@@ -58,6 +58,12 @@ function tupleClassName(elements: string[]): string {
|
|
|
58
58
|
return `__fs_tuple_${elements.length}_${elements.map(sanitize).join("_")}`;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
function assemblyScriptValueType(type: string): string {
|
|
62
|
+
if (BOOLEAN_TYPES.has(type)) return "bool";
|
|
63
|
+
const tuple = parseTupleElementTypeList(type);
|
|
64
|
+
return tuple ? tupleClassName(tuple) : type;
|
|
65
|
+
}
|
|
66
|
+
|
|
61
67
|
function transformKernelSource(code: string): {
|
|
62
68
|
transformed: string;
|
|
63
69
|
allTuples: Map<string, string[]>;
|
|
@@ -366,19 +372,12 @@ export async function prepareAssemblyScript(
|
|
|
366
372
|
|
|
367
373
|
for (const [clsName, elemTypes] of allTuples) {
|
|
368
374
|
const fieldsDef = elemTypes
|
|
369
|
-
.map(
|
|
370
|
-
(t, fi) =>
|
|
371
|
-
` _${fi}: ${BOOLEAN_TYPES.has(t) ? "bool" : parseTupleElementTypeList(t) ? tupleClassName(parseTupleElementTypeList(t)!) : t};`,
|
|
372
|
-
)
|
|
375
|
+
.map((t, fi) => ` _${fi}: ${assemblyScriptValueType(t)};`)
|
|
373
376
|
.join("\n");
|
|
374
377
|
let opDef = "";
|
|
375
378
|
const firstType = elemTypes[0];
|
|
376
379
|
if (elemTypes.every((t) => t === firstType)) {
|
|
377
|
-
const asElem =
|
|
378
|
-
? "bool"
|
|
379
|
-
: parseTupleElementTypeList(firstType)
|
|
380
|
-
? tupleClassName(parseTupleElementTypeList(firstType)!)
|
|
381
|
-
: firstType;
|
|
380
|
+
const asElem = assemblyScriptValueType(firstType);
|
|
382
381
|
const branches = elemTypes
|
|
383
382
|
.map((_, fi) => `if (index == ${fi}) return this._${fi};`)
|
|
384
383
|
.join(" ");
|
|
@@ -387,10 +386,7 @@ export async function prepareAssemblyScript(
|
|
|
387
386
|
compileSource += `class ${clsName} {\n${fieldsDef}${opDef}\n}\n`;
|
|
388
387
|
|
|
389
388
|
const ctorParams = elemTypes
|
|
390
|
-
.map(
|
|
391
|
-
(t, fi) =>
|
|
392
|
-
`_${fi}: ${BOOLEAN_TYPES.has(t) ? "bool" : parseTupleElementTypeList(t) ? tupleClassName(parseTupleElementTypeList(t)!) : t}`,
|
|
393
|
-
)
|
|
389
|
+
.map((t, fi) => `_${fi}: ${assemblyScriptValueType(t)}`)
|
|
394
390
|
.join(", ");
|
|
395
391
|
const ctorBody = elemTypes
|
|
396
392
|
.map((_, fi) => `o._${fi} = _${fi};`)
|
|
@@ -399,33 +395,19 @@ export async function prepareAssemblyScript(
|
|
|
399
395
|
|
|
400
396
|
for (let fi = 0; fi < elemTypes.length; fi++) {
|
|
401
397
|
const t = elemTypes[fi];
|
|
402
|
-
const asType =
|
|
403
|
-
? "bool"
|
|
404
|
-
: parseTupleElementTypeList(t)
|
|
405
|
-
? tupleClassName(parseTupleElementTypeList(t)!)
|
|
406
|
-
: t;
|
|
398
|
+
const asType = assemblyScriptValueType(t);
|
|
407
399
|
compileSource += `export function __fs_get_${clsName}_${fi}(o: ${clsName}): ${asType} { return o._${fi}; }\n`;
|
|
408
400
|
}
|
|
409
401
|
}
|
|
410
402
|
|
|
411
403
|
for (const cls of classes.values()) {
|
|
412
404
|
for (const field of cls.fields) {
|
|
413
|
-
const
|
|
414
|
-
const asFieldType = BOOLEAN_TYPES.has(field.type)
|
|
415
|
-
? "bool"
|
|
416
|
-
: tupleElems
|
|
417
|
-
? tupleClassName(tupleElems)
|
|
418
|
-
: field.type;
|
|
405
|
+
const asFieldType = assemblyScriptValueType(field.type);
|
|
419
406
|
compileSource += `export function __fs_get_${cls.name}_${field.name}(obj: ${cls.name}): ${asFieldType} { return obj.${field.name}; }\n`;
|
|
420
407
|
}
|
|
421
408
|
const ctorParams = cls.fields
|
|
422
409
|
.map((f) => {
|
|
423
|
-
const
|
|
424
|
-
const asType = BOOLEAN_TYPES.has(f.type)
|
|
425
|
-
? "bool"
|
|
426
|
-
: tupleElems
|
|
427
|
-
? tupleClassName(tupleElems)
|
|
428
|
-
: f.type;
|
|
410
|
+
const asType = assemblyScriptValueType(f.type);
|
|
429
411
|
return `${f.name}: ${asType}`;
|
|
430
412
|
})
|
|
431
413
|
.join(", ");
|
|
@@ -457,11 +439,7 @@ export async function prepareAssemblyScript(
|
|
|
457
439
|
|
|
458
440
|
for (const elem of arrayElemTypes) {
|
|
459
441
|
const tupleElems = parseTupleElementTypeList(elem);
|
|
460
|
-
const asElem =
|
|
461
|
-
? "bool"
|
|
462
|
-
: tupleElems
|
|
463
|
-
? tupleClassName(tupleElems)
|
|
464
|
-
: elem;
|
|
442
|
+
const asElem = assemblyScriptValueType(elem);
|
|
465
443
|
const safeElem =
|
|
466
444
|
elem === "boolean"
|
|
467
445
|
? "bool"
|
|
@@ -788,38 +766,34 @@ export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
|
788
766
|
? tupleClassName(tupleElems)
|
|
789
767
|
: elem;
|
|
790
768
|
const tsElem = toTsType(elem);
|
|
791
|
-
const elemReadExpr =
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
? "BigInt.asUintN(64, raw)"
|
|
803
|
-
: elem === "u32"
|
|
804
|
-
? "(raw >>> 0)"
|
|
805
|
-
: "raw";
|
|
769
|
+
const elemReadExpr = (() => {
|
|
770
|
+
if (BOOLEAN_TYPES.has(elem)) return "Boolean(raw)";
|
|
771
|
+
if (elem === "string") return `${prefix}ReadString(exports, raw)`;
|
|
772
|
+
if (classes.has(elem)) return `${prefix}Read_${elem}(exports, raw)`;
|
|
773
|
+
if (tupleElems)
|
|
774
|
+
return `${prefix}Read_${tupleClassName(tupleElems)}(exports, raw)`;
|
|
775
|
+
if (elem === "i64") return "BigInt.asIntN(64, raw)";
|
|
776
|
+
if (elem === "u64") return "BigInt.asUintN(64, raw)";
|
|
777
|
+
if (elem === "u32") return "(raw >>> 0)";
|
|
778
|
+
return "raw";
|
|
779
|
+
})();
|
|
806
780
|
|
|
807
|
-
const itemAllocExpr =
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
781
|
+
const itemAllocExpr = (() => {
|
|
782
|
+
if (BOOLEAN_TYPES.has(elem)) return "";
|
|
783
|
+
if (elem === "string")
|
|
784
|
+
return `const __v = ${prefix}AllocString(exports, item);`;
|
|
785
|
+
if (classes.has(elem))
|
|
786
|
+
return `const __v = ${prefix}Alloc_${elem}(exports, item);`;
|
|
787
|
+
if (tupleElems)
|
|
788
|
+
return `const __v = ${prefix}Alloc_${tupleClassName(tupleElems)}(exports, item);`;
|
|
789
|
+
return "";
|
|
790
|
+
})();
|
|
791
|
+
const itemVal = (() => {
|
|
792
|
+
if (BOOLEAN_TYPES.has(elem)) return "item ? 1 : 0";
|
|
793
|
+
if (elem === "string" || classes.has(elem) || tupleElems) return "__v";
|
|
794
|
+
if (elem === "u32") return "(item >>> 0)";
|
|
795
|
+
return "item";
|
|
796
|
+
})();
|
|
823
797
|
|
|
824
798
|
lines.push(
|
|
825
799
|
`function ${prefix}Read_Array_${safeElem}(exports: Record<string, unknown>, ptr: number): ${tsElem}[] {`,
|
|
@@ -971,18 +945,22 @@ export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
|
971
945
|
.join(" ");
|
|
972
946
|
return `${head} ${checks}`;
|
|
973
947
|
}
|
|
974
|
-
const invalid =
|
|
975
|
-
type
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
948
|
+
const invalid = (() => {
|
|
949
|
+
switch (type) {
|
|
950
|
+
case "i64":
|
|
951
|
+
return `typeof ${valExpr} !== "bigint" || ${valExpr} < -9223372036854775808n || ${valExpr} > 9223372036854775807n`;
|
|
952
|
+
case "u64":
|
|
953
|
+
return `typeof ${valExpr} !== "bigint" || ${valExpr} < 0n || ${valExpr} > 18446744073709551615n`;
|
|
954
|
+
case "i32":
|
|
955
|
+
return `!Number.isInteger(${valExpr}) || ${valExpr} < -2147483648 || ${valExpr} > 2147483647`;
|
|
956
|
+
case "u32":
|
|
957
|
+
return `!Number.isInteger(${valExpr}) || ${valExpr} < 0 || ${valExpr} > 4294967295`;
|
|
958
|
+
case "f32":
|
|
959
|
+
return `!Number.isFinite(${valExpr}) || Math.abs(${valExpr}) > 3.4028234663852886e38`;
|
|
960
|
+
default:
|
|
961
|
+
return `!Number.isFinite(${valExpr})`;
|
|
962
|
+
}
|
|
963
|
+
})();
|
|
986
964
|
return `if (${invalid}) return Effect.fail({ _tag: "WasmInputError" as const, function: ${JSON.stringify(fnName)}, parameter: ${JSON.stringify(paramName)}, expected: ${JSON.stringify(type)}, value: ${valExpr} });`;
|
|
987
965
|
};
|
|
988
966
|
|
|
@@ -1026,20 +1004,15 @@ export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
|
1026
1004
|
decl.code.includes("Array") ||
|
|
1027
1005
|
decl.code.includes("new ") ||
|
|
1028
1006
|
decl.code.includes("v128");
|
|
1029
|
-
const
|
|
1007
|
+
const wasmResultType =
|
|
1030
1008
|
fn.result === "void"
|
|
1031
|
-
? "
|
|
1032
|
-
:
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
: isBooleanResult
|
|
1039
|
-
? "(...args: (number | bigint)[]) => number"
|
|
1040
|
-
: BIGINT_SCALAR_TYPES.has(fn.result)
|
|
1041
|
-
? "(...args: (number | bigint)[]) => bigint"
|
|
1042
|
-
: `(...args: (number | bigint)[]) => number`;
|
|
1009
|
+
? "void"
|
|
1010
|
+
: !isHandleResult &&
|
|
1011
|
+
!isBooleanResult &&
|
|
1012
|
+
BIGINT_SCALAR_TYPES.has(fn.result)
|
|
1013
|
+
? "bigint"
|
|
1014
|
+
: "number";
|
|
1015
|
+
const wasmFnType = `(...args: (number | bigint)[]) => ${wasmResultType}`;
|
|
1043
1016
|
|
|
1044
1017
|
const errorType = `{ readonly _tag: "WasmInputError"; readonly function: string; readonly parameter: string; readonly expected: string; readonly value: unknown } | { readonly _tag: "WasmTrapError"; readonly function: string; readonly cause: unknown } | { readonly _tag: "WasmInitializationError"; readonly function: string; readonly cause: unknown }`;
|
|
1045
1018
|
|
|
@@ -1053,38 +1026,31 @@ export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
|
1053
1026
|
let effectTryBody: string;
|
|
1054
1027
|
let rawBody: string;
|
|
1055
1028
|
|
|
1056
|
-
const readExpr =
|
|
1057
|
-
fn.result === "void"
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
: fn.result === "u32"
|
|
1082
|
-
? `((__res) >>> 0)`
|
|
1083
|
-
: fn.result === "u64"
|
|
1084
|
-
? `BigInt.asUintN(64, __res as bigint)`
|
|
1085
|
-
: fn.result === "i64"
|
|
1086
|
-
? `BigInt.asIntN(64, __res as bigint)`
|
|
1087
|
-
: `__res`;
|
|
1029
|
+
const readExpr = (() => {
|
|
1030
|
+
if (fn.result === "void") return "";
|
|
1031
|
+
if (isBooleanResult) return "Boolean(__res)";
|
|
1032
|
+
if (isBufferResult)
|
|
1033
|
+
return `${prefix}Read(exports, __res, ${toJsTypedArray(fn.result)})`;
|
|
1034
|
+
if (isStringResult) return `${prefix}ReadString(exports, __res)`;
|
|
1035
|
+
if (isClassResult) return `${prefix}Read_${fn.result}(exports, __res)`;
|
|
1036
|
+
if (isTupleResult)
|
|
1037
|
+
return `${prefix}Read_${tupleClassName(parseTupleElementTypeList(fn.result)!)}(exports, __res)`;
|
|
1038
|
+
if (isArrayResult) {
|
|
1039
|
+
const elem = parseArrayElementType(fn.result)!;
|
|
1040
|
+
const tupleElems = parseTupleElementTypeList(elem);
|
|
1041
|
+
const safeElem =
|
|
1042
|
+
elem === "boolean"
|
|
1043
|
+
? "bool"
|
|
1044
|
+
: tupleElems
|
|
1045
|
+
? tupleClassName(tupleElems)
|
|
1046
|
+
: elem;
|
|
1047
|
+
return `${prefix}Read_Array_${safeElem}(exports, __res)`;
|
|
1048
|
+
}
|
|
1049
|
+
if (fn.result === "u32") return "((__res) >>> 0)";
|
|
1050
|
+
if (fn.result === "u64") return "BigInt.asUintN(64, __res as bigint)";
|
|
1051
|
+
if (fn.result === "i64") return "BigInt.asIntN(64, __res as bigint)";
|
|
1052
|
+
return "__res";
|
|
1053
|
+
})();
|
|
1088
1054
|
|
|
1089
1055
|
if (!hasBuffers) {
|
|
1090
1056
|
const passed = fn.params
|
|
@@ -1094,30 +1060,18 @@ export function emitAssemblyScript(decl: AsDeclaration): string {
|
|
|
1094
1060
|
.join(", ");
|
|
1095
1061
|
const call = `(${prefix}Get()[${JSON.stringify(fn.name)}] as ${wasmFnType})(${passed})`;
|
|
1096
1062
|
const effectCall = `(exports[${JSON.stringify(fn.name)}] as ${wasmFnType})(${passed})`;
|
|
1097
|
-
const
|
|
1098
|
-
fn.result === "void"
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
const effectRaw =
|
|
1110
|
-
fn.result === "void"
|
|
1111
|
-
? `${effectCall};`
|
|
1112
|
-
: isBooleanResult
|
|
1113
|
-
? `Boolean(${effectCall})`
|
|
1114
|
-
: fn.result === "u32"
|
|
1115
|
-
? `(${effectCall} as number >>> 0)`
|
|
1116
|
-
: fn.result === "u64"
|
|
1117
|
-
? `BigInt.asUintN(64, ${effectCall} as bigint)`
|
|
1118
|
-
: fn.result === "i64"
|
|
1119
|
-
? `BigInt.asIntN(64, ${effectCall} as bigint)`
|
|
1120
|
-
: effectCall;
|
|
1063
|
+
const convertCallResult = (callExpr: string): string => {
|
|
1064
|
+
if (fn.result === "void") return `${callExpr};`;
|
|
1065
|
+
if (isBooleanResult) return `Boolean(${callExpr})`;
|
|
1066
|
+
if (fn.result === "u32") return `(${callExpr} as number >>> 0)`;
|
|
1067
|
+
if (fn.result === "u64")
|
|
1068
|
+
return `BigInt.asUintN(64, ${callExpr} as bigint)`;
|
|
1069
|
+
if (fn.result === "i64")
|
|
1070
|
+
return `BigInt.asIntN(64, ${callExpr} as bigint)`;
|
|
1071
|
+
return callExpr;
|
|
1072
|
+
};
|
|
1073
|
+
const raw = convertCallResult(call);
|
|
1074
|
+
const effectRaw = convertCallResult(effectCall);
|
|
1121
1075
|
syncBody = fn.result === "void" ? raw : `return ${raw};`;
|
|
1122
1076
|
effectTryBody = fn.result === "void" ? effectRaw : `return ${effectRaw};`;
|
|
1123
1077
|
rawBody = syncBody;
|
package/src/formatter/index.ts
CHANGED
|
@@ -13,6 +13,11 @@ import {
|
|
|
13
13
|
type FunctionExpression,
|
|
14
14
|
type Identifier,
|
|
15
15
|
type ImportDeclaration,
|
|
16
|
+
type JSXAttribute,
|
|
17
|
+
type JSXChild,
|
|
18
|
+
type JSXElement,
|
|
19
|
+
type JSXSelfClosingElement,
|
|
20
|
+
type JSXTagName,
|
|
16
21
|
type Literal,
|
|
17
22
|
type MatchArm,
|
|
18
23
|
type MatchExpression,
|
|
@@ -65,6 +70,7 @@ export interface FormatOptions {
|
|
|
65
70
|
export class Formatter {
|
|
66
71
|
private indentSize: number;
|
|
67
72
|
private lineWidth: number;
|
|
73
|
+
private inlinePipelineDepth = 0;
|
|
68
74
|
private leadingComments = new Map<AstInspectNode, Comment[]>();
|
|
69
75
|
private trailingComments = new Map<number, Comment>();
|
|
70
76
|
private eofComments: Comment[] = [];
|
|
@@ -564,6 +570,12 @@ export class Formatter {
|
|
|
564
570
|
}
|
|
565
571
|
|
|
566
572
|
private shouldExpandPipeline(chain: Expression[], indent: number): boolean {
|
|
573
|
+
if (
|
|
574
|
+
this.inlinePipelineDepth > 0 &&
|
|
575
|
+
chain[0].span.start.line === chain[chain.length - 1].span.end.line
|
|
576
|
+
) {
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
567
579
|
if (chain.length > 2) return true;
|
|
568
580
|
if (
|
|
569
581
|
chain[0].span.end.line < chain[1].span.start.line ||
|
|
@@ -589,7 +601,13 @@ export class Formatter {
|
|
|
589
601
|
const isMulti = this.shouldExpandPipeline(chain, indent);
|
|
590
602
|
|
|
591
603
|
if (!isMulti) {
|
|
592
|
-
return
|
|
604
|
+
return chain
|
|
605
|
+
.map((step, index) =>
|
|
606
|
+
index === 0
|
|
607
|
+
? this.formatExpression(step, indent)
|
|
608
|
+
: `${operators[index - 1]} ${this.formatExpression(step, indent)}`,
|
|
609
|
+
)
|
|
610
|
+
.join(" ");
|
|
593
611
|
}
|
|
594
612
|
|
|
595
613
|
const head = this.formatExpression(chain[0], indent);
|
|
@@ -1222,8 +1240,9 @@ export class Formatter {
|
|
|
1222
1240
|
case "UpperIdentifier":
|
|
1223
1241
|
return expr.name;
|
|
1224
1242
|
case "JSXElement":
|
|
1243
|
+
return this.formatJsxElement(expr, indent);
|
|
1225
1244
|
case "JSXSelfClosingElement":
|
|
1226
|
-
return this.
|
|
1245
|
+
return this.formatJsxSelfClosingElement(expr, indent);
|
|
1227
1246
|
case "JsExpression":
|
|
1228
1247
|
return `js ${this.formatExpression(expr.expression, indent)}`;
|
|
1229
1248
|
case "TsEscapeExpression":
|
|
@@ -1233,6 +1252,214 @@ export class Formatter {
|
|
|
1233
1252
|
}
|
|
1234
1253
|
}
|
|
1235
1254
|
|
|
1255
|
+
private formatJsxTagName(name: JSXTagName): string {
|
|
1256
|
+
switch (name.kind) {
|
|
1257
|
+
case "Identifier":
|
|
1258
|
+
case "UpperIdentifier":
|
|
1259
|
+
return name.name;
|
|
1260
|
+
case "MemberExpression":
|
|
1261
|
+
return `${this.formatJsxTagName(name.object as JSXTagName)}.${name.property.name}`;
|
|
1262
|
+
default:
|
|
1263
|
+
return "";
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
private formatJsxAttribute(attr: JSXAttribute, indent: number): string {
|
|
1268
|
+
if (attr.kind === "JSXSpreadAttribute") {
|
|
1269
|
+
return `{...${this.formatExpression(attr.argument, indent)}}`;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
const name = attr.name.name;
|
|
1273
|
+
if (attr.value === undefined) {
|
|
1274
|
+
return name;
|
|
1275
|
+
}
|
|
1276
|
+
if (attr.value.kind === "StringLiteral") {
|
|
1277
|
+
return `${name}=${this.formatStringLiteral(attr.value)}`;
|
|
1278
|
+
}
|
|
1279
|
+
if (attr.value.kind === "JSXExpressionContainer") {
|
|
1280
|
+
return `${name}=${this.formatJsxExpressionContainer(
|
|
1281
|
+
attr.value.expression,
|
|
1282
|
+
attr.value.span,
|
|
1283
|
+
indent,
|
|
1284
|
+
)}`;
|
|
1285
|
+
}
|
|
1286
|
+
return `${name}=${this.formatJsxExpressionContainer(
|
|
1287
|
+
attr.value,
|
|
1288
|
+
attr.value.span,
|
|
1289
|
+
indent,
|
|
1290
|
+
)}`;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
private formatJsxExpressionContainer(
|
|
1294
|
+
expression: Expression,
|
|
1295
|
+
span: { start: { line: number }; end: { line: number } },
|
|
1296
|
+
indent: number,
|
|
1297
|
+
): string {
|
|
1298
|
+
const isSingleLine = span.start.line === span.end.line;
|
|
1299
|
+
if (isSingleLine) this.inlinePipelineDepth++;
|
|
1300
|
+
try {
|
|
1301
|
+
return `{${this.formatExpression(expression, indent)}}`;
|
|
1302
|
+
} finally {
|
|
1303
|
+
if (isSingleLine) this.inlinePipelineDepth--;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
private formatJsxOpeningTag(
|
|
1308
|
+
tagName: string,
|
|
1309
|
+
attributes: JSXAttribute[],
|
|
1310
|
+
startLine: number,
|
|
1311
|
+
endLine: number,
|
|
1312
|
+
indent: number,
|
|
1313
|
+
selfClosing: boolean,
|
|
1314
|
+
): string {
|
|
1315
|
+
if (tagName === "") {
|
|
1316
|
+
return selfClosing ? "< />" : "<>";
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
const closeToken = selfClosing ? "/>" : ">";
|
|
1320
|
+
if (attributes.length === 0) {
|
|
1321
|
+
return selfClosing ? `<${tagName} />` : `<${tagName}>`;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
const attrIndentCount = indent + this.indentSize;
|
|
1325
|
+
const formattedAttrs = attributes.map((attr) =>
|
|
1326
|
+
this.formatJsxAttribute(attr, attrIndentCount),
|
|
1327
|
+
);
|
|
1328
|
+
const isMultiline =
|
|
1329
|
+
endLine > startLine || formattedAttrs.some((a) => a.includes("\n"));
|
|
1330
|
+
|
|
1331
|
+
if (!isMultiline) {
|
|
1332
|
+
return selfClosing
|
|
1333
|
+
? `<${tagName} ${formattedAttrs.join(" ")} />`
|
|
1334
|
+
: `<${tagName} ${formattedAttrs.join(" ")}>`;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
const attrIndent = this.spaces(attrIndentCount);
|
|
1338
|
+
const closeIndent = this.spaces(indent);
|
|
1339
|
+
const attrLines = formattedAttrs.map((a) => `${attrIndent}${a}`).join("\n");
|
|
1340
|
+
return `<${tagName}\n${attrLines}\n${closeIndent}${closeToken}`;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
private formatJsxSelfClosingElement(
|
|
1344
|
+
el: JSXSelfClosingElement,
|
|
1345
|
+
indent = 0,
|
|
1346
|
+
): string {
|
|
1347
|
+
const tagName = this.formatJsxTagName(el.name);
|
|
1348
|
+
return this.formatJsxOpeningTag(
|
|
1349
|
+
tagName,
|
|
1350
|
+
el.attributes,
|
|
1351
|
+
el.span.start.line,
|
|
1352
|
+
el.span.end.line,
|
|
1353
|
+
indent,
|
|
1354
|
+
true,
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
private formatJsxChild(child: JSXChild, indent: number): string {
|
|
1359
|
+
switch (child.kind) {
|
|
1360
|
+
case "JSXText":
|
|
1361
|
+
return child.value;
|
|
1362
|
+
case "JSXExpressionContainer":
|
|
1363
|
+
return this.formatJsxExpressionContainer(
|
|
1364
|
+
child.expression,
|
|
1365
|
+
child.span,
|
|
1366
|
+
indent,
|
|
1367
|
+
);
|
|
1368
|
+
case "JSXElement":
|
|
1369
|
+
return this.formatJsxElement(child, indent);
|
|
1370
|
+
case "JSXSelfClosingElement":
|
|
1371
|
+
return this.formatJsxSelfClosingElement(child, indent);
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
private getJsxChildLineGap(prev: JSXChild, curr: JSXChild): number {
|
|
1376
|
+
if (prev.kind === "JSXText") {
|
|
1377
|
+
const prevRaw = prev.raw ?? prev.value;
|
|
1378
|
+
const trailingMatch = prevRaw.match(/((?:\r?\n\s*)+)$/);
|
|
1379
|
+
if (!trailingMatch) return 0;
|
|
1380
|
+
const newlineCount = (trailingMatch[1].match(/\n/g) ?? []).length;
|
|
1381
|
+
return Math.max(1, newlineCount);
|
|
1382
|
+
}
|
|
1383
|
+
if (curr.kind === "JSXText") {
|
|
1384
|
+
const currRaw = curr.raw ?? curr.value;
|
|
1385
|
+
const leadingMatch = currRaw.match(/^((?:\s*\r?\n)+)/);
|
|
1386
|
+
if (!leadingMatch) return 0;
|
|
1387
|
+
const newlineCount = (leadingMatch[1].match(/\n/g) ?? []).length;
|
|
1388
|
+
return Math.max(1, newlineCount);
|
|
1389
|
+
}
|
|
1390
|
+
return Math.max(0, curr.span.start.line - prev.span.end.line);
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
private formatJsxElement(el: JSXElement, indent = 0): string {
|
|
1394
|
+
const tagName = this.formatJsxTagName(el.openingElement.name);
|
|
1395
|
+
const openTag = this.formatJsxOpeningTag(
|
|
1396
|
+
tagName,
|
|
1397
|
+
el.openingElement.attributes,
|
|
1398
|
+
el.openingElement.span.start.line,
|
|
1399
|
+
el.openingElement.span.end.line,
|
|
1400
|
+
indent,
|
|
1401
|
+
false,
|
|
1402
|
+
);
|
|
1403
|
+
const closeTag = tagName === "" ? "</>" : `</${tagName}>`;
|
|
1404
|
+
|
|
1405
|
+
if (el.children.length === 0) {
|
|
1406
|
+
return `${openTag}${closeTag}`;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
const childIndentCount = indent + this.indentSize;
|
|
1410
|
+
const formattedChildren = el.children.map((child) => ({
|
|
1411
|
+
node: child,
|
|
1412
|
+
text: this.formatJsxChild(child, childIndentCount),
|
|
1413
|
+
}));
|
|
1414
|
+
|
|
1415
|
+
const isMultiline =
|
|
1416
|
+
el.span.end.line > el.span.start.line ||
|
|
1417
|
+
openTag.includes("\n") ||
|
|
1418
|
+
formattedChildren.some((c) => c.text.includes("\n"));
|
|
1419
|
+
|
|
1420
|
+
if (!isMultiline) {
|
|
1421
|
+
return `${openTag}${formattedChildren.map((c) => c.text).join("")}${closeTag}`;
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
const childIndent = this.spaces(childIndentCount);
|
|
1425
|
+
const closeIndent = this.spaces(indent);
|
|
1426
|
+
const lines: string[] = [];
|
|
1427
|
+
let currentLine = "";
|
|
1428
|
+
|
|
1429
|
+
const flushLine = () => {
|
|
1430
|
+
if (currentLine.length > 0) {
|
|
1431
|
+
lines.push(`${childIndent}${currentLine}`);
|
|
1432
|
+
currentLine = "";
|
|
1433
|
+
}
|
|
1434
|
+
};
|
|
1435
|
+
|
|
1436
|
+
for (let i = 0; i < formattedChildren.length; i++) {
|
|
1437
|
+
const curr = formattedChildren[i];
|
|
1438
|
+
if (i === 0) {
|
|
1439
|
+
currentLine = curr.text;
|
|
1440
|
+
continue;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
const prev = formattedChildren[i - 1];
|
|
1444
|
+
const gap = this.getJsxChildLineGap(prev.node, curr.node);
|
|
1445
|
+
const mustStayInline =
|
|
1446
|
+
gap === 0 && !prev.text.includes("\n") && !curr.text.includes("\n");
|
|
1447
|
+
|
|
1448
|
+
if (mustStayInline) {
|
|
1449
|
+
currentLine += curr.text;
|
|
1450
|
+
} else {
|
|
1451
|
+
flushLine();
|
|
1452
|
+
if (gap > 1) {
|
|
1453
|
+
lines.push("");
|
|
1454
|
+
}
|
|
1455
|
+
currentLine = curr.text;
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
flushLine();
|
|
1459
|
+
|
|
1460
|
+
return `${openTag}\n${lines.join("\n")}\n${closeIndent}${closeTag}`;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1236
1463
|
private formatFunctionBody(body: FunctionBody, indent: number): string {
|
|
1237
1464
|
if (body.kind === "BlockStatement") {
|
|
1238
1465
|
return this.formatBlockStatement(body, indent);
|