newline-after-var
变量声明后需要或不允许空行(newline-after-var)
在--fix
命令行上的选项可以自动修复一些被这条规则反映的问题。
此规则在ESLint v4.0.0 中已弃用
,并由填充行间语句规则取代。
从今天起,在将变量声明与其他代码分离时没有一致性。有些开发人员在var语句和其他代码之间留下了一条空行,如下所示:
var foo;
// do something with foo
而其他人根本没有留下任何空白的换行符。
var foo;
// do something with foo
问题在于这些开发人员在一个项目中一起工作。此规则强制执行,其中空换行符允许或禁止后一种编码风格var
,let
或const
语句。它有助于代码在整个项目中保持一致。
规则细节
此规则强制执行,其中空线都需要或禁止后一种编码风格var
,let
或const
语句来实现整个项目一致的编码风格。
选项
这条规则有一个字符串选项:
"always"
(默认)需要后一个空行var
,let
或const
在一条线上评论之后直接变种语句被当作附加的变种语句。
"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
版本
此规则在ESLint 0.18.0中引入。