2023-09-17 18:13:05 +03:00
|
|
|
import {
|
|
|
|
AST_NODE_TYPES,
|
2023-09-19 02:38:57 +03:00
|
|
|
ESLintUtils,
|
|
|
|
TSESTree,
|
2024-01-04 01:07:25 +03:00
|
|
|
} from '@typescript-eslint/utils';
|
|
|
|
import { isIdentifier } from '@typescript-eslint/utils/ast-utils';
|
2023-09-17 18:13:05 +03:00
|
|
|
|
2024-01-04 01:07:25 +03:00
|
|
|
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-matching-state-variable"
|
|
|
|
export const RULE_NAME = 'matching-state-variable';
|
2023-09-17 18:13:05 +03:00
|
|
|
|
2024-01-04 01:07:25 +03:00
|
|
|
export const rule = ESLintUtils.RuleCreator(() => __filename)({
|
|
|
|
name: RULE_NAME,
|
|
|
|
meta: {
|
|
|
|
type: 'problem',
|
|
|
|
docs: {
|
|
|
|
description:
|
|
|
|
'Ensure recoil value and setter are named after their atom name',
|
|
|
|
recommended: 'recommended',
|
|
|
|
},
|
|
|
|
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: [],
|
2023-09-17 18:13:05 +03:00
|
|
|
create: (context) => {
|
|
|
|
return {
|
2023-09-19 02:38:57 +03:00
|
|
|
VariableDeclarator: (node: TSESTree.VariableDeclarator) => {
|
2023-09-17 18:13:05 +03:00
|
|
|
if (
|
|
|
|
node?.init?.type === AST_NODE_TYPES.CallExpression &&
|
2024-01-04 01:07:25 +03:00
|
|
|
isIdentifier(node.init.callee) &&
|
2023-09-17 18:13:05 +03:00
|
|
|
[
|
2024-01-04 01:07:25 +03:00
|
|
|
'useRecoilState',
|
|
|
|
'useRecoilScopedState',
|
|
|
|
'useRecoilFamilyState',
|
|
|
|
'useRecoilScopedFamilyState',
|
|
|
|
'useRecoilValue',
|
|
|
|
'useRecoilScopedValue',
|
2023-09-17 18:13:05 +03:00
|
|
|
].includes(node.init.callee.name)
|
|
|
|
) {
|
2024-01-04 01:07:25 +03:00
|
|
|
const stateNameBase = isIdentifier(node.init.arguments[0])
|
|
|
|
? node.init.arguments[0].name
|
|
|
|
: undefined;
|
2023-09-17 18:13:05 +03:00
|
|
|
|
|
|
|
if (!stateNameBase) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-19 02:38:57 +03:00
|
|
|
const expectedVariableNameBase = stateNameBase.replace(
|
2023-09-17 18:13:05 +03:00
|
|
|
/(State|FamilyState|Selector|ScopedState|ScopedFamilyState|ScopedSelector)$/,
|
2024-01-04 01:07:25 +03:00
|
|
|
'',
|
2023-09-17 18:13:05 +03:00
|
|
|
);
|
|
|
|
|
2024-01-04 01:07:25 +03:00
|
|
|
if (isIdentifier(node.id)) {
|
2023-09-17 18:13:05 +03:00
|
|
|
const actualVariableName = node.id.name;
|
2024-01-04 01:07:25 +03:00
|
|
|
|
2023-09-17 18:13:05 +03:00
|
|
|
if (actualVariableName !== expectedVariableNameBase) {
|
|
|
|
context.report({
|
|
|
|
node,
|
2024-01-04 01:07:25 +03:00
|
|
|
messageId: 'invalidVariableName',
|
2023-09-17 18:13:05 +03:00
|
|
|
data: {
|
2023-09-19 02:38:57 +03:00
|
|
|
actualName: actualVariableName,
|
|
|
|
expectedName: expectedVariableNameBase,
|
|
|
|
hookName: stateNameBase,
|
2023-09-17 18:13:05 +03:00
|
|
|
callee: node.init.callee.name,
|
|
|
|
},
|
2023-09-19 02:38:57 +03:00
|
|
|
fix: (fixer) => {
|
2023-09-17 18:13:05 +03:00
|
|
|
return fixer.replaceText(node.id, expectedVariableNameBase);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-01-04 01:07:25 +03:00
|
|
|
|
2023-09-17 18:13:05 +03:00
|
|
|
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;
|
2024-01-04 01:07:25 +03:00
|
|
|
|
2023-09-17 18:13:05 +03:00
|
|
|
if (
|
|
|
|
actualVariableName &&
|
|
|
|
actualVariableName !== expectedVariableNameBase
|
|
|
|
) {
|
|
|
|
context.report({
|
|
|
|
node,
|
2024-01-04 01:07:25 +03:00
|
|
|
messageId: 'invalidVariableName',
|
2023-09-17 18:13:05 +03:00
|
|
|
data: {
|
|
|
|
actual: actualVariableName,
|
|
|
|
expected: expectedVariableNameBase,
|
|
|
|
callee: node.init.callee.name,
|
|
|
|
},
|
2023-09-19 02:38:57 +03:00
|
|
|
fix: (fixer) => {
|
2023-09-17 18:13:05 +03:00
|
|
|
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
|
|
|
|
return fixer.replaceText(
|
|
|
|
node.id.elements[0] as TSESTree.Node,
|
2023-09-19 02:38:57 +03:00
|
|
|
expectedVariableNameBase,
|
2023-09-17 18:13:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-04 01:07:25 +03:00
|
|
|
if (isIdentifier(node.id.elements[1])) {
|
2023-09-17 18:13:05 +03:00
|
|
|
const actualSetterName = node.id.elements[1].name;
|
|
|
|
const expectedSetterName = `set${expectedVariableNameBase
|
|
|
|
.charAt(0)
|
|
|
|
.toUpperCase()}${expectedVariableNameBase.slice(1)}`;
|
|
|
|
|
|
|
|
if (actualSetterName !== expectedSetterName) {
|
|
|
|
context.report({
|
|
|
|
node,
|
2024-01-04 01:07:25 +03:00
|
|
|
messageId: 'invalidSetterName',
|
2023-09-17 18:13:05 +03:00
|
|
|
data: {
|
2023-09-19 02:38:57 +03:00
|
|
|
hookName: stateNameBase,
|
|
|
|
actualName: actualSetterName,
|
|
|
|
expectedName: expectedSetterName,
|
2023-09-17 18:13:05 +03:00
|
|
|
},
|
2023-09-19 02:38:57 +03:00
|
|
|
fix: (fixer) => {
|
2023-09-17 18:13:05 +03:00
|
|
|
if (node.id.type === AST_NODE_TYPES.ArrayPattern) {
|
|
|
|
return fixer.replaceText(
|
|
|
|
node.id.elements[1]!,
|
2023-09-19 02:38:57 +03:00
|
|
|
expectedSetterName,
|
2023-09-17 18:13:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|