newline-after-var

在变量声明后需要或不允许空行

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

该规则在 ESLint v4.0.0 中已弃用,并由 padding-line-between-statements 规则取代。

¥This rule was deprecated in ESLint v4.0.0 and replaced by the padding-line-between-statements rule.

到今天为止,将变量声明与其余代码分开还没有一致性。一些开发者在 var 语句和其余代码之间留了一个空行,例如:

¥As of today there is no consistency in separating variable declarations from the rest of the code. Some developers leave an empty line between var statements and the rest of the code like:

var foo;

// do something with foo

而其他人根本不会留下任何空的换行符。

¥Whereas others don’t leave any empty newlines at all.

var foo;
// do something with foo

问题是当这些开发者在一个项目中一起工作时。此规则强制执行编码样式,在 varletconst 语句之后允许或禁止空换行符。它有助于代码在整个项目中看起来一致。

¥The problem is when these developers work together in a project. This rule enforces a coding style where empty newlines are allowed or disallowed after var, let, or const statements. It helps the code to look consistent across the entire project.

规则详情

¥Rule Details

此规则强制执行编码风格,其中在 varletconst 语句之后需要或不允许空行,以在整个项目中实现一致的编码风格。

¥This rule enforces a coding style where empty lines are required or disallowed after var, let, or const statements to achieve a consistent coding style across the project.

选项

¥Options

此规则有一个字符串选项:

¥This rule has a string option:

  • "always"(默认)需要在 varletconst 之后有一个空行

    ¥"always" (default) requires an empty line after var, let, or const

    在 var 语句之后的一行上的注释被视为附加 var 语句。

    ¥Comments on a line directly after var statements are treated like additional var statements.

  • "never" 不允许在 varletconst 之后有空行

    ¥"never" disallows empty lines after var, let, or const

always

使用默认 "always" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the default "always" option:

在线运行
/*eslint newline-after-var: ["error", "always"]*/

var greet = "hello,",
    name = "world";
console.log(greet, name);

let hello = "hello,",
    world = "world";
console.log(hello, world);

var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);

var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);

使用默认 "always" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default "always" option:

在线运行
/*eslint newline-after-var: ["error", "always"]*/

var greet = "hello,",
    name = "world";

console.log(greet, name);

let hello = "hello,",
    world = "world";

console.log(hello, world);

var greet = "hello,";
const NAME = "world";

console.log(greet, NAME);

var greet = "hello,";
var name = "world";
// var name = require("world");

console.log(greet, name);

never

使用 "never" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint newline-after-var: ["error", "never"]*/

var greet = "hello,",
    name = "world";

console.log(greet, name);

let hello = "hello,",
    world = "world";

console.log(hello, world);

var greet = "hello,";
const NAME = "world";

console.log(greet, NAME);

var greet = "hello,";
var name = "world";
// var name = require("world");

console.log(greet, name);

使用 "never" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never" option:

在线运行
/*eslint newline-after-var: ["error", "never"]*/

var greet = "hello,",
    name = "world";
console.log(greet, name);

let hello = "hello,",
    world = "world";
console.log(hello, world);

var greet = "hello,";
const NAME = "world";
console.log(greet, NAME);

var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);

版本

此规则是在 ESLint v0.18.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号