diff --git a/src/jshint.js b/src/jshint.js index bfc29627e7..6fc31fe7fc 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -177,7 +177,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; } } @@ -3506,6 +3507,10 @@ var JSHINT = (function() { break; } + if (state.option.varstmt) { + warning("W132", this); + } + this.first = this.first.concat(names); if (state.tokens.next.id === "=") { diff --git a/src/messages.js b/src/messages.js index 4f11c52387..7603a4b7b0 100644 --- a/src/messages.js +++ b/src/messages.js @@ -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 = { diff --git a/src/options.js b/src/options.js index 017d2dabf4..b1c6f6c841 100644 --- a/src/options.js +++ b/src/options.js @@ -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. @@ -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 +}; diff --git a/tests/unit/options.js b/tests/unit/options.js index 21413d458d..30b14cf094 100644 --- a/tests/unit/options.js +++ b/tests/unit/options.js @@ -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(); +};