[ESLint rule] prevent useRecoilCallback without a dependency array (#4411)
Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
committed by
GitHub
parent
41c7cd8cf7
commit
60598bf235
@ -38,6 +38,10 @@ import {
|
||||
rule as useGetLoadableAndGetValueToGetAtoms,
|
||||
RULE_NAME as useGetLoadableAndGetValueToGetAtomsName,
|
||||
} from './rules/use-getLoadable-and-getValue-to-get-atoms';
|
||||
import {
|
||||
rule as useRecoilCallbackHasDependencyArray,
|
||||
RULE_NAME as useRecoilCallbackHasDependencyArrayName,
|
||||
} from './rules/useRecoilCallback-has-dependency-array';
|
||||
|
||||
/**
|
||||
* Import your custom workspace rules at the top of this file.
|
||||
@ -77,5 +81,7 @@ module.exports = {
|
||||
[useGetLoadableAndGetValueToGetAtomsName]:
|
||||
useGetLoadableAndGetValueToGetAtoms,
|
||||
[maxConstsPerFileName]: maxConstsPerFile,
|
||||
[useRecoilCallbackHasDependencyArrayName]:
|
||||
useRecoilCallbackHasDependencyArray,
|
||||
},
|
||||
};
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
import { TSESLint } from '@typescript-eslint/utils';
|
||||
|
||||
import { rule, RULE_NAME } from './useRecoilCallback-has-dependency-array';
|
||||
|
||||
const ruleTester = new TSESLint.RuleTester({
|
||||
parser: require.resolve('@typescript-eslint/parser'),
|
||||
});
|
||||
|
||||
ruleTester.run(RULE_NAME, rule, {
|
||||
valid: [
|
||||
{
|
||||
code: 'const someValue = useRecoilCallback(() => () => {}, []);',
|
||||
},
|
||||
{
|
||||
code: 'const someValue = useRecoilCallback(() => () => {}, [dependency]);',
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: 'const someValue = useRecoilCallback(({}) => () => {});',
|
||||
errors: [
|
||||
{
|
||||
messageId: 'isNecessaryDependencyArray',
|
||||
},
|
||||
],
|
||||
output: 'const someValue = useRecoilCallback(({}) => () => {}, []);',
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -0,0 +1,46 @@
|
||||
import { ESLintUtils } from '@typescript-eslint/utils';
|
||||
|
||||
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-useRecoilCallback-has-dependency-array"
|
||||
export const RULE_NAME = 'useRecoilCallback-has-dependency-array';
|
||||
|
||||
export const rule = ESLintUtils.RuleCreator(() => __filename)({
|
||||
name: RULE_NAME,
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Ensure `useRecoilCallback` is used with a dependency array',
|
||||
recommended: 'recommended',
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
isNecessaryDependencyArray:
|
||||
'Is necessary dependency array with useRecoilCallback',
|
||||
},
|
||||
fixable: 'code',
|
||||
},
|
||||
defaultOptions: [],
|
||||
create: (context) => {
|
||||
return {
|
||||
CallExpression: (node) => {
|
||||
const { callee } = node;
|
||||
if (
|
||||
callee.type === 'Identifier' &&
|
||||
callee.name === 'useRecoilCallback'
|
||||
) {
|
||||
const depsArg = node.arguments;
|
||||
if (depsArg.length === 1) {
|
||||
context.report({
|
||||
node: callee,
|
||||
messageId: 'isNecessaryDependencyArray',
|
||||
data: {
|
||||
callee,
|
||||
deps: depsArg[0],
|
||||
},
|
||||
fix: (fixer) => fixer.insertTextAfter(depsArg[0], ', []'),
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user