Eslint
规则 | Rules

lines-around-directive

要求或不允许围绕指令换行符(指令周围)

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

此规则在 ESLint v4.0.0 中已弃用,并由填充行间语句规则取代。

在 JavaScript 中使用指令来向执行环境指示脚本想要加入如下特性中"strict mode"。指令在文件或功能块顶部的指令序言中组合在一起,并应用于它们出现的范围。

// Strict mode is invoked for the entire script "use strict"; var foo; function bar() { var baz; }

var foo; function bar() { // Strict mode is only invoked within this function "use strict"; var baz; }

规则细节

这条规则要求或禁止围绕指令序言的空白换行符。此规则不强制执行有关各个指令之间空白换行符的任何约定。另外,它不需要在指令序言前留出空行,除非它们之前有评论。如果这是您想强制执行的样式,请使用填充块规则。

选项

该规则有一个选项。它可以是一个字符串或一个对象:

  • "always" (默认)强制执行指令周围的空白换行符。

  • "never" 禁止围绕指令的空白换行符。

或者

{ "before": "always" or "never" "after": "always" or "never", }

总是

这是默认选项。

此规则的错误代码示例包含以下"always"选项:

/* eslint lines-around-directive: ["error", "always"] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

此规则的正确代码示例包含以下"always"选项:

/* eslint lines-around-directive: ["error", "always"] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

never

此规则的错误代码示例包含以下"never"选项:

/* eslint lines-around-directive: ["error", "never"] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

此规则的正确代码示例包含以下"never"选项:

/* eslint lines-around-directive: ["error", "never"] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

before & after

此规则的错误代码示例包含以下{ "before": "never", "after": "always" }选项:

/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

此规则的正确代码示例包含以下{ "before": "never", "after": "always" }选项:

/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

此规则的错误代码示例包含以下{ "before": "always", "after": "never" }选项:

/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

此规则的正确代码示例包含以下{ "before": "always", "after": "never" }选项:

/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */ /* Top of file */ "use strict"; var foo; /* Top of file */ // comment "use strict"; "use asm"; var foo; function foo() { "use strict"; "use asm"; var bar; } function foo() { // comment "use strict"; var bar; }

何时不使用它

如果你没有任何关于指令序言是否在它们之前或之后应该有空白换行符的严格约定,可以放心地禁用这条规则。

相关规则

  • lines-around-comment

  • padded-blocks

兼容性

  • JSCS: requirePaddingNewLinesAfterUseStrict

  • JSCS: disallowPaddingNewLinesAfterUseStrict

版本

该规则在 ESLint 3.5.0 中引入。

资源