Skip to content

Commit

Permalink
feat: add plugin react hooks #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Jun 5, 2020
1 parent 2393dcc commit 2ac2275
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ then run `npm run lint`
- [Imports](./documentation/imports.md) (ES6/7/8)
- [React](./documentation/react.md) (ES6/7/8)
- [React A11y](./documentation/react-a11y.md) (ES6/7/8)
- [React hooks](./documentation/react-hooks.md) (ES6/7/8)
- [Typescript](./documentation/typescript.md) (typescript)

## Thanks to
Expand Down
6 changes: 5 additions & 1 deletion bin/write-documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function getESLintUrl(name, groupName) {
return 'https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/' + name + '.md';
case 'react-a11y':
return 'https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/' + name + '.md';
case 'react-hooks':
return 'https://reactjs.org/docs/hooks-rules.html';
case 'typescript':
return 'https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/' + name.replace(/.ts$/, '') + '.md';
default:
return 'http://eslint.org/docs/rules/' + name;
}
Expand Down Expand Up @@ -133,7 +137,7 @@ function addSnippet(arr, fileData) {
function addRule(arr, name, path, groupName) {

var fileData = utils.readFile(path);
var nameCleaned = name.replace(/\.js$/, '');
var nameCleaned = name.replace(/\.js$/, '').replace(/\.ts$/, '');
var title = utils.capitalize(nameCleaned.replace(/-/g, ' '));

arr.push('### [' + title + '](' + getESLintUrl(nameCleaned, groupName) + ')');
Expand Down
1 change: 1 addition & 0 deletions configurations/es6-react-disable-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'./es6-disable-styles.js',
'../rules/react-disable-styles.js',
'../rules/react-a11y-disable-styles.js',
'../rules/react-hooks-disable-styles.js',
].map(require.resolve),

};
2 changes: 2 additions & 0 deletions configurations/es6-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module.exports = {
'plugins': [
'jsx-a11y',
'react',
'react-hooks',
],
'extends': [
'./es6.js',
'../rules/react.js',
'../rules/react-a11y.js',
'../rules/react-hooks.js',
].map(require.resolve),
'env': {
'browser': true,
Expand Down
1 change: 1 addition & 0 deletions configurations/es7-react-disable-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'./es7-disable-styles.js',
'../rules/react-disable-styles.js',
'../rules/react-a11y-disable-styles.js',
'../rules/react-hooks-disable-styles.js',
].map(require.resolve),

};
2 changes: 2 additions & 0 deletions configurations/es7-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module.exports = {
'plugins': [
'jsx-a11y',
'react',
'react-hooks',
],
'extends': [
'./es7.js',
'../rules/react.js',
'../rules/react-a11y.js',
'../rules/react-hooks.js',
].map(require.resolve),
'env': {
'browser': true,
Expand Down
1 change: 1 addition & 0 deletions configurations/es8-react-disable-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'./es8-disable-styles.js',
'../rules/react-disable-styles.js',
'../rules/react-a11y-disable-styles.js',
'../rules/react-hooks-disable-styles.js',
].map(require.resolve),

};
2 changes: 2 additions & 0 deletions configurations/es8-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module.exports = {
'plugins': [
'jsx-a11y',
'react',
'react-hooks',
],
'extends': [
'./es8.js',
'../rules/react.js',
'../rules/react-a11y.js',
'../rules/react-hooks.js',
].map(require.resolve),
'env': {
'browser': true,
Expand Down
1 change: 1 addition & 0 deletions configurations/typescript-react-disable-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
'./typescript-disable-styles.js',
'../rules/react-disable-styles.js',
'../rules/react-a11y-disable-styles.js',
'../rules/react-hooks-disable-styles.js',
].map(require.resolve),
};
2 changes: 2 additions & 0 deletions configurations/typescript-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module.exports = {
'@typescript-eslint',
'jsx-a11y',
'react',
'react-hooks',
],
extends: [
'./typescript.js',
'../rules/react.js',
'../rules/react-a11y.js',
'../rules/react-hooks.js',
].map(require.resolve),
env: {
browser: true,
Expand Down
99 changes: 99 additions & 0 deletions documentation/react-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

## React hooks


### [Exhaustive deps](https://reactjs.org/docs/hooks-rules.html)

> Checks effect dependencies

:white_check_mark: Enabled (error)

```javascript

// Bad
/*
// https://github.com/facebook/react/blob/master/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js
function MyComponent() {
const local = {};
useEffect(() => {
console.log(local);
}, []);
}
*/

// Good
function MyComponent2() {
const local = {};
useEffect(() => {
console.log(local);
}, [local]);
}

```
<br />



### [Rules of hooks](https://reactjs.org/docs/hooks-rules.html)

> Checks rules of Hooks

:white_check_mark: Enabled (error)

```javascript

// Bad
/*
function getFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
// ...
return isOnline;
}
function Foo({ alt, src }) {
const [name, setName] = useState('Mary');
const [surname, setSurname] = useState('Poppins');
// 🔴 We're breaking the first rule by using a Hook in a condition
if (name !== '') {
useEffect(function persistForm() {
localStorage.setItem('formData', name);
});
}
return <img alt={alt} src={src} />;
}
*/

// Good
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
// ...
return isOnline;
}

function Foo2({ alt, src }) {
// 1. Use the name state variable
const [name, setName] = useState('Mary');

// 2. Use an effect for persisting the form
useEffect(function persistForm() {
localStorage.setItem('formData', name);
});

// 3. Use the surname state variable
const [surname, setSurname] = useState('Poppins');

// 4. Use an effect for updating the title
useEffect(function updateTitle() {
document.title = name + ' ' + surname;
});
return <img alt={alt} src={src} />;
}

```
<br />


Loading

0 comments on commit 2ac2275

Please sign in to comment.