@nudojs/cli 0.1.0 → 0.2.1
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/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/chunk-3HCPXPEW.js +5286 -0
- package/dist/evaluator-api.d.ts +96 -2
- package/dist/evaluator-api.js +191 -3
- package/dist/index.js +595 -120
- package/examples/real-world.js +66 -0
- package/examples/validate-real-world.ts +160 -0
- package/package.json +28 -3
- package/src/__tests__/builtin-advanced.test.ts +292 -0
- package/src/__tests__/builtin-api.test.ts +308 -0
- package/src/__tests__/builtin-functions.test.ts +313 -0
- package/src/__tests__/builtin-map.test.ts +129 -0
- package/src/__tests__/builtin-promise.test.ts +150 -0
- package/src/__tests__/builtin-regexp.test.ts +133 -0
- package/src/__tests__/builtin-set.test.ts +163 -0
- package/src/__tests__/builtin-url.test.ts +256 -0
- package/src/__tests__/builtin-web.test.ts +340 -0
- package/src/__tests__/combination-scenarios.test.ts +384 -0
- package/src/__tests__/edge-cases.test.ts +749 -0
- package/src/__tests__/evaluator-loops.test.ts +283 -0
- package/src/__tests__/evaluator-nullish-coalescing.test.ts +86 -0
- package/src/__tests__/evaluator-optional-chaining.test.ts +37 -0
- package/src/__tests__/evaluator-phase3.test.ts +353 -2
- package/src/__tests__/evaluator-refined.test.ts +121 -0
- package/src/__tests__/evaluator-string-methods.test.ts +151 -0
- package/src/__tests__/evaluator.test.ts +702 -2
- package/src/__tests__/mock-arrow-function.test.ts +169 -0
- package/src/__tests__/mock-comparison.test.ts +221 -0
- package/src/__tests__/mock-comprehensive.test.ts +250 -0
- package/src/__tests__/mock-final-integration.test.ts +263 -0
- package/src/__tests__/mock-helpers.test.ts +174 -0
- package/src/__tests__/mock-realworld.test.ts +191 -0
- package/src/__tests__/mock-rest-debug.test.ts +110 -0
- package/src/__tests__/mock-rest-edge.test.ts +176 -0
- package/src/__tests__/mock-sinon-runtime.test.ts +168 -0
- package/src/__tests__/narrowing-discriminated.test.ts +35 -0
- package/src/__tests__/narrowing-in-operator.test.ts +56 -0
- package/src/__tests__/narrowing-switch.test.ts +56 -0
- package/src/__tests__/narrowing-truthiness.test.ts +52 -0
- package/src/__tests__/nudo-mock-syntax.test.ts +245 -0
- package/src/__tests__/realworld-patterns.test.ts +430 -0
- package/src/__tests__/syntax-sugar.test.ts +345 -0
- package/src/builtins/builtin-intl.ts +17 -0
- package/src/builtins/builtin-map.ts +174 -0
- package/src/builtins/builtin-promise.ts +116 -0
- package/src/builtins/builtin-reflect.ts +30 -0
- package/src/builtins/builtin-regexp.ts +34 -0
- package/src/builtins/builtin-set.ts +131 -0
- package/src/builtins/builtin-symbol.ts +16 -0
- package/src/builtins/builtin-url.ts +58 -0
- package/src/builtins/builtin-weak.ts +22 -0
- package/src/builtins/builtin-web.ts +111 -0
- package/src/builtins/index.ts +61 -0
- package/src/config.ts +32 -0
- package/src/env-loader.ts +174 -0
- package/src/evaluator-api.ts +22 -0
- package/src/evaluator.ts +4218 -171
- package/src/index.ts +776 -125
- package/src/narrowing.ts +286 -15
- package/src/resolve-npm.ts +63 -0
- package/dist/chunk-JW3RMCNY.js +0 -1467
- package/dist/chunk-RBHWWZII.js +0 -1467
- package/dist/chunk-VN5HMBGB.js +0 -1467
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from "@nudojs/core";
|
|
9
9
|
import type { TypeValue } from "@nudojs/core";
|
|
10
10
|
import { parse } from "@nudojs/parser";
|
|
11
|
-
import { evaluate, evaluateFunction, evaluateFunctionFull, evaluateProgram, resetMemo, setModuleResolver, setCurrentFileDir } from "../evaluator.ts";
|
|
11
|
+
import { evaluate, evaluateFunction, evaluateFunctionFull, evaluateProgram, resetMemo, setModuleResolver, setCurrentFileDir, setUnknownCollector, setMockModules, resetMockModules, type UnknownRecord } from "../evaluator.ts";
|
|
12
12
|
|
|
13
13
|
function evalCode(code: string): TypeValue {
|
|
14
14
|
const ast = parse(code);
|
|
@@ -207,7 +207,15 @@ describe("instanceof", () => {
|
|
|
207
207
|
const x = 42;
|
|
208
208
|
x instanceof Error
|
|
209
209
|
`);
|
|
210
|
-
|
|
210
|
+
// primitives can never be instanceof anything — decided statically
|
|
211
|
+
expect(typeValueEquals(result, T.literal(false))).toBe(true);
|
|
212
|
+
// unknown receivers keep the conservative boolean fallback
|
|
213
|
+
const fn = evaluateFunction(
|
|
214
|
+
parse("function f(x) { return x instanceof Error; }").program.body[0],
|
|
215
|
+
[T.unknown],
|
|
216
|
+
createEnvironment(),
|
|
217
|
+
);
|
|
218
|
+
expect(fn).toBe(T.boolean);
|
|
211
219
|
});
|
|
212
220
|
});
|
|
213
221
|
|
|
@@ -301,6 +309,51 @@ describe("recursion with memoization", () => {
|
|
|
301
309
|
const result = env.lookup("result");
|
|
302
310
|
expect(result.kind).toBe("unknown");
|
|
303
311
|
});
|
|
312
|
+
|
|
313
|
+
it("budget-truncated recursion falls back to the function's observed results instead of unknown", () => {
|
|
314
|
+
// eq over chains deeper than MAX_CALL_DEPTH truncates mid-recursion.
|
|
315
|
+
// The first truncation has no observations and stays unknown; precise
|
|
316
|
+
// calls then accumulate true/false; a later truncation (fresh argument
|
|
317
|
+
// shapes, so no memo hit) degrades to the observed union.
|
|
318
|
+
const nest = (n: number, leaf: string): string => (n === 0 ? leaf : `{ x: ${nest(n - 1, leaf)} }`);
|
|
319
|
+
const ast = parse(`
|
|
320
|
+
function eq(a, b) {
|
|
321
|
+
if (a === b) return true;
|
|
322
|
+
if (typeof a !== "object") return false;
|
|
323
|
+
return eq(a.x, b.x);
|
|
324
|
+
}
|
|
325
|
+
const r1 = eq(${nest(70, "1")}, ${nest(70, "2")});
|
|
326
|
+
const t = eq(1, 1);
|
|
327
|
+
const f = eq(1, 2);
|
|
328
|
+
const r2 = eq(${nest(80, "3")}, ${nest(80, "4")});
|
|
329
|
+
`);
|
|
330
|
+
const env = createEnvironment();
|
|
331
|
+
evaluateProgram(ast, env);
|
|
332
|
+
expect(env.lookup("r1").kind).toBe("unknown");
|
|
333
|
+
expect(typeValueToString(env.lookup("t"))).toBe("true");
|
|
334
|
+
expect(typeValueToString(env.lookup("f"))).toBe("false");
|
|
335
|
+
const r2 = env.lookup("r2");
|
|
336
|
+
expect(r2.kind).toBe("union");
|
|
337
|
+
if (r2.kind === "union") {
|
|
338
|
+
expect(r2.members).toHaveLength(2);
|
|
339
|
+
expect(typeValueToString(r2)).toBe("true | false");
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("budget-truncated recursion without observed results stays unknown", () => {
|
|
344
|
+
const nest = (n: number, leaf: string): string => (n === 0 ? leaf : `{ x: ${nest(n - 1, leaf)} }`);
|
|
345
|
+
const ast = parse(`
|
|
346
|
+
function eq(a, b) {
|
|
347
|
+
if (a === b) return true;
|
|
348
|
+
if (typeof a !== "object") return false;
|
|
349
|
+
return eq(a.x, b.x);
|
|
350
|
+
}
|
|
351
|
+
const r = eq(${nest(70, "5")}, ${nest(70, "6")});
|
|
352
|
+
`);
|
|
353
|
+
const env = createEnvironment();
|
|
354
|
+
evaluateProgram(ast, env);
|
|
355
|
+
expect(env.lookup("r").kind).toBe("unknown");
|
|
356
|
+
});
|
|
304
357
|
});
|
|
305
358
|
|
|
306
359
|
describe("modules import/export", () => {
|
|
@@ -436,6 +489,304 @@ describe("modules import/export", () => {
|
|
|
436
489
|
|
|
437
490
|
setModuleResolver(null);
|
|
438
491
|
});
|
|
492
|
+
|
|
493
|
+
it("resolves require() of a CJS module exporting an object", () => {
|
|
494
|
+
setModuleResolver((source) => {
|
|
495
|
+
if (source === "./b") {
|
|
496
|
+
return {
|
|
497
|
+
ast: parse(`function triple(x) { return x * 3; }\nmodule.exports = { triple };\n`),
|
|
498
|
+
filePath: "/fake/b.js",
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
return null;
|
|
502
|
+
});
|
|
503
|
+
setCurrentFileDir("/fake");
|
|
504
|
+
|
|
505
|
+
const ast = parse(`
|
|
506
|
+
const b = require("./b");
|
|
507
|
+
const r = b.triple(4);
|
|
508
|
+
`);
|
|
509
|
+
const env = createEnvironment();
|
|
510
|
+
evaluateProgram(ast, env);
|
|
511
|
+
|
|
512
|
+
const b = env.lookup("b");
|
|
513
|
+
expect(b.kind).toBe("object");
|
|
514
|
+
if (b.kind === "object") {
|
|
515
|
+
expect(b.properties.triple.kind).toBe("function");
|
|
516
|
+
}
|
|
517
|
+
expect(typeValueEquals(env.lookup("r"), T.literal(12))).toBe(true);
|
|
518
|
+
|
|
519
|
+
setModuleResolver(null);
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it("require() picks up a chained module.exports = internals.x = function assignment", () => {
|
|
523
|
+
setModuleResolver((source) => {
|
|
524
|
+
if (source === "./clone") {
|
|
525
|
+
return {
|
|
526
|
+
ast: parse(`
|
|
527
|
+
const internals = {};
|
|
528
|
+
module.exports = internals.clone = function (obj, options = {}) {
|
|
529
|
+
if (options.shallow) {
|
|
530
|
+
return obj;
|
|
531
|
+
}
|
|
532
|
+
return obj;
|
|
533
|
+
};
|
|
534
|
+
`),
|
|
535
|
+
filePath: "/fake/clone.js",
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
return null;
|
|
539
|
+
});
|
|
540
|
+
setCurrentFileDir("/fake");
|
|
541
|
+
|
|
542
|
+
const ast = parse(`
|
|
543
|
+
const Clone = require("./clone");
|
|
544
|
+
const r = Clone(5);
|
|
545
|
+
`);
|
|
546
|
+
const env = createEnvironment();
|
|
547
|
+
evaluateProgram(ast, env);
|
|
548
|
+
|
|
549
|
+
expect(env.lookup("Clone").kind).toBe("function");
|
|
550
|
+
expect(typeValueEquals(env.lookup("r"), T.literal(5))).toBe(true);
|
|
551
|
+
|
|
552
|
+
setModuleResolver(null);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it("supports exports.name assignments", () => {
|
|
556
|
+
setModuleResolver((source) => {
|
|
557
|
+
if (source === "./b") {
|
|
558
|
+
return {
|
|
559
|
+
ast: parse(`exports.triple = function (x) { return x * 3; };\n`),
|
|
560
|
+
filePath: "/fake/b.js",
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
return null;
|
|
564
|
+
});
|
|
565
|
+
setCurrentFileDir("/fake");
|
|
566
|
+
|
|
567
|
+
const ast = parse(`
|
|
568
|
+
const b = require("./b");
|
|
569
|
+
const r = b.triple(2);
|
|
570
|
+
`);
|
|
571
|
+
const env = createEnvironment();
|
|
572
|
+
evaluateProgram(ast, env);
|
|
573
|
+
|
|
574
|
+
expect(typeValueEquals(env.lookup("r"), T.literal(6))).toBe(true);
|
|
575
|
+
|
|
576
|
+
setModuleResolver(null);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it("require() of an ES module returns its exports as properties", () => {
|
|
580
|
+
setModuleResolver((source) => {
|
|
581
|
+
if (source === "./esm") {
|
|
582
|
+
return {
|
|
583
|
+
ast: parse(`export const n = 1;\nexport function f(x) { return x + 1; }\n`),
|
|
584
|
+
filePath: "/fake/esm.js",
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
return null;
|
|
588
|
+
});
|
|
589
|
+
setCurrentFileDir("/fake");
|
|
590
|
+
|
|
591
|
+
const ast = parse(`
|
|
592
|
+
const b = require("./esm");
|
|
593
|
+
const r = b.f(b.n);
|
|
594
|
+
`);
|
|
595
|
+
const env = createEnvironment();
|
|
596
|
+
evaluateProgram(ast, env);
|
|
597
|
+
|
|
598
|
+
expect(typeValueEquals(env.lookup("r"), T.literal(2))).toBe(true);
|
|
599
|
+
|
|
600
|
+
setModuleResolver(null);
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
it("require() with a bare specifier returns unknown and records a warning", () => {
|
|
604
|
+
setCurrentFileDir("/fake");
|
|
605
|
+
|
|
606
|
+
const records: UnknownRecord[] = [];
|
|
607
|
+
setUnknownCollector((r) => records.push(r));
|
|
608
|
+
const env = createEnvironment();
|
|
609
|
+
try {
|
|
610
|
+
const ast = parse(`const u = require("@hapi/hoek");\n`);
|
|
611
|
+
evaluateProgram(ast, env);
|
|
612
|
+
} finally {
|
|
613
|
+
setUnknownCollector(null);
|
|
614
|
+
setCurrentFileDir("");
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
expect(env.lookup("u").kind).toBe("unknown");
|
|
618
|
+
expect(records.some((r) => r.kind === "global" && r.name === `require('@hapi/hoek')`)).toBe(true);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
// --- 模块加载边界守卫(环 / 深度 / 缺失),来源:evaluator.ts loadModuleEnv 加固 ---
|
|
622
|
+
|
|
623
|
+
it("reports a circular import chain a -> b -> a and keeps partial-binding semantics", () => {
|
|
624
|
+
const aAst = parse(`import { bVal } from "./b.js";\nexport const aVal = 1;\nexport const mixed = bVal;\n`);
|
|
625
|
+
const bAst = parse(`import { aVal } from "./a.js";\nexport const bVal = aVal;\n`);
|
|
626
|
+
const files: Record<string, { ast: ReturnType<typeof parse>; filePath: string }> = {
|
|
627
|
+
"./a.js": { ast: aAst, filePath: "/fake/a.js" },
|
|
628
|
+
"./b.js": { ast: bAst, filePath: "/fake/b.js" },
|
|
629
|
+
};
|
|
630
|
+
setModuleResolver((source) => files[source] ?? null);
|
|
631
|
+
setCurrentFileDir("/fake");
|
|
632
|
+
|
|
633
|
+
const records: UnknownRecord[] = [];
|
|
634
|
+
setUnknownCollector((r) => records.push(r));
|
|
635
|
+
const env = createEnvironment();
|
|
636
|
+
evaluateProgram(parse(`import { aVal, mixed } from "./a.js";\n`), env);
|
|
637
|
+
|
|
638
|
+
const cycle = records.find((r) => r.kind === "global" && r.name.startsWith("module-cycle:"));
|
|
639
|
+
expect(cycle).toBeDefined();
|
|
640
|
+
expect(cycle?.name).toBe("module-cycle:a.js");
|
|
641
|
+
expect(cycle?.reason).toBe("Circular module load: /fake/a.js -> /fake/b.js -> /fake/a.js (bindings inside the cycle resolve to their partially evaluated types)");
|
|
642
|
+
// 环内 aVal 在 b 求值时尚未执行 → unknown;环后补齐的导出照常可得
|
|
643
|
+
expect(typeValueEquals(env.lookup("aVal"), T.literal(1))).toBe(true);
|
|
644
|
+
expect(env.lookup("mixed").kind).toBe("unknown");
|
|
645
|
+
|
|
646
|
+
setUnknownCollector(null);
|
|
647
|
+
setModuleResolver(null);
|
|
648
|
+
setCurrentFileDir("");
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
it("reports a mock-module cycle when the mock file re-imports the mocked module", () => {
|
|
652
|
+
const mockAst = parse(`import { helper } from "calc";\nexport const value = helper;\nexport const own = 2;\n`);
|
|
653
|
+
setMockModules(new Map([["calc", { fromPath: "./mockCalc.js" }]]));
|
|
654
|
+
setModuleResolver((source) => {
|
|
655
|
+
if (source === "./mockCalc.js") return { ast: mockAst, filePath: "/fake/mockCalc.js" };
|
|
656
|
+
return null;
|
|
657
|
+
});
|
|
658
|
+
setCurrentFileDir("/fake");
|
|
659
|
+
|
|
660
|
+
const records: UnknownRecord[] = [];
|
|
661
|
+
setUnknownCollector((r) => records.push(r));
|
|
662
|
+
const env = createEnvironment();
|
|
663
|
+
evaluateProgram(parse(`import { value, own } from "calc";\nconst sum = own + 40;\n`), env);
|
|
664
|
+
|
|
665
|
+
const cycle = records.find((r) => r.kind === "global" && r.name.startsWith("module-cycle:"));
|
|
666
|
+
expect(cycle).toBeDefined();
|
|
667
|
+
expect(cycle?.reason).toBe("Circular module load: /fake/mockCalc.js -> /fake/mockCalc.js (bindings inside the cycle resolve to their partially evaluated types)");
|
|
668
|
+
// 环截断后 mock 文件其余导出照常求值,分析不中断
|
|
669
|
+
expect(env.lookup("value").kind).toBe("unknown");
|
|
670
|
+
expect(typeValueEquals(env.lookup("sum"), T.literal(42))).toBe(true);
|
|
671
|
+
|
|
672
|
+
setUnknownCollector(null);
|
|
673
|
+
setModuleResolver(null);
|
|
674
|
+
setCurrentFileDir("");
|
|
675
|
+
resetMockModules();
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it("truncates module chains deeper than the depth limit and reports the chain", () => {
|
|
679
|
+
// f1 -> f2 -> ... -> f20:第 17 层(f17)触发深度上限(MAX_MODULE_LOAD_DEPTH = 16)
|
|
680
|
+
const files: Record<string, { ast: ReturnType<typeof parse>; filePath: string }> = {};
|
|
681
|
+
for (let i = 1; i <= 20; i++) {
|
|
682
|
+
const body = i < 20
|
|
683
|
+
? `import { value as inner } from "./f${i + 1}.js";\nexport const value = inner;\n`
|
|
684
|
+
: `export const value = 20;\n`;
|
|
685
|
+
files[`./f${i}.js`] = { ast: parse(body), filePath: `/fake/f${i}.js` };
|
|
686
|
+
}
|
|
687
|
+
setModuleResolver((source) => files[source] ?? null);
|
|
688
|
+
setCurrentFileDir("/fake");
|
|
689
|
+
|
|
690
|
+
const records: UnknownRecord[] = [];
|
|
691
|
+
setUnknownCollector((r) => records.push(r));
|
|
692
|
+
const env = createEnvironment();
|
|
693
|
+
evaluateProgram(parse(`import { value } from "./f1.js";\n`), env);
|
|
694
|
+
|
|
695
|
+
const depth = records.find((r) => r.kind === "global" && r.name.startsWith("module-depth:"));
|
|
696
|
+
expect(depth).toBeDefined();
|
|
697
|
+
expect(depth?.name).toBe("module-depth:f17.js");
|
|
698
|
+
expect(depth?.reason).toContain("depth 17 > 16 max");
|
|
699
|
+
const expectedChain = Array.from({ length: 17 }, (_, k) => `/fake/f${k + 1}.js`).join(" -> ");
|
|
700
|
+
expect(depth?.reason).toContain(expectedChain);
|
|
701
|
+
// 截断为 unknown,而非栈溢出崩溃
|
|
702
|
+
expect(env.lookup("value").kind).toBe("unknown");
|
|
703
|
+
|
|
704
|
+
setUnknownCollector(null);
|
|
705
|
+
setModuleResolver(null);
|
|
706
|
+
setCurrentFileDir("");
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
it("reports a missing @nudo:mock-module target with its full path and falls back to the original", () => {
|
|
710
|
+
setMockModules(new Map([["b", { fromPath: "./nope.js" }]]));
|
|
711
|
+
setModuleResolver((source) => {
|
|
712
|
+
if (source === "b") return { ast: parse(`export const x = 41;\n`), filePath: "/fake/b.js" };
|
|
713
|
+
return null; // "./nope.js" 不存在
|
|
714
|
+
});
|
|
715
|
+
setCurrentFileDir("/fake");
|
|
716
|
+
|
|
717
|
+
const records: UnknownRecord[] = [];
|
|
718
|
+
setUnknownCollector((r) => records.push(r));
|
|
719
|
+
const env = createEnvironment();
|
|
720
|
+
evaluateProgram(parse(`import { x } from "b";\nconst y = x + 1;\n`), env);
|
|
721
|
+
|
|
722
|
+
const missing = records.find((r) => r.kind === "global" && r.name.startsWith("module-missing:"));
|
|
723
|
+
expect(missing).toBeDefined();
|
|
724
|
+
expect(missing?.name).toBe("module-missing:nope.js");
|
|
725
|
+
expect(missing?.reason).toContain('@nudo:mock-module target for "b" not found');
|
|
726
|
+
expect(missing?.reason).toContain("/fake/nope.js");
|
|
727
|
+
// 回落到原模块,mock 缺失不中断分析
|
|
728
|
+
expect(typeValueEquals(env.lookup("y"), T.literal(42))).toBe(true);
|
|
729
|
+
|
|
730
|
+
setUnknownCollector(null);
|
|
731
|
+
setModuleResolver(null);
|
|
732
|
+
setCurrentFileDir("");
|
|
733
|
+
resetMockModules();
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
it("reports an unresolvable relative import with the attempted full path", () => {
|
|
737
|
+
setModuleResolver(() => null);
|
|
738
|
+
setCurrentFileDir("/fake");
|
|
739
|
+
|
|
740
|
+
const records: UnknownRecord[] = [];
|
|
741
|
+
setUnknownCollector((r) => records.push(r));
|
|
742
|
+
const env = createEnvironment();
|
|
743
|
+
evaluateProgram(parse(`import { x } from "./gone.js";\nconst y = 1;\n`), env);
|
|
744
|
+
|
|
745
|
+
const missing = records.find((r) => r.kind === "global" && r.name.startsWith("module-missing:"));
|
|
746
|
+
expect(missing).toBeDefined();
|
|
747
|
+
expect(missing?.reason).toContain("Cannot resolve import './gone.js'");
|
|
748
|
+
expect(missing?.reason).toContain("/fake/gone.js");
|
|
749
|
+
expect(typeValueEquals(env.lookup("y"), T.literal(1))).toBe(true);
|
|
750
|
+
|
|
751
|
+
setUnknownCollector(null);
|
|
752
|
+
setModuleResolver(null);
|
|
753
|
+
setCurrentFileDir("");
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
it("loads nested mock-module dependencies without triggering guard diagnostics", () => {
|
|
757
|
+
const helperAst = parse(`export const h = 2;\n`);
|
|
758
|
+
const mockAst = parse(`import { h } from "./helper.js";\nexport const answer = h * 21;\n`);
|
|
759
|
+
const files: Record<string, { ast: ReturnType<typeof parse>; filePath: string }> = {
|
|
760
|
+
"./helper.js": { ast: helperAst, filePath: "/fake/helper.js" },
|
|
761
|
+
"./mockCalc.js": { ast: mockAst, filePath: "/fake/mockCalc.js" },
|
|
762
|
+
};
|
|
763
|
+
setMockModules(new Map([["calc", { fromPath: "./mockCalc.js" }]]));
|
|
764
|
+
setModuleResolver((source) => files[source] ?? null);
|
|
765
|
+
setCurrentFileDir("/fake");
|
|
766
|
+
|
|
767
|
+
const records: UnknownRecord[] = [];
|
|
768
|
+
setUnknownCollector((r) => records.push(r));
|
|
769
|
+
const env = createEnvironment();
|
|
770
|
+
evaluateProgram(parse(`import { answer } from "calc";\nconst life = answer;\n`), env);
|
|
771
|
+
|
|
772
|
+
// 正常嵌套 mock:不产生任何守卫诊断
|
|
773
|
+
expect(records.filter((r) => r.name.startsWith("module-"))).toHaveLength(0);
|
|
774
|
+
expect(typeValueEquals(env.lookup("life"), T.literal(42))).toBe(true);
|
|
775
|
+
|
|
776
|
+
setUnknownCollector(null);
|
|
777
|
+
setModuleResolver(null);
|
|
778
|
+
setCurrentFileDir("");
|
|
779
|
+
resetMockModules();
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
it("pre-binds __dirname and __filename as strings", () => {
|
|
783
|
+
const ast = parse(`const d = __dirname;\nconst f = __filename;\n`);
|
|
784
|
+
const env = createEnvironment();
|
|
785
|
+
evaluateProgram(ast, env);
|
|
786
|
+
|
|
787
|
+
expect(typeValueEquals(env.lookup("d"), T.string)).toBe(true);
|
|
788
|
+
expect(typeValueEquals(env.lookup("f"), T.string)).toBe(true);
|
|
789
|
+
});
|
|
439
790
|
});
|
|
440
791
|
|
|
441
792
|
describe("SwitchStatement", () => {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { T, typeValueEquals, typeValueToString, isSubtypeOf } from "@nudojs/core";
|
|
3
|
+
import { isTemplate, getTemplateParts, isRange, getRangeMeta } from "@nudojs/core";
|
|
4
|
+
import { evaluate, evaluateProgram } from "../evaluator.ts";
|
|
5
|
+
import { createEnvironment } from "@nudojs/core";
|
|
6
|
+
import { parse } from "@nudojs/parser";
|
|
7
|
+
|
|
8
|
+
function evalExpr(code: string): import("@nudojs/core").TypeValue {
|
|
9
|
+
const ast = parse(code);
|
|
10
|
+
const env = createEnvironment();
|
|
11
|
+
return evaluateProgram(ast, env);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function evalWithBindings(code: string, bindings: Record<string, import("@nudojs/core").TypeValue>): import("@nudojs/core").TypeValue {
|
|
15
|
+
const ast = parse(code);
|
|
16
|
+
const env = createEnvironment();
|
|
17
|
+
for (const [k, v] of Object.entries(bindings)) {
|
|
18
|
+
env.bind(k, v);
|
|
19
|
+
}
|
|
20
|
+
return evaluateProgram(ast, env);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("Template Literal Evaluation", () => {
|
|
24
|
+
it("all-literal template produces literal", () => {
|
|
25
|
+
const result = evalExpr("`hello world`");
|
|
26
|
+
expect(typeValueEquals(result, T.literal("hello world"))).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("template with abstract expression produces template type", () => {
|
|
30
|
+
const result = evalWithBindings("`xy${x}`", { x: T.string });
|
|
31
|
+
expect(isTemplate(result)).toBe(true);
|
|
32
|
+
expect(typeValueToString(result)).toBe("`xy${string}`");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("template with multiple parts", () => {
|
|
36
|
+
const result = evalWithBindings("`${x}!`", { x: T.string });
|
|
37
|
+
expect(isTemplate(result)).toBe(true);
|
|
38
|
+
expect(typeValueToString(result)).toBe("`${string}!`");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("string concatenation with + produces template", () => {
|
|
42
|
+
const result = evalWithBindings('"xy" + x', { x: T.string });
|
|
43
|
+
expect(isTemplate(result)).toBe(true);
|
|
44
|
+
expect(typeValueToString(result)).toBe("`xy${string}`");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("chained concatenation produces template", () => {
|
|
48
|
+
const result = evalWithBindings('"x" + x + "!"', { x: T.string });
|
|
49
|
+
expect(isTemplate(result)).toBe(true);
|
|
50
|
+
expect(typeValueToString(result)).toBe("`x${string}!`");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("Template Method Dispatch", () => {
|
|
55
|
+
it("startsWith on template with known prefix returns true", () => {
|
|
56
|
+
const result = evalWithBindings(
|
|
57
|
+
'const s = "xy" + x; s.startsWith("x")',
|
|
58
|
+
{ x: T.string },
|
|
59
|
+
);
|
|
60
|
+
// The evaluateMethodCall should dispatch to template's startsWith
|
|
61
|
+
// and return T.literal(true)
|
|
62
|
+
// Note: this requires string method support in evaluateMethodCall
|
|
63
|
+
// which we add in 6.1. For now, refined dispatch handles it.
|
|
64
|
+
if (result.kind === "literal") {
|
|
65
|
+
expect(result.value).toBe(true);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("Range Narrowing", () => {
|
|
71
|
+
it("x >= 0 narrows to range in true branch", () => {
|
|
72
|
+
const code = `
|
|
73
|
+
function f(x) {
|
|
74
|
+
if (x >= 0) {
|
|
75
|
+
return x;
|
|
76
|
+
}
|
|
77
|
+
return x;
|
|
78
|
+
}
|
|
79
|
+
`;
|
|
80
|
+
const ast = parse(code);
|
|
81
|
+
const env = createEnvironment();
|
|
82
|
+
evaluateProgram(ast, env);
|
|
83
|
+
const fn = env.lookup("f");
|
|
84
|
+
expect(fn.kind).toBe("function");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("range comparison produces correct results", () => {
|
|
88
|
+
const result = evalWithBindings(
|
|
89
|
+
'const r = x >= 0 ? x : 0; r',
|
|
90
|
+
{ x: T.number },
|
|
91
|
+
);
|
|
92
|
+
// In the true branch, x is narrowed to range(min:0)
|
|
93
|
+
// In the false branch, result is T.literal(0)
|
|
94
|
+
// Union of both
|
|
95
|
+
expect(result).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("User-defined Refined Types", () => {
|
|
100
|
+
it("refined type with custom ops", () => {
|
|
101
|
+
const odd = T.refine(T.number, {
|
|
102
|
+
name: "odd",
|
|
103
|
+
meta: {},
|
|
104
|
+
ops: {
|
|
105
|
+
"%"(self, other) {
|
|
106
|
+
if (other.kind === "literal" && other.value === 2) return T.literal(1);
|
|
107
|
+
return undefined;
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const result = evalWithBindings("x % 2", { x: odd });
|
|
113
|
+
expect(typeValueEquals(result, T.literal(1))).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("refined type falls back to base for undefined ops", () => {
|
|
117
|
+
const odd = T.refine(T.number, { name: "odd", meta: {} });
|
|
118
|
+
const result = evalWithBindings("x + 1", { x: odd });
|
|
119
|
+
expect(typeValueEquals(result, T.number)).toBe(true);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { T, typeValueEquals, createEnvironment } from "@nudojs/core";
|
|
3
|
+
import { evaluateProgram } from "../evaluator.ts";
|
|
4
|
+
import { parse } from "@nudojs/parser";
|
|
5
|
+
import type { TypeValue } from "@nudojs/core";
|
|
6
|
+
|
|
7
|
+
function evalExpr(code: string): TypeValue {
|
|
8
|
+
const ast = parse(code);
|
|
9
|
+
const env = createEnvironment();
|
|
10
|
+
return evaluateProgram(ast, env);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function evalWith(code: string, bindings: Record<string, TypeValue>): TypeValue {
|
|
14
|
+
const ast = parse(code);
|
|
15
|
+
const env = createEnvironment();
|
|
16
|
+
for (const [k, v] of Object.entries(bindings)) env.bind(k, v);
|
|
17
|
+
return evaluateProgram(ast, env);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("String methods - literal receiver", () => {
|
|
21
|
+
it("toUpperCase", () => {
|
|
22
|
+
expect(typeValueEquals(evalExpr('"hello".toUpperCase()'), T.literal("HELLO"))).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("toLowerCase", () => {
|
|
26
|
+
expect(typeValueEquals(evalExpr('"HELLO".toLowerCase()'), T.literal("hello"))).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("trim", () => {
|
|
30
|
+
expect(typeValueEquals(evalExpr('" hi ".trim()'), T.literal("hi"))).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("trimStart", () => {
|
|
34
|
+
expect(typeValueEquals(evalExpr('" hi".trimStart()'), T.literal("hi"))).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("trimEnd", () => {
|
|
38
|
+
expect(typeValueEquals(evalExpr('"hi ".trimEnd()'), T.literal("hi"))).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("charAt", () => {
|
|
42
|
+
expect(typeValueEquals(evalExpr('"abc".charAt(1)'), T.literal("b"))).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("charCodeAt", () => {
|
|
46
|
+
expect(typeValueEquals(evalExpr('"A".charCodeAt(0)'), T.literal(65))).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("at", () => {
|
|
50
|
+
expect(typeValueEquals(evalExpr('"abc".at(-1)'), T.literal("c"))).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("startsWith", () => {
|
|
54
|
+
expect(typeValueEquals(evalExpr('"hello".startsWith("he")'), T.literal(true))).toBe(true);
|
|
55
|
+
expect(typeValueEquals(evalExpr('"hello".startsWith("lo")'), T.literal(false))).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("endsWith", () => {
|
|
59
|
+
expect(typeValueEquals(evalExpr('"hello".endsWith("lo")'), T.literal(true))).toBe(true);
|
|
60
|
+
expect(typeValueEquals(evalExpr('"hello".endsWith("he")'), T.literal(false))).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("includes", () => {
|
|
64
|
+
expect(typeValueEquals(evalExpr('"hello".includes("ell")'), T.literal(true))).toBe(true);
|
|
65
|
+
expect(typeValueEquals(evalExpr('"hello".includes("xyz")'), T.literal(false))).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("indexOf", () => {
|
|
69
|
+
expect(typeValueEquals(evalExpr('"hello".indexOf("l")'), T.literal(2))).toBe(true);
|
|
70
|
+
expect(typeValueEquals(evalExpr('"hello".indexOf("z")'), T.literal(-1))).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("lastIndexOf", () => {
|
|
74
|
+
expect(typeValueEquals(evalExpr('"hello".lastIndexOf("l")'), T.literal(3))).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("slice", () => {
|
|
78
|
+
expect(typeValueEquals(evalExpr('"hello".slice(1, 3)'), T.literal("el"))).toBe(true);
|
|
79
|
+
expect(typeValueEquals(evalExpr('"hello".slice(2)'), T.literal("llo"))).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("substring", () => {
|
|
83
|
+
expect(typeValueEquals(evalExpr('"hello".substring(1, 4)'), T.literal("ell"))).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("split", () => {
|
|
87
|
+
const result = evalExpr('"a,b,c".split(",")');
|
|
88
|
+
expect(result.kind).toBe("tuple");
|
|
89
|
+
if (result.kind === "tuple") {
|
|
90
|
+
expect(result.elements.length).toBe(3);
|
|
91
|
+
expect(typeValueEquals(result.elements[0], T.literal("a"))).toBe(true);
|
|
92
|
+
expect(typeValueEquals(result.elements[1], T.literal("b"))).toBe(true);
|
|
93
|
+
expect(typeValueEquals(result.elements[2], T.literal("c"))).toBe(true);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("replace", () => {
|
|
98
|
+
expect(typeValueEquals(evalExpr('"hello".replace("l", "r")'), T.literal("herlo"))).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("replaceAll", () => {
|
|
102
|
+
expect(typeValueEquals(evalExpr('"hello".replaceAll("l", "r")'), T.literal("herro"))).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("repeat", () => {
|
|
106
|
+
expect(typeValueEquals(evalExpr('"ab".repeat(3)'), T.literal("ababab"))).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("padStart", () => {
|
|
110
|
+
expect(typeValueEquals(evalExpr('"5".padStart(3, "0")'), T.literal("005"))).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("padEnd", () => {
|
|
114
|
+
expect(typeValueEquals(evalExpr('"5".padEnd(3, "0")'), T.literal("500"))).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("String methods - abstract receiver", () => {
|
|
119
|
+
it("toUpperCase returns string", () => {
|
|
120
|
+
const result = evalWith("x.toUpperCase()", { x: T.string });
|
|
121
|
+
expect(typeValueEquals(result, T.string)).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("startsWith returns boolean", () => {
|
|
125
|
+
const result = evalWith('x.startsWith("a")', { x: T.string });
|
|
126
|
+
expect(typeValueEquals(result, T.boolean)).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("indexOf returns number", () => {
|
|
130
|
+
const result = evalWith('x.indexOf("a")', { x: T.string });
|
|
131
|
+
expect(typeValueEquals(result, T.number)).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("split returns string[]", () => {
|
|
135
|
+
const result = evalWith('x.split(",")', { x: T.string });
|
|
136
|
+
expect(result.kind).toBe("array");
|
|
137
|
+
if (result.kind === "array") {
|
|
138
|
+
expect(typeValueEquals(result.element, T.string)).toBe(true);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("charAt returns string", () => {
|
|
143
|
+
const result = evalWith("x.charAt(0)", { x: T.string });
|
|
144
|
+
expect(typeValueEquals(result, T.string)).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("slice returns string", () => {
|
|
148
|
+
const result = evalWith("x.slice(1)", { x: T.string });
|
|
149
|
+
expect(typeValueEquals(result, T.string)).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
});
|