Search code examples
eslinteslintrctypescript-eslint

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


How force declaration to be on new line ? How to make this illegal?

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

and force this pattern

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

my current rule

 "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"]
        
    }

Is there a way to prevent this ?


Solution

  • padding-line-between-statements will do what you want, you just have to configure it appropriately.

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

    currently you only have it configured to enforce there are lines in relation to variable declarations - but you've indicated you want to enforce around ifs as well - so you'll want to add config for "block-like" as well.

    Here's an example of it working with a really lazy config using "*"

    https://eslint.org/demo#eyJ0ZXh0IjoiLypcbmVzbGludCBwYWRkaW5nLWxpbmUtYmV0d2Vlbi1zdGF0ZW1lbnRzOiBbXG4gICdlcnJvcicsXG4gIHsgXCJibGFua0xpbmVcIjogXCJhbHdheXNcIiwgXCJwcmV2XCI6IFwiKlwiLCBcIm5leHRcIjogXCIqXCIgfVxuXVxuKi9cblxuZnVuY3Rpb24gZm9vKCkge1xuICAgIGlmICggJ2Vycm9yJyApIHtcbiAgICAgICAgcmV0dXJuICdJdCBmYWlsZWQnO1xuICAgIH1pZiAoICdsb2FkaW5nJyApIHsvLyBpZiBuZWVkIHRvIGJlIG9uIG5ldyBsaW5lXG4gICAgICAgIHJldHVybiBcIkl0J3Mgc3RpbGwgbG9hZGluZ1wiO1xuICAgIH1cbn1cbiIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6MTIsInNvdXJjZVR5cGUiOiJzY3JpcHQiLCJlY21hRmVhdHVyZXMiOnsianN4Ijp0cnVlfX0sInJ1bGVzIjp7fSwiZW52Ijp7fX19