Skip to content

Commit

Permalink
docs: add docs for new rule
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomini committed Jan 24, 2024
1 parent ba52a67 commit 3119d96
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The "recommended" preset contains the rules listed below. If you need custom con
| ------------------------------------------------------------------------------------ | -------------------------------------------------------------- | ----------- |
| [`@trilon/enforce-close-testing-module`](docs/rules/enforce-close-testing-module.md) | Ensures NestJS testing modules are closed properly after tests ||
| [`@trilon/check-inject-decorator`](docs/rules/check-inject-decorator.md) | Detects incorrect usage of `@Inject(TOKEN)` decorator ||

| [`@trilon/detect-circular-reference`](docs/rules/detect-circular-reference.md) | Detects usage of `forwardRef()` method ||
---

# Trilon Consulting
Expand Down
55 changes: 55 additions & 0 deletions docs/rules/detect-circular-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
description: 'Detects usage of `forwardRef()` function commonly used to handle circular references'
---

[Forward references](https://docs.nestjs.com/fundamentals/circular-dependency#forward-reference) are commonly used to handle circular dependencies between services or modules. For example, if `FooService` and `BarService` depend on each other, you can use `forwardRef()` to resolve the circular dependency. However, `forwardRef()` must be a last resort, and we generally recommend changing your code to avoid circular dependencies. This rule detects usage of the `forwardRef()` method so you can keep track of what potentially needs refactoring.

One strategy to avoid circular dependencies between services is to make each responsible for a single use case. That way you decrease the interface and likelihood of circular dependencies. You can also avoid using services as facades for data repositories. Instead, services should be usually used to encapsulate business logic (commands). Leave query responsibilities to repositories.

## Options

This rule has no additional options yet.


## Examples

### ❌ Incorrect

```ts
import { forwardRef } from '@nestjs/common';

@Injectable()
export class CatsService {
constructor(
@Inject(forwardRef(() => CommonService)) // ⚠️ Circular-dependency detected
private commonService: CommonService,
) {}
}

@Module({
imports: [forwardRef(() => CatsModule)], // ⚠️ Circular-dependency detected
})
export class FooModule {}
```

### ✅ Correct

```ts
import { forwardRef } from '@nestjs/common';

@Injectable()
export class CatsService {
constructor(
private commonService: CommonService,
) {}
}

@Module({
imports: [CatsModule],
})
export class FooModule {}
```

## When Not To Use It

If your project uses `forwardRef()` extensively, you can disable this rule.

0 comments on commit 3119d96

Please sign in to comment.