@@ -4,7 +4,7 @@ import { type RuleContext } from "@typescript-eslint/utils/ts-eslint";
4
4
5
5
import typescript from "#/conditional-imports/typescript" ;
6
6
7
- import { type BaseOptions } from "./rule" ;
7
+ import { type BaseOptions , getTypeOfNode } from "./rule" ;
8
8
import {
9
9
isBlockStatement ,
10
10
isCallExpression ,
@@ -18,6 +18,7 @@ import {
18
18
isMethodDefinition ,
19
19
isObjectExpression ,
20
20
isProgram ,
21
+ isPromiseType ,
21
22
isProperty ,
22
23
isTSInterfaceBody ,
23
24
isTSInterfaceHeritage ,
@@ -104,6 +105,51 @@ export function isInReadonly(node: TSESTree.Node): boolean {
104
105
return getReadonly ( node ) !== null ;
105
106
}
106
107
108
+ /**
109
+ * Test if the given node is in a catch function callback of a promise.
110
+ */
111
+ export function isInPromiseCatchFunction <
112
+ Context extends RuleContext < string , BaseOptions > ,
113
+ > ( node : TSESTree . Node , context : Context ) : boolean {
114
+ const functionNode = getAncestorOfType (
115
+ ( n , c ) : n is TSESTree . FunctionLike => isFunctionLike ( n ) && n . body === c ,
116
+ node ,
117
+ ) ;
118
+
119
+ if (
120
+ functionNode === null ||
121
+ ! isCallExpression ( functionNode . parent ) ||
122
+ ! isMemberExpression ( functionNode . parent . callee ) ||
123
+ ! isIdentifier ( functionNode . parent . callee . property )
124
+ ) {
125
+ return false ;
126
+ }
127
+
128
+ const { object, property } = functionNode . parent . callee ;
129
+ switch ( property . name ) {
130
+ case "then" : {
131
+ if ( functionNode . parent . arguments [ 1 ] !== functionNode ) {
132
+ return false ;
133
+ }
134
+ break ;
135
+ }
136
+
137
+ case "catch" : {
138
+ if ( functionNode . parent . arguments [ 0 ] !== functionNode ) {
139
+ return false ;
140
+ }
141
+ break ;
142
+ }
143
+
144
+ default : {
145
+ return false ;
146
+ }
147
+ }
148
+
149
+ const objectType = getTypeOfNode ( object , context ) ;
150
+ return isPromiseType ( objectType ) ;
151
+ }
152
+
107
153
/**
108
154
* Test if the given node is shallowly inside a `Readonly<{...}>`.
109
155
*/
0 commit comments