Skip to content

Commit

Permalink
New: Add init-declarations rule (fixes eslint#2606)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Jun 29, 2015
1 parent 59ea183 commit f594d8a
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"guard-for-in": 0,
"handle-callback-err": 0,
"indent": 0,
"init-declarations": 0,
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"lines-around-comment": 0,
"max-depth": [0, 4],
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ These rules relate to using strict mode.

These rules have to do with variable declarations.

* [init-declarations](init-declarations.md) - enforce or disallow variable initializations at definition (off by default)
* [no-catch-shadow](no-catch-shadow.md) - disallow the catch clause parameter name being the same as a variable in the outer scope (off by default in the node environment)
* [no-delete-var](no-delete-var.md) - disallow deletion of variables
* [no-label-var](no-label-var.md) - disallow labels that share a name with a variable
Expand Down
54 changes: 54 additions & 0 deletions docs/rules/init-declarations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Enforce/Disallow Variable Initializations (init-declarations)

In JavaScript, it's possible to redeclare the same variable name using `var`. This can lead to confusion as to where the variable is actually declared and initialized.

## Rule Details

This rule is aimed at enforcing or eliminating variable initializations during declaration. For example, in the following code, `foo` is initialized during declaration, while `bar` is not.

```js
var foo = 1;
var bar;

bar = 2;
```

This rule aims to bring consistency to variable initializations and declarations.

### Options

This rule is configured by passing in the string `"always"` (the default) to enforce initialization at declaration, or `"never"` to disallow initialization during declaration. This rule applies to `var`, `let`, and `const`, however `"never"` is ignored for `const` variables.

You can configure the rule as follows:

```javascript
{
// (default) All variables must be initialized at declaration
"init-declarations": [2, "always"],

// Variables must not be initialized at declaration
"init-declarations": [2, "always"]
}
```

When configured with `"always"` (the default), the following patterns are considered warnings:

```js
function foo() {
var bar;
let baz;
const qux;
}
```

The following patterns are not considered warnings:

```js
function foo() {
var bar = 1;
let baz = 2;
const qux = 3;
}
```

When configured with `"never"`, the previous conditions are reversed.
48 changes: 48 additions & 0 deletions lib/rules/init-declarations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @fileoverview A rule to control the style of variable initializations.
* @author Colin Ihrig
* @copyright 2015 Colin Ihrig. All rights reserved.
*/

"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

var MODE_ALWAYS = "always",
MODE_NEVER = "never";

var mode = context.options[0] || MODE_ALWAYS;

//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------

return {
"VariableDeclaration": function(node) {

var kind = node.kind,
declarations = node.declarations;

for (var i = 0; i < declarations.length; ++i) {
var initialized = declarations[i].init !== null;

if (mode === MODE_ALWAYS && !initialized) {
context.report(node, "Variables should be initialized on declaration.");
} else if (mode === MODE_NEVER && kind !== "const" && initialized) {
context.report(node, "Variables should not be initialized on declaration.");
}
}
}
};

};

module.exports.schema = [
{
"enum": ["always", "never"]
}
];
Loading

0 comments on commit f594d8a

Please sign in to comment.