JSLint:控制注释(选择性忽略)

发布于 2024-07-14 07:05:27 字数 216 浏览 5 评论 0原文

JSLint 是否有类似于 JavaScript Lint 的 控制注释(例如/*jsl:fallthru*/)以使其忽略某些段落?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

放血 2024-07-21 07:05:28

放在

/*ignore jslint start*/

之前和

/*ignore jslint end*/ 

要忽略的代码 之后。 例如:

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

或者导出 JsLint 设置,定义 IgnoreErrorStart/ IgnoreErrorEnd 符号并导入。


Edit
Some folks may confuse this answer with JSHint. In that case, use these:

/*jshint ignore:start*/
  <!-- code in here -->
/*jshint ignore:end*/

摘自https://stackoverflow.com/a/26012357/313501

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

烟凡古楼 2024-07-21 07:05:28

是的。 从文档中[注意,这是来自旧版本的文档,但它仍然适用]:

JSLint 的实现接受一个选项对象,该对象允许您确定您可接受的 JavaScript 子集。 还可以在脚本源中设置这些选项。

选项规范可以如下所示:

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

选项规范以 /*jslint 开头。 请注意,j 之前没有空格。 该规范包含一系列名称值对,其中名称是 JSLint 选项,值为 true 或 false。 选项规范优先于选项对象。

文档没有具体提及它,但您可以使用多个 jslint 注释在整个代码中启用和禁用不同的检查(感谢 Dominic Mitchell)。

文档中有完整的选项列表

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.

你げ笑在眉眼 2024-07-21 07:05:28

这是一个代码示例来补充 Matthew Crumley 的出色答案:

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

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));
星軌x 2024-07-21 07:05:28

据我所知,这里没有任何内容可以回答这个问题。 以下代码仍然给我带来验证错误,并且我无法让 JSLint 接受我现在没有时间在给定实际优先级的情况下更正工作正则表达式。

我收到的错误涉及未转义的“{”,可能会导致我的新专业团队拒绝将 JSLint 作为可行的工具。 似乎没有办法阻止我们为更有效地发展所做的努力,这似乎是有问题的。

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

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;
        }
    };
}());
过度放纵 2024-07-21 07:05:28

看来并非如此。 一些谷歌搜索发现了其他人的几篇帖子,以及 JSLint 人员的回复,大意是“修复你的代码,而不是故意标记它有缺陷”。 看起来并不完全友好。 当然,也许在这种情况下您应该只修复代码,但我将其留给您来回答。

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.

樱娆 2024-07-21 07:05:28

您还可以:......

ignoreThis(); // jslint 忽略:行

有没有一种方法可以抑制给定行的 JSHint 警告?

You can also: ......

ignoreThis(); // jslint ignore:line

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

我一向站在原地 2024-07-21 07:05:28

我想说,不。JSLint 似乎没有一个简单的方法来表示“忽略这段代码”,这让我很恼火。 单独禁用不同的选项,然后像马特建议的那样重新打开它们应该可行,但它不会像 JSHint 的 /* jshintignore:start *//* jshint 那样优雅忽略:结束*/

我之所以研究这个问题是因为我使用的是 Brackets,它附带 JSLint 作为默认的 linter。 我暂时将一些缩小的第 3 方代码复制到我的 .js 中,只要它在那里,JSLint 就会抱怨。 糟糕的是我经常想尽早粘贴一个缩小的块,这也是当我希望 linter 查看我的代码来帮助我排除模糊错误时。 稍后我将使用 Grunt 缩小并连接所有内容,但无法尽早禁用它的块真的非常非常烦人。

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.

北城半夏 2024-07-21 07:05:28

这似乎对我有用。 (使用 zedapp 中嵌入的 jslint - 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>

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>

左秋 2024-07-21 07:05:28

/*jsl:ignore*//*jsl:end*/ 放在代码周围。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文