Skip to content

Commit

Permalink
docs: add docs for new rule and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomini committed Feb 26, 2024
1 parent b03a987 commit 58647c7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ The "recommended" preset contains the rules listed below. If you need custom con

## Rules

| Rule | Description | Recommended |
| Rule | Description | Type |
| ------------------------------------------------------------------------------------ | -------------------------------------------------------------- | ----------- |
| [`@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/enforce-close-testing-module`](docs/rules/enforce-close-testing-module.md) | Ensures NestJS testing modules are closed properly after tests | Recommended ✅ |
| [`@trilon/check-inject-decorator`](docs/rules/check-inject-decorator.md) | Detects incorrect usage of `@Inject(TOKEN)` decorator | Recommended ✅ |
| [`@trilon/detect-circular-reference`](docs/rules/detect-circular-reference.md) | Detects usage of `forwardRef()` method | Recommended ✅ |
| [`@trilon/enforce-custom-provider-type`](docs/rules/enforce-custom-provider-type.md) | Enforces a styleguide for provider types | Strict ⚠️ |
---

# Trilon Consulting
Expand Down
55 changes: 55 additions & 0 deletions docs/rules/enforce-custom-provider-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
description: 'Enforces a styleguide for provider types'
---

Large teams can have the desire to limit or enforce a particular style of creating [custom providers](https://docs.nestjs.com/fundamentals/custom-providers); e.g. banning request-scoped providers to avoid potential circular dependencies, or [preferring factory providers over value providers to significantly increase performance](https://github.com/nestjs/nest/pull/12753). This rule enforces a particular type of provider to be used.

## Options

This rule accepts an object with the "prefer" property, which might be one of the following values:

- `value`: Enforces the use of value providers.
- `factory`: Enforces the use of factory providers.
- `class`: Enforces the use of class providers.
- `existing`: Enforces the use of existing providers.


### Example of Options

```json
"rules": {
"@trilon/enforce-custom-provider-type": [
"warn", {
"prefer": "factory"
}
]
}
```

## Examples
Considering the options above, the following examples will show how the rule behaves when the `prefer` option is set to `factory`.

### ❌ Incorrect

```ts
const customValueProvider: Provider = {
provide: 'TOKEN',
useValue: 'some-value' // ⚠️ provider is not of type "factory"
}

const customClassProvider: Provider = {
provide: AbstractClass,
useClass: SomeClass // ⚠️ provider is not of type "factory"
}
```

### ✅ Correct

const factoryProvider: Provider = {
provide: 'TOKEN',
useFactory: () => 'some-value'
}

## When Not To Use It

If you don't want to enforce a particular style of provider, you can disable this rule.
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enforceCloseTestingModuleRule from './rules/enforce-close-testing-module.rule';
import checkInjectDecoratorRule from './rules/check-inject-decorator.rule';
import detectCircularReferenceRule from './rules/detect-circular-reference.rule';
import enforceCustomProviderTypeRule from './rules/enforce-custom-provider-type.rule';
// TODO: we should type this as ESLint.Plugin but there's a type incompatibilities with the utils package
const plugin = {
configs: {
Expand All @@ -11,11 +12,20 @@ const plugin = {
'@trilon/detect-circular-reference': 'warn',
},
},
strict: {
rules: {
'@trilon/enforce-close-testing-module': 'error',
'@trilon/check-inject-decorator': 'error',
'@trilon/detect-circular-reference': 'error',
'@trilon/enforce-custom-provider-type': 'error',
},
},
},
rules: {
'enforce-close-testing-module': enforceCloseTestingModuleRule,
'check-inject-decorator': checkInjectDecoratorRule,
'detect-circular-reference': detectCircularReferenceRule,
'@trilon/enforce-custom-provider-type': enforceCustomProviderTypeRule,
},
};

Expand Down

0 comments on commit 58647c7

Please sign in to comment.