Angular @Injectable Class Name
ID |
javascript.angular_naming_service |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
JavaScript |
Tags |
angular, framework, naming |
Description
Reports an Angular @Injectable class whose name does not end in one of the recognised service-style suffixes:
Service, Repository, Store, Provider, Resolver, Guard, Interceptor, Validator, Adapter.
// Bad
@Injectable({ providedIn: "root" })
export class UserApi { } // FLAW — should be UserService / UserApiService
// Good
@Injectable({ providedIn: "root" })
export class UserService { }
Rationale
Angular injectables are widely understood through their suffix: a *Service is a stateful collaborator, a *Resolver populates route data, a *Guard controls navigation, a *Interceptor rewrites HTTP traffic. Without one of these suffixes a reader can’t tell what role the class plays in the DI graph and the import site stops being self-documenting.
Remediation
Rename the class to end in the appropriate suffix:
@Injectable({ providedIn: "root" })
export class UserService { }
@Injectable({ providedIn: "root" })
export class AuthGuard { }
If the class is genuinely something else, drop @Injectable and the rule no longer applies.