|
1 | | -package rules_of_hooks |
2 | | - |
3 | | -import ( |
4 | | - "github.com/microsoft/typescript-go/shim/ast" |
5 | | - "github.com/web-infra-dev/rslint/internal/plugins/import/utils" |
6 | | - "github.com/web-infra-dev/rslint/internal/rule" |
7 | | -) |
8 | | - |
9 | | -// Message functions for different error types |
10 | | -func buildConditionalHookMessage(hookName string) rule.RuleMessage { |
11 | | - return rule.RuleMessage{ |
12 | | - Id: "conditionalHook", |
13 | | - Description: `React Hook "` + hookName + `" is called conditionally. React Hooks must be ` + |
14 | | - "called in the exact same order in every component render.", |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -func buildLoopHookMessage(hookName string) rule.RuleMessage { |
19 | | - return rule.RuleMessage{ |
20 | | - Id: "loopHook", |
21 | | - Description: `React Hook "` + hookName + `" may be executed more than once. Possibly ` + |
22 | | - "because it is called in a loop. React Hooks must be called in the " + |
23 | | - "exact same order in every component render.", |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -func buildFunctionHookMessage(hookName, functionName string) rule.RuleMessage { |
28 | | - return rule.RuleMessage{ |
29 | | - Id: "functionHook", |
30 | | - Description: `React Hook "` + hookName + `" is called in function "` + functionName + `" that is neither ` + |
31 | | - "a React function component nor a custom React Hook function." + |
32 | | - " React component names must start with an uppercase letter." + |
33 | | - " React Hook names must start with the word \"use\".", |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -func buildGenericHookMessage(hookName string) rule.RuleMessage { |
38 | | - return rule.RuleMessage{ |
39 | | - Id: "genericHook", |
40 | | - Description: `React Hook "` + hookName + `" cannot be called inside a callback. React Hooks ` + |
41 | | - "must be called in a React function component or a custom React " + |
42 | | - "Hook function.", |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -func buildTopLevelHookMessage(hookName string) rule.RuleMessage { |
47 | | - return rule.RuleMessage{ |
48 | | - Id: "topLevelHook", |
49 | | - Description: `React Hook "` + hookName + `" cannot be called at the top level. React Hooks ` + |
50 | | - "must be called in a React function component or a custom React " + |
51 | | - "Hook function.", |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -func buildClassHookMessage(hookName string) rule.RuleMessage { |
56 | | - return rule.RuleMessage{ |
57 | | - Id: "classHook", |
58 | | - Description: `React Hook "` + hookName + `" cannot be called in a class component. React Hooks ` + |
59 | | - "must be called in a React function component or a custom React " + |
60 | | - "Hook function.", |
61 | | - } |
62 | | -} |
63 | | - |
64 | | -func buildAsyncComponentHookMessage(hookName string) rule.RuleMessage { |
65 | | - return rule.RuleMessage{ |
66 | | - Id: "asyncComponentHook", |
67 | | - Description: `React Hook "` + hookName + `" cannot be called in an async function.`, |
68 | | - } |
69 | | -} |
70 | | - |
71 | | -func buildTryCatchUseMessage(hookName string) rule.RuleMessage { |
72 | | - return rule.RuleMessage{ |
73 | | - Id: "tryCatchUse", |
74 | | - Description: `React Hook "` + hookName + `" cannot be called inside a try/catch block.`, |
75 | | - } |
76 | | -} |
77 | | - |
78 | | -var RulesOfHooksRule = rule.Rule{ |
79 | | - Name: "react-hooks/rules-of-hooks", |
80 | | - Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { |
81 | | - return utils.VisitModules(func(source, node *ast.Node) { |
82 | | - |
83 | | - }, utils.VisitModulesOptions{ |
84 | | - Commonjs: true, |
85 | | - ESModule: true, |
86 | | - }) |
87 | | - }, |
88 | | -} |
0 commit comments