# lines-around-directive

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

指令在 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;
}

# 规则详情

此规则要求或禁止指令序言周围的空白换行符。此规则不强制执行有关各个指令之间的空白换行符的任何约定。此外,它不需要在指令序言之前使用空白换行符,除非它们前面有注释。如果这是您想要强制执行的样式,请使用 padded-blocks 规则。

# 选项

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

  • "always"(默认)在指令周围强制使用空白换行符。
  • "never" 不允许在指令周围出现空白换行符。

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

# always

这是默认选项。

此规则使用 "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;
}

# 何时不使用

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

# 兼容性

  • JSCSrequirePaddingNewLinesAfterUseStrict
  • JSCSdisallowPaddingNewLinesAfterUseStrict
Last Updated: 5/13/2023, 8:55:38 PM