File tree Expand file tree Collapse file tree 2 files changed +61
-41
lines changed Expand file tree Collapse file tree 2 files changed +61
-41
lines changed Original file line number Diff line number Diff line change @@ -37,11 +37,11 @@ const isPaginated = (objectType) => {
3737 if ( isPaginationName ( objectType . typeName . name ) ) {
3838 return true ;
3939 }
40-
40+ } else if ( objectType . type === "TSTypeLiteral" ) {
4141 if (
4242 objectType . members != null &&
4343 objectType . members . some (
44- ( member ) => member . key != null && isPaginationName ( member . key )
44+ ( member ) => member . key != null && isPaginationName ( member . key . name )
4545 )
4646 ) {
4747 return true ;
@@ -93,15 +93,21 @@ const rule = createRule({
9393 : null ;
9494
9595 if ( returnType != null ) {
96- if ( returnType . typeName . name === "Promise" ) {
96+ if (
97+ returnType . type === "TSTypeReference" &&
98+ returnType . typeName . name === "Promise"
99+ ) {
97100 if (
98101 returnType . typeParameters . params . length === 1 &&
99102 ! isPaginated ( returnType . typeParameters . params [ 0 ] )
100103 ) {
101- report ( context , returnType ) ;
102- } else if ( ! isPaginated ( returnType ) ) {
103- report ( context , returnType ) ;
104+ report ( context , node ) ;
104105 }
106+ } else if (
107+ returnType . type === "TSArrayType" ||
108+ ! isPaginated ( returnType )
109+ ) {
110+ report ( context , node ) ;
105111 }
106112 }
107113 }
Original file line number Diff line number Diff line change @@ -5,51 +5,65 @@ const ruleTester = new ESLintUtils.RuleTester({
55 parser : "@typescript-eslint/parser" ,
66} ) ;
77
8+ const expectedError = {
9+ messageId : "PreferReturnCollectionsWithPagination" ,
10+ type : "Decorator" ,
11+ } ;
12+
813ruleTester . run ( "prefer-collections-with-pagination" , rule , {
914 valid : [
10- // `
11- // @Controller ()
12- // public class Test {
13- // @Get ()
14- // public find(): Page {}
15- // }
16- // `,
15+ `
16+ @Controller()
17+ public class Test {
18+ @Get()
19+ public find(): Page {}
20+ }
21+ ` ,
1722 `
1823 @Controller()
1924 public class Test {
2025 @Get()
2126 public find(): Promise<Pagination> {}
2227 }
2328 ` ,
24- // `
25- // @Controller ()
26- // public class Test {
27- // @Get ()
28- // public find(): Promise<{items: string[], currentPage: number, totalPages: number}> {}
29- // }
30- // `,
29+ `
30+ @Controller()
31+ public class Test {
32+ @Get()
33+ public find(): Promise<{items: string[], currentPage: number, totalPages: number}> {}
34+ }
35+ ` ,
3136 ] ,
3237 invalid : [
33- // `
34- // @Controller ()
35- // public class Test {
36- // @Get ()
37- // public find(): Promise<string[]> {}
38- // }
39- // `,
40- // `
41- // @Controller ()
42- // public class Test {
43- // @Get ()
44- // public find(): string[] {}
45- // }
46- // `,
47- // `
48- // @Controller ()
49- // public class Test {
50- // @Get ()
51- // public find(): Promise<{items: string[]}> {}
52- // }
53- // `,
38+ {
39+ code : `
40+ @Controller()
41+ public class Test {
42+ @Get()
43+ public find(): Promise<string[]> {}
44+ }
45+ ` ,
46+ errors : [ expectedError ] ,
47+ } ,
48+ {
49+ code : `
50+ @Controller()
51+ public class Test {
52+ @Get()
53+ public find(): string[] {}
54+ }
55+ ` ,
56+ errors : [ expectedError ] ,
57+ } ,
58+ {
59+ code : `
60+ @Controller()
61+ public class Test {
62+ @Get()
63+ public find(): Promise<{items: string[]}> {}
64+ }
65+ ` ,
66+ errors : [ expectedError ] ,
67+ } ,
5468 ] ,
5569} ) ;
You can’t perform that action at this time.
0 commit comments