eslint-plugin-ioc
v0.1.0
Published
Minimal eslint plugin for basic IoC linting
Downloads
154
Maintainers
Readme
eslint-plugin-ioc
Minimal eslint typescript plugin that provides some basic linting around IoC. Should work with popular node IoC packages(inversify, ts-syringe) and NestJs.
Basic usage
// .eslintrs.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
sourceType: 'module',
},
plugins: ['ioc'],
rules: {
'ioc/injection-token': 'error',
},
};
Rules
injection-token
In ts inject decorator, due to poor reflection capabilities, has to have explicit injection token provided as a parameter. Since there is no way of implementing type safe inject decorator, that approach is error-prone. This rule aims to improve that(a little) by injection token name and type comparison. Ofc that won't work in some cases, but with strict interface and token naming convention it can save few wtf's per line of code ;)
const IOrganizationsDatabaseToken = Symbol.for('IOrganizationsDatabase');
interface IOrganizationsDatabase {};
class GetOrganizationQueryHandler {
constructor(
// error
@inject(IUsersDatabaseToken)
private database: IOrganizationsDatabase,
) {
}
}
const IUsersDatabaseToken = Symbol.for('IUsersDatabase');
interface IUsersDatabase {};
class GetUserQueryHandler {
constructor(
// correct
@inject(IUsersDatabaseToken)
private database: IUsersDatabase,
) {
}
}
injectionTokenNameRegex
- set to some regexp to enforce strict naming patter of injection tokensinjectDecoratorRegex
- set to some regexp if you are using other naming for inject decorators than /^(i|I)nject$/
injection-token-type
Lint injection token type
allowedTypes
- array of allowed types, can be:['symbol', 'string', 'object']
injectionTokenNameRegex
- set to some regexp to enforce strict naming patter of injection tokensinjectDecoratorRegex
- set to some regexp if you are using other naming for inject decorators than/^(i|I)nject$/
class-injection
Forbid class(implementation) injection
class OrganizationsDatabase {};
class GetOrganizationQueryHandler
implements IGetOrganization {
constructor(
// error
@inject(OrganizationsDatabase)
private database: OrganizationsDatabase,
) {
}
}
injectDecoratorRegex
- set to some regexp if you are using other naming for inject decorators than/^(i|I)nject$/
Complete usage
// .eslintrs.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
sourceType: 'module',
},
plugins: ['ioc'],
rules: {
'ioc/injection-token': [
'error', {
injectDecoratorRegex: /^(i|I)nject$/,
injectionTokenNameRegex: /^[A-z]*Token$/
}
],
'ioc/injection-token-type': [
'error', {
allowedTypes: ['symbol'],
injectDecoratorRegex: /^(i|I)nject$/
}
],
'ioc/class-injection': [
'error', {
injectDecoratorRegex: /^(i|I)nject$/
}
],
},
};