forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add init-declarations rule (fixes eslint#2606)
- Loading branch information
Showing
5 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
]; |
Oops, something went wrong.