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.
Files changed (171) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/build-watch.d.ts.map +1 -1
  3. package/dist/cli/build-watch.js +4 -24
  4. package/dist/cli/build-watch.js.map +1 -1
  5. package/dist/cli/build.d.ts.map +1 -1
  6. package/dist/cli/build.js +3 -58
  7. package/dist/cli/build.js.map +1 -1
  8. package/dist/cli/check-policy.d.ts +9 -0
  9. package/dist/cli/check-policy.d.ts.map +1 -0
  10. package/dist/cli/check-policy.js +39 -0
  11. package/dist/cli/check-policy.js.map +1 -0
  12. package/dist/cli/check.d.ts.map +1 -1
  13. package/dist/cli/check.js +16 -30
  14. package/dist/cli/check.js.map +1 -1
  15. package/dist/cli/commands.d.ts.map +1 -1
  16. package/dist/cli/commands.js +11 -0
  17. package/dist/cli/commands.js.map +1 -1
  18. package/dist/cli/doctor.d.ts.map +1 -1
  19. package/dist/cli/doctor.js +2 -6
  20. package/dist/cli/doctor.js.map +1 -1
  21. package/dist/cli/index.js +3 -0
  22. package/dist/cli/index.js.map +1 -1
  23. package/dist/cli/query.d.ts +3 -0
  24. package/dist/cli/query.d.ts.map +1 -0
  25. package/dist/cli/query.js +152 -0
  26. package/dist/cli/query.js.map +1 -0
  27. package/dist/cli/run.d.ts.map +1 -1
  28. package/dist/cli/run.js +2 -29
  29. package/dist/cli/run.js.map +1 -1
  30. package/dist/cli/tsc.d.ts.map +1 -1
  31. package/dist/cli/tsc.js +2 -6
  32. package/dist/cli/tsc.js.map +1 -1
  33. package/dist/compiler/check.d.ts.map +1 -1
  34. package/dist/compiler/check.js +18 -28
  35. package/dist/compiler/check.js.map +1 -1
  36. package/dist/compiler/diagnostics-json.d.ts +73 -0
  37. package/dist/compiler/diagnostics-json.d.ts.map +1 -0
  38. package/dist/compiler/diagnostics-json.js +957 -0
  39. package/dist/compiler/diagnostics-json.js.map +1 -0
  40. package/dist/compiler/escape-audit.d.ts +46 -0
  41. package/dist/compiler/escape-audit.d.ts.map +1 -0
  42. package/dist/compiler/escape-audit.js +682 -0
  43. package/dist/compiler/escape-audit.js.map +1 -0
  44. package/dist/compiler/modules/output-plan.d.ts.map +1 -1
  45. package/dist/compiler/modules/output-plan.js +10 -5
  46. package/dist/compiler/modules/output-plan.js.map +1 -1
  47. package/dist/compiler/modules/source-files.d.ts +2 -0
  48. package/dist/compiler/modules/source-files.d.ts.map +1 -1
  49. package/dist/compiler/modules/source-files.js +8 -0
  50. package/dist/compiler/modules/source-files.js.map +1 -1
  51. package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
  52. package/dist/compiler/numeric-comparisons.js +13 -7
  53. package/dist/compiler/numeric-comparisons.js.map +1 -1
  54. package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
  55. package/dist/compiler/pipelines/resolution.js +24 -23
  56. package/dist/compiler/pipelines/resolution.js.map +1 -1
  57. package/dist/compiler/query.d.ts +53 -0
  58. package/dist/compiler/query.d.ts.map +1 -0
  59. package/dist/compiler/query.js +781 -0
  60. package/dist/compiler/query.js.map +1 -0
  61. package/dist/compiler/source-map.d.ts.map +1 -1
  62. package/dist/compiler/source-map.js +2 -7
  63. package/dist/compiler/source-map.js.map +1 -1
  64. package/dist/compiler/type-view-paths.d.ts +2 -0
  65. package/dist/compiler/type-view-paths.d.ts.map +1 -1
  66. package/dist/compiler/type-view-paths.js +8 -0
  67. package/dist/compiler/type-view-paths.js.map +1 -1
  68. package/dist/compiler/typescript-context.d.ts.map +1 -1
  69. package/dist/compiler/typescript-context.js +5 -7
  70. package/dist/compiler/typescript-context.js.map +1 -1
  71. package/dist/emitter/assemblyscript.d.ts.map +1 -1
  72. package/dist/emitter/assemblyscript.js +120 -136
  73. package/dist/emitter/assemblyscript.js.map +1 -1
  74. package/dist/formatter/index.d.ts +9 -0
  75. package/dist/formatter/index.d.ts.map +1 -1
  76. package/dist/formatter/index.js +158 -2
  77. package/dist/formatter/index.js.map +1 -1
  78. package/dist/index.d.ts +3 -0
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +3 -0
  81. package/dist/index.js.map +1 -1
  82. package/dist/lsp/completion.d.ts +25 -0
  83. package/dist/lsp/completion.d.ts.map +1 -1
  84. package/dist/lsp/completion.js +1114 -51
  85. package/dist/lsp/completion.js.map +1 -1
  86. package/dist/lsp/definition.d.ts +2 -0
  87. package/dist/lsp/definition.d.ts.map +1 -1
  88. package/dist/lsp/definition.js +52 -58
  89. package/dist/lsp/definition.js.map +1 -1
  90. package/dist/lsp/hover.d.ts +5 -1
  91. package/dist/lsp/hover.d.ts.map +1 -1
  92. package/dist/lsp/hover.js +1000 -41
  93. package/dist/lsp/hover.js.map +1 -1
  94. package/dist/lsp/imports.d.ts +2 -0
  95. package/dist/lsp/imports.d.ts.map +1 -1
  96. package/dist/lsp/imports.js +35 -20
  97. package/dist/lsp/imports.js.map +1 -1
  98. package/dist/lsp/navigation.d.ts +1 -1
  99. package/dist/lsp/navigation.d.ts.map +1 -1
  100. package/dist/lsp/navigation.js +30 -12
  101. package/dist/lsp/navigation.js.map +1 -1
  102. package/dist/lsp/range-format.d.ts +4 -0
  103. package/dist/lsp/range-format.d.ts.map +1 -0
  104. package/dist/lsp/range-format.js +88 -0
  105. package/dist/lsp/range-format.js.map +1 -0
  106. package/dist/lsp/schema-import-hover.d.ts +8 -0
  107. package/dist/lsp/schema-import-hover.d.ts.map +1 -0
  108. package/dist/lsp/schema-import-hover.js +52 -0
  109. package/dist/lsp/schema-import-hover.js.map +1 -0
  110. package/dist/lsp/server.d.ts +1 -0
  111. package/dist/lsp/server.d.ts.map +1 -1
  112. package/dist/lsp/server.js +53 -48
  113. package/dist/lsp/server.js.map +1 -1
  114. package/dist/lsp/workspace.d.ts +3 -1
  115. package/dist/lsp/workspace.d.ts.map +1 -1
  116. package/dist/lsp/workspace.js +66 -10
  117. package/dist/lsp/workspace.js.map +1 -1
  118. package/dist/plugins/bun.d.ts +2 -18
  119. package/dist/plugins/bun.d.ts.map +1 -1
  120. package/dist/plugins/bun.js +5 -30
  121. package/dist/plugins/bun.js.map +1 -1
  122. package/dist/plugins/esbuild.d.ts +1 -1
  123. package/dist/plugins/esbuild.d.ts.map +1 -1
  124. package/dist/plugins/esbuild.js +10 -36
  125. package/dist/plugins/esbuild.js.map +1 -1
  126. package/dist/plugins/server-boundary.d.ts +14 -0
  127. package/dist/plugins/server-boundary.d.ts.map +1 -0
  128. package/dist/plugins/server-boundary.js +25 -0
  129. package/dist/plugins/server-boundary.js.map +1 -0
  130. package/dist/plugins/vite.d.ts +2 -18
  131. package/dist/plugins/vite.d.ts.map +1 -1
  132. package/dist/plugins/vite.js +4 -29
  133. package/dist/plugins/vite.js.map +1 -1
  134. package/package.json +1 -1
  135. package/src/cli/build-watch.ts +4 -26
  136. package/src/cli/build.ts +9 -60
  137. package/src/cli/check-policy.ts +54 -0
  138. package/src/cli/check.ts +19 -31
  139. package/src/cli/commands.ts +13 -0
  140. package/src/cli/doctor.ts +2 -5
  141. package/src/cli/index.ts +3 -0
  142. package/src/cli/query.ts +191 -0
  143. package/src/cli/run.ts +5 -30
  144. package/src/cli/tsc.ts +2 -5
  145. package/src/compiler/check.ts +28 -48
  146. package/src/compiler/diagnostics-json.ts +1205 -0
  147. package/src/compiler/escape-audit.ts +913 -0
  148. package/src/compiler/modules/output-plan.ts +11 -9
  149. package/src/compiler/modules/source-files.ts +9 -0
  150. package/src/compiler/numeric-comparisons.ts +14 -8
  151. package/src/compiler/pipelines/resolution.ts +30 -26
  152. package/src/compiler/query.ts +1177 -0
  153. package/src/compiler/source-map.ts +3 -7
  154. package/src/compiler/type-view-paths.ts +7 -0
  155. package/src/compiler/typescript-context.ts +6 -7
  156. package/src/emitter/assemblyscript.ts +101 -147
  157. package/src/formatter/index.ts +229 -2
  158. package/src/index.ts +3 -0
  159. package/src/lsp/completion.ts +1452 -66
  160. package/src/lsp/definition.ts +50 -58
  161. package/src/lsp/hover.ts +1442 -40
  162. package/src/lsp/imports.ts +55 -21
  163. package/src/lsp/navigation.ts +41 -22
  164. package/src/lsp/range-format.ts +102 -0
  165. package/src/lsp/schema-import-hover.ts +76 -0
  166. package/src/lsp/server.ts +69 -69
  167. package/src/lsp/workspace.ts +63 -9
  168. package/src/plugins/bun.ts +14 -66
  169. package/src/plugins/esbuild.ts +16 -55
  170. package/src/plugins/server-boundary.ts +41 -0
  171. 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 = outputFile
