@nudojs/service 0.2.0 → 0.3.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.
Files changed (72) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +27 -0
  3. package/dist/index.d.ts +638 -5
  4. package/dist/index.js +4439 -208
  5. package/package.json +17 -4
  6. package/src/__tests__/abs-bindings.test.ts +53 -0
  7. package/src/__tests__/abs-call-records.test.ts +33 -0
  8. package/src/__tests__/abs-calls.test.ts +59 -0
  9. package/src/__tests__/abs-module-cache.test.ts +121 -0
  10. package/src/__tests__/abs-modules-graph.test.ts +100 -0
  11. package/src/__tests__/abs-typevalue-diff.test.ts +111 -0
  12. package/src/__tests__/analyzer.test.ts +745 -2
  13. package/src/__tests__/as-replace.test.ts +165 -0
  14. package/src/__tests__/bpath-arg-provenance.test.ts +104 -0
  15. package/src/__tests__/bpath-as.test.ts +54 -0
  16. package/src/__tests__/bpath-cache-key.test.ts +33 -0
  17. package/src/__tests__/bpath-calls.test.ts +55 -0
  18. package/src/__tests__/bpath-diagnostics.test.ts +78 -0
  19. package/src/__tests__/bpath-env-modules.test.ts +30 -0
  20. package/src/__tests__/bpath-env.test.ts +80 -0
  21. package/src/__tests__/bpath-hosted-skip.test.ts +71 -0
  22. package/src/__tests__/bpath-import-default-ns.test.ts +106 -0
  23. package/src/__tests__/bpath-method-dedupe.test.ts +65 -0
  24. package/src/__tests__/bpath-method-diag.test.ts +45 -0
  25. package/src/__tests__/bpath-module-diags.test.ts +101 -0
  26. package/src/__tests__/bpath-path-env.test.ts +46 -0
  27. package/src/__tests__/bpath-reexport.test.ts +131 -0
  28. package/src/__tests__/bpath-replace.test.ts +52 -0
  29. package/src/__tests__/bpath-require.test.ts +66 -0
  30. package/src/__tests__/bpath-run.test.ts +109 -0
  31. package/src/__tests__/case-emitter.test.ts +636 -0
  32. package/src/__tests__/cjs-require.test.ts +54 -0
  33. package/src/__tests__/diagnostics.test.ts +60 -0
  34. package/src/__tests__/dts-generator.test.ts +290 -6
  35. package/src/__tests__/fn-cache-evict.test.ts +56 -0
  36. package/src/__tests__/fn-sig-impl.test.ts +247 -0
  37. package/src/__tests__/guard-generator.test.ts +29 -0
  38. package/src/__tests__/harvest-auto.test.ts +43 -0
  39. package/src/__tests__/harvest-node.test.ts +24 -0
  40. package/src/__tests__/harvest-package.test.ts +38 -0
  41. package/src/__tests__/harvest-to-abs.test.ts +46 -0
  42. package/src/__tests__/hover-intension.test.ts +39 -0
  43. package/src/__tests__/infer-json.test.ts +35 -0
  44. package/src/__tests__/integration.test.ts +421 -0
  45. package/src/__tests__/m3-intension.test.ts +28 -0
  46. package/src/__tests__/mock-seed-fingerprint.test.ts +64 -0
  47. package/src/__tests__/schema-generator.test.ts +38 -0
  48. package/src/__tests__/session-cache-evict.test.ts +90 -0
  49. package/src/__tests__/static-imports.test.ts +44 -0
  50. package/src/abs-modules-graph.ts +387 -0
  51. package/src/analysis-file-cache.ts +51 -0
  52. package/src/analyzer.ts +2569 -94
  53. package/src/bpath-diagnostics.ts +283 -0
  54. package/src/bpath-run.ts +409 -0
  55. package/src/case-emitter.ts +477 -0
  56. package/src/dts-generator.ts +220 -24
  57. package/src/env-to-abs.ts +57 -0
  58. package/src/fn-analysis-cache.ts +119 -0
  59. package/src/guard-generator.ts +57 -0
  60. package/src/harvest-auto.ts +86 -0
  61. package/src/harvest-node.ts +48 -0
  62. package/src/harvest-package.ts +204 -0
  63. package/src/harvest-to-abs.ts +129 -0
  64. package/src/index.ts +128 -0
  65. package/src/infer-json.ts +137 -0
  66. package/src/load-module.ts +34 -0
  67. package/src/mock-abs.ts +275 -0
  68. package/src/schema-generator.ts +43 -0
  69. package/src/semantic-tokens.ts +216 -0
  70. package/src/session-cache.ts +50 -0
  71. package/src/static-imports.ts +241 -0
  72. package/src/target-path.ts +17 -0
