Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
eslint-plugin-better-mutation
Advanced tools
Readme
ESLint rules so you and your team use immutable values when you should, and permits mutation when it's safe.
Preventing unsafe mutation of shared variables prevents a huge class of bugs from ever appearing.
The plugin's goal is to prevent modification of shared variables, such as function parameters or globals whether via assignment, operators, functions or methods.
Locally declared variables are permitted to use mutation, because in most circumstances this is safe.
See WHY.
Block scoped (let
) or function scoped variables (var
) can be reassigned safely. Even objects or arrays marked with const
can have nested properties changed within the block scope they belong to. The same rules apply for mutating functions (e.g. Object.assign()
and mutating methods [].push()
function foo() {
let i = 1;
i = 2;
const o = { a: 0 };
o.a += 1;
}
module.exports = { foo }; // by default commonjs exports are configured to be safe
Also modification of parameters within reducer functions/methods is safe. The parameters of a reducer a safe because the loop that the reducer executes limits the scope of the accumulator to the reducer function.
function sum(numbers) {
return numbers.reduce((acc, val) => {
acc += val; // this is safe!
return acc;
}, 0);
}
Mutating variables of shared variables is unsafe. This means reassignment outside of the scope they are declared within whether its because they are globals, function parameters, or closed over variables can lead to undefined behaviour that is hard to debug and can cause race conditions. Instead of mutating always return a new value.
g = 2 // global reassignment is unsafe
function foo(i) {
i += 1 // don't resassign function parameters
}
let a = 1;
function bar() {
a = 2; // don't reassign closed over vars
}
$ npm install --save-dev eslint eslint-plugin-better-mutation
Configure it in .eslintrc
.
{
"name": "my-awesome-project",
"eslintConfig": {
"env": {
"es6": true
},
"plugins": [
"better-mutation"
],
"rules": {
"better-mutation/no-mutating-functions": "error",
"better-mutation/no-mutating-methods": "error",
"better-mutation/no-mutation": "error",
}
}
}
Object.assign() and lodash mutation methods
with a non-local variable as first argument.This plugin exports a recommended
configuration that enforces good practices.
To enable this configuration, use the extends
property in your .eslintrc
.
{
"name": "my-awesome-project",
"eslintConfig": {
"plugins": [
"better-mutation"
],
"extends": "plugin:better-mutation/recommended"
}
}
See ESLint documentation for more information about extending configuration files.
MIT © Andres Olave
Thanks to Jeroen Engels for providing the basis of this plugin. Checkout https://github.com/jfmengels/eslint-plugin-fp for a strict functional programming approach.
FAQs
ESLint rules for controlling where and how mutation is used.
The npm package eslint-plugin-better-mutation receives a total of 2,996 weekly downloads. As such, eslint-plugin-better-mutation popularity was classified as popular.
We found that eslint-plugin-better-mutation demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.