16
- ? outputFile
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 = filename.endsWith(".funx")
178
- ? filename.slice(0, -5) + ".tsx"
179
- : filename.endsWith(".fun")
180
- ? filename.slice(0, -4) + ".ts"
181
- : filename.endsWith(".ts") || filename.endsWith(".tsx")
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 = BOOLEAN_TYPES.has(firstType)
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 = BOOLEAN_TYPES.has(t)
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 tupleElems = parseTupleElementTypeList(field.type);
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 tupleElems = parseTupleElementTypeList(f.type);
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 = BOOLEAN_TYPES.has(elem)
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 = BOOLEAN_TYPES.has(elem)
792
- ? "Boolean(raw)"
793
- : elem === "string"
794
- ? `${prefix}ReadString(exports, raw)`
795
- : classes.has(elem)
796
- ? `${prefix}Read_${elem}(exports, raw)`
797
- : tupleElems
798
- ? `${prefix}Read_${tupleClassName(tupleElems)}(exports, raw)`
799
- : elem === "i64"
800
- ? "BigInt.asIntN(64, raw)"
801
- : elem === "u64"
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 = BOOLEAN_TYPES.has(elem)
808
- ? ""
809
- : elem === "string"
810
- ? `const __v = ${prefix}AllocString(exports, item);`
811
- : classes.has(elem)
812
- ? `const __v = ${prefix}Alloc_${elem}(exports, item);`
813
- : tupleElems
814
- ? `const __v = ${prefix}Alloc_${tupleClassName(tupleElems)}(exports, item);`
815
- : "";
816
- const itemVal = BOOLEAN_TYPES.has(elem)
817
- ? "item ? 1 : 0"
818
- : elem === "string" || classes.has(elem) || tupleElems
819
- ? "__v"
820
- : elem === "u32"
821
- ? "(item >>> 0)"
822
- : "item";
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 === "i64"
976
- ? `typeof ${valExpr} !== "bigint" || ${valExpr} < -9223372036854775808n || ${valExpr} > 9223372036854775807n`
977
- : type === "u64"
978
- ? `typeof ${valExpr} !== "bigint" || ${valExpr} < 0n || ${valExpr} > 18446744073709551615n`
979
- : type === "i32"
980
- ? `!Number.isInteger(${valExpr}) || ${valExpr} < -2147483648 || ${valExpr} > 2147483647`
981
- : type === "u32"
982
- ? `!Number.isInteger(${valExpr}) || ${valExpr} < 0 || ${valExpr} > 4294967295`
983
- : type === "f32"
984
- ? `!Number.isFinite(${valExpr}) || Math.abs(${valExpr}) > 3.4028234663852886e38`
985
- : `!Number.isFinite(${valExpr})`;
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 wasmFnType =
1007
+ const wasmResultType =
1030
1008
  fn.result === "void"
