JSLint: control comments (selective ignore)

2019-01-08 16:20发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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


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


回答5:

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.



回答6:

You can also: ......

ignoreThis(); // jslint ignore:line

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



回答7:

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.



回答8:

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>



回答9:

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