lines-around-directive
要求或禁止指令周圍的新行
🔧 可修正
此規則回報的某些問題可以透過 --fix
命令列 選項自動修正
指令在 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
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
在 Playground 中開啟
/* eslint lines-around-directive: ["error", "always"] */
// comment
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
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
在 Playground 中開啟
/* eslint lines-around-directive: ["error", "never"] */
// comment
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
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
在 Playground 中開啟
/* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
// comment
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
var foo;
function foo() {
"use strict";
var bar;
}
function foo() {
// comment
var bar;
}
在 Playground 中開啟
/* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
// comment
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 中引入。