1031
- ? "(...args: (number | bigint)[]) => void"
1032
- : isBufferResult ||
1033
- isStringResult ||
1034
- isClassResult ||
1035
- isArrayResult ||
1036
- isTupleResult
1037
- ? "(...args: (number | bigint)[]) => number"
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
- : isBooleanResult
1060
- ? `Boolean(__res)`
1061
- : isBufferResult
1062
- ? `${prefix}Read(exports, __res, ${toJsTypedArray(fn.result)})`
1063
- : isStringResult
1064
- ? `${prefix}ReadString(exports, __res)`
1065
- : isClassResult
1066
- ? `${prefix}Read_${fn.result}(exports, __res)`
1067
- : isTupleResult
1068
- ? `${prefix}Read_${tupleClassName(parseTupleElementTypeList(fn.result)!)}(exports, __res)`
1069
- : isArrayResult
1070
- ? (() => {
1071
- const elem = parseArrayElementType(fn.result)!;
1072
- const tupleElems = parseTupleElementTypeList(elem);
1073
- const safeElem =
1074
- elem === "boolean"
1075
- ? "bool"
1076
- : tupleElems
1077
- ? tupleClassName(tupleElems)
1078
- : elem;
1079
- return `${prefix}Read_Array_${safeElem}(exports, __res)`;
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 raw =
1098
- fn.result === "void"
1099
- ? `${call};`
1100
- : isBooleanResult
1101
- ? `Boolean(${call})`
1102
- : fn.result === "u32"
1103
- ? `(${call} as number >>> 0)`
1104
- : fn.result === "u64"
1105
- ? `BigInt.asUintN(64, ${call} as bigint)`
1106
- : fn.result === "i64"
1107
- ? `BigInt.asIntN(64, ${call} as bigint)`
1108
- : call;
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;
@@ -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 `${this.formatExpression(chain[0], indent)} ${operators[0]} ${this.formatExpression(chain[1], indent)}`;
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.source.slice(expr.span.start.offset, expr.span.end.offset);
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);