lines-around-directive
要求或禁止指令周圍有換行符
此規則報告的某些問題可以透過 --fix
命令列選項自動修正
此規則在 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"] */
// comment
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
/* eslint lines-around-directive: ["error", "always"] */
// comment
var foo;
使用 "always"
選項時,此規則的正確程式碼範例
/* 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"
選項時,此規則的不正確程式碼範例
/* eslint lines-around-directive: ["error", "never"] */
// comment
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
/* eslint lines-around-directive: ["error", "never"] */
// comment
var foo;
使用 "never"
選項時,此規則的正確程式碼範例
/* 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" }
選項時,此規則的不正確程式碼範例
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
var foo;
使用 { "before": "never", "after": "always" }
選項時,此規則的正確程式碼範例
/* 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" }
選項時,此規則的不正確程式碼範例
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
var foo;
使用 { "before": "always", "after": "never" }
選項時,此規則的正確程式碼範例
/* 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;
何時不使用
如果您對指令序言之前或之後是否應有空白換行符沒有任何嚴格的慣例,則可以安全地停用此規則。
相容性
相關規則
版本
此規則在 ESLint v3.5.0 中引入。