@@ -0,0 +1,109 @@
1
+ import { describe, it, expect, afterAll } from "vitest";
2
+ import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { runTranspiled, callTranspiledExport, $lit, litValue } from "@nudojs/core";
6
+ import { tryBPathCall, isBPathCapable, analyzeFile } from "@nudojs/service";
7
+ import { typeValueToString } from "@nudojs/core";
8
+
9
+ const dirs: string[] = [];
10
+ afterAll(() => {
11
+ for (const d of dirs) rmSync(d, { recursive: true, force: true });
12
+ });
13
+
14
+ function tmpPair(math: string, main: string) {
15
+ const dir = mkdtempSync(join(tmpdir(), "nudo-bpath-imp-"));
16
+ dirs.push(dir);
17
+ writeFileSync(join(dir, "math.js"), math, "utf-8");
18
+ writeFileSync(join(dir, "main.js"), main, "utf-8");
19
+ return { dir, mainPath: join(dir, "main.js"), main };
20
+ }
21
+
22
+ describe("B-path imported functions", () => {
23
+ it("runTranspiled + injected JS exports from dep", () => {
24
+ const dep = runTranspiled(`export function double(x) { return x * 2; }`);
25
+ const main = runTranspiled(
26
+ `import { double } from "./math.js";
27
+ export function run(n) { return double(n); }`,
28
+ { modules: { "./math.js": dep as never } },
29
+ );
30
+ const r = callTranspiledExport(main, "run", [$lit(21)]);
31
+ expect(litValue(r)).toBe(42);
32
+ });
33
+
34
+ it("tryBPathCall via module graph (Abs absFunction dep)", () => {
35
+ const { main, mainPath } = tmpPair(
36
+ `export function double(x) { return x * 2; }`,
37
+ `import { double } from "./math.js";
38
+ export function run(n) { return double(n); }
39
+ `,
40
+ );
41
+ const r = tryBPathCall(main, mainPath, "run", [$lit(21)]);
42
+ expect(r).toBeDefined();
43
+ expect(litValue(r!)).toBe(42);
44
+ });
45
+
46
+ it("analyzeFile case polish uses B path for relative import", () => {
47
+ const { dir, main, mainPath } = tmpPair(
48
+ `export function triple(x) { return x * 3; }`,
49
+ `import { triple } from "./b.js";
50
+ /**
51
+ * @nudo:case "four" (4)
52
+ */
53
+ export function run(n) { return triple(n); }
54
+ `.replace("./b.js", "./math.js"),
55
+ );
56
+ void dir;
57
+ const result = analyzeFile(mainPath, main);
58
+ const run = result.functions.find((f) => f.name === "run");
59
+ expect(run).toBeDefined();
60
+ const c = run!.cases.find((x) => x.name === "four");
61
+ expect(c).toBeDefined();
62
+ expect(typeValueToString(c!.result)).toBe("12");
63
+ // B 路径应挂上 intension
64
+ expect(c!.intension?.abs ?? c!.intension?.display).toBeTruthy();
65
+ });
66
+
67
+ it("isBPathCapable allows class, async, and require; rejects env", () => {
68
+ expect(isBPathCapable("function f() { return 1; }")).toBe(true);
69
+ expect(isBPathCapable("class A { constructor() { this.x = 1; } }")).toBe(true);
70
+ expect(isBPathCapable("async function f() { return 1; }")).toBe(true);
71
+ expect(isBPathCapable("const x = require('lodash');")).toBe(true);
72
+ });
73
+
74
+ it("object slots holding functions become first-class Abs fns (no raw JS leak)", () => {
75
+ // 回归:transpile 把函数表达式编译成真实 JS 函数;流进对象槽后
76
+ // 下游 absToTypeValue 读 .shape 曾裸崩(Cannot read properties of undefined (reading 'k'))
77
+ const dir = mkdtempSync(join(tmpdir(), "nudo-bpath-fnslot-"));
78
+ dirs.push(dir);
79
+ const p = join(dir, "main.js");
80
+ const src = `
81
+ function helper(a, b) { return a; }
82
+ export function make() {
83
+ const f = (x) => x + 1;
84
+ return { load: f, arrow: (y) => y, ref: helper };
85
+ }
86
+ `;
87
+ writeFileSync(p, src, "utf-8");
88
+ const r = tryBPathCall(src, p, "make", []);
89
+ expect(r).toBeDefined();
90
+ expect(r!.shape.k).toBe("obj");
91
+ const slots = (r!.shape as { slots: Record<string, { value: { shape: { k: string; params?: string[] } } }> }).slots;
92
+ expect(slots.load.value.shape.k).toBe("fn");
93
+ expect(slots.arrow.value.shape.params).toEqual(["y"]);
94
+ // 声明函数引用无参数名可恢复 → argN 回退(与 analyzer extractParamNames 口径一致)
95
+ expect(slots.ref.value.shape.params).toEqual(["arg0", "arg1"]);
96
+ });
97
+ });
98
+
99
+ describe("transpile import emission", () => {
100
+ it("preserves relative import", async () => {
101
+ const { transpile } = await import("@nudojs/core");
102
+ const out = transpile(
103
+ `import { double } from "./math.js";
104
+ export function run(n) { return double(n); }`,
105
+ );
106
+ expect(out).toContain('from "./math.js"');
107
+ expect(out).toContain("double");
108
+ });
109
+ });