lines-around-directive
要求或禁止在指令周围换行
此规则报告的一些问题可通过 --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.
在 JavaScript 中,指令用于向执行环境表明脚本希望使用某个功能,例如 "strict mode"。指令会在文件或函数块的顶部被聚集在一个 指令序言 中,并且作用于它们出现的作用域。
🌐 Directives are used in JavaScript to indicate to the execution environment that a script would like to opt into a feature such as "strict mode". Directives are grouped together in a directive prologue at the top of either a file or function block and are applied to the scope in which they occur.
// 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;
}
规则详情
🌐 Rule Details
此规则要求或禁止在指令序言周围使用空白换行。此规则不强制执行各个指令之间的空白换行惯例。此外,除非指令序言前有注释,否则不要求在指令序言前使用空白换行。如果你希望强制执行这种风格,请使用 padded-blocks 规则。
🌐 This rule requires or disallows blank newlines around directive prologues. This rule does not enforce any conventions about blank newlines between the individual directives. In addition, it does not require blank newlines before directive prologues unless they are preceded by a comment. Please use the padded-blocks rule if this is a style you would like to enforce.
选项
🌐 Options
此规则有一个选项。它可以是字符串或对象:
🌐 This rule has one option. It can either be a string or an object:
"always"(默认)在指令周围强制添加空白换行。"never"不允许指令周围有空行。
or
{
"before": "always" or "never"
"after": "always" or "never",
}
always
这是默认选项。
🌐 This is the default option.
使用 "always" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always" option:
/* eslint lines-around-directive: ["error", "always"] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", "always"] */
// comment
"use strict";
"use asm";
var foo;
使用 "always" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always" option:
/* eslint lines-around-directive: ["error", "always"] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", "always"] */
// comment
"use strict";
"use asm";
var foo;
never
使用 "never" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never" option:
/* eslint lines-around-directive: ["error", "never"] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", "never"] */
// comment
"use strict";
"use asm";
var foo;
使用 "never" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "never" option:
/* eslint lines-around-directive: ["error", "never"] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", "never"] */
// comment
"use strict";
"use asm";
var foo;
before & after
使用 { "before": "never", "after": "always" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "before": "never", "after": "always" } option:
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
"use strict";
"use asm";
var foo;
使用 { "before": "never", "after": "always" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "before": "never", "after": "always" } option:
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
"use strict";
"use asm";
var foo;
使用 { "before": "always", "after": "never" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "before": "always", "after": "never" } option:
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
"use strict";
"use asm";
var foo;
使用 { "before": "always", "after": "never" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "before": "always", "after": "never" } option:
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
"use strict";
var foo;
function foo() {
"use strict";
"use asm";
var bar;
}
function foo() {
// comment
"use strict";
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
"use strict";
"use asm";
var foo;
何时不使用
🌐 When Not To Use It
如果你没有关于指令序言是否应该在它们之前或之后有空白换行符的任何严格约定,你可以安全地禁用此规则。
🌐 You can safely disable this rule if you do not have any strict conventions about whether or not directive prologues should have blank newlines before or after them.
兼容性
🌐 Compatibility
相关规则
版本
此规则是在 ESLint v3.5.0 中引入。