diff --git a/base.js b/base.js index 10a3cb1..db50e32 100644 --- a/base.js +++ b/base.js @@ -2,6 +2,7 @@ import { defineConfig } from 'eslint/config'; 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'; @@ -29,6 +30,7 @@ export default defineConfig( react, 'react-hooks': reactHooks, 'react-refresh': reactRefresh, + 'react-you-might-not-need-an-effect': reactEffects, }, settings: { react: { @@ -37,6 +39,7 @@ export default defineConfig( 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', diff --git a/package.json b/package.json index 9b85c2a..0d01f35 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/rule_helpers.js b/test/rule_helpers.js new file mode 100644 index 0000000..5833d9b --- /dev/null +++ b/test/rule_helpers.js @@ -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(); +} diff --git a/test/test.js b/test/test.js index ea125c1..dedd478 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); @@ -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']);