npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-better-mutation

v1.4.1

Published

ESLint rules for controlling where and how mutation is used.

Downloads

19,193

Readme

eslint-plugin-better-mutation

CircleCI codecov XO code style

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.

What's safe?

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);
}

What's not safe?

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
}

Install

$ npm install --save-dev eslint eslint-plugin-better-mutation

Usage

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",
    }
  }
}

Rules

Recommended configuration

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.