Skip to content

Commit

Permalink
[[FEAT]] add varstmt enforcement option to disallow use of Variable…
Browse files Browse the repository at this point in the history
…Statements

Closes #1549
  • Loading branch information
caitp committed Mar 4, 2015
1 parent a8cfae6 commit 59396f7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ var JSHINT = (function() {
function processenforceall() {
if (state.option.enforceall) {
for (var enforceopt in options.bool.enforcing) {
if (state.option[enforceopt] === undefined) {
if (state.option[enforceopt] === undefined &&
!options.noenforceall[enforceopt]) {
state.option[enforceopt] = true;
}
}
Expand Down Expand Up @@ -3507,6 +3508,10 @@ var JSHINT = (function() {
break;
}

if (state.option.varstmt) {
warning("W132", this);
}

this.first = this.first.concat(names);

if (state.tokens.next.id === "=") {
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ var warnings = {
W129: "'{a}' is defined in a future version of JavaScript. Use a " +
"different variable name to avoid migration issues.",
W130: "Invalid element after rest element.",
W131: "Invalid parameter after rest parameter."
W131: "Invalid parameter after rest parameter.",
W132: "`var` declarations are forbidden. Use `let` or `const` instead."
};

var info = {
Expand Down
16 changes: 16 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ exports.bool = {
*/
singleGroups: false,

/**
* When set to true, the use of VariableStatements are forbidden.
* For example:
*
* // jshint varstmt: true
*
* var a; // Warning: `var` declarations are forbidden. Use `let` or `const` instead.
*/
varstmt: false,

/**
* This option is a short hand for the most strict JSHint configuration. It
* enables all enforcing options and disables all relaxing options.
Expand Down Expand Up @@ -945,3 +955,9 @@ exports.removed = {
smarttabs: true,
trailing: true
};

// Add options here which should not be automatically enforced by
// `enforceall`.
exports.noenforceall = {
varstmt: true
};
22 changes: 22 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2290,3 +2290,25 @@ exports.futureHostile = function (test) {

test.done();
};


exports.varstmt = function (test) {
var code = [
"var x;",
"var y = 5;",
"var fn = function() {",
" var x;",
" var y = 5;",
"};"
];

TestRun(test)
.addError(1, "`var` declarations are forbidden. Use `let` or `const` instead.")
.addError(2, "`var` declarations are forbidden. Use `let` or `const` instead.")
.addError(3, "`var` declarations are forbidden. Use `let` or `const` instead.")
.addError(4, "`var` declarations are forbidden. Use `let` or `const` instead.")
.addError(5, "`var` declarations are forbidden. Use `let` or `const` instead.")
.test(code, { varstmt: true });

test.done();
};

0 comments on commit 59396f7

Please sign in to comment.