eslint-plugin-ioc
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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 tokens
  • injectDecoratorRegex - 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 tokens
  • injectDecoratorRegex - 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$/
      }
    ],
  },
};

Package Sidebar

Install

npm i eslint-plugin-ioc

Weekly Downloads

66

Version

0.1.0

License

MIT

Unpacked Size

25.3 kB

Total Files

27

Last publish

Collaborators

  • amaro0