版本

keyword-spacing

強制在關鍵字前後使用一致的空格

🔧 可修正

此規則報告的一些問題可透過 --fix 命令列選項自動修正

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中對應的規則

關鍵字是 JavaScript 的語法元素,例如 tryif。這些關鍵字對於該語言具有特殊含義,因此在程式碼編輯器中通常會以不同的顏色顯示。作為該語言的重要組成部分,風格指南通常會提及關鍵字周圍應使用的空格。例如,您的風格指南可能會說關鍵字應始終被空格包圍,這表示 if-else 語句必須看起來像這樣

if (foo) {
    // ...
} else {
    // ...
}

當然,您也可以使用不允許關鍵字周圍有空格的風格指南。

但是,如果您想強制執行 function 關鍵字與後續左括號之間的空格樣式,請參考 space-before-function-paren

規則詳情

此規則強制在關鍵字和類似關鍵字的符號周圍使用一致的空格:as(在模組宣告中)、async(非同步函式)、await(await 表達式)、breakcasecatchclassconstcontinuedebuggerdefaultdeletedoelseexportextendsfinallyforfrom(在模組宣告中)、functionget(getter)、ifimportin(在 for-in 語句中)、letnewof(在 for-of 語句中)、returnset(setter)、staticsuperswitchthisthrowtrytypeofvarvoidwhilewithyield。此規則的設計經過仔細考慮,不會與其他空格規則衝突:它不適用於其他規則報告問題的空格。

選項

此規則有一個物件選項

  • "before": true(預設值)要求關鍵字前至少有一個空格
  • "before": false 不允許關鍵字前有空格
  • "after": true(預設值)要求關鍵字後至少有一個空格
  • "after": false 不允許關鍵字後有空格
  • "overrides" 允許覆寫指定關鍵字的空格樣式

before

使用預設的 { "before": true } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "before": true }]*/

if (foo) {
    //...
}else if (bar) {
    //...
}else {
    //...
}

使用預設的 { "before": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "before": true }]*/

if (foo) {
    //...
} else if (bar) {
    //...
} else {
    //...
}

// Avoid conflict with `array-bracket-spacing`
let a = [this];
let b = [function() {}];

// Avoid conflict with `arrow-spacing`
let c = ()=> this.foo;

// Avoid conflict with `block-spacing`
{function foo() {}}

// Avoid conflict with `comma-spacing`
let d = [100,this.foo, this.bar];

// Avoid conflict with `computed-property-spacing`
obj[this.foo] = 0;

// Avoid conflict with `generator-star-spacing`
function *bar() {}

// Avoid conflict with `key-spacing`
let obj1 = {
    foo:function() {}
};

// Avoid conflict with `object-curly-spacing`
let obj2 = {foo: this};

// Avoid conflict with `semi-spacing`
let e = this;function foo() {}

// Avoid conflict with `space-in-parens`
(function () {})();

// Avoid conflict with `space-infix-ops`
if ("foo"in {foo: 0}) {}
if (10+this.foo<= this.bar) {}

// Avoid conflict with `jsx-curly-spacing`
let f = <A foo={this.foo} bar={function(){}} />

使用 { "before": false } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "before": false }]*/

if (foo) {
    //...
} else if (bar) {
    //...
} else {
    //...
}

使用 { "before": false } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "before": false }]*/

if (foo) {
    //...
}else if (bar) {
    //...
}else {
    //...
}

after

使用預設的 { "after": true } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "after": true }]*/

if(foo) {
    //...
} else if(bar) {
    //...
} else{
    //...
}

使用預設的 { "after": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "after": true }]*/

if (foo) {
    //...
} else if (bar) {
    //...
} else {
    //...
}

// Avoid conflict with `array-bracket-spacing`
let a = [this];

// Avoid conflict with `arrow-spacing`
let b = ()=> this.foo;

// Avoid conflict with `comma-spacing`
let c = [100, this.foo, this.bar];

// Avoid conflict with `computed-property-spacing`
obj[this.foo] = 0;

// Avoid conflict with `generator-star-spacing`
function* foo() {}

// Avoid conflict with `key-spacing`
let obj1 = {
    foo:function() {}
};

// Avoid conflict with `func-call-spacing`
class A extends B {
    constructor() {
        super();
    }
}

// Avoid conflict with `object-curly-spacing`
let obj2 = {foo: this};

// Avoid conflict with `semi-spacing`
let d = this;function bar() {}

// Avoid conflict with `space-before-function-paren`
(function() {})();

// Avoid conflict with `space-infix-ops`
if ("foo"in{foo: 0}) {}
if (10+this.foo<= this.bar) {}

// Avoid conflict with `space-unary-ops`
function* baz(a) {
    return yield+a;
}

// Avoid conflict with `yield-star-spacing`
function* qux(a) {
    return yield* a;
}

// Avoid conflict with `jsx-curly-spacing`
let e = <A foo={this.foo} bar={function(){}} />

使用 { "after": false } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "after": false }]*/

if (foo) {
    //...
} else if (bar) {
    //...
} else {
    //...
}

使用 { "after": false } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "after": false }]*/

if(foo) {
    //...
} else if(bar) {
    //...
} else{
    //...
}

overrides

使用 { "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false }, "as": { "after": false } } } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint keyword-spacing: ["error", { "overrides": {
  "if": { "after": false },
  "for": { "after": false },
  "while": { "after": false },
  "static": { "after": false },
  "as": { "after": false }
} }]*/

if(foo) {
    //...
} else if(bar) {
    //...
} else {
    //...
}

for(;;);

while(true) {
    //...
}

class C {
    static{
        //...
    }
}

export { C as"my class" };

何時不應使用

如果您不想在關鍵字空格上強制一致性,則可以安全地停用此規則。

版本

此規則是在 ESLint v2.0.0-beta.1 中引入的。

資源

變更語言