Skip to content

Commit 4f9ec3d

Browse files
authored
feat(reacthookPlugin): add all reactHookPlugin recommended rules (#146)
1 parent d26dc1f commit 4f9ec3d

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

example-app/eslint-breaking-examples/break-react-hook-eslint-rules.tsx

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,46 @@
22

33
// This should trigger an error breaking eslint-plugin-react-hooks:
44
// react-hooks/exhaustive-deps
5+
// react-hooks/rules-of-hooks
6+
// react-hooks/error-boundaries
57

6-
import { useState, useEffect } from "react";
8+
import { useEffect, useState } from "react";
9+
import { View } from "react-native";
710

8-
const MyComponent = () => {
9-
const [count, setCount] = useState(0);
11+
// react-hooks/exhaustive-deps
12+
// Missing `count` in the dependency array
13+
const ExhaustiveDepsComponent = () => {
14+
const [count] = useState(0);
15+
16+
useEffect(() => void String(count), []);
17+
18+
return <View />;
19+
};
1020

11-
useEffect(() => {
12-
setCount(count + 1);
13-
}, []);
21+
// react-hooks/rules-of-hooks
22+
// Hooks must not be called conditionally
23+
const RulesOfHooksComponent = ({ isEnabled }: { isEnabled: boolean }) => {
24+
if (isEnabled) {
25+
const [value] = useState("conditional");
26+
return <View testID={value} />;
27+
}
1428

15-
return <></>;
29+
return <View />;
1630
};
1731

18-
export default MyComponent;
32+
// react-hooks/error-boundaries
33+
// Use error boundaries instead of try/catch for child component errors
34+
const ErrorBoundaryComponent = () => {
35+
try {
36+
const result = JSON.parse("{}") as Record<string, string>;
37+
return <View testID={result["id"]} />;
38+
} catch {
39+
return <View testID="fallback" />;
40+
}
41+
};
42+
43+
export {
44+
ErrorBoundaryComponent,
45+
ExhaustiveDepsComponent,
46+
RulesOfHooksComponent,
47+
};

packages/eslint-plugin/lib/configs/declarations.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
declare module "eslint-plugin-react-hooks" {
2-
import type { Linter, Rule } from "eslint";
3-
export const rules: {
4-
"rules-of-hooks": Rule.RuleModule;
5-
"exhaustive-deps": Rule.RuleModule;
6-
};
7-
}
8-
91
declare module "eslint-plugin-react-native" {
102
export const deprecatedRules: Record<string, any>;
113
export const rules: Record<string, any>;

packages/eslint-plugin/lib/configs/recommended.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const recommendedConfig = tseslint.config(
1717
tseslint.configs.recommended,
1818
eslint.configs.recommended,
1919
eslintPluginPrettierRecommended,
20+
reactHookPlugin.configs.flat.recommended,
2021
{
2122
rules: {
2223
"no-var": "error",
@@ -50,7 +51,6 @@ export const recommendedConfig = tseslint.config(
5051
plugins: {
5152
react,
5253
"react-native": reactNativePlugin,
53-
"react-hooks": reactHookPlugin,
5454
"@bam.tech": {
5555
rules: {
5656
"no-different-displayname": noDifferentDisplaynameRule,

0 commit comments

Comments
 (0)