Eslint参考手册
规则 | Rules
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中引入。
资源
- 规则来源
- 文档来源
规则 | Rules相关

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。
主页 | https://eslint.org/ |
源码 | https://github.com/eslint/eslint |
发布版本 | 4.12.0 |