1
1
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/pull/3579/commits/ebb739a0fe99a2ee77055870bfda9f67a2691374
2
2
import * as AST from "@eslint-react/ast" ;
3
- import { isReactHookCall , isReactHookCallWithNameLoose , isUseStateCall } from "@eslint-react/core" ;
3
+ import { isReactHookCall , isReactHookCallWithNameLoose , isReactHookName , isUseStateCall } from "@eslint-react/core" ;
4
4
import type { RuleContext , RuleFeature } from "@eslint-react/shared" ;
5
5
import { getSettingsFromContext } from "@eslint-react/shared" ;
6
6
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint" ;
@@ -14,8 +14,16 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[];
14
14
15
15
export type MessageID = CamelCase < typeof RULE_NAME > ;
16
16
17
- // variables should be defined here
18
- const ALLOW_LIST = [ "Boolean" , "String" , "Number" ] ;
17
+ // identifier names for allowed function names
18
+ const ALLOW_LIST = [
19
+ "Boolean" ,
20
+ "String" ,
21
+ "Number" ,
22
+ ] ;
23
+
24
+ function isAllowedName ( name : string ) : boolean {
25
+ return ALLOW_LIST . includes ( name ) || isReactHookName ( name ) ;
26
+ }
19
27
20
28
// rule takes inspiration from https://github.com/facebook/react/issues/26520
21
29
export default createRule < [ ] , MessageID > ( {
@@ -54,11 +62,11 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
54
62
const nestedCallExpressions = AST . getNestedCallExpressions ( useStateInput ) ;
55
63
const hasFunctionCall = nestedCallExpressions . some ( ( n ) => {
56
64
return "name" in n . callee
57
- && ! ALLOW_LIST . includes ( n . callee . name ) ;
65
+ && ! isAllowedName ( n . callee . name ) ;
58
66
} ) ;
59
67
const hasNewCall = AST . getNestedNewExpressions ( useStateInput ) . some ( ( n ) => {
60
68
return "name" in n . callee
61
- && ! ALLOW_LIST . includes ( n . callee . name ) ;
69
+ && ! isAllowedName ( n . callee . name ) ;
62
70
} ) ;
63
71
if ( ! hasFunctionCall && ! hasNewCall ) {
64
72
return ;
0 commit comments