Permalink
Browse files

Ability to ignore any warnings.

This commit adds special syntax to the /*jshint directive making it
possible to ignore any warnings (but not errors). For example:

	/*jshint -W121 */

	var a = 12.;

The code above would raise a trailing dot warning but '-W121' suppresses
it. For advanced use only.

Closes GH-578.
Closes GH-455.
Closes GH-624.
Closes GH-683.
Closes GH-699.
Closes GH-755.
  • Loading branch information...
1 parent a47843f commit e3df309b2a84ca627d5fe85e60c296b6735d4b66 @valueof valueof committed Dec 7, 2012
Showing with 47 additions and 6 deletions.
  1. +27 −6 src/stable/jshint.js
  2. +4 −0 tests/stable/unit/fixtures/ignored.js
  3. +16 −0 tests/stable/unit/options.js
View
@@ -196,6 +196,7 @@ var JSHINT = (function () {
functions, // All of the functions
global, // The global scope
+ ignored, // Ignored warnings
implied, // Implied globals
inblock,
indent,
@@ -219,6 +220,12 @@ var JSHINT = (function () {
function F() {} // Used by Object.create
function checkOption(name, t) {
+ name = name.trim();
+
+ if (/^-W\d{3}$/g.test(name)) {
+ return true;
+ }
+
if (valOptions[name] === undefined && boolOptions[name] === undefined) {
if (t.type !== "jslint" || renamedOptions[name] === undefined) {
error("E001", t, name);
@@ -425,7 +432,11 @@ var JSHINT = (function () {
function warning(code, t, a, b, c, d) {
var ch, l, w, msg;
- if (/W\d{3}/.test(code)) {
+ if (/^W\d{3}$/.test(code)) {
+ if (ignored[code]) {
+ return;
+ }
+
msg = messages.warnings[code];
} else if (/E\d{3}/.test(code)) {
msg = messages.errors[code];
@@ -675,6 +686,11 @@ var JSHINT = (function () {
return;
}
+ if (/^-W\d{3}$/g.test(key)) {
+ ignored[key.slice(1)] = true;
+ return;
+ }
+
var tn;
if (val === "true" || val === "false") {
if (nt.type === "jslint") {
@@ -3143,6 +3159,7 @@ var JSHINT = (function () {
predefined = Object.create(vars.ecmaIdentifiers);
declared = Object.create(null);
exported = Object.create(null);
+ ignored = Object.create(null);
combine(predefined, g || {});
if (o) {
@@ -3166,13 +3183,17 @@ var JSHINT = (function () {
optionKeys = Object.keys(o);
for (x = 0; x < optionKeys.length; x++) {
- newOptionObj[optionKeys[x]] = o[optionKeys[x]];
+ if (/^-W\d{3}$/g.test(optionKeys[x])) {
+ ignored[optionKeys[x].slice(1)] = true;
+ } else {
+ newOptionObj[optionKeys[x]] = o[optionKeys[x]];
- if (optionKeys[x] === "newcap" && o[optionKeys[x]] === false)
- newOptionObj["(explicitNewcap)"] = true;
+ if (optionKeys[x] === "newcap" && o[optionKeys[x]] === false)
+ newOptionObj["(explicitNewcap)"] = true;
- if (optionKeys[x] === "indent")
- newOptionObj["(explicitIndent)"] = true;
+ if (optionKeys[x] === "indent")
+ newOptionObj["(explicitIndent)"] = true;
+ }
}
}
@@ -0,0 +1,4 @@
+//jshint -W008
+
+var a = .12;
+var b = 12.;
@@ -1378,3 +1378,19 @@ exports.maxcomplexity = function (test) {
test.done();
};
+
+/*
+ * Tests ignored warnings.
+ */
+exports.ignored = function (test) {
+ var src = fs.readFileSync(__dirname + "/fixtures/ignored.js", "utf-8");
+
+ TestRun(test)
+ .addError(4, "A trailing decimal point can be confused with a dot: '12.'.")
+ .test(src);
+
+ TestRun(test)
+ .test(src, { "-W121": true });
+
+ test.done();
+};

3 comments on commit e3df309

@cowwoc
cowwoc commented on e3df309 Dec 19, 2012

Anton,

It's not clear what the syntax of "-W" is. Specifically, I don't understand why an argument of "121" causes it to suppress a warning for "var a = 12.;". Is 121 a warning number? A line number? ...it's not clear.

@valueof
Member

Yes, of course, it's confusing since there are no docs. :-) I will write docs for that and other features before 1.0.0 release. I just need to close a few tickets before then. For now you can go to messages.js and see the code/message mappings there.

@DDR0
DDR0 commented on e3df309 Mar 31, 2013

OK, so assuming a basic understanding of order of operations, and a certain reasonableness on the author's part:
To suppress "confusing use of '!'" messages, add -W018 to your first-line jsHint flags. example: /*jshint -W018 */. (sans quotes) This suppresses any "confusing use of …" messages, however, some of which are useful.
The file antonkovalyov mentions is https://github.com/jshint/jshint/blob/master/src/shared/messages.js#L69 - it contains many useful values, and is quite easy to read.

Please sign in to comment.