Skip to content

Commit a1e4e29

Browse files
committed
refactor: replace zod with TypeScript generics for variable typing
Remove zod runtime dependency since it was only used for type inference, not runtime validation. The variable API now uses TypeScript generics directly: `variable<T>()` instead of `variable(z.type())`.
1 parent d615c44 commit a1e4e29

19 files changed

Lines changed: 339 additions & 385 deletions

bun.lock

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,5 @@
5454
},
5555
"peerDependencies": {
5656
"typescript": "^5.9.3"
57-
},
58-
"dependencies": {
59-
"zod": "^4.3.6"
6057
}
6158
}

src/compile.test.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { describe, expect, test } from "bun:test";
2-
import { z } from "zod";
32
import { compile, expr, variable } from "./index";
43

54
describe("compile 单元测试", () => {
65
describe("编译输出格式", () => {
76
test("基本结构: [变量名数组, ...表达式]", () => {
8-
const x = variable(z.number());
9-
const y = variable(z.number());
7+
const x = variable<number>();
8+
const y = variable<number>();
109

1110
const sum = expr({ x, y })("x + y");
1211
const result = compile(sum, { x, y });
@@ -17,8 +16,8 @@ describe("compile 单元测试", () => {
1716
});
1817

1918
test("变量占位符替换", () => {
20-
const x = variable(z.number());
21-
const y = variable(z.number());
19+
const x = variable<number>();
20+
const y = variable<number>();
2221

2322
const sum = expr({ x, y })("x + y");
2423
const result = compile(sum, { x, y });
@@ -28,9 +27,9 @@ describe("compile 单元测试", () => {
2827
});
2928

3029
test("变量顺序与声明顺序一致", () => {
31-
const a = variable(z.number());
32-
const b = variable(z.number());
33-
const c = variable(z.number());
30+
const a = variable<number>();
31+
const b = variable<number>();
32+
const c = variable<number>();
3433

3534
const expr1 = expr({ a, b, c })("c + b + a");
3635
const result = compile(expr1, { a, b, c });
@@ -39,8 +38,8 @@ describe("compile 单元测试", () => {
3938
});
4039

4140
test("相似变量名正确区分", () => {
42-
const x = variable(z.number());
43-
const xy = variable(z.number());
41+
const x = variable<number>();
42+
const xy = variable<number>();
4443

4544
// xy 不应该被 x 的替换影响
4645
const e = expr({ xy, x })("xy + x");
@@ -53,8 +52,8 @@ describe("compile 单元测试", () => {
5352

5453
describe("错误检测", () => {
5554
test("检测未定义的变量引用", () => {
56-
const x = variable(z.number());
57-
const y = variable(z.number());
55+
const x = variable<number>();
56+
const y = variable<number>();
5857

5958
const sum = expr({ x, y })("x + y + z");
6059

@@ -64,7 +63,7 @@ describe("compile 单元测试", () => {
6463
});
6564

6665
test("检测表达式中引用不存在的上下文变量", () => {
67-
const x = variable(z.number());
66+
const x = variable<number>();
6867

6968
const e1 = expr({ x })("x + 1");
7069
const e2 = expr({ e1 })("e1 + e2"); // e2 不存在
@@ -77,8 +76,8 @@ describe("compile 单元测试", () => {
7776

7877
describe("内联优化输出", () => {
7978
test("内联模式减少表达式数量", () => {
80-
const x = variable(z.number());
81-
const y = variable(z.number());
79+
const x = variable<number>();
80+
const y = variable<number>();
8281

8382
const sum = expr({ x, y })("x + y");
8483
const product = expr({ x, y })("x * y");
@@ -92,8 +91,8 @@ describe("compile 单元测试", () => {
9291
});
9392

9493
test("非内联模式保留所有中间表达式", () => {
95-
const x = variable(z.number());
96-
const y = variable(z.number());
94+
const x = variable<number>();
95+
const y = variable<number>();
9796

9897
const sum = expr({ x, y })("x + y");
9998
const product = expr({ x, y })("x * y");
@@ -111,8 +110,8 @@ describe("compile 单元测试", () => {
111110

112111
describe("AST 规范化", () => {
113112
test("输出不包含多余空格", () => {
114-
const x = variable(z.number());
115-
const y = variable(z.number());
113+
const x = variable<number>();
114+
const y = variable<number>();
116115

117116
// 原始表达式有空格
118117
const e = expr({ x, y })("x + y * 2");
@@ -123,8 +122,8 @@ describe("compile 单元测试", () => {
123122
});
124123

125124
test("保留必要的括号", () => {
126-
const x = variable(z.number());
127-
const y = variable(z.number());
125+
const x = variable<number>();
126+
const y = variable<number>();
128127

129128
const e = expr({ x, y })("(x + y) * 2");
130129
const result = compile(e, { x, y });

src/compile.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { z } from "zod";
21
import { collectIdentifiers, generate, parse, type ASTNode } from "./parser";
32
import type {
43
BranchNode,
@@ -34,7 +33,7 @@ export interface CompileOptions {
3433
/**
3534
* 表达式上下文类型约束
3635
*/
37-
type ExpressionContext = Record<string, Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>>;
36+
type ExpressionContext = Record<string, Variable | Expression<Record<string, unknown>, unknown>>;
3837

3938
/**
4039
* 将表达式树编译为可序列化的 JSON 结构
@@ -59,7 +58,7 @@ type ExpressionContext = Record<string, Variable<z.ZodType> | Expression<Record<
5958
*/
6059
export function compile<TResult>(
6160
expression: Expression<ExpressionContext, TResult>,
62-
variables: Record<string, Variable<z.ZodType>>,
61+
variables: Record<string, Variable>,
6362
options: CompileOptions = {}
6463
): CompiledData {
6564
const { inline = true, shortCircuit = true } = options;
@@ -87,12 +86,11 @@ export function compile<TResult>(
8786
const visited = new Set<symbol>();
8887
const visiting = new Set<symbol>();
8988

90-
for (const [name, variable] of Object.entries(variables)) {
89+
for (const [name] of Object.entries(variables)) {
9190
const id = Symbol(`var:${name}`);
9291
const node: ExprNode = {
9392
id,
9493
tag: "variable",
95-
schema: variable.schema,
9694
};
9795
nodeMap.set(id, node);
9896
variableNodes.set(name, node);
@@ -117,7 +115,7 @@ export function compile<TResult>(
117115

118116
// 收集表达式上下文中的所有节点
119117
for (const [key, contextItem] of Object.entries(expr.context)) {
120-
const item = contextItem as Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>;
118+
const item = contextItem as Variable | Expression<Record<string, unknown>, unknown>;
121119
if (item._tag === "variable") {
122120
const varNode = variableNodes.get(key);
123121
if (!varNode) {

src/constant.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expect, test } from "bun:test";
2-
import { z } from "zod";
32
import { compile, constant, evaluate, expr, variable } from "./index";
43

54
test("constant: 数字常量", () => {
@@ -46,7 +45,7 @@ test("constant: 对象常量", () => {
4645

4746
test("constant: 在表达式中使用常量", () => {
4847
const PI = constant(3.14159);
49-
const radius = variable(z.number());
48+
const radius = variable<number>();
5049
const area = expr({ PI, radius })("PI * radius * radius");
5150

5251
const compiled = compile(area, { radius });
@@ -66,7 +65,7 @@ test("constant: 多个常量组合", () => {
6665

6766
test("constant: 常量与变量混合", () => {
6867
const multiplier = constant(2);
69-
const x = variable(z.number());
68+
const x = variable<number>();
7069
const doubled = expr({ multiplier, x })("multiplier * x");
7170

7271
const compiled = compile(doubled, { x });

src/evaluate.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { describe, expect, test } from "bun:test";
2-
import { z } from "zod";
32
import { compile, evaluate, expr, variable } from "./index";
43

54
describe("evaluate 单元测试", () => {
65
describe("缓存机制", () => {
76
test("重复调用使用缓存的求值函数", () => {
8-
const x = variable(z.number());
7+
const x = variable<number>();
98
const sum = expr({ x })("x + 1");
109
const compiled = compile(sum, { x });
1110

@@ -19,8 +18,8 @@ describe("evaluate 单元测试", () => {
1918

2019
describe("错误处理", () => {
2120
test("缺少必需变量时抛出错误", () => {
22-
const x = variable(z.number());
23-
const y = variable(z.number());
21+
const x = variable<number>();
22+
const y = variable<number>();
2423

2524
const sum = expr({ x, y })("x + y");
2625
const compiled = compile(sum, { x, y });
@@ -31,9 +30,9 @@ describe("evaluate 单元测试", () => {
3130
});
3231

3332
test("缺少多个变量时报告第一个", () => {
34-
const a = variable(z.number());
35-
const b = variable(z.number());
36-
const c = variable(z.number());
33+
const a = variable<number>();
34+
const b = variable<number>();
35+
const c = variable<number>();
3736

3837
const e = expr({ a, b, c })("a + b + c");
3938
const compiled = compile(e, { a, b, c });
@@ -53,8 +52,8 @@ describe("evaluate 单元测试", () => {
5352
});
5453

5554
test("undefined 和 null 值", () => {
56-
const x = variable(z.union([z.number(), z.null(), z.undefined()]));
57-
const y = variable(z.number());
55+
const x = variable<number | null | undefined>();
56+
const y = variable<number>();
5857

5958
const e = expr({ x, y })("x ?? y");
6059
const compiled = compile(e, { x, y });

src/expr.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { z } from "zod";
21
import type { InferExpressionResult, ValidateExpression } from "./type-parser";
32
import type { Expression, Variable } from "./types";
43

54
/**
65
* 表达式上下文类型约束
76
*/
8-
type ExprContext = Record<string, Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>>;
7+
type ExprContext = Record<string, Variable | Expression<Record<string, unknown>, unknown>>;
98

109
/**
1110
* 表达式错误类型

0 commit comments

Comments
 (0)