Package Exports
- eslint-plugin-functional
Readme
eslint-plugin-functional
An ESLint plugin to disable mutation and promote functional programming in JavaScript and TypeScript.
Donate
Any donations would be much appreciated. 😄
Getting Started
See our getting started guide.
Rulesets
The following rulesets are made available by this plugin:
Presets:
Strict (
plugin:functional/strict
)
Enforce recommended rules designed to strictly enforce functional programming.Recommended (
plugin:functional/recommended
)
Has the same goal as thestrict
preset but a little more lenient, allowing for functional-like coding styles and nicer integration with non-functional 3rd-party libraries.Lite (
plugin:functional/lite
)
Good if you're new to functional programming or are converting a large codebase.
Categorized:
Currying (
plugin:functional/currying
)
JavaScript functions support syntax that is not compatible with curried functions. To enforce currying, this syntax should be prevented.No Exceptions (
plugin:functional/no-exceptions
)
Functional programming style does not use run-time exceptions. Instead expressions produces values to indicate errors.No Mutations (
plugin:functional/no-mutations
)
Prevent mutating any data as that's not functionalNo Other Paradigms (
plugin:functional/no-other-paradigms
)
JavaScript is multi-paradigm, allowing not only functional, but object-oriented as well as other programming styles. To promote a functional style, prevent the use of other paradigm styles.No Statements (
plugin:functional/no-statements
)
In functional programming everything is an expression that produces a value. JavaScript has a lot of syntax that is just statements that does not produce a value. That syntax has to be prevented to promote a functional style.Stylistic (
plugin:functional/stylistic
)
Enforce code styles that can be considered to be more functional.
Other:
All (
plugin:functional/all
)
Enables all rules defined in this plugin.Off (
plugin:functional/off
)
Disable all rules defined in this plugin.External Vanilla Recommended (
plugin:functional/external-vanilla-recommended
)
Configures recommended vanilla ESLint rules.External Typescript Recommended (
plugin:functional/external-typescript-recommended
)
Configures recommended TypeScript ESLint rules. Enabling this ruleset will also enable the vanilla one.
The below section gives details on which rules are enabled by each ruleset.
Rules
💼 Configurations enabled in.
⚠️ Configurations set to warn in.
🚫 Configurations disabled in.
☑️ Set in the lite
configuration.
✅ Set in the recommended
configuration.
🔒 Set in the strict
configuration.
🎨 Set in the stylistic
configuration.
🔧 Automatically fixable by the --fix
CLI option.
💡 Manually fixable by editor suggestions.
❌ Deprecated.
Currying
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
functional-parameters | Enforce functional parameters. | ☑️ ✅ 🔒 |
No Exceptions
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
no-promise-reject | Disallow try-catch[-finally] and try-finally patterns. | ||||||
no-throw-statements | Disallow throwing exceptions. | ☑️ ✅ 🔒 |
|||||
no-try-statements | Disallow try-catch[-finally] and try-finally patterns. | 🔒 |
☑️ ✅ |
No Mutations
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
immutable-data | Enforce treating data as immutable. | ☑️ ✅ 🔒 |
|||||
no-let | Disallow mutable variables. | ☑️ ✅ 🔒 |
|||||
prefer-immutable-types | Require function parameters to be typed as certain immutability | ☑️ ✅ 🔒 |
🔧 | 💡 | |||
prefer-readonly-type | Prefer readonly types over mutable types. | 🔧 | ❌ | ||||
type-declaration-immutability | Enforce the immutability of types based on patterns. | ☑️ ✅ 🔒 |
🔧 |
No Other Paradigms
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
no-classes | Disallow classes. | ☑️ ✅ 🔒 |
|||||
no-mixed-types | Restrict types so that only members of the same kind are allowed in them. | ☑️ ✅ 🔒 |
|||||
no-this-expressions | Disallow this access. | 🔒 |
☑️ ✅ |
No Statements
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
no-conditional-statements | Disallow conditional statements. | ✅ 🔒 |
☑️ | ||||
no-expression-statements | Disallow expression statements. | ✅ 🔒 |
☑️ | ||||
no-loop-statements | Disallow imperative loops. | ☑️ ✅ 🔒 |
|||||
no-return-void | Disallow functions that don't return anything. | ☑️ ✅ 🔒 |
Stylistic
Name | Description | 💼 | ⚠️ | 🚫 | 🔧 | 💡 | ❌ |
---|---|---|---|---|---|---|---|
prefer-property-signatures | Prefer property signatures over method signatures. | 🎨 | |||||
prefer-tacit | Replaces x => f(x) with just f . |
🎨 | 💡 | ||||
readonly-type | Require consistently using either readonly keywords or Readonly<T> |
🎨 | 🔧 |
External Recommended Rules
In addition to the above rules, there are a few other rules we recommended.
These rules are what are included in the external recommended rulesets.
Vanilla Rules
no-var
Without this rule, it is still possible to createvar
variables that are mutable.no-param-reassign
Don't allow function parameters to be reassigned, they should be treated as constants.prefer-const
This rule provides a helpful fixer when converting from an imperative code style to a functional one.
Typescript Rules
@typescript-eslint/prefer-readonly
This rule is helpful when working with classes.@typescript-eslint/switch-exhaustiveness-check
Although our no-conditional-statements rule also performs this check, this rule has a fixer that will implement the unimplemented cases which can be useful.
Contributing
Prior work
This project started off as a port of tslint-immutable which was originally inspired by eslint-plugin-immutable.