简体   繁体   English

ESLint / Nodejs:由于默认开关大小写而导致错误掉线

[英]ESLint/Nodejs: Error linting due to default switch case

While trying to validate my .js file using ESLint I received a an unexpected lexical declaration (no-case declarations) error. 在尝试使用ESLint验证我的.js文件时,收到一个意外的词汇声明(无大小写声明)错误。

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

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

Following is my code that is causing the error 以下是导致错误的我的代码

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

There is no error shown in vs-code for this. vs代码中没有显示此错误。 When i transform my code like: 当我像这样转换我的代码时:

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

all errors disappear so I am thinking it must be coming off the default case block. 所有错误都消失了,所以我认为它一定来自默认的case块。

Following is my esLint.rc: 以下是我的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"
    }
}

What could be the issue here? 这里可能是什么问题? As far as i read, the default ESLint rules have a default-case rule but not a no-default-enforced rule. 据我所知,默认的ESLint规则具有默认情况下的规则,没有 无默认值的规则。

This is a tricky one cause it's not quite what you think. 这是一个棘手的原因,因为它与您的想法不完全相同。 The default not having {...} is the problem. 默认情况下没有{...}是问题所在。 So changing default: to default: { ... } will fix this. 因此,将default:更改为default:{...}将解决此问题。 If you want to turn it off, you'll have to remove an extension in eslint. 如果要关闭它,则必须删除eslint中的扩展名。

Specifically, this is being produced by the "extends" : "eslint:recommended" 具体来说,这是由“扩展”产生的:“ eslint:recommended”

More information here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 切换案例内的Nodejs Promise给出错误 - Nodejs Promise inside switch case giving error 我如何安全地修复 ESLint`no-fallthrough` 错误,以防止 break/return/throw 的故意 switch case 遗漏? - How do I safely fix ESLint`no-fallthrough` error for intentional switch case ommissions of break/return/throw? 根据ESLint,Switch语句默认返回“ Unreachable” - Switch Statement default return “Unreachable” according to ESLint 虽然服务器正在运行,但 eslint 没有 linting - eslint not linting though server is running ESlint linting gulpfile.js - ESlint linting gulpfile.js 如果“案例”之一发生错误,“开关案例”javascript 中的“案例”能否忽略错误并运行“默认”? - Can a “case” in “switch-case” javascript ignore errors and run “default” if one of “case” an error occurred? 首选默认导出 eslint 错误 - Prefer default export eslint error switch语句中的默认情况是否可选? - Is the default case in a switch statement optional? Javascript switch case一直跳到default case - Javascript switch case keeps skipping to the default case 使用 monorepo (Lerna + Yarn Workspaces) 共享代码的 ReactJS linting (eslint) 和 transpiling (babel) 错误 - ReactJS linting (eslint) and transpiling (babel) error on shared code using monorepo (Lerna + Yarn Workspaces)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM