为什么 JSLint 警告我我的 switch-case 语句格式不正确?

Why is JSLint warning me about my switch-case statement being incorrectly formatted?

在 Adob​​e Brackets 中,我在编写严格代码时收到来自 JSLint 的警告 ['use strict'] 我的 switch case 语句格式不正确:
例如。应在第 # 列 'case',而不是第 #

如果我将 switch 语句中的所有内容移回一个 "tab" JSLint 很高兴。
但是,Adobe Brackets(和类似的代码应用程序)想要缩进 case 语句,甚至在使用 Code Beautify 时它也会将代码格式化为在 case 语句之前有一个缩进。

  • When using strict code, is what JSLint is suggesting really the proper way of to format the switch-case statements?
  • Is there a way to fix/make JSLint in Adobe Brackets so it thinks this indentation is correct? (I would like to stick to not hacking up the JSLint Code)
  • Why would Editors format the switch-case statement this way if strict code does not want you to do that?
  • Am I really just doing something wrong here?
  • Is this just a downside of JSLint and is there a way to avoid using the switch-case statement then altogether thus in the process also making JSLint happy?
  • Should I really just stop using JSLint altogether? And Switch to something else?

此代码嵌套在 for 循环中:

switch (curButton.button.innerText.toLowerCase()) {
    case this.Part1.Button.ButtonText.toLowerCase():
        this.Part1.Button.ButtonText = curButton.button.innerText;
        this.Part1.Button.Element = curButton.button;
        this.Part1.Button.CurrentClass = curButton.button.className;
        console.log(smgData.PodCast.Parts.Part1.Button);
        break;
    case this.Part2.Button.ButtonText.toLowerCase():
        this.Part2.Button.ButtonText = curButton.button.innerText;
        this.Part2.Button.Element = curButton.button;
        this.Part2.Button.CurrentClass = curButton.button.className;
        console.log(smgData.PodCast.Parts.Part2.Button);
        break;
    case this.Part3.Button.ButtonText.toLowerCase():
        this.Part3.Button.ButtonText = curButton.button.innerText;
        this.Part3.Button.Element = curButton.button;
        this.Part2.Button.CurrentClass = curButton.button.className;
        console.log(smgData.PodCast.Parts.Part3.Button);
        break;
}

这是一些基本代码,可以在 https://www.jslint.com/

上重现
function abcd() {
    var a;
    var b;
    switch (a) {
        case 1:
            a=b;
            break;
        case 2:
            b=a;
            break;
    }
}

这听起来像是 JSLint 的问题。

这不完全是您要问的,但是重新编写代码并完全避免 switch 的一种方法(因此 JSLint 在 switch 中存在的问题)是 .findButtonText 匹配的 Part。然后使用括号符号查找 this:

上的按钮
const currentText = curButton.button.innerText.toLowerCase();
const matchingPart = ['Part1', 'Part2', 'Part3']
    .find(part => currentText === this[part].Button.ButtonText.toLowerCase());
if (matchingPart) {
    const { button } = this[matchingPart];
    button.ButtonText = curButton.button.innerText;
    button.Element = curButton.button;
    button.CurrentClass = curButton.button.className;
    console.log(smgData.PodCast.Parts[matchingPart].Button);
}

如果您可以控制 this 对象的形状,那么如果 Part 是一个数组而不是 3 个不同的属性,可能会更容易。然后,您可以 .find 遍历该数组,而不是对 3 Part 属性进行硬编码。

我认为上面的代码 非常好,但要使其通过所有 JSLint 的(IMO - 自以为是且不太好的)规则,它' d 必须

const currentText = curButton.button.innerText.toLowerCase();
const props = ["Part1", "Part2", "Part3"];
const matchingPart = props.find(function(part) {
    return currentText === this[part].Button.ButtonText.toLowerCase();
});
if (matchingPart) {
    const { button } = this[matchingPart];
    button.ButtonText = curButton.button.innerText;
    button.Element = curButton.button;
    button.CurrentClass = curButton.button.className;
    console.log(smgData.PodCast.Parts[matchingPart].Button);
}