From 0226e866b51c1f07da1a4e25677843656a72ccda Mon Sep 17 00:00:00 2001 From: Grant Date: Sun, 16 Jun 2024 00:24:46 +0200 Subject: [PATCH] Handle when relation doesn't have arrow fn defined --- CHANGELOG.md | 1 + src/rules/enforce-relation-types.test.ts | 13 +++++++++++++ src/rules/enforce-relation-types.ts | 21 +++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e74cd04..a3b5a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Date column decorators `CreateDateColumn`, `UpdateDateColumn` is date by default, `DeleteDateColumn` is nullable, - Version column decorator `VersionColumn` is number. - Allow lazy relations with `Promise` ([#10](https://github.com/daniel7grant/eslint-plugin-typeorm-typescript/issues/10)) +- Report error when the relation doesn't have an arrow function defined ### Changed diff --git a/src/rules/enforce-relation-types.test.ts b/src/rules/enforce-relation-types.test.ts index 80ef265..cf14723 100644 --- a/src/rules/enforce-relation-types.test.ts +++ b/src/rules/enforce-relation-types.test.ts @@ -135,6 +135,19 @@ ruleTester.run('enforce-relation-types', enforceRelationTypes, { }, ], invalid: [ + { + name: 'should fail on nullable one-to-one relations', + code: `class Entity { + @OneToOne() + @JoinColumn() + other: Other; + }`, + errors: [ + { + messageId: 'typescript_typeorm_relation_missing', + }, + ], + }, { name: 'should fail on nullable one-to-one relations', code: `class Entity { diff --git a/src/rules/enforce-relation-types.ts b/src/rules/enforce-relation-types.ts index dea2ddf..bb5e9b3 100644 --- a/src/rules/enforce-relation-types.ts +++ b/src/rules/enforce-relation-types.ts @@ -1,4 +1,4 @@ -import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils'; +import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'; import { ReportSuggestionArray } from '@typescript-eslint/utils/ts-eslint'; import { findEitherDecoratorArguments, @@ -20,6 +20,7 @@ const createRule = ESLintUtils.RuleCreator( ); type EnforceColumnMessages = + | 'typescript_typeorm_relation_missing' | 'typescript_typeorm_relation_mismatch' | 'typescript_typeorm_relation_array_to_many' | 'typescript_typeorm_relation_nullable_by_default' @@ -37,6 +38,8 @@ const enforceColumnTypes = createRule({ }, hasSuggestions: true, messages: { + typescript_typeorm_relation_missing: + 'Relation {{ relation }} of {{ propertyName }}{{ className }} does not have an arrow function with the relation type.', typescript_typeorm_relation_mismatch: 'Type of {{ propertyName }}{{ className }} is not consistent with the TypeORM relation type {{ relation }}{{ expectedValue }}.', typescript_typeorm_relation_array_to_many: @@ -66,7 +69,21 @@ const enforceColumnTypes = createRule({ const [relation, relArguments] = relationArguments; const typeormType = convertArgumentToRelationType(relation, relArguments); if (!typeormType) { - return; // TODO: report error + const propertyName = + node.key?.type === AST_NODE_TYPES.Identifier ? node.key.name : 'property'; + const classObject = findParentClass(node); + const className = classObject?.id ? ` in ${classObject.id.name}` : ''; + context.report({ + node, + messageId: 'typescript_typeorm_relation_missing', + data: { + className, + propertyName, + relation, + }, + loc: node.loc, + }); + return; } if (!node.typeAnnotation) {