@@ -3,18 +3,23 @@ import type { TSESTree } from '@typescript-eslint/utils';
3
3
import { AST_NODE_TYPES } from '@typescript-eslint/utils' ;
4
4
import * as ts from 'typescript' ;
5
5
6
+ import type { TypeOrValueSpecifier } from '../util' ;
7
+
6
8
import {
7
9
createRule ,
8
10
getParserServices ,
9
11
isErrorLike ,
10
12
isTypeAnyType ,
11
13
isTypeUnknownType ,
14
+ typeMatchesSomeSpecifier ,
15
+ typeOrValueSpecifiersSchema ,
12
16
} from '../util' ;
13
17
14
18
type MessageIds = 'object' | 'undef' ;
15
19
16
20
type Options = [
17
21
{
22
+ allow ?: TypeOrValueSpecifier [ ] ;
18
23
allowThrowingAny ?: boolean ;
19
24
allowThrowingUnknown ?: boolean ;
20
25
} ,
@@ -39,6 +44,10 @@ export default createRule<Options, MessageIds>({
39
44
type : 'object' ,
40
45
additionalProperties : false ,
41
46
properties : {
47
+ allow : {
48
+ ...typeOrValueSpecifiersSchema ,
49
+ description : 'Type specifiers that can be thrown.' ,
50
+ } ,
42
51
allowThrowingAny : {
43
52
type : 'boolean' ,
44
53
description :
@@ -55,13 +64,14 @@ export default createRule<Options, MessageIds>({
55
64
} ,
56
65
defaultOptions : [
57
66
{
67
+ allow : [ ] ,
58
68
allowThrowingAny : true ,
59
69
allowThrowingUnknown : true ,
60
70
} ,
61
71
] ,
62
72
create ( context , [ options ] ) {
63
73
const services = getParserServices ( context ) ;
64
-
74
+ const allow = options . allow ;
65
75
function checkThrowArgument ( node : TSESTree . Node ) : void {
66
76
if (
67
77
node . type === AST_NODE_TYPES . AwaitExpression ||
@@ -72,6 +82,10 @@ export default createRule<Options, MessageIds>({
72
82
73
83
const type = services . getTypeAtLocation ( node ) ;
74
84
85
+ if ( typeMatchesSomeSpecifier ( type , allow , services . program ) ) {
86
+ return ;
87
+ }
88
+
75
89
if ( type . flags & ts . TypeFlags . Undefined ) {
76
90
context . report ( { node, messageId : 'undef' } ) ;
77
91
return ;
0 commit comments