Skip to content

Commit a9d181d

Browse files
authored
as const typescript fix (#234)
fix
1 parent 2a77387 commit a9d181d

File tree

4 files changed

+138
-5
lines changed

4 files changed

+138
-5
lines changed

src/parser/tag.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,28 @@ pub fn get_sql_from_expr(
302302
Expr::JSXEmpty(_) => {}
303303
Expr::JSXElement(_) => {}
304304
Expr::JSXFragment(_) => {}
305-
Expr::TsTypeAssertion(_) => {}
306-
Expr::TsConstAssertion(_) => {}
307-
Expr::TsAs(_) => {}
308-
Expr::TsInstantiation(_) => {}
305+
Expr::TsTypeAssertion(ts_type_assertion) => {
306+
// Traverse into type assertions like `<Type>expr`
307+
get_sql_from_expr(sqls, var_decl_name, &ts_type_assertion.expr, span, import_alias);
308+
}
309+
Expr::TsConstAssertion(ts_const_assertion) => {
310+
// Traverse into const assertions like `expr as const`
311+
get_sql_from_expr(sqls, var_decl_name, &ts_const_assertion.expr, span, import_alias);
312+
}
313+
Expr::TsAs(ts_as) => {
314+
// Traverse into type assertions like `expr as Type`
315+
get_sql_from_expr(sqls, var_decl_name, &ts_as.expr, span, import_alias);
316+
}
317+
Expr::TsInstantiation(ts_instantiation) => {
318+
// Traverse into generic instantiations like `expr<Type>`
319+
get_sql_from_expr(sqls, var_decl_name, &ts_instantiation.expr, span, import_alias);
320+
}
309321
Expr::PrivateName(_) => {}
310322
Expr::Invalid(_) => {}
311-
Expr::TsSatisfies(_) => {}
323+
Expr::TsSatisfies(ts_satisfies) => {
324+
// Traverse into satisfies expressions like `expr satisfies Type`
325+
get_sql_from_expr(sqls, var_decl_name, &ts_satisfies.expr, span, import_alias);
326+
}
312327
}
313328
}
314329

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export type BasicAsConstParams = [];
2+
3+
export interface IBasicAsConstResult {
4+
id: number;
5+
name: string;
6+
}
7+
8+
export interface IBasicAsConstQuery {
9+
params: BasicAsConstParams;
10+
result: IBasicAsConstResult;
11+
}
12+
13+
export type FunctionReturningAsConstParams = [string | null];
14+
15+
export interface IFunctionReturningAsConstResult {
16+
id: number;
17+
name: string;
18+
}
19+
20+
export interface IFunctionReturningAsConstQuery {
21+
params: FunctionReturningAsConstParams;
22+
result: IFunctionReturningAsConstResult;
23+
}
24+
25+
export type NestedAsConstParams = [number];
26+
27+
export interface INestedAsConstResult {
28+
id: number;
29+
name: string;
30+
}
31+
32+
export interface INestedAsConstQuery {
33+
params: NestedAsConstParams;
34+
result: INestedAsConstResult;
35+
}
36+
37+
export type AsConstWithTypeAssertionParams = [];
38+
39+
export interface IAsConstWithTypeAssertionResult {
40+
id: number;
41+
name: string;
42+
}
43+
44+
export interface IAsConstWithTypeAssertionQuery {
45+
params: AsConstWithTypeAssertionParams;
46+
result: IAsConstWithTypeAssertionResult;
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export type BasicAsConstParams = [];
2+
3+
export interface IBasicAsConstResult {
4+
id: number;
5+
name: string;
6+
}
7+
8+
export interface IBasicAsConstQuery {
9+
params: BasicAsConstParams;
10+
result: IBasicAsConstResult;
11+
}
12+
13+
export type FunctionReturningAsConstParams = [string | null];
14+
15+
export interface IFunctionReturningAsConstResult {
16+
id: number;
17+
name: string;
18+
}
19+
20+
export interface IFunctionReturningAsConstQuery {
21+
params: FunctionReturningAsConstParams;
22+
result: IFunctionReturningAsConstResult;
23+
}
24+
25+
export type NestedAsConstParams = [number];
26+
27+
export interface INestedAsConstResult {
28+
id: number;
29+
name: string;
30+
}
31+
32+
export interface INestedAsConstQuery {
33+
params: NestedAsConstParams;
34+
result: INestedAsConstResult;
35+
}
36+
37+
export type AsConstWithTypeAssertionParams = [];
38+
39+
export interface IAsConstWithTypeAssertionResult {
40+
id: number;
41+
name: string;
42+
}
43+
44+
export interface IAsConstWithTypeAssertionQuery {
45+
params: AsConstWithTypeAssertionParams;
46+
result: IAsConstWithTypeAssertionResult;
47+
}
48+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { sql } from 'sqlx-ts'
2+
3+
// Basic as const
4+
const basicAsConst = {
5+
somequery: sql`SELECT id, name FROM items`
6+
} as const
7+
8+
// Function returning as const
9+
const functionReturningAsConst = {
10+
somequery: () => sql`SELECT id, name FROM items WHERE rarity = $1`
11+
} as const
12+
13+
// Nested as const
14+
const nestedAsConst = {
15+
queries: {
16+
item: sql`SELECT id, name FROM items WHERE id = $1`
17+
}
18+
} as const
19+
20+
// as const with type assertion
21+
const asConstWithTypeAssertion = {
22+
query: sql`SELECT id, name FROM items`
23+
} as const satisfies Record<string, any>

0 commit comments

Comments
 (0)