newline-after-var
在变量声明后需要或不允许空行
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v4.0.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
截至今天,在将变量声明与其余代码分开的做法上还没有统一。一些开发者在 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
问题在于这些开发者在一个项目中一起工作时。这条规则强制使用一种编码风格,即在 var、let 或 const 语句之后是否允许空行。它有助于整个项目的代码保持一致。
🌐 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
此规则强制执行一种编码风格,在 var、let 或 const 语句后要求或禁止空行,以在整个项目中实现一致的编码风格。
🌐 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"(默认)要求在var、let或const之后有一个空行在 var 语句之后的一行上的注释被视为附加 var 语句。
-
"never"不允许在var、let或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 中引入。