Skip to content

feat: setup react-you-might-not-need-an-effect eslint plugin with warnings #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import react from 'eslint-plugin-react';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';
import reactEffects from 'eslint-plugin-react-you-might-not-need-an-effect';
import globals from 'globals';

import { restrictedGlobals } from './noRestrictedGlobals.js';
Expand Down Expand Up @@ -29,6 +30,7 @@
react,
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
'react-you-might-not-need-an-effect': reactEffects,
},
settings: {
react: {
Expand All @@ -37,6 +39,7 @@
linkComponents: [{ name: 'Link', linkAttribute: 'to' }],
},
rules: {
'react-you-might-not-need-an-effect/you-might-not-need-an-effect': 'warn',
'no-restricted-globals': ['error', ...restrictedGlobals],
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'error',
Expand Down Expand Up @@ -78,7 +81,7 @@
'react/jsx-no-comment-textnodes': 'error',
'react/jsx-no-constructed-context-values': 'error',
'react/jsx-no-duplicate-props': ['error', { ignoreCase: true }],
// TODO: enable when https://github.com/jsx-eslint/eslint-plugin-react/issues/3292 is fixed.

Check warning on line 84 in base.js

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected 'todo' comment: 'TODO: enable when...'
'react/jsx-no-leaked-render': 'off',
'react/jsx-no-target-blank': 'error',
'react/jsx-no-undef': 'error',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-plugin-react": "^7.37.5",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.20",
"eslint-plugin-react-you-might-not-need-an-effect": "^0.0.32",
"globals": "^16.1.0"
},
"peerDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions test/rule_helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function isError(message) {
return message.severity === 2;
}

export function isWarning(message) {
return message.severity === 1;
}

export function excludeJsdoc(message) {
return !message.ruleId.startsWith('jsdoc/');
}

export function getRuleId(message) {
return message.ruleId;
}

export function getMessageId(message) {
return message.messageId;
}

export function getRuleMessageIds(messages, ruleId) {
return messages
.filter((w) => getRuleId(w) === ruleId)
.map(getMessageId)
.sort();
}
37 changes: 18 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import assert from 'node:assert';

import { loadESLint } from 'eslint';

import {
excludeJsdoc,
getRuleId,
getRuleMessageIds,
isError,
isWarning,
} from './rule_helpers.js';

const ESLint = await loadESLint({ useFlatConfig: true });
/** @type {import('eslint').ESLint} */
const eslint = new ESLint();
Expand All @@ -20,25 +28,16 @@ assert.deepStrictEqual(errors, [
'react-hooks/exhaustive-deps',
]);

const warnings = notOkResult.messages
.filter(isWarning)
.filter(excludeJsdoc)
.map(getRuleId)
.sort();
assert.deepStrictEqual(warnings, []);

function isError(message) {
return message.severity === 2;
}
const warnings = notOkResult.messages.filter(isWarning).filter(excludeJsdoc);

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

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

function getRuleId(message) {
return message.ruleId;
}
assert.deepStrictEqual(effectMessageIds, ['avoidInternalEffect']);