Skip to content

Commit 35faf67

Browse files
Feat/required named effect rule (#16)
* chore: create empty new rule * feat: implement the rule * feat: export rule in plugin * feat: add rule in recommended config * feat: add example file for require-named-effect * fix: fix errors from eslint on the rule * fix: do not merge eslint config between eslint-plugin and example-app * chore: add testing environment * lint: lint docs * chore: add helper for example incorrect rules * docs: update README for creating new rule * feat: require-named-effect is not recommended --------- Co-authored-by: Alexis Delage <alexisd@bam.tech>
1 parent d11abff commit 35faf67

28 files changed

Lines changed: 1801 additions & 4159 deletions

.github/workflows/quality-eslint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ jobs:
3636
- name: Install dependencies
3737
run: yarn --frozen-lockfile
3838

39+
- name: Linter
40+
run: yarn lint
41+
3942
- name: Tests
4043
run: yarn test

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
33
"editor.formatOnSave": true,
4+
"editor.codeActionsOnSave": { "source.fixAll": true },
45
"eslint.workingDirectories": [
56
"packages/eslint-plugin/example-app",
67
"packages/eslint-plugin"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const prettier = require("prettier");
2+
const { prettier: prettierRC } = require("./package.json"); // or wherever your prettier config lies
3+
4+
/** @type {import('eslint-doc-generator').GenerateOptions} */
5+
const config = {
6+
postprocess: (content, path) =>
7+
prettier.format(content, { ...prettierRC, parser: "markdown" }),
8+
};
9+
10+
module.exports = config;

packages/eslint-plugin/.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,27 @@ module.exports = {
77
"plugin:eslint-plugin/recommended",
88
"plugin:node/recommended",
99
],
10+
rules: {
11+
"eslint-plugin/require-meta-docs-description": [
12+
2,
13+
{ pattern: "^(Enforce|Require|Disallow)" },
14+
],
15+
"eslint-plugin/require-meta-docs-url": [
16+
"error",
17+
{
18+
pattern:
19+
"https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/{{name}}.md",
20+
},
21+
],
22+
},
1023
env: {
1124
node: true,
1225
},
26+
overrides: [
27+
{
28+
files: ["tests/**/*.js"],
29+
env: { mocha: true },
30+
},
31+
],
32+
ignorePatterns: ["example-app"],
1333
};

packages/eslint-plugin/CONTRIBUTING.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,34 @@ Here is a small summary:
1212

1313
### Adding new rules
1414

15-
Adding new rules is quite simple, you just have to modify `index.js` and add your rule to the `rules` object:
16-
17-
```js
18-
module.exports = {
19-
...
20-
rules: {
21-
...
22-
"my-new-rule": "error",
23-
},
24-
...
25-
}
26-
```
15+
Adding new rules is quite simple:
16+
17+
1. Create a new rule from the template:
18+
19+
```bash
20+
cd eslint-plugin
21+
yo eslint:rule
22+
```
23+
24+
1. Add it to the default shared config:
25+
26+
```js
27+
// recommended.js
28+
module.exports = {
29+
...
30+
rules: {
31+
...
32+
"@bam.tech/my-new-rule": "error",
33+
},
34+
}
35+
```
36+
37+
1. Update the README and run the tests:
38+
```bash
39+
yarn update:eslint-docs
40+
yarn lint
41+
yarn test
42+
```
2743

2844
### Extending new shareable configurations
2945

packages/eslint-plugin/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ In your `.eslintrc` config file, extend the exported recommended configuration (
3636
]
3737
```
3838

39+
## Rules implemented in this plugin
40+
41+
<!-- begin auto-generated rules list -->
42+
43+
| Name | Description |
44+
| :--------------------------------------------------------- | :----------------------------------------------------- |
45+
| [require-named-effect](docs/rules/require-named-effect.md) | Enforces the use of named functions inside a useEffect |
46+
47+
<!-- end auto-generated rules list -->
48+
3949
## How to customize?
4050

4151
You can still customize your ESLint config by adding configurations, plugins and rules to your `.eslintrc` config file.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Enforces the use of named functions inside a useEffect (`@bam.tech/require-named-effect`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Force to use named functions inside a useEffect instead of lambda functions.
6+
7+
## Rule Details
8+
9+
Examples of **incorrect** code for this rule:
10+
11+
```jsx
12+
useEffect(() => {}, []);
13+
```
14+
15+
```jsx
16+
useEffect(() => {
17+
const t = 1;
18+
disallowTwoThings(t);
19+
}, []);
20+
```
21+
22+
Examples of **correct** code for this rule:
23+
24+
```jsx
25+
useEffect(function namedFunction() {}, []);
26+
```
27+
28+
```jsx
29+
useEffect(theNameOfAFunction(), []);
30+
```
31+
32+
```jsx
33+
useEffect(() => theNameOfAFunction(), []);
34+
```
35+
36+
```jsx
37+
useEffect(() => {
38+
theOnlyChildIsAFunctionCall();
39+
}, []);
40+
```
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
2+
"root": true,
23
"extends": "plugin:@bam.tech/recommended",
34
"overrides": [
45
{
56
"files": ["*.test.tsx"],
67
"extends": "plugin:@bam.tech/tests"
78
}
8-
]
9+
],
10+
"rules": {
11+
"@bam.tech/require-named-effect": "error"
12+
}
913
}

packages/eslint-plugin/example-app/break-eslint-recommended-rules.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Save without formatting: [⌘ + K] > [S]
2+
13
"use strict";
24

35
console.log(

packages/eslint-plugin/example-app/break-jest-formatting-rules.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Save without formatting: [⌘ + K] > [S]
2+
13
// This should trigger an error breaking eslint-jest-formatting rule:
24
// jest-formatting/padding-around-all
35

0 commit comments

Comments
 (0)