ESlint 如何强制声明新行?做出非法的内联声明?

ESlint how force declaration new line ? make illegal inline declaration?

如何强制声明在新行上? 如何让这个不合法?

function foo() {
    if ( 'error' ) {
        return 'It failed';
    }if ( 'loading' ) {// if need to be on new line
        return "It's still loading";
    }
}

并强制使用此模式

function foo() {
    if ( 'error' ) {
        return 'It failed';
    }
    if ( 'loading' ) {
        return "It's still loading";
    }
}

我目前的规则

 "rules": {
        "brace-style":["warn", "1tbs"],
       "array-bracket-newline": ["warn", { "multiline": true }],
       "comma-spacing": ["warn", { "before": true, "after": true }],
       "curly": [2],
        "array-bracket-spacing": ["warn","always"],
        "object-curly-spacing": ["warn", "always"],
        "object-curly-newline": ["warn", {
            "ObjectExpression": { "consistent": true, "multiline": true ,"minProperties": 4},
            "ObjectPattern": { "consistent": true, "multiline": true },
            "ImportDeclaration": "never",
            "ExportDeclaration": { "multiline": true, "minProperties": 3 }
        }],
        "computed-property-spacing": ["warn", "never", { "enforceForClassMembers": true }],
        "space-in-parens": ["warn", "always"],
        "padding-line-between-statements": [
            "warn",
            { "blankLine": "always", "prev": ["const", "let", "var"], "next": "*"},
            { "blankLine": "any",    "prev": ["const", "let", "var"], "next": ["const", "let", "var"]}
        ],
        "sort-imports": ["warn",{
            "ignoreCase": false,
            "ignoreDeclarationSort": true,
            "ignoreMemberSort": false,
            "memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
            "allowSeparatedGroups": false
        }],
       "operator-linebreak": ["warn", "none"],
       "no-mixed-spaces-and-tabs": "warn",
        "object-property-newline": ["warn",{ "allowAllPropertiesOnSameLine": true }],
        "no-multiple-empty-lines": ["warn", { "max": 1, "maxEOF": 4 }],
        "lines-between-class-members": ["warn", "always"],
        "no-multi-spaces": "warn",
       "comma-dangle": ["warn", "always-multiline"],
        "padded-blocks": ["warn", { "classes": "always","blocks": "never" }, { "allowSingleLineBlocks": true }],
        "comma-style": ["warn", "last"],
        "arrow-parens": ["warn", "always"],
        "indent": ["warn", "tab"],
        "no-console": "warn",
        "no-return-assign": "warn",
        "no-var": "warn",
        "no-duplicate-imports": "error",
        "react/prop-types": [
            0,
            {}
        ],
        "react/jsx-curly-brace-presence": "warn",
        "space-infix-ops": "warn",
        "spaced-comment": ["warn", "always"],
        "space-unary-ops": "warn",
       "no-else-return": ["error", {"allowElseIf": false}],
        //style "vudge"
        "grouped-accessor-pairs": ["warn", "getBeforeSet"]
        
    }

有什么办法可以防止这种情况发生吗?

padding-line-between-statements 会做你想做的,你只需要适当地配置它。

https://eslint.org/docs/rules/padding-line-between-statements

目前您只将它配置为强制执行与变量声明相关的行 - 但您已经表示您也想强制执行 ifs - 所以您需要添加配置"block-like" 还有。

这是一个使用 "*"

来处理真正惰性配置的示例

https://eslint.org/demo#eyJ0ZXh0IjoiLypcbmVzbGludCBwYWRkaW5nLWxpbmUtYmV0d2Vlbi1zdGF0ZW1lbnRzOiBbXG4gICdlcnJvcicsXG4gIHsgXCJibGFua0xpbmVcIjogXCJhbHdheXNcIiwgXCJwcmV2XCI6IFwiKlwiLCBcIm5leHRcIjogXCIqXCIgfVxuXVxuKi9cblxuZnVuY3Rpb24gZm9vKCkge1xuICAgIGlmICggJ2Vycm9yJyApIHtcbiAgICAgICAgcmV0dXJuICdJdCBmYWlsZWQnO1xuICAgIH1pZiAoICdsb2FkaW5nJyApIHsvLyBpZiBuZWVkIHRvIGJlIG9uIG5ldyBsaW5lXG4gICAgICAgIHJldHVybiBcIkl0J3Mgc3RpbGwgbG9hZGluZ1wiO1xuICAgIH1cbn1cbiIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6MTIsInNvdXJjZVR5cGUiOiJzY3JpcHQiLCJlY21hRmVhdHVyZXMiOnsianN4Ijp0cnVlfX0sInJ1bGVzIjp7fSwiZW52Ijp7fX19