7
votes

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

3

3 Answers

7
votes

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.

2
votes

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

1
votes

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.