Skip to content

Commit b8ec393

Browse files
authored
feat: Add React and React Hook linting rules (#729)
* Added new config that extends base rules with react and react hook rules * feat: Add react rules to .tsx file in the base config (removed react specific config)
1 parent 1ebf104 commit b8ec393

File tree

6 files changed

+685
-4
lines changed

6 files changed

+685
-4
lines changed

.eslintrc-react.cjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
extends: ['./.eslintrc.cjs'],
3+
4+
overrides: [
5+
{
6+
/** Overrides for typescript */
7+
files: ['**/*.ts', '**/*.tsx'],
8+
extends: [
9+
'plugin:react/recommended',
10+
'plugin:react-hooks/recommended',
11+
],
12+
rules: {
13+
'@typescript-eslint/no-unused-vars': 'error',
14+
'@typescript-eslint/explicit-function-return-type': 'error',
15+
},
16+
},
17+
],
18+
};

.eslintrc.cjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ module.exports = {
3434
},
3535
parser: '@typescript-eslint/parser',
3636
},
37-
37+
{
38+
/** Overrides for tsx (this will add to the above .tsx rules) */
39+
files: ['**/*.tsx'],
40+
extends: [
41+
'plugin:react/recommended',
42+
'plugin:react-hooks/recommended',
43+
],
44+
},
3845
{
3946
/**
4047
* Overrides for typescript tests

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ IntelliJ has ESLint support by default,
6363
npm install @linzjs/style
6464
```
6565

66-
2. Create the base configuration files
66+
2. Applying eslint config
67+
There are two ways to apply the config
6768

69+
**Either create the base configuration files**
6870
```bash
6971
# If on windows run `node ./node_modules/@linzjs/style/build/src/install.js`
7072
# - tsconfig.json
@@ -73,12 +75,32 @@ npm install @linzjs/style
7375
npx linz-style-install
7476
```
7577

78+
**Or extend your existing `eslintrc.js` config**
79+
80+
Example extending the `.eslintrc.js` file in your project
81+
```js
82+
module.exports = {
83+
extends: ["./node_modules/@linzjs/style/.eslintrc.cjs"],
84+
85+
overrides: [
86+
{
87+
/** Overrides for typescript */
88+
files: ["**/*.ts", "**/*.tsx"],
89+
rules: {
90+
"@typescript-eslint/super-crazy-hook-rule": "error",
91+
},
92+
},
93+
]
94+
}
95+
```
96+
7697
3. Apply the formatting/linting to all source code
7798

7899
```
79100
npx eslint .
80101
```
81102

103+
82104
## Migration from 3.x to 4.x
83105

84106
See [Migration Docs](./migration.4.md)

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
"lint": "npx eslint . --ignore-path .gitignore"
2222
},
2323
"devDependencies": {
24-
"@types/node": "^20.2.5"
24+
"@types/node": "^20.2.5",
25+
"@types/react": "^18.2.12"
2526
},
2627
"dependencies": {
2728
"@typescript-eslint/eslint-plugin": "^5.59.9",
2829
"@typescript-eslint/parser": "^5.59.9",
2930
"eslint": "^8.42.0",
3031
"eslint-config-prettier": "^8.8.0",
3132
"eslint-plugin-prettier": "^4.2.1",
33+
"eslint-plugin-react": "^7.32.2",
34+
"eslint-plugin-react-hooks": "^4.6.0",
3235
"prettier": "^2.8.8",
3336
"typescript": "^5.1.3"
3437
},

src/__test__/MyComponent.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { ReactElement, useEffect } from 'react';
2+
3+
/**
4+
* Basic React Component for testing react linting rules
5+
*
6+
* @constructor
7+
*/
8+
export default function MyComponent(): ReactElement {
9+
useEffect(() => {
10+
console.log('test');
11+
});
12+
13+
return <div></div>;
14+
}

0 commit comments

Comments
 (0)