twenty/tools/eslint-rules/rules/no-navigate-prefer-link.ts
Félix Malfait 44aee860d9
Consolidate Prettier config and improve consistency (#15191)
## Summary

Clean up and consolidate formatting configuration across the monorepo.

## Changes

### 1. Remove Redundant Configs
-  Delete `packages/twenty-server/.prettierrc` (had invalid
`brakeBeforeElse` typo)
-  Delete `packages/twenty-zapier/.prettierrc`
-  Use root `.prettierrc` only (Prettier searches up directory tree
automatically)

### 2. Improve Root Prettier Config
- Change `endOfLine: 'auto'` → `'lf'` for consistent Unix line endings
across all OSes

### 3. Enhance VSCode Settings
- `files.eol: 'auto'` → `'\\n'` (consistent with Prettier)
- Add `files.insertFinalNewline: true` (explicit editor behavior)
- Add `files.trimTrailingWhitespace: true` (cleaner files)

### 4. Add `.gitattributes`
- Enforce LF line endings at Git level
- Prevents `core.autocrlf` from converting based on contributor's OS
- Mark patch files as binary (they have mixed line endings by design)
- Explicitly define binary file types

### 5. Fix Line Endings
- Convert 3 selectable-list state files from CRLF → LF
- These were the only source files with Windows line endings

## Why These Changes Matter

**Before:**
- 3 different Prettier configs (inconsistent, one had typo)
- Mixed CRLF/LF depending on contributor's OS
- No Git-level enforcement

**After:**
- Single source of truth for formatting
- All files use LF (Unix standard)
- Git enforces line endings regardless of OS
- Prettier warning about invalid option removed

## Result

-  Single `.prettierrc` config
-  Consistent LF line endings enforced by Git
-  Better VSCode defaults
-  No more CRLF files sneaking in from Windows contributors
2025-10-18 12:24:35 +02: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',
});
}
}
}
},
};
},
});