【问题标题】:Can I disable jslint's 'empty block' warning in Brackets?我可以在括号中禁用 jslint 的“空块”警告吗?
【发布时间】:2018-01-30 08:46:44
【问题描述】:

我创建了以下 isqrt 函数。

function isqrt(n) {
    var shift, result, candidateResult;
    if (n < 0) {
        return false;
    }
    for (shift = 2; n >> shift; shift += 2) {}
    for (result = 0, shift -= 2; shift >= 0; shift -= 2) {
        result = result << 1;
        candidateResult = result + 1;
        if (candidateResult * candidateResult <= n >> shift) {
            result = candidateResult;
        }
    }
    return result;
}

这里有一个空块。

for (shift = 2; n >> shift; shift += 2) {}

但这是故意的。 我知道我可以按如下方式改进此代码。

shift = 2;
while (n >> shift) {
    shift += 2;
}

但我不想要它。

我在 Windows 10 上使用最新版本的支架。:)

【问题讨论】:

    标签: javascript jslint brackets


    【解决方案1】:

    您可以忽略整个文件或特定行或给定块的 jshint 规则。

    忽略行:

    function isqrt(n) {
        var shift, result, candidateResult;
        if (n < 0) {
            return false;
        }
        for (shift = 2; n >> shift; shift += 2) {} // jshint ignore:line
        for (result = 0, shift -= 2; shift >= 0; shift -= 2) {
            result = result << 1;
            candidateResult = result + 1;
            if (candidateResult * candidateResult <= n >> shift) {
                result = candidateResult;
            }
        }
        return result;
    }
    

    忽略阻止

    /* jshint ignore:start */
    // Code here will be ignored by JSHint. So write your code here
    /* jshint ignore:end */
    // Code here will be linted with JSHint.\
    

    或者只是在文件开头写// jshint ignore: start,那么整个文件将被忽略

    【讨论】:

    • 我试过了,但警告仍然有效。我不知道我错过了什么。 orz...
    • 尝试在您的 js 文件中添加 /*jshint noempty: true */ 或在您的块中添加空注释,例如 { // comment to ignore jshint error.}
    【解决方案2】:

    您可以执行以下操作

    for (shift = 2; n >> shift; shift += 2) { // do nothing. }

    或在您的 js 中添加以下内容

    /*jshint noempty: true */

    【讨论】:

      【解决方案3】:

      你也可以做类似的事情

        /*jslint ignore:start**/
        for (shift = 2; n >> shift; shift += 2) {}
        /*jslint ignore:end**/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-05
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2021-06-10
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多