twenty/packages/twenty-eslint-rules/rules/component-props-naming.ts
Félix Malfait c737028dd6
Move tools/eslint-rules to packages/twenty-eslint-rules (#17203)
## Summary

Moves the custom ESLint rules from `tools/eslint-rules` to
`packages/twenty-eslint-rules` for better organization within the
monorepo packages structure.

## Changes

- Move `eslint-rules` from `tools/` to `packages/twenty-eslint-rules`
- Use `loadWorkspaceRules` from `@nx/eslint-plugin` to load custom rules
- Update all ESLint configs to use the `twenty/` rule prefix instead of
`@nx/workspace-`
- Update `project.json`, `jest.config.mjs` with new paths
- Update `package.json` workspaces and `nx.json` cache inputs
- Update Dockerfile reference

## Technical Details

The custom ESLint rules are now loaded using Nx's `loadWorkspaceRules`
utility which:
- Handles TypeScript transpilation automatically
- Allows loading workspace rules from any directory
- Provides a cleaner approach than the previous `@nx/workspace-`
convention

## Testing

- Verified all 17 custom ESLint rules load correctly from the new
location
- Verified linting works on dependent packages (twenty-front,
twenty-server, etc.)
2026-01-17 07:37:17 +01:00

78 lines
2.3 KiB
TypeScript

import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
import {
isIdentifier,
isVariableDeclarator,
} from '@typescript-eslint/utils/ast-utils';
import { type RuleContext } from '@typescript-eslint/utils/ts-eslint';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-component-props-naming"
export const RULE_NAME = 'component-props-naming';
const checkPropsTypeName = ({
node,
context,
functionName,
}: {
node: TSESTree.FunctionDeclaration | TSESTree.ArrowFunctionExpression;
context: Readonly<RuleContext<'invalidPropsTypeName', any[]>>;
functionName: string;
}) => {
const expectedPropTypeName = `${functionName}Props`;
if (!functionName.match(/^[A-Z]/)) return;
node.params.forEach((param) => {
if (
(param.type === TSESTree.AST_NODE_TYPES.ObjectPattern ||
isIdentifier(param)) &&
param.typeAnnotation?.typeAnnotation.type ===
TSESTree.AST_NODE_TYPES.TSTypeReference &&
isIdentifier(param.typeAnnotation?.typeAnnotation.typeName)
) {
const { typeName } = param.typeAnnotation.typeAnnotation;
const actualPropTypeName = typeName.name;
if (actualPropTypeName !== expectedPropTypeName) {
context.report({
node: param,
messageId: 'invalidPropsTypeName',
data: { expectedPropTypeName, actualPropTypeName },
fix: (fixer) => fixer.replaceText(typeName, expectedPropTypeName),
});
}
}
});
};
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Ensure component props follow naming convention',
recommended: 'recommended',
},
fixable: 'code',
schema: [],
messages: {
invalidPropsTypeName:
"Expected prop type to be '{{ expectedPropTypeName }}' but found '{{ actualPropTypeName }}'",
},
},
defaultOptions: [],
create: (context) => {
return {
ArrowFunctionExpression: (node) => {
if (isVariableDeclarator(node.parent) && isIdentifier(node.parent.id)) {
checkPropsTypeName({
node,
context,
functionName: node.parent.id.name,
});
}
},
FunctionDeclaration: (node) => {
checkPropsTypeName({ node, context, functionName: node.id.name });
},
};
},
});