Files
twenty/packages/twenty-eslint-rules/rules/matching-state-variable.spec.ts
T
Charles Bochet bc4ae35bc4 Rework atom naming (#18240)
## Summary

- **Enhanced the `matching-state-variable` ESLint rule** to enforce
consistent naming for `ComponentState`, `FamilyState`, and
`ComponentFamilyState` hooks — previously it only covered `useAtomState`
and `useAtomStateValue`
- **The rule now checks 12 hooks** across three categories: value hooks
(`useAtomComponentStateValue`, `useAtomFamilyStateValue`, etc.), state
hooks (`useAtomComponentState`, `useAtomComponentFamilyState`), and
setter hooks (`useSetAtomState`, `useSetAtomComponentState`,
`useSetAtomFamilyState`, `useSetAtomComponentFamilyState`)
- **Fixed all 225 resulting lint violations** across 151 files, renaming
variables to match their state atom names (e.g. `currentViewId` →
`contextStoreCurrentViewId`, `selectedRecord` → `recordStore`). Cases
where the same state is accessed with different family keys/instance IDs
are suppressed with `eslint-disable-next-line`.

## Naming convention

| Hook | State argument | Valid | Invalid |
|------|---------------|-------|---------|
| `useAtomStateValue` | `fooState` | `const foo = ...` | `const bar =
...` |
| `useAtomComponentStateValue` | `fooComponentState` | `const foo = ...`
| `const bar = ...` |
| `useAtomFamilyStateValue` | `fooFamilyState` | `const foo = ...` |
`const bar = ...` |
| `useAtomComponentFamilyStateValue` | `fooComponentFamilyState` |
`const foo = ...` | `const bar = ...` |
| `useAtomState` | `fooState` | `const [foo, setFoo] = ...` | `const
[bar, setBar] = ...` |
| `useAtomComponentState` | `fooComponentState` | `const [foo, setFoo] =
...` | `const [bar, setBar] = ...` |
| `useAtomComponentFamilyState` | `fooComponentFamilyState` | `const
[foo, setFoo] = ...` | `const [bar, setBar] = ...` |
| `useSetAtomState` | `fooState` | `const setFoo = ...` | `const setBar
= ...` |
| `useSetAtomComponentState` | `fooComponentState` | `const setFoo =
...` | `const setBar = ...` |
| `useSetAtomFamilyState` | `fooFamilyState` | `const setFoo = ...` |
`const setBar = ...` |
| `useSetAtomComponentFamilyState` | `fooComponentFamilyState` | `const
setFoo = ...` | `const setBar = ...` |
2026-02-25 22:28:25 +01:00

136 lines
4.2 KiB
TypeScript

import { TSESLint } from '@typescript-eslint/utils';
import { rule, RULE_NAME } from './matching-state-variable';
const ruleTester = new TSESLint.RuleTester();
ruleTester.run(RULE_NAME, rule, {
valid: [
// useAtomState / useAtomStateValue (existing)
{
code: 'const variable = useAtomStateValue(variableState);',
},
{
code: 'const [variable, setVariable] = useAtomState(variableState);',
},
// useAtomComponentStateValue / useAtomComponentState
{
code: 'const variable = useAtomComponentStateValue(variableComponentState);',
},
{
code: 'const [variable, setVariable] = useAtomComponentState(variableComponentState);',
},
// useAtomFamilyStateValue
{
code: 'const variable = useAtomFamilyStateValue(variableFamilyState, key);',
},
// useAtomComponentFamilyStateValue / useAtomComponentFamilyState
{
code: 'const variable = useAtomComponentFamilyStateValue(variableComponentFamilyState, key);',
},
{
code: 'const [variable, setVariable] = useAtomComponentFamilyState(variableComponentFamilyState, key);',
},
// Setter hooks
{
code: 'const setVariable = useSetAtomState(variableState);',
},
{
code: 'const setVariable = useSetAtomComponentState(variableComponentState);',
},
{
code: 'const setVariable = useSetAtomFamilyState(variableFamilyState, key);',
},
{
code: 'const setVariable = useSetAtomComponentFamilyState(variableComponentFamilyState, key);',
},
],
invalid: [
// useAtomStateValue - wrong variable name
{
code: 'const myValue = useAtomStateValue(variableState);',
errors: [{ messageId: 'invalidVariableName' }],
},
// useAtomState - wrong variable and setter
{
code: 'const [myValue, setMyValue] = useAtomState(variableState);',
errors: [
{ messageId: 'invalidVariableName' },
{ messageId: 'invalidSetterName' },
],
},
{
code: 'const [myValue] = useAtomState(variableState);',
errors: [{ messageId: 'invalidVariableName' }],
},
{
code: 'const [, setMyValue] = useAtomState(variableState);',
errors: [{ messageId: 'invalidSetterName' }],
},
// useAtomComponentStateValue - wrong variable name
{
code: 'const myValue = useAtomComponentStateValue(variableComponentState);',
errors: [{ messageId: 'invalidVariableName' }],
},
// useAtomComponentState - wrong variable and setter
{
code: 'const [myValue, setMyValue] = useAtomComponentState(variableComponentState);',
errors: [
{ messageId: 'invalidVariableName' },
{ messageId: 'invalidSetterName' },
],
},
// useAtomFamilyStateValue - wrong variable name
{
code: 'const myValue = useAtomFamilyStateValue(variableFamilyState, key);',
errors: [{ messageId: 'invalidVariableName' }],
},
// useAtomComponentFamilyStateValue - wrong variable name
{
code: 'const myValue = useAtomComponentFamilyStateValue(variableComponentFamilyState, key);',
errors: [{ messageId: 'invalidVariableName' }],
},
// useAtomComponentFamilyState - wrong variable and setter
{
code: 'const [myValue, setMyValue] = useAtomComponentFamilyState(variableComponentFamilyState, key);',
errors: [
{ messageId: 'invalidVariableName' },
{ messageId: 'invalidSetterName' },
],
},
// useSetAtomState - wrong setter name
{
code: 'const myValue = useSetAtomState(variableState);',
errors: [{ messageId: 'invalidSetterName' }],
},
// useSetAtomComponentState - wrong setter name
{
code: 'const myValue = useSetAtomComponentState(variableComponentState);',
errors: [{ messageId: 'invalidSetterName' }],
},
// useSetAtomFamilyState - wrong setter name
{
code: 'const myValue = useSetAtomFamilyState(variableFamilyState, key);',
errors: [{ messageId: 'invalidSetterName' }],
},
// useSetAtomComponentFamilyState - wrong setter name
{
code: 'const myValue = useSetAtomComponentFamilyState(variableComponentFamilyState, key);',
errors: [{ messageId: 'invalidSetterName' }],
},
],
});