ESLint/Nodejs:由于默认 switch case 导致的错误 linting

ESLint/Nodejs: Error linting due to default switch case

在尝试使用 ESLint 验证我的 .js 文件时,我收到了意外的词法声明(无大小写声明)错误。

c:\>eslint C:\modules\myFile.js

503:17  error  Unexpected lexical declaration in case block        no-case-declarations

以下是导致错误的我的代码

switch (type) {
case String(type.match(/.*test.*/i)):                        
        console.log('test')
        break;
default:
    console.log('error')
    return err;
}

vs-code 中没有为此显示错误。 当我像这样转换我的代码时:

switch (type) {
case String(type.match(/.*test.*/i)):                        
        console.log('test')
        break;
/*default:
    console.log('error')
    return err;*/
}

所有错误都消失了,所以我认为它一定是从 default case 块中脱落的。

以下是我的esLint.rc:

{
    "root": true,
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "script"
    },
    "env": {
        "node": true,
        "browser": false
    },
    "extends": "eslint:recommended",
    "rules": {
        "semi": ["error", "always"],
        "indent": ["error", 4, {
            "VariableDeclarator": 2,
            "SwitchCase": 1,
            "MemberExpression": 1,
            "ArrayExpression": "first"
        }],
        "no-mixed-requires": "off",
        "no-restricted-imports": "off",
        "no-undef":"off",
        "no-console":2,
        "no-trailing-spaces": "error",
        "no-unused-vars": "warn"
    }
}

这可能是什么问题?据我所知,默认的 ESLint 规则有一个 default-case rulenot a no-default-enforced规则。

这是一个棘手的问题,因为它与您的想法不尽相同。默认没有 {...} 是问题所在。因此,将 default: 更改为 default: { ... } 将解决此问题。如果你想关闭它,你必须在 eslint 中删除一个扩展。

具体来说,这是由 "extends" 生成的:"eslint:recommended"

More information here