Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Merge d961689 into b615bd7
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjoelkemp committed Sep 19, 2014
2 parents b615bd7 + d961689 commit fbd271c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -866,9 +866,9 @@ function a (){}

Disallows multiple `var` declaration (except for-loop).

Type: `Boolean`
Type: `Boolean` or `String`

Values: `true`
Values: `true` or 'strict' (to disallow multiple variable declarations within a for loop)

#### Example

Expand Down
16 changes: 7 additions & 9 deletions lib/rules/disallow-multiple-var-decl.js
Expand Up @@ -6,13 +6,11 @@ module.exports.prototype = {

configure: function(disallowMultipleVarDecl) {
assert(
typeof disallowMultipleVarDecl === 'boolean',
'disallowMultipleVarDecl option requires boolean value'
);
assert(
disallowMultipleVarDecl === true,
'disallowMultipleVarDecl option requires true value or should be removed'
disallowMultipleVarDecl === true || disallowMultipleVarDecl === 'strict',
'disallowMultipleVarDecl option requires true or "strict" value'
);

this.strictMode = disallowMultipleVarDecl === 'strict';
},

getOptionName: function() {
Expand All @@ -21,12 +19,12 @@ module.exports.prototype = {

check: function(file, errors) {
file.iterateNodesByType('VariableDeclaration', function(node) {
// allow multiple var declarations in for statement
// allow multiple var declarations in for statement unless we're in strict mode
// for (var i = 0, j = myArray.length; i < j; i++) {}
if (node.declarations.length > 1 && node.parentNode.type !== 'ForStatement') {
if (node.declarations.length > 1 && (this.strictMode || node.parentNode.type !== 'ForStatement')) {
errors.add('Multiple var declaration', node.loc.start);
}
});
}.bind(this));
}

};
10 changes: 10 additions & 0 deletions test/rules/disallow-multiple-var-decl.js
Expand Up @@ -3,24 +3,34 @@ var assert = require('assert');

describe('rules/disallow-multiple-var-decl', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});

it('should report multiple var decl', function() {
checker.configure({ disallowMultipleVarDecl: true });
assert(checker.checkString('var x, y;').getErrorCount() === 1);
});

it('should not report single var decl', function() {
checker.configure({ disallowMultipleVarDecl: true });
assert(checker.checkString('var x;').isEmpty());
});

it('should not report separated var decl', function() {
checker.configure({ disallowMultipleVarDecl: true });
assert(checker.checkString('var x; var y;').isEmpty());
});

it('should not report multiple var decl in for statement', function() {
checker.configure({ disallowMultipleVarDecl: true });
assert(checker.checkString('for (var i = 0, j = arr.length; i < j; i++) {}').isEmpty());
});

it('should report multiple var decl in a for statement if given the "strict" value (#46)', function() {
checker.configure({ disallowMultipleVarDecl: 'strict' });
assert(!checker.checkString('for (var i = 0, j = arr.length; i < j; i++) {}').isEmpty());
});
});

0 comments on commit fbd271c

Please sign in to comment.