Skip to content

team-parallax/eslint-plugin-use-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eslint-plugin-use-decorator

Version npm Dependencies peerDependencies Known Vulnerabilities License

This is a custom plugin for eslint which enforces the use of decorators on methods.

Note

This only works with the @typescript-eslint plugin

Installation

npm install --save-dev eslint-plugin-use-decorator

Usage

Add this to your .eslintrc

// .eslintrc
{
  "plugins": ["use-decorator"],
  "rules": {
    "use-decorator/use-decorator": [1, {
      "params": [
        {
          "name": "assertParameter",
          "public": true
        }
      ],
      "methods": [
        {
          "name": "assert",
          "public": true
        },
        {
          "name": "errorcatch",
          "async": true,
          "private": true
        }
      ],
      "class": [
        {
          "superClass": ["Vue", "Mixins"],
          "name": "Component"
        },
      ]
    }]
  }
}

Now methods and its parameters are checked for their decorators. Above rule would enforce following custom decorators:

class SomeClass {
  @assert
  someMethod(
    @assertParameter
      someParameter: number
  ): number {
    return someParameter;
  }
  
  @errorcatch
  private async somePrivateAsyncMethod(
      someParameter: number
  ): Promise<number> {
    return someParameter;
  }
  
  /**
   *  This one does not need a decorator, because its only async.
   *  It would need to be private as well for the linting to take effect.
   */
  async someAsyncMethod(
      someParameter: number
  ): Promise<number> {
    return someParameter;
  }
}

The options object looks like this:

Options

{
  "params": [
    {
      "name": "string",
      "private?": "boolean", // default: false
      "public?": "boolean", // default: false
      "async?": "boolean", // default: false, 
      "static?": "boolean" // default: false
    }
  ],
  "methods": [
    {
      "name": "assert",
      "private?": "boolean", // default: false
      "public?": "boolean", // default: false
      "async?": "boolean", // default: false
      "static?": "boolean" // default: false
    }
  ]
}

About

This is a custom plugin for eslint which enforces the use of decorators on methods.

Resources

License

Stars

Watchers

Forks

Packages

No packages published