Files
twenty/packages/twenty-eslint-rules/rules/no-hardcoded-colors.ts
T
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

65 lines
1.8 KiB
TypeScript

import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-no-hardcoded-colors"
export const RULE_NAME = 'no-hardcoded-colors';
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
docs: {
description:
'Do not use hardcoded RGBA or Hex colors. Please use a color from the theme file.',
},
messages: {
hardcodedColor:
'Hardcoded color {{ color }} found. Please use a color from the theme file.',
},
type: 'suggestion',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create: (context) => {
const testHardcodedColor = (
literal: TSESTree.Literal | TSESTree.TemplateLiteral
) => {
const colorRegex = /(?:rgba?\()|(?:#[0-9a-fA-F]{3,6})\b/i;
if (
literal.type === TSESTree.AST_NODE_TYPES.Literal &&
typeof literal.value === 'string'
) {
if (colorRegex.test(literal.value)) {
context.report({
node: literal,
messageId: 'hardcodedColor',
data: {
color: literal.value,
},
});
}
} else if (literal.type === TSESTree.AST_NODE_TYPES.TemplateLiteral) {
for (const quasi of literal.quasis) {
const firstStringValue = quasi.value.raw;
if (colorRegex.test(firstStringValue)) {
context.report({
node: literal,
messageId: 'hardcodedColor',
data: {
color: firstStringValue,
},
});
}
}
}
};
return {
Literal: (literal: TSESTree.Literal) => testHardcodedColor(literal),
TemplateLiteral: (templateLiteral: TSESTree.TemplateLiteral) =>
testHardcodedColor(templateLiteral),
};
},
});