Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint: control comments (selective ignore)

People also ask

How do I ignore JSHint warning?

In October 2013 jshint added a way to ignore blocks of code like this: // Code here will be linted with JSHint. /* jshint ignore:start */ // Code here will be ignored by JSHint. /* jshint ignore:end */ // Code here will be linted with JSHint.

Should I use JSLint?

Use JSLint if you're looking for a very high standard for yourself or your team, but bear in mind that it's not necessarily the standard, only a standard, some of which comes to us dogmatically from Doug Crockford.

What is JSLint used for?

JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It is provided primarily as a browser-based web application accessible through the domain jslint.com, but there are also command-line adaptations.


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


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.


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));

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;
        }
    };
}());

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.