Files
twenty/packages/twenty-eslint-rules/rules/explicit-boolean-predicates-in-if.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

48 lines
1.3 KiB
TypeScript

import { ESLintUtils, type TSESTree } from '@typescript-eslint/utils';
import ts from 'typescript';
export const RULE_NAME = 'explicit-boolean-predicates-in-if';
const isBooleanType = (type: ts.Type) => {
// check if boolean flag(s) is set on the type (e.g. boolean, true, false, etc.)
return type && (type.flags & ts.TypeFlags.BooleanLike) !== 0;
};
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce explicit boolean predicates in if statements',
},
fixable: 'code',
schema: [],
messages: {
nonExplicitPredicate:
'Use an explicit boolean predicate in if statements.',
},
},
defaultOptions: [],
create: (context) => {
const services = ESLintUtils.getParserServices(context);
const typeChecker = services.program.getTypeChecker();
return {
IfStatement: (node: TSESTree.IfStatement) => {
const tsNode = services.esTreeNodeToTSNodeMap.get(node.test);
const type = typeChecker.getTypeAtLocation(tsNode);
if (!isBooleanType(type)) {
const { test } = node;
context.report({
node: test,
messageId: 'nonExplicitPredicate',
});
}
},
};
},
});
export default rule;