GithubHelp home page GithubHelp logo

eslint-plugin-no-var-reassign's Introduction

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.

eslint-plugin-no-var-reassign's People

Contributors

jacksonrayhamilton avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.