lines-around-directive

要求或禁止在指令周围换行

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

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

¥This rule was deprecated in ESLint v4.0.0 and replaced by the padding-line-between-statements rule.

指令在 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"(默认)在指令周围强制使用空白换行符。

    ¥"always" (default) enforces blank newlines around directives.

  • "never" 不允许指令周围有空白换行符。

    ¥"never" disallows blank newlines around directives.

或者

¥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 中引入。

资源

ESLint 中文网
粤ICP备13048890号