twenty/packages/twenty-eslint-rules/rules/no-navigate-prefer-link.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

101 lines
3 KiB
TypeScript

import { ESLintUtils, type TSESTree } from '@typescript-eslint/utils';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-no-navigate-prefer-link"
export const RULE_NAME = 'no-navigate-prefer-link';
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Discourage usage of navigate() where a simple <Link> component would suffice.',
},
messages: {
preferLink: 'Use <Link> instead of navigate() for pure navigation.',
},
schema: [],
},
defaultOptions: [],
create: (context) => {
const functionMap: Record<string, TSESTree.ArrowFunctionExpression> = {};
const checkFunctionBodyHasSingleNavigateCall = (
func: TSESTree.ArrowFunctionExpression,
) => {
// Check for simple arrow function with single navigate call
if (
func.body.type === 'CallExpression' &&
func.body.callee.type === 'Identifier' &&
func.body.callee.name === 'navigate'
) {
return true;
}
// Check for block arrow function with single navigate call
if (
func.body.type === 'BlockStatement' &&
func.body.body.length === 1 &&
func.body.body[0].type === 'ExpressionStatement' &&
func.body.body[0].expression.type === 'CallExpression' &&
func.body.body[0].expression.callee.type === 'Identifier' &&
func.body.body[0].expression.callee.name === 'navigate'
) {
return true;
}
return false;
};
return {
VariableDeclarator: (node) => {
// Check for function declaration on onClick
if (
node.init &&
node.init.type === 'ArrowFunctionExpression' &&
node.id.type === 'Identifier'
) {
const func = node.init;
functionMap[node.id.name] = func;
if (checkFunctionBodyHasSingleNavigateCall(func)) {
context.report({
node: func,
messageId: 'preferLink',
});
}
}
},
JSXAttribute: (node) => {
// Check for navigate call directly on onClick
if (
node.name.name === 'onClick' &&
node.value.type === 'JSXExpressionContainer'
) {
const expression = node.value.expression;
if (
expression.type === 'ArrowFunctionExpression' &&
checkFunctionBodyHasSingleNavigateCall(expression)
) {
context.report({
node: expression,
messageId: 'preferLink',
});
} else if (
expression.type === 'Identifier' &&
functionMap[expression.name]
) {
const func = functionMap[expression.name];
if (checkFunctionBodyHasSingleNavigateCall(func)) {
context.report({
node: expression,
messageId: 'preferLink',
});
}
}
}
},
};
},
});