简体   繁体   English

ESlint 在函数中的第一条语句之前是否有关于空行的规则?

[英]Has ESlint a rule about blank line before the first statement in function?

Due ESLint I found a rule newline-before-return about empty line before return statements.由于 ESLint,我在 return 语句之前发现了一个关于空行的规则newline-before-return But did not see a rule about empty line before the first statement in function.但是没看到函数中第一条语句前有空行的规则。 Fe:铁:

function (a) {

    var b = +a;
}

Has ESlint a rule about this? ESlint 有这方面的规定吗? If it has, what is the name this rule?如果有,这条规则的名称是什么? Thanks谢谢

The padded-blocks rule allows you to require newlines at the start and end of blocks, including function bodies. padded-blocks规则允许您在块的开始和结束处要求换行,包括函数体。 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.除了函数体之外,它还涵盖了if语句体、 forwhile循环以及您可能需要也可能不需要的其他类似块的结构。

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.如果您只想检查函数体,您可以按照@Dhananjay 的建议将规则的源代码编辑为您自己的自定义规则。

我不认为有这样的一个基于规则的名单上盒可用了可用规则你可以尝试添加自定义规则在本次检查的解释这里

Such rule is implemented in the HAPI ESLint plugin , installed this way:此类规则在HAPI ESLint 插件中实现,安装方式如下:

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 .或者您可以将其用作HAPI ESLint 配置的一部分。
Mind, that Airbnb style guide recommends against padding blocks.请注意, Airbnb 风格指南建议不要使用填充块。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM