Skip to content

Commit

Permalink
Handle when relation doesn't have arrow fn defined
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel7grant committed Jun 15, 2024
1 parent a98b50d commit 0226e86
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Relation>` ([#10](https://github.com/daniel7grant/eslint-plugin-typeorm-typescript/issues/10))
- Report error when the relation doesn't have an arrow function defined

### Changed

Expand Down
13 changes: 13 additions & 0 deletions src/rules/enforce-relation-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 19 additions & 2 deletions src/rules/enforce-relation-types.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand All @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0226e86

Please sign in to comment.