Skip to content

Commit fd70675

Browse files
committed
fix: fix TypeScript ESLint warnings
1 parent 59f0467 commit fd70675

8 files changed

Lines changed: 130 additions & 113 deletions

File tree

src/compile.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { z } from "zod";
12
import { collectIdentifiers, generate, parse, type ASTNode } from "./parser";
23
import type { CompileContext, CompiledData, Expression, ExprNode, Variable } from "./types";
34

@@ -13,6 +14,11 @@ export interface CompileOptions {
1314
inline?: boolean;
1415
}
1516

17+
/**
18+
* 表达式上下文类型约束
19+
*/
20+
type ExpressionContext = Record<string, Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>>;
21+
1622
/**
1723
* 将表达式树编译为可序列化的 JSON 结构
1824
*
@@ -35,8 +41,8 @@ export interface CompileOptions {
3541
* ```
3642
*/
3743
export function compile<TResult>(
38-
expression: Expression<any, TResult>,
39-
variables: Record<string, Variable<any>>,
44+
expression: Expression<ExpressionContext, TResult>,
45+
variables: Record<string, Variable<z.ZodType>>,
4046
options: CompileOptions = {}
4147
): CompiledData {
4248
const { inline = true } = options;
@@ -48,12 +54,16 @@ export function compile<TResult>(
4854
};
4955

5056
// 第一步:为每个表达式分配唯一 ID
51-
const exprIdMap = new WeakMap<Expression<any, any>, symbol>();
52-
const getExprId = (expr: Expression<any, any>): symbol => {
57+
const exprIdMap = new WeakMap<Expression<Record<string, unknown>, unknown>, symbol>();
58+
const getExprId = (expr: Expression<Record<string, unknown>, unknown>): symbol => {
5359
if (!exprIdMap.has(expr)) {
5460
exprIdMap.set(expr, Symbol("expr"));
5561
}
56-
return exprIdMap.get(expr)!;
62+
const id = exprIdMap.get(expr);
63+
if (id === undefined) {
64+
throw new Error("Expression ID not found");
65+
}
66+
return id;
5767
};
5868

5969
// 为所有变量创建 node
@@ -75,7 +85,7 @@ export function compile<TResult>(
7585

7686
// 第二步:递归收集所有依赖的节点,并检测循环依赖
7787
const exprNodes = new Map<symbol, ExprNode>();
78-
const collectNodes = (expr: Expression<any, any>): ExprNode => {
88+
const collectNodes = (expr: Expression<Record<string, unknown>, unknown>): ExprNode => {
7989
const exprId = getExprId(expr);
8090

8191
if (visited.has(exprId)) {
@@ -92,7 +102,7 @@ export function compile<TResult>(
92102

93103
// 收集表达式上下文中的所有节点
94104
for (const [key, contextItem] of Object.entries(expr.context)) {
95-
const item = contextItem as Variable<any> | Expression<any, any>;
105+
const item = contextItem as Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>;
96106
if (item._tag === "variable") {
97107
const varNode = variableNodes.get(key);
98108
if (!varNode) {

src/evaluate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function evaluate<TResult>(data: CompiledData, values: Record<string, unk
5656
if (!evaluator) {
5757
// 构造求值函数
5858
const functionBody = buildEvaluatorFunctionBody(expressions, variableNames.length);
59+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
5960
evaluator = new Function("$values", functionBody) as (values: unknown[]) => unknown;
6061
evaluatorCache.set(cacheKey, evaluator);
6162
}

src/expr.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { z } from "zod";
12
import type { InferExpressionResult, ValidateExpression } from "./type-parser";
23
import type { Expression, Variable } from "./types";
34

45
/**
56
* 表达式上下文类型约束
67
*/
7-
type ExprContext = Record<string, Variable<any> | Expression<any, any>>;
8+
type ExprContext = Record<string, Variable<z.ZodType> | Expression<Record<string, unknown>, unknown>>;
89

910
/**
1011
* 表达式错误类型
@@ -58,7 +59,7 @@ export function expr<TContext extends ExprContext>(
5859
_tag: "expression",
5960
context,
6061
source,
61-
_type: undefined as any,
62+
_type: undefined as unknown,
6263
} as Expression<TContext, ExprResult<Source, TContext>>;
6364
};
6465
}

src/parser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,11 @@ export function generate(node: ASTNode): string {
777777
return `{${props.join(",")}}`;
778778
}
779779

780-
default:
781-
throw new Error(`Unknown node type: ${(node as any).type}`);
780+
default: {
781+
const unknownNode = node as { type?: string };
782+
const nodeType = unknownNode.type ?? "unknown";
783+
throw new Error(`Unknown node type: ${nodeType}`);
784+
}
782785
}
783786
}
784787

0 commit comments

Comments
 (0)