45

Does JSLint have anything like JavaScript Lint's control comments (e.g. /*jsl:fallthru*/) to make it ignore certain passages?

  • Which warning/rule do you want to ignore? – Kevin Mar 01 '09 at 17:09
  • [connecting this to my now-registered account] I wanted to ignore the fallthrough in one particular switch..case block. – AnC Mar 28 '09 at 08:28
  • This would be great. Especially in enterprise environments, global linting rules might be set by one group, but mindful overrides are understood and accepted. – SimplGy Nov 05 '12 at 22:46

9 Answers9

30

Put

/*ignore jslint start*/

before and

/*ignore jslint end*/ 

after the code to be ignored. Ex:

function ignore(){
    /*ignore jslint start*/
    var x; var y;
    /*ignore jslint end*/
}

Or export JsLint settings, define your IgnoreErrorStart/ IgnoreErrorEnd symbols and import.


Edit
Some folks may confuse this answer with JSHint. In that case, use these:
/*jshint ignore:start*/
  <!-- code in here -->
/*jshint ignore:end*/

Taken from https://stackoverflow.com/a/26012357/313501

Community
  • 1
  • 1
napoleonss
  • 1,139
  • 13
  • 19
  • 1
    some context will help the OP – dove Nov 01 '12 at 11:09
  • 1
    You may review those two instructions at VS > JS Lint options – napoleonss Nov 21 '12 at 16:34
  • 2
    Note: /*ignore jslint end*/ must NOT be in the last line of the file. It will not work if it is not. (by anonymous) – napoleonss Jun 24 '13 at 14:10
  • Agree with @ZachL , though you [can hack JSLint yourself](http://stackoverflow.com/a/29014953/1028230) (link is to an example of how to do it on another question) if you're feeling evil. I can't think of a time that's *ever* useful outside of that linked question's use case, however, and even that's not smart, just a bad situation. – ruffin Mar 12 '15 at 16:19
  • 2
    This is working for me: http://stackoverflow.com/a/26012357/313501 - it uses `/*jshint ignore:start*/` and `:end` – Philipp Sep 27 '15 at 19:05
19

Yes. From the documentation [note that this is from an older version of the docs, but it still applies]:

The implementation of JSLint accepts an option object that allows you to determine the subset of JavaScript that is acceptable to you. It is also possible to set those options within the source of a script.

An option specification can look like this:

/*jslint nomen: true, debug: true,
  evil: false, vars: true */

An option specification starts with /*jslint. Notice that there is no space before the j. The specification contains a sequence of name value pairs, where the names are JSLint options, and the values are true or false. An option specification takes precedence over the option object.

The documentation doesn't specifically mention it, but you can enable and disable different checks throughout the code with multiple jslint comments (thanks Dominic Mitchell).

There is a complete list of options in the documentation.

Community
  • 1
  • 1
Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
  • 1
    Actually, you can add these jslint comments in to your code at any point, enabling and disabling as you see fit. But it's probably wiser to rewrite and avoid the need to. – Dominic Mitchell Mar 24 '09 at 10:47
  • Thanks, I didn't know you could do that. I guess I assumed it was the same as passing the options argument. – Matthew Crumley Mar 24 '09 at 21:58
5

Here is a code example to supplement Matthew Crumley's excellent answer:

(function ($) {
  $.isValidEmail = function(email){
    /*jslint maxlen: 1000*/
    var EMAIL_REGEXP = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
    /*jslint maxlen: 200*/
    return EMAIL_REGEXP.test(email);
  };
}(jQuery));
Andrew De Andrade
  • 3,606
  • 5
  • 32
  • 37
  • Please elaborate on how is this the answer to the question, ie. how does this force JSLint to ignore errors in the marked piece of code. – antichris Feb 19 '12 at 01:06
  • 1
    My code was an example. In some cases this approach allows you to ignore certain rules for 1+ lines of code and then turn it back on. If you want to turn off JSLint entire, this approach won't work. However, much of the time you just want to turn off one or two rules and not turn off JSLint entirely. With this approach it may be possible to turn off JSLint entirely, but I'm not sure what the exact comment text would be for that. – Andrew De Andrade Feb 29 '12 at 21:23
4

Nothing here has answered this question as far as I am able to tell. The following code still gives me validation errors and I can't get JSLint to accept that I don't have time to correct a working regular expression right now given ACTUAL PRIORITIES.

The error I'm receiving regards an unescaped '{' and may result in my new, professional team rejecting JSLint as a feasible tool. There appears to be no way to shut it up regarding our efforts to develop more efficiently, which seems problematic.

/*jslint browser: true, devel: true, todo: true, regexp: true */
/*global $ */
/*
    Abstract:
        + This module constitutes a layer of abstraction surrounding our bootstrap dependencies.
        + This module also contains some utility functions (so find a better place for them already!).
    Validation:
        + This module has been validated using JSLint (www.jslint.com).
*/
var shoelaceModule = (function () {
    'use strict';
    return {
        showModal: function ($target) {
            $target.modal('show');
        },

        hideModal: function ($target) {
            $target.modal('hide');
        },

        /*jsl:ignore */
        /*ignore jslint start */
        stringFormat: function (format) {
            var args = Array.prototype.slice.call(arguments, 1);
            return format.replace(/{([^{}]*)}/g, function (match, number) {
                return args[number] !== 'undefined' ? args[number] : match;
            });
        },
        /*ignore jslint end */
        /*jsl:end */

        init: function () {
            return this;
        }
    };
}());
Mike Manard
  • 1,020
  • 9
  • 13
3

It doesn't seem so. Some Googling finds several posts by others, and responses from JSLint people along the lines of "Fix your code instead of labeling it intentionally defective." Doesn't seem entirely friendly. Of course, maybe in this case you should just fix the code, but I leave that for you to answer.

ironfroggy
  • 7,991
  • 7
  • 33
  • 44
  • Well, there was an intentional fallthrough - it really made sense. I ended up creating a sort of dummy function, which was a bit annoying. – AnC Mar 28 '09 at 08:29
2

You can also: ......

ignoreThis(); // jslint ignore:line

Is there a way to suppress JSHint warning for one given line?

Joe Johnston
  • 2,794
  • 2
  • 31
  • 54
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
0

I'm going to say, No. JSLint does not seem to have a simple way to say, 'ignore this block of code', very much to my annoyance. Disabling the different options individually and then turning them back on like Matt seems to suggest should work, but it's not going to be elegant like JSHint's /* jshint ignore:start */, /* jshint ignore:end */.

The reason I was looking into this is because I'm using Brackets, which comes with JSLint as it's default linter. I have some minified 3rd party code that I copied into my .js, temporarily, and as long as it's in there JSLint is going to complain. What stinks is I often want to paste a minified chunk early on, which is also when I want the linter looking at my code to help me rule out obscure errors. Later I'll just minify and concat everything with Grunt, but not being able to disable it for a block early on is really, really, annoying.

0

This seems to work for me. (using the jslint embedded in zedapp - zedapp.org)

/*jslint ignore:start*/
require([],function(require, exports, module) {
  /*jslint ignore:end*/
  
  var api = {
    life: {
      universe: {
        everything: function() {
          this.everything = {answer : 42};
        }
      }
    }
  };
 api.life.universe.everything.bind(api.life.universe)();
 console.log(JSON.stringify(api));

  /*jslint ignore:start*/
  return api;
});
/*jslint ignore:end*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>
cestmoi
  • 11
  • 1
-2

Put /*jsl:ignore*/ and /*jsl:end*/ around the code.

HoLyVieR
  • 10,985
  • 5
  • 42
  • 67
BengalTigger
  • 114
  • 4