Skip to content

Commit 5a6274b

Browse files
committed
chore(api): 移除多余类型断言并简化代理返回
详细的提交内容: - 去掉 `getProxyMetadata`、`parse`、`variableIds.set` 等位置的多余 `as object` / `as string` 类型断言,直接使用推导类型 - 精简 `lambda` 与模板表达式相关实现,移除不必要的显式返回类型断言 - 调整 `serializeArgumentToAST` 中基础字面量节点的构造方式,减少对 AST 类型的冗余强转 - 同步更新单元测试中的调用方式,确保测试覆盖新的类型推导写法 - 这次改动主要用于提升代码可读性与类型推导一致性,降低后续维护成本 - 影响范围:`api`、`proxy` 及相关单元测试
1 parent fa2ca4e commit 5a6274b

8 files changed

Lines changed: 22 additions & 25 deletions

File tree

‎src/__tests__/unit/proxy-variable.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("代理变量 单元测试", () => {
88
test("创建代理变量", () => {
99
const x = variable<number>();
1010
expect(typeof x).toBe("function"); // Proxy wraps function
11-
const meta = getProxyMetadata(x as object);
11+
const meta = getProxyMetadata(x);
1212
expect(meta?.type).toBe("variable");
1313
});
1414

@@ -20,7 +20,7 @@ describe("代理变量 单元测试", () => {
2020
const config = variable<Config>();
2121
const timeout = config.timeout;
2222

23-
const meta = getProxyMetadata(timeout as object);
23+
const meta = getProxyMetadata(timeout);
2424
expect(meta?.type).toBe("expression");
2525
expect(meta?.ast).toBeDefined();
2626
if (meta?.ast) {
@@ -47,7 +47,7 @@ describe("代理变量 单元测试", () => {
4747
const builder = variable<Builder>();
4848
const result = builder.build("test");
4949

50-
const meta = getProxyMetadata(result as object);
50+
const meta = getProxyMetadata(result);
5151
expect(meta?.type).toBe("expression");
5252
expect(meta?.ast).toBeDefined();
5353
if (meta?.ast) {
@@ -153,7 +153,7 @@ describe("代理变量 单元测试", () => {
153153
const ui = variable<UI>();
154154
const configured = ui.configure({ padding: 10, margin: 5 });
155155

156-
const meta = getProxyMetadata(configured as object);
156+
const meta = getProxyMetadata(configured);
157157
expect(meta?.ast).toBeDefined();
158158
if (meta?.ast) {
159159
const source = generate(meta.ast);

‎src/__tests__/unit/template.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("模板标签 单元测试", () => {
99
const name = variable<string>();
1010
const greeting = t`Hello, ${name}!`;
1111

12-
const meta = getProxyMetadata(greeting as object);
12+
const meta = getProxyMetadata(greeting);
1313
expect(meta?.type).toBe("expression");
1414
expect(meta?.ast).toBeDefined();
1515
if (meta?.ast) {

‎src/__tests__/unit/type-parser.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe("type-parser 单元测试", () => {
121121
const _y = variable<number>();
122122

123123
const _sum = expr({ x: _x, y: _y })("x + y");
124-
const meta = getProxyMetadata(_sum as object);
124+
const meta = getProxyMetadata(_sum);
125125

126126
// 验证元数据
127127
expect(meta?.type).toBe("expression");
@@ -134,7 +134,7 @@ describe("type-parser 单元测试", () => {
134134

135135
const _sum = expr({ x: _x, y: _y })("x + y");
136136
const _doubled = expr({ sum: _sum })("sum * 2");
137-
const meta = getProxyMetadata(_doubled as object);
137+
const meta = getProxyMetadata(_doubled);
138138

139139
expect(meta?.type).toBe("expression");
140140
expect(meta?.ast).toBeDefined();
@@ -144,7 +144,7 @@ describe("type-parser 单元测试", () => {
144144
const _age = variable<number>();
145145

146146
const _isAdult = expr({ age: _age })("age >= 18");
147-
const meta = getProxyMetadata(_isAdult as object);
147+
const meta = getProxyMetadata(_isAdult);
148148

149149
expect(meta?.type).toBe("expression");
150150
expect(meta?.ast).toBeDefined();
@@ -156,7 +156,7 @@ describe("type-parser 单元测试", () => {
156156

157157
// (x + y) * 2 - 1
158158
const _complex = expr({ x: _x, y: _y })("(x + y) * 2 - 1");
159-
const meta = getProxyMetadata(_complex as object);
159+
const meta = getProxyMetadata(_complex);
160160

161161
expect(meta?.type).toBe("expression");
162162
expect(meta?.ast).toBeDefined();
@@ -167,7 +167,7 @@ describe("type-parser 单元测试", () => {
167167
const _b = variable<boolean>();
168168

169169
const _result = expr({ a: _a, b: _b })("a && b || !a");
170-
const meta = getProxyMetadata(_result as object);
170+
const meta = getProxyMetadata(_result);
171171

172172
// 逻辑表达式最终返回 boolean
173173
expect(meta?.type).toBe("expression");
@@ -179,7 +179,7 @@ describe("type-parser 单元测试", () => {
179179
const _lastName = variable<string>();
180180

181181
const _fullName = expr({ firstName: _firstName, lastName: _lastName })("firstName + lastName");
182-
const meta = getProxyMetadata(_fullName as object);
182+
const meta = getProxyMetadata(_fullName);
183183

184184
expect(meta?.type).toBe("expression");
185185
expect(meta?.ast).toBeDefined();
@@ -195,8 +195,8 @@ describe("type-parser 单元测试", () => {
195195
const _sum = expr({ x: _x, y: _y })("x + y");
196196
const _isPositive = expr({ sum: _sum })("sum > 0");
197197

198-
const sumMeta = getProxyMetadata(_sum as object);
199-
const isPositiveMeta = getProxyMetadata(_isPositive as object);
198+
const sumMeta = getProxyMetadata(_sum);
199+
const isPositiveMeta = getProxyMetadata(_isPositive);
200200

201201
expect(sumMeta?.type).toBe("expression");
202202
expect(isPositiveMeta?.type).toBe("expression");

‎src/api/expr.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function expr<TContext extends Record<string, unknown>>(
9292
}
9393

9494
// 解析用户输入的字符串为 AST
95-
const ast = parse(source as string);
95+
const ast = parse(source);
9696

9797
// 统计每个标识符的引用次数,用于决定是否内联
9898
const refCounts = new Map<string, number>();

‎src/api/lambda.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function lambda<Args extends unknown[], R>(builder: LambdaBuilder<Args, R
9696
});
9797
}
9898

99-
return lambdaProxy as Lambda<Args, R>;
99+
return lambdaProxy;
100100
}
101101

102102
/**

‎src/api/template.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { collectDepsFromArgs, createProxyExpressionWithAST, serializeArgumentToAST } from "../proxy/proxy-variable";
22
import type { Proxify } from "../types";
3-
import type { ASTNode, BinaryExpr, StringLiteral } from "../types/ast-types";
3+
import type { ASTNode, StringLiteral } from "../types/ast-types";
44

55
/**
66
* Tagged template 函数,用于创建包含变量的字符串表达式
@@ -33,7 +33,7 @@ export function t(strings: TemplateStringsArray, ...values: unknown[]): Proxify<
3333
type: "StringLiteral",
3434
value: str,
3535
quote: '"',
36-
} as StringLiteral);
36+
});
3737
}
3838

3939
if (i < values.length) {
@@ -66,7 +66,7 @@ export function t(strings: TemplateStringsArray, ...values: unknown[]): Proxify<
6666
operator: "+",
6767
left: resultAst,
6868
right: parts[i]!,
69-
} as BinaryExpr;
69+
};
7070
}
7171

7272
return createProxyExpressionWithAST<string>(resultAst, deps);

‎src/api/variable.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let variableCounter = 0;
2525
export function variable<T>(): Variable<T> {
2626
const id = Symbol(`var_${variableCounter++}`);
2727
const proxy = createProxyVariable<T>(id);
28-
variableIds.set(proxy as object, id);
28+
variableIds.set(proxy, id);
2929
return proxy;
3030
}
3131

‎src/proxy/proxy-variable.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import type { Proxify } from "../types";
33
import type {
44
ArrayExpr,
55
ASTNode,
6-
BooleanLiteral,
76
CallExpr,
87
Identifier,
98
MemberExpr,
10-
NullLiteral,
119
NumberLiteral,
12-
ObjectExpr,
1310
Placeholder,
1411
StringLiteral,
1512
} from "../types/ast-types";
@@ -205,13 +202,13 @@ export function serializeArgumentToAST(arg: unknown): ASTNode {
205202
const key: ASTNode = isValidIdentifier ? identifier(k) : stringLiteral(k);
206203
return { key, value: serializeArgumentToAST(v), computed: false, shorthand: false };
207204
});
208-
return { type: "ObjectExpr", properties } as ObjectExpr;
205+
return { type: "ObjectExpr", properties };
209206
}
210207

211208
// 4. 原始值
212-
if (arg === null) return { type: "NullLiteral" } as NullLiteral;
209+
if (arg === null) return { type: "NullLiteral" };
213210
if (arg === undefined) return identifier("undefined");
214-
if (typeof arg === "boolean") return { type: "BooleanLiteral", value: arg } as BooleanLiteral;
211+
if (typeof arg === "boolean") return { type: "BooleanLiteral", value: arg };
215212
if (typeof arg === "number") return numberLiteral(arg);
216213
if (typeof arg === "string") return stringLiteral(arg);
217214
if (typeof arg === "bigint") return callExpr(identifier("BigInt"), [stringLiteral(arg.toString())]);

0 commit comments

Comments
 (0)