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

Commit 3c2dbf7

Browse files
seanpdoylemarkelog
authored andcommitted
New rule: disallowVar
When `{ disallowVar: true }`, assert that all variable declarations use `let` or `const` Closes gh-1983
1 parent ecec961 commit 3c2dbf7

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

lib/config/configuration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ Configuration.prototype.registerDefaultRules = function() {
951951
this.registerRule(require('../rules/require-object-destructuring'));
952952
this.registerRule(require('../rules/require-enhanced-object-literals'));
953953
this.registerRule(require('../rules/require-array-destructuring'));
954+
this.registerRule(require('../rules/disallow-var'));
954955
/* ES6 only (end) */
955956

956957
this.registerRule(require('../rules/require-curly-braces'));

lib/rules/disallow-var.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Disallows declaring variables with `var`.
3+
*
4+
* Types: `Boolean`
5+
*
6+
* Values: `true`
7+
*
8+
* Version: `ES6`
9+
*
10+
* #### Example
11+
*
12+
* ```js
13+
* "disallowVar": true
14+
* ```
15+
*
16+
* ##### Valid
17+
*
18+
* ```js
19+
* let foo;
20+
* const bar;
21+
* ```
22+
*
23+
* ##### Invalid
24+
*
25+
* ```js
26+
* var baz;
27+
* ```
28+
*/
29+
30+
var assert = require('assert');
31+
32+
module.exports = function() { };
33+
34+
module.exports.prototype = {
35+
configure: function(option) {
36+
assert(option === true, this.getOptionName() + ' requires a true value');
37+
},
38+
39+
getOptionName: function() {
40+
return 'disallowVar';
41+
},
42+
43+
check: function(file, errors) {
44+
file.iterateNodesByType('VariableDeclaration', function(node) {
45+
for (var i = 0; i < node.declarations.length; i++) {
46+
var thisDeclaration = node.declarations[i];
47+
48+
if (thisDeclaration.parentNode.kind === 'var') {
49+
errors.add(
50+
'Variable declarations should use `let` or `const` not `var`',
51+
node.loc.start
52+
);
53+
}
54+
}
55+
});
56+
}
57+
};

test/specs/rules/disallow-var.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var Checker = require('../../../lib/checker');
2+
var expect = require('chai').expect;
3+
4+
describe('rules/disallow-var', function() {
5+
describe('when { disallowVar: true }', function() {
6+
it('disallows `var` declarations', function() {
7+
var checker = buildChecker({ disallowVar: true });
8+
9+
expect(checker.checkString('var foo;')).to.have.one.validation.error.from('disallowVar');
10+
});
11+
12+
it('allows `let` declarations', function() {
13+
var checker = buildChecker({ disallowVar: true });
14+
15+
expect(checker.checkString('let foo;')).to.have.no.errors();
16+
});
17+
18+
it('allows `const` declarations', function() {
19+
var checker = buildChecker({ disallowVar: true });
20+
21+
expect(checker.checkString('const foo = 1;')).to.have.no.errors();
22+
});
23+
});
24+
25+
describe('when { disallowVar: false }', function() {
26+
it('allows `var` declarations', function() {
27+
var checker = buildChecker({ disallowVar: false });
28+
29+
expect(checker.checkString('var foo;')).to.have.no.errors();
30+
});
31+
32+
it('allows `let` declarations', function() {
33+
var checker = buildChecker({ disallowVar: false });
34+
35+
expect(checker.checkString('let foo;')).to.have.no.errors();
36+
});
37+
38+
it('allows `const` declarations', function() {
39+
var checker = buildChecker({ disallowVar: false });
40+
41+
expect(checker.checkString('const foo = 1;')).to.have.no.errors();
42+
});
43+
});
44+
45+
function buildChecker(rules) {
46+
var checker = new Checker();
47+
checker.registerDefaultRules();
48+
checker.configure(rules);
49+
50+
return checker;
51+
}
52+
});

0 commit comments

Comments
 (0)