JSPM

  • Created
  • Published
  • Downloads 161907
  • Score
    100M100P100Q166325F
  • License MIT

ESLint rules to disable mutation and promote fp in TypeScript.

Package Exports

  • eslint-plugin-functional

Readme

eslint-logo

eslint-plugin-functional

npm version CI Coverage Status semantic-release code style: prettier MIT license GitHub Discussions

An ESLint plugin to disable mutation and promote functional programming in JavaScript and TypeScript.

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 the strict 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 functional

  • No 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

No Mutations Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
immutable-data Disallow mutating objects and arrays ✔️ ✔️ ✔️ 🟢 💙
no-let Disallow mutable variables ✔️ ✔️ 🟢 🟢
prefer-immutable-parameter-types Require parameters to be deeply readonly ✔️ ✔️ 🟢 🟢 💭
type-declaration-immutability Enforce type immutability with patterns ✔️ ✔️ ✔️ ✔️ 💭

No Other Paradigms Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
no-class Disallow classes ✔️ ✔️ ✔️ ✔️
no-mixed-type Disallow types that contain both callable and non-callable members ✔️ ✔️ ✔️ ✔️ 💭
no-this-expression Disallow this access ✔️ ✔️ ✔️ ✔️

No Statements Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
no-conditional-statement Disallow conditional statements (if and switch statements) ✔️ ✔️ 🟢 💭
no-expression-statement Disallow expressions to cause side-effects ✔️ ✔️ 💭
no-loop-statement Disallow imperative loops ✔️ ✔️ ✔️ ✔️
no-return-void Disallow functions that return nothing ✔️ ✔️ ✔️ ✔️ 💭

No Exceptions Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
no-promise-reject Disallow rejecting Promises
no-throw-statement Disallow throwing exceptions ✔️ ✔️ 🟢 🟢
no-try-statement Disallow try-catch[-finally] and try-finally patterns ✔️ ✔️

Currying Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
functional-parameters Functions must have functional parameters ✔️ ✔️ ✔️ 🟢

Stylistic Rules

Name Description ⚜️ 🙊 🙈 🙉 🔧 💙
prefer-property-signatures Enforce property signatures over method signatures ✔️ 💭
prefer-tacit Tacit/Point-Free style. ✔️ 🔧 💙

Key

Symbol Meaning
⚜️ Ruleset: Current
🙊 Ruleset: Strict
🙈 Ruleset: Recommended
🙉 Ruleset: Lite
✔️ Enabled as Error
🟢 Enabled as Error with Overrides
🔧 Fixable
💭 Only Available for TypeScript
💙 Works better with TypeScript

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 create var 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

Prior work

This project started off as a port of tslint-immutable which was originally inspired by eslint-plugin-immutable.