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-no-var-reassign

v1.0.0

Published

Disallow assignment to declared variables

Downloads

2

Readme

eslint-plugin-no-var-reassign

let and const weren’t really necessary additions to JavaScript; all you need is a linter and var.

var is easier to declare. Function scoping and even redeclaration can both be useful:

if (…) {
  var foo = 1
} else {
  var foo = 2
}
doSomething(foo)

Can’t do that with let or const, since they’re block-scoped.

let and const will throw runtime errors when you try to use their variables before they’ve been declared. However, you could instead rely on ESLint’s no-use-before-define rule to catch such mistakes with var.

One could pretend that a var was a constant, and he could theoretically use a linter to enforce that. Considering the qualities of var, if a linter were to enforce using variables after they were defined and if it were to enforce their constancy, then var could be superior to let or const.

This ESLint plugin provides a rule achieving that: no-var-reassign

Installation

Install ESLint globally or locally:

yarn global add eslint
yarn add -D eslint

Install this plugin globally or locally:

yarn global add eslint-plugin-no-var-reassign
yarn add -D eslint-plugin-no-var-reassign

Configuration

Add “no-var-reassign” to your plugins section, and enable the rule by adding “no-var-reassign/no-var-reassign” to your rules section. Normally this is configured in the .eslintrc config file, like so:

{
  "plugins": [
    "no-var-reassign"
  ],
  "rules": {
    "no-var-reassign/no-var-reassign": "warn"
  }
}

Usage

Now, whenever you try to assign to an already-declared variable, you’ll be warned as if that variable was a constant. Here, ESLint will report an error on line 2:

var foo = 1
foo = 2

Since tail calls eliminate the need for assignment in loops, assignment is never truly necessary in the language. This code (which needed assignment):

for (var i = 0; i < 10; i++) {}

Becomes:

(function loop (i = 0) {
  if (i + 1 < 10) loop(i + 1)
}())

However, if you’re writing code for an environment without tail calls, or if you simply wish to use assignment on some occassions, you can selectively disable the rule. ESLint won’t report an error in the following example:

var foo = 1
if (…) foo = 2 // eslint-disable-line no-var-reassign/no-var-reassign

License

MIT; see the “LICENSE” file.