版本

lines-around-directive

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

🔧 可修复

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

重要提示

此规则在 ESLint v4.0.0 中已弃用。请使用 相应的规则@stylistic/eslint-plugin-js 中。

了解更多

指令在 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" 选项的此规则,不正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", "always"] */

// comment
"use strict";
"use asm";
var foo;

对于使用 "always" 选项的此规则,正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", "always"] */

// comment

"use strict";
"use asm";

var foo;

never

对于使用 "never" 选项的此规则,不正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", "never"] */

// comment

"use strict";
"use asm";

var foo;

对于使用 "never" 选项的此规则,正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", "never"] */

// comment
"use strict";
"use asm";
var foo;

before & after

对于使用 { "before": "never", "after": "always" } 选项的此规则,不正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */

// comment

"use strict";
"use asm";
var foo;

对于使用 { "before": "never", "after": "always" } 选项的此规则,正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */

// comment
"use strict";
"use asm";

var foo;

对于使用 { "before": "always", "after": "never" } 选项的此规则,不正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */

// comment
"use strict";
"use asm";

var foo;

对于使用 { "before": "always", "after": "never" } 选项的此规则,正确代码的示例

在 Playground 中打开
/* 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;
}
在 Playground 中打开
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */

// comment

"use strict";
"use asm";
var foo;

何时不使用它

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

兼容性

版本

此规则在 ESLint v3.5.0 中引入。

资源

更改语言