Has ESlint a rule about blank line before the first statement in function?
Asked Answered
W

3

8

Due ESLint I found a rule newline-before-return about empty line before return statements. But did not see a rule about empty line before the first statement in function. F.e.:

function (a) {

    var b = +a;
}

Has ESlint a rule about this? If it has, what is the name this rule? Thanks

Wylde answered 23/8, 2016 at 10:33 Comment(0)
N
9

The padded-blocks rule allows you to require newlines at the start and end of blocks, including function bodies. In addition to function bodies, it also covers the bodies of if statements, for and while loops, and other block-like structures, which you may or may not want.

Try pasting the following code in the demo to see if it works for you:

/* eslint padded-blocks: "error" */
function foo(bar) {

    if (bar) {

        foo();

    }

}

If you only want to check function bodies, you could follow @Dhananjay's suggestion and edit the rule's source code into your own custom rule.

Natelson answered 23/8, 2016 at 17:44 Comment(0)
R
2

I don't think there is such a rule available out of the box based on the list of available rules You can try to add a custom rule for this check as explained here

Raila answered 23/8, 2016 at 10:49 Comment(0)
C
1

Such rule is implemented in the HAPI ESLint plugin, installed this way:

npm install @hapi/eslint-plugin-hapi --save-dev
// Add in your `.eslintrc`
{
  plugins: [
    '@hapi/eslint-plugin-hapi',
  ],
  rules: {
    '@hapi/hapi/scope-start': ['error'],
  },
};

}

Or you may use it as part of HAPI ESLint config.
Mind, that Airbnb style guide recommends against padding blocks.

Catamaran answered 27/11, 2016 at 18:29 Comment(1)
It works if you don't want to use the --fix option of lint.Deterge

© 2022 - 2024 — McMap. All rights reserved.