# newline-after-var

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

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

var foo;

// do something with foo

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

var foo;
// do something with foo

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

# 规则详情

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

# 选项

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

  • "always"(默认)要求在 var、let 或 const 之后有一个空行 在 var 语句之后的一行上的注释被视为附加 var 语句。

  • "never" 不允许在 var、let 或 const 之后出现空行

# always

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

/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/

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

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

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" 选项的正确代码示例:

/*eslint newline-after-var: ["error", "always"]*/
/*eslint-env es6*/

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

console.log(greet, name);

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

console.log(greet, name);

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" 选项的错误代码示例:

/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/

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

console.log(greet, name);

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

console.log(greet, name);

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" 选项的正确代码示例:

/*eslint newline-after-var: ["error", "never"]*/
/*eslint-env es6*/

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

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

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

var greet = "hello,";
var name = "world";
// var name = require("world");
console.log(greet, name);
Last Updated: 5/13/2023, 8:55:38 PM