Files
twenty/packages/twenty-eslint-rules/rules/matching-state-variable.ts
T
Charles Bochet 121788c42f Fully deprecate old recoil (#18210)
## Summary

Removes the `recoil` dependency entirely from `package.json` and
`twenty-front/package.json`, completing the migration to Jotai as the
sole state management library.

Removes all Recoil infrastructure: `RecoilRoot` wrapper from `App.tsx`
and test decorators, `RecoilDebugObserver`, Recoil-specific ESLint rules
(`use-getLoadable-and-getValue-to-get-atoms`,
`useRecoilCallback-has-dependency-array`), and legacy Recoil utility
hooks/types (`useRecoilComponentState`, `useRecoilComponentValue`,
`createComponentState`, `createFamilyState`, `getSnapshotValue`,
`cookieStorageEffect`, `localStorageEffect`, etc.).

Renames all `V2`-suffixed Jotai state files and types to their canonical
names (e.g., `ComponentStateV2` -> `ComponentState`,
`agentChatInputStateV2` -> `agentChatInputState`, `SelectorCallbacksV2`
-> `SelectorCallbacks`), and removes the now-redundant V1 counterparts.

Updates ~433 files across the codebase to use the renamed Jotai imports,
remove Recoil imports, and clean up test wrappers (`RecoilRootDecorator`
-> `JotaiRootDecorator`).
2026-02-25 12:26:42 +01:00

137 lines
4.4 KiB
TypeScript

import {
AST_NODE_TYPES,
ESLintUtils,
type TSESTree,
} from '@typescript-eslint/utils';
import { isIdentifier } from '@typescript-eslint/utils/ast-utils';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-matching-state-variable"
export const RULE_NAME = 'matching-state-variable';
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Ensure state value and setter are named after their atom name',
},
fixable: 'code',
schema: [],
messages: {
invalidVariableName:
"Invalid usage of {{ hookName }}: the variable should be named '{{ expectedName }}' but found '{{ actualName }}'.",
invalidSetterName:
"Invalid usage of {{ hookName }}: Expected setter '{{ expectedName }}' but found '{{ actualName }}'.",
},
},
defaultOptions: [],
create: (context) => {
return {
VariableDeclarator: (node: TSESTree.VariableDeclarator) => {
if (
node?.init?.type === AST_NODE_TYPES.CallExpression &&
isIdentifier(node.init.callee) &&
['useAtomState', 'useAtomStateValue'].includes(
node.init.callee.name,
)
) {
const stateNameBase = isIdentifier(node.init.arguments[0])
? node.init.arguments[0].name
: undefined;
if (!stateNameBase) {
return;
}
const expectedVariableNameBase = stateNameBase.replace(
/(State|Selector|ScopedState|ScopedFamilyState|ScopedSelector)$/,
'',
);
if (isIdentifier(node.id)) {
const actualVariableName = node.id.name;
if (actualVariableName !== expectedVariableNameBase) {
context.report({
node,
messageId: 'invalidVariableName',
data: {
actualName: actualVariableName,
expectedName: expectedVariableNameBase,
hookName: stateNameBase,
callee: node.init.callee.name,
},
fix: (fixer) => {
return fixer.replaceText(node.id, expectedVariableNameBase);
},
});
}
return;
}
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
const actualVariableName =
node.id.elements?.[0]?.type === AST_NODE_TYPES.Identifier
? node.id.elements[0].name
: undefined;
if (
actualVariableName &&
actualVariableName !== expectedVariableNameBase
) {
context.report({
node,
messageId: 'invalidVariableName',
data: {
actual: actualVariableName,
expected: expectedVariableNameBase,
callee: node.init.callee.name,
},
fix: (fixer) => {
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
return fixer.replaceText(
node.id.elements[0] as TSESTree.Node,
expectedVariableNameBase,
);
}
return null;
},
});
}
if (isIdentifier(node.id.elements[1])) {
const actualSetterName = node.id.elements[1].name;
const expectedSetterName = `set${expectedVariableNameBase
.charAt(0)
.toUpperCase()}${expectedVariableNameBase.slice(1)}`;
if (actualSetterName !== expectedSetterName) {
context.report({
node,
messageId: 'invalidSetterName',
data: {
hookName: stateNameBase,
actualName: actualSetterName,
expectedName: expectedSetterName,
},
fix: (fixer) => {
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
return fixer.replaceText(
node.id.elements[1]!,
expectedSetterName,
);
}
return null;
},
});
}
}
}
}
},
};
},
});