Skip to content

Commit 23ad2b5

Browse files
authored
chore: fix method chain recognition issue (#228)
* chore: fix method chain recognition issue * tests: include tests for method chains
1 parent e5ccc05 commit 23ad2b5

File tree

6 files changed

+273
-5
lines changed

6 files changed

+273
-5
lines changed

src/parser/tag.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ pub fn get_sql_from_expr(
7575
get_sql_from_expr(sqls, var_decl_name, &expr.expr, span, import_alias);
7676
}
7777
Expr::Call(call_expr) => {
78-
let num_args = &call_expr.args.len();
79-
78+
// Always traverse the callee to detect SQL queries in method chains
8079
if let Some(callee_expr) = &call_expr.callee.as_expr() {
81-
if num_args == &0 {
82-
get_sql_from_expr(sqls, var_decl_name, callee_expr, span, import_alias);
83-
}
80+
get_sql_from_expr(sqls, var_decl_name, callee_expr, span, import_alias);
8481
}
82+
// Also traverse all arguments
8583
for arg in &call_expr.args {
8684
get_sql_from_expr(sqls, var_decl_name, &arg.expr, span, import_alias);
8785
}

tests/demo/typescript/expression/await.queries.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ export interface IAsyncLambdaAwaitedQuery {
2020
result: IAsyncLambdaAwaitedResult;
2121
}
2222

23+
export type AsyncLambdaParams = [];
24+
25+
export interface IAsyncLambdaResult {
26+
id: number;
27+
}
28+
29+
export interface IAsyncLambdaQuery {
30+
params: AsyncLambdaParams;
31+
result: IAsyncLambdaResult;
32+
}
33+
2334
export type IifLambdaParams = [];
2435

2536
export interface IIifLambdaResult {

tests/demo/typescript/expression/await.snapshot.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ export interface IAsyncLambdaAwaitedQuery {
2020
result: IAsyncLambdaAwaitedResult;
2121
}
2222

23+
export type AsyncLambdaParams = [];
24+
25+
export interface IAsyncLambdaResult {
26+
id: number;
27+
}
28+
29+
export interface IAsyncLambdaQuery {
30+
params: AsyncLambdaParams;
31+
result: IAsyncLambdaResult;
32+
}
33+
2334
export type IifLambdaParams = [];
2435

2536
export interface IIifLambdaResult {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
export type MethodChainSingleParams = [];
2+
3+
export interface IMethodChainSingleResult {
4+
id: number;
5+
}
6+
7+
export interface IMethodChainSingleQuery {
8+
params: MethodChainSingleParams;
9+
result: IMethodChainSingleResult;
10+
}
11+
12+
export type MethodChainMultipleParams = [];
13+
14+
export interface IMethodChainMultipleResult {
15+
id: number;
16+
}
17+
18+
export interface IMethodChainMultipleQuery {
19+
params: MethodChainMultipleParams;
20+
result: IMethodChainMultipleResult;
21+
}
22+
23+
export type MethodChainWithMapParams = [];
24+
25+
export interface IMethodChainWithMapResult {
26+
id: number;
27+
}
28+
29+
export interface IMethodChainWithMapQuery {
30+
params: MethodChainWithMapParams;
31+
result: IMethodChainWithMapResult;
32+
}
33+
34+
export type NestedChainParams = [];
35+
36+
export interface INestedChainResult {
37+
id: number;
38+
}
39+
40+
export interface INestedChainQuery {
41+
params: NestedChainParams;
42+
result: INestedChainResult;
43+
}
44+
45+
export type PromiseChainParams = [];
46+
47+
export interface IPromiseChainResult {
48+
id: number;
49+
}
50+
51+
export interface IPromiseChainQuery {
52+
params: PromiseChainParams;
53+
result: IPromiseChainResult;
54+
}
55+
56+
export type ArrayChainParams = [];
57+
58+
export interface IArrayChainResult {
59+
id: number;
60+
}
61+
62+
export interface IArrayChainQuery {
63+
params: ArrayChainParams;
64+
result: IArrayChainResult;
65+
}
66+
67+
export type ComplexChainParams = [];
68+
69+
export interface IComplexChainResult {
70+
id: number;
71+
}
72+
73+
export interface IComplexChainQuery {
74+
params: ComplexChainParams;
75+
result: IComplexChainResult;
76+
}
77+
78+
export type TernaryChainParams = [];
79+
80+
export interface ITernaryChainResult {
81+
id: number;
82+
}
83+
84+
export interface ITernaryChainQuery {
85+
params: TernaryChainParams;
86+
result: ITernaryChainResult;
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
export type MethodChainSingleParams = [];
2+
3+
export interface IMethodChainSingleResult {
4+
id: number;
5+
}
6+
7+
export interface IMethodChainSingleQuery {
8+
params: MethodChainSingleParams;
9+
result: IMethodChainSingleResult;
10+
}
11+
12+
export type MethodChainMultipleParams = [];
13+
14+
export interface IMethodChainMultipleResult {
15+
id: number;
16+
}
17+
18+
export interface IMethodChainMultipleQuery {
19+
params: MethodChainMultipleParams;
20+
result: IMethodChainMultipleResult;
21+
}
22+
23+
export type MethodChainWithMapParams = [];
24+
25+
export interface IMethodChainWithMapResult {
26+
id: number;
27+
}
28+
29+
export interface IMethodChainWithMapQuery {
30+
params: MethodChainWithMapParams;
31+
result: IMethodChainWithMapResult;
32+
}
33+
34+
export type NestedChainParams = [];
35+
36+
export interface INestedChainResult {
37+
id: number;
38+
}
39+
40+
export interface INestedChainQuery {
41+
params: NestedChainParams;
42+
result: INestedChainResult;
43+
}
44+
45+
export type PromiseChainParams = [];
46+
47+
export interface IPromiseChainResult {
48+
id: number;
49+
}
50+
51+
export interface IPromiseChainQuery {
52+
params: PromiseChainParams;
53+
result: IPromiseChainResult;
54+
}
55+
56+
export type ArrayChainParams = [];
57+
58+
export interface IArrayChainResult {
59+
id: number;
60+
}
61+
62+
export interface IArrayChainQuery {
63+
params: ArrayChainParams;
64+
result: IArrayChainResult;
65+
}
66+
67+
export type ComplexChainParams = [];
68+
69+
export interface IComplexChainResult {
70+
id: number;
71+
}
72+
73+
export interface IComplexChainQuery {
74+
params: ComplexChainParams;
75+
result: IComplexChainResult;
76+
}
77+
78+
export type TernaryChainParams = [];
79+
80+
export interface ITernaryChainResult {
81+
id: number;
82+
}
83+
84+
export interface ITernaryChainQuery {
85+
params: TernaryChainParams;
86+
result: ITernaryChainResult;
87+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { sql } from 'sqlx-ts'
2+
3+
// Simulating Effect-like API for demonstration
4+
class Effect<T> {
5+
static succeed<T>(value: T): Effect<T> {
6+
return new Effect(value)
7+
}
8+
9+
constructor(private value: T) {}
10+
11+
pipe<U>(fn: (value: T) => U): Effect<U> {
12+
return new Effect(fn(this.value))
13+
}
14+
15+
map<U>(fn: (value: T) => U): Effect<U> {
16+
return new Effect(fn(this.value))
17+
}
18+
}
19+
20+
// Issue #224: Method chaining with .pipe() - these should be recognized
21+
const methodChainSingle = Effect.succeed(sql`
22+
SELECT id FROM items
23+
`).pipe((str) => str)
24+
25+
const methodChainMultiple = Effect.succeed(sql`
26+
SELECT id FROM items
27+
`)
28+
.pipe((str) => str)
29+
.pipe((x) => x)
30+
31+
const methodChainWithMap = Effect.succeed(sql`
32+
-- @name: method chain with map
33+
SELECT id FROM items
34+
`).map((result) => result)
35+
36+
// Nested method chains
37+
const nestedChain = Effect.succeed(
38+
Effect.succeed(sql`
39+
-- @name: nested chain
40+
SELECT id FROM items
41+
`).pipe((x) => x)
42+
).pipe((y) => y)
43+
44+
// Promise-like chaining
45+
const promiseChain = Promise.resolve(sql`
46+
-- @name: promise chain
47+
SELECT id FROM items
48+
`)
49+
.then((result) => result)
50+
.catch((err) => err)
51+
52+
// Array method chaining
53+
const arrayChain = [sql`
54+
-- @name: array chain
55+
SELECT id FROM items
56+
`]
57+
.map((x) => x)
58+
.filter((x) => x)
59+
60+
// Complex expression with method chain
61+
const complexChain = (
62+
Effect.succeed(sql`
63+
-- @name: complex chain
64+
SELECT id FROM items
65+
`).pipe((data) => data)
66+
)
67+
68+
// Method chain in ternary operator
69+
const ternaryChain = true
70+
? Effect.succeed(sql`
71+
-- @name: ternary chain
72+
SELECT id FROM items
73+
`).pipe((x) => x)
74+
: null

0 commit comments

Comments
 (0)