description |
---|
Enforces a styleguide for provider types |
Large teams can have the desire to limit or enforce a particular style of creating custom providers; e.g. banning request-scoped providers to avoid potential circular dependencies, or preferring factory providers over value providers to significantly increase performance. This rule enforces a particular type of provider to be used.
This rule accepts an object with the "prefer" property, which is an array containing one or more 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.
"rules": {
"@trilon/enforce-custom-provider-type": [
"warn", {
"prefer": ["factory", "value"]
}
]
}
Considering the options above, the following examples will show how the rule behaves when the prefer
option is set to factory
.
const customValueProvider: Provider = {
provide: 'TOKEN',
useExisting: 'some-value' // ⚠️ provider is not of type ["factory", "value"]
}
const customClassProvider: Provider = {
provide: AbstractClass,
useClass: SomeClass // ⚠️ provider is not of type ["factory", "value"]
}
const factoryProvider: Provider = { provide: 'TOKEN', useFactory: () => 'some-value' }
If you don't want to enforce a particular style of provider, you can disable this rule.