What is eslint-plugin-fp?
eslint-plugin-fp is an ESLint plugin that enforces functional programming best practices. It helps developers write more predictable and maintainable code by avoiding side effects, mutations, and other non-functional programming patterns.
What are eslint-plugin-fp's main functionalities?
No Mutating Methods
This rule disallows the use of mutating array methods like push, pop, shift, unshift, etc., to encourage the use of immutable operations.
/* eslint fp/no-mutating-methods: 'error' */
const arr = [1, 2, 3];
arr.push(4); // This will trigger an ESLint error
No Mutating Assign
This rule prevents the mutation of object properties, promoting the use of immutable data structures.
/* eslint fp/no-mutation: 'error' */
let obj = { a: 1 };
obj.a = 2; // This will trigger an ESLint error
No Let
This rule disallows the use of `let` and `var` to encourage the use of `const` for variable declarations, promoting immutability.
/* eslint fp/no-let: 'error' */
let x = 1; // This will trigger an ESLint error
No This
This rule disallows the use of `this` to avoid issues related to context and state, encouraging the use of pure functions.
/* eslint fp/no-this: 'error' */
class MyClass {
constructor() {
this.value = 1; // This will trigger an ESLint error
}
}
Other packages similar to eslint-plugin-fp
eslint-plugin-immutable
eslint-plugin-immutable enforces immutability rules in JavaScript code. It is similar to eslint-plugin-fp but focuses more on preventing mutations and ensuring data immutability.
eslint-plugin-functional
eslint-plugin-functional is another ESLint plugin that enforces functional programming principles. It provides a broader set of rules compared to eslint-plugin-fp, including rules for immutability, pure functions, and function composition.
eslint-plugin-prefer-let
eslint-plugin-prefer-let enforces the use of `let` over `var` and `const` where appropriate. While it does not focus on functional programming, it helps in writing cleaner and more predictable code by avoiding the pitfalls of `var`.
eslint-plugin-fp
ESLint rules for functional programming
Install
$ npm install --save-dev eslint eslint-plugin-fp
Usage
Configure it in package.json
.
{
"name": "my-awesome-project",
"eslintConfig": {
"env": {
"es6": true
},
"plugins": [
"fp"
],
"rules": {
"fp/no-arguments": "error",
"fp/no-class": "error",
"fp/no-delete": "error",
"fp/no-events": "error",
"fp/no-get-set": "error",
"fp/no-let": "error",
"fp/no-loops": "error",
"fp/no-mutating-assign": "error",
"fp/no-mutating-methods": "error",
"fp/no-mutation": "error",
"fp/no-nil": "error",
"fp/no-proxy": "error",
"fp/no-rest-parameters": "error",
"fp/no-this": "error",
"fp/no-throw": "error",
"fp/no-unused-expression": "error",
"fp/no-valueof-field": "error",
"no-var": "error"
}
}
}
Rules
Recommended configuration
This plugin exports a recommended
configuration that enforces good practices.
To enable this configuration, use the extends
property in your package.json
.
{
"name": "my-awesome-project",
"eslintConfig": {
"plugins": [
"fp"
],
"extends": "plugin:fp/recommended"
}
}
See ESLint documentation for more information about extending configuration files.
MIT © Jeroen Engels