eslint-plugin-optional-params
v0.0.5
Published
A ESLint plugin for linting optional parameters
Downloads
53
Readme
eslint-plugin-optional-params
A ESLint plugin that allows you to limit the number of optional params a function may have. Works with:
- function declarations
- function expressions
- arrow functions
- class methods
- class properties (class methods declared as arrow functions)
- function type declarations
- function declarations within interfaces
- arrow function declarations within interfaces
- abstract methods
- abstract properties (abstract methods declared as arrow functions)
Installation
npm i -D eslint-plugin-optional-paramsRules
max-optional-params: controls the count of optional params a function may have.- Example:
// 'optional-params/max-optional-params': ['error', 1] // ❌ // Only 1 optional param(s) are allowed (eslint) function buildMech(head: Head, core: Core, arms?: Arm[], legs?: Leg[]): Mech // ✅ // A fix function buildMech(params: { head: Head, core: Core, arms?: Arm[], legs?: Leg[] }): Mech - When to use it:
The rule must be used if multiple optional params are dissatisfactory.
To pass a succeeding optional param a redundantundefinedvalue should be passed as an argument for the preceding param.
- Example:
Usage
- The plugin must be added to the
pluginsarray in a.eslintrc.*config. - the
eslint-pluginpart of the name can be omitted.
// .eslintrc.js
module.exports = {
// ...
plugins: [/* other plugins */, 'optional-params']
rules: {
// ...
'optional-params/max-optional-params': ['error', 2],
}
// ...
}