Skip to content

Commit 4ec4a01

Browse files
authored
feat: setup react-you-might-not-need-an-effect eslint plugin with warnings (#50)
1 parent ca23f4a commit 4ec4a01

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig } from 'eslint/config';
22
import react from 'eslint-plugin-react';
33
import reactHooks from 'eslint-plugin-react-hooks';
44
import reactRefresh from 'eslint-plugin-react-refresh';
5+
import reactEffects from 'eslint-plugin-react-you-might-not-need-an-effect';
56
import globals from 'globals';
67

78
import { restrictedGlobals } from './noRestrictedGlobals.js';
@@ -29,6 +30,7 @@ export default defineConfig(
2930
react,
3031
'react-hooks': reactHooks,
3132
'react-refresh': reactRefresh,
33+
'react-you-might-not-need-an-effect': reactEffects,
3234
},
3335
settings: {
3436
react: {
@@ -37,6 +39,7 @@ export default defineConfig(
3739
linkComponents: [{ name: 'Link', linkAttribute: 'to' }],
3840
},
3941
rules: {
42+
'react-you-might-not-need-an-effect/you-might-not-need-an-effect': 'warn',
4043
'no-restricted-globals': ['error', ...restrictedGlobals],
4144
'react-hooks/rules-of-hooks': 'error',
4245
'react-hooks/exhaustive-deps': 'error',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"eslint-plugin-react": "^7.37.5",
3737
"eslint-plugin-react-hooks": "^5.2.0",
3838
"eslint-plugin-react-refresh": "^0.4.20",
39+
"eslint-plugin-react-you-might-not-need-an-effect": "^0.0.32",
3940
"globals": "^16.1.0"
4041
},
4142
"peerDependencies": {

test/rule_helpers.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function isError(message) {
2+
return message.severity === 2;
3+
}
4+
5+
export function isWarning(message) {
6+
return message.severity === 1;
7+
}
8+
9+
export function excludeJsdoc(message) {
10+
return !message.ruleId.startsWith('jsdoc/');
11+
}
12+
13+
export function getRuleId(message) {
14+
return message.ruleId;
15+
}
16+
17+
export function getMessageId(message) {
18+
return message.messageId;
19+
}
20+
21+
export function getRuleMessageIds(messages, ruleId) {
22+
return messages
23+
.filter((w) => getRuleId(w) === ruleId)
24+
.map(getMessageId)
25+
.sort();
26+
}

test/test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import assert from 'node:assert';
22

33
import { loadESLint } from 'eslint';
44

5+
import {
6+
excludeJsdoc,
7+
getRuleId,
8+
getRuleMessageIds,
9+
isError,
10+
isWarning,
11+
} from './rule_helpers.js';
12+
513
const ESLint = await loadESLint({ useFlatConfig: true });
614
/** @type {import('eslint').ESLint} */
715
const eslint = new ESLint();
@@ -20,25 +28,16 @@ assert.deepStrictEqual(errors, [
2028
'react-hooks/exhaustive-deps',
2129
]);
2230

23-
const warnings = notOkResult.messages
24-
.filter(isWarning)
25-
.filter(excludeJsdoc)
26-
.map(getRuleId)
27-
.sort();
28-
assert.deepStrictEqual(warnings, []);
29-
30-
function isError(message) {
31-
return message.severity === 2;
32-
}
31+
const warnings = notOkResult.messages.filter(isWarning).filter(excludeJsdoc);
3332

34-
function isWarning(message) {
35-
return message.severity === 1;
36-
}
33+
const warningRules = warnings.map(getRuleId).sort();
34+
assert.deepStrictEqual(warningRules, [
35+
'react-you-might-not-need-an-effect/you-might-not-need-an-effect',
36+
]);
3737

38-
function excludeJsdoc(message) {
39-
return !message.ruleId.startsWith('jsdoc/');
40-
}
38+
const effectMessageIds = getRuleMessageIds(
39+
warnings,
40+
'react-you-might-not-need-an-effect/you-might-not-need-an-effect',
41+
);
4142

42-
function getRuleId(message) {
43-
return message.ruleId;
44-
}
43+
assert.deepStrictEqual(effectMessageIds, ['avoidInternalEffect']);

0 commit comments

Comments
 (0)