keyword-spacing

在关键字前后强制保持一致的间距

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

关键字是 JavaScript 的语法元素,例如 tryif。这些关键字对语言具有特殊意义,因此在代码编辑器中经常以不同的颜色出现。作为语言的重要组成部分,样式指南通常指的是应该在关键字周围使用的间距。例如,您可能有一个样式指南,它说关键字应该始终被空格包围,这意味着 if-else 语句必须如下所示:

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

当然,您也可以有一个不允许关键字周围有空格的样式指南。

但是,如果要强制 function 关键字和后面的左括号之间的空格样式,请参考 space-before-function-paren

规则详情

ogLWmzQgdFj9z3t7oCrtXV+NosUFGmw/Rt+rseooWQKpNhtcJLDumAQkgL2gtaHJoiqMI/w0J4MmArHYS16rOZV5rg11JWjYpVGQQX7XlXxt/pNOQfRXQ3kDwUdpArBBEgUYe+o2tiDsZVU8GiBRPBe4xRRbGfjRUBLNwMccDxXxj5K9dRks4MCDzNwp4Y03yicWjHfQFLSyFy9O1tVo5wQViqm+TBwrOtN2GkUOXHcQqi8aoPZAenVHFGVb4+rp7Ad2gUvP0XyymJVFcDWoEbwOrlod3q0SGBa6+TXNQ3RDIkURlqoFpHCUY+0ctR9g3AWRvzNjlRRvk0y6lASze1mIB8AHOIPrVxo+Jr5Qj+d5HpETKk00/p2f/zmKn3Q1fTEqELNQnqHj/Ge2vRYUHNt4hZwLx3YW5wN7Ymi4cyGrLwtQHgV10PGWh44u7eWmCcwSt/22IHLhIGHTmp4z+48z/qgqwrzLaf9q2No7jRqSOt8s5oTvOOqZIWRfusng/Vuk0HBtl74IxIUWdGwmkdkTUZ/Vl3qAFx+teHjCuF5XT6tWzIA7s2JU2jwBjbUtLRR7BFRpevbCp1oAewHqoMKF9q31dEgIMaaLu8PU2r+NEcPgCWNpW2OsNRm0ouvCD7K/JgLktUkZmziEr1kyedAFSyfPUxShy+gpzM8mHWWJN7R28OfeYiH7YRQ79JnETvgXzTxb36TTtcvZOMEqrzwmo9QgXGPEm+ItbjW+4VqX/0EoxOp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VWimZFRgtzcaK0tEZx8yYV+pZ/DAaWhQyQX1hcz3mSecdngq0A0b5SGWhsOmDM0NhrP33IZoMJfomMKnLdeG0fr33Rdvb86j/CLzLSUPPmwrQ/EYvXZXVnyTFquOCvSIvg0tVrugicUwjCPgW2gJpg/tuwR7AQxyuvA3mvzWeF9MgaWddq4ChsIBGXLv17p/8uuRaz+NqJ9nIkr3jvc231AychYKnhDH9adkF146KTmuFQdxW3nt9ZIWvUSOGqTHoWsgH+jew3pFbGFe7aiRd4oPP9eAXxDdd47aTAq5++UbZO3Mtqivie0lx/PLlpopOao3tVGivX491wuz8hUN1R0WVBlrxQLMXQU6N+YiJ1bx7rgl5Yxe3JvoM+99vX9myGrTap/fgSs2MuYeT/zjT7Wx74DqsA==

before

1usau4ITNzPbSW1XK9odV6qzIy5+8rad8XW0jtpvmzc6JjNUQ1xm/joZpLKkjI9JLoWuA1sAWRLIDV6EE14BqEDmWnlmSnWpjnHJev0rR2nKCrsYJY2vdaSQh3ZD4tzQ

/*eslint keyword-spacing: ["error", { "before": true }]*/

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

1usau4ITNzPbSW1XK9odV6qzIy5+8rad8XW0jtpvmzc6JjNUQ1xm/joZpLKkjI9JMNHh9u38kJr6AF6xU8TJh6IRwhYRNrVM693fxppU9XMDaOgdufLhrkhGZe89GniM

/*eslint keyword-spacing: ["error", { "before": true }]*/
/*eslint-env es6*/

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

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

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

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

// Avoid conflict with `comma-spacing`
let a = [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 obj = {
    foo:function() {}
};

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

// Avoid conflict with `semi-spacing`
let a = 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 a = <A foo={this.foo} bar={function(){}} />

dnC4C79+++7hmkZ7ssvw8+u9kMdsv/rkyCX6LrT4PKUU60vsAlB14LJF1eGw7GyineZhAXHn/qidKEMbtYvTCgMTKORnmXRFvCBvI8mrgnk=

/*eslint keyword-spacing: ["error", { "before": false }]*/

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

dnC4C79+++7hmkZ7ssvw8+u9kMdsv/rkyCX6LrT4PKUU60vsAlB14LJF1eGw7GyiLUsXjrorCwQgdq3ZNoBKciGoGouh6FfDxhWVCdhtH8M=

/*eslint keyword-spacing: ["error", { "before": false }]*/

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

after

1usau4ITNzPbSW1XK9odV72XJZXGPJzxUJIDnMLzPOikBbu/OBMceQcgvZYa0hVc8ih5QN/al0MxuY6gjatu2lxxvq5kWNAPdrnGBe/CjSfrpQDVac7IL2dDU7DM9I9R

/*eslint keyword-spacing: ["error", { "after": true }]*/

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

1usau4ITNzPbSW1XK9odV72XJZXGPJzxUJIDnMLzPOikBbu/OBMceQcgvZYa0hVc9eVONHuW1SMtwfSZPDAXGwTZSb8TecfponyMTlyHXysH+PegFUBbVpUBfMFr1KPK

/*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 a = ()=> this.foo;

// Avoid conflict with `comma-spacing`
let a = [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 obj = {
    foo:function() {}
};

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

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

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

// 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* foo(a) {
    return yield+a;
}

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

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

dnC4C79+++7hmkZ7ssvw81kD7TQqLd1REgdbyHHShziHRUkhtz1Js3tzZKUzP1ZW3hg3bv8f2BwW/83+X7M4+BRwx6hZBuS8kFNkqJQZTtc=

/*eslint keyword-spacing: ["error", { "after": false }]*/

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

dnC4C79+++7hmkZ7ssvw81kD7TQqLd1REgdbyHHShziHRUkhtz1Js3tzZKUzP1ZWrdfzequHTBNftBdyXR9nW41B6fc/MCgxL3PErw9Ju3w=

/*eslint keyword-spacing: ["error", { "after": false }]*/

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

overrides

dnC4C79+++7hmkZ7ssvw88huzSnWmB0KrBgzg/UJ6shE439GCkePl8It4mtSoZR/o7yXzXjcN1kgdfItJcr5PgP84pJPwBG4g8MJTbsQo/kgmNOaFLFOoa4jCMcXxFBJL8t0NUYZptLzvGqoi+/GRCaWkeF9PSCnhTmfOVsMnUdG9KkTpCWYwCw7XqUwGlZPHp09BAPK+UuemgVvXSQrZkIb7fR2G5sh+sJbcD0RQ2eSvc5c7Ycwr7aK8TJgHHe5IrN4W74o35p0ysnywdymH1SJ3XwFwY50z3ixAgfT//8=

/*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" };

何时不使用

ubzb76sjyPfAvhrhq6LVKwQLd+VHU2GRJR96Ka7vMcjnec0wN+0vN4NzWNQiFMaFKJ47g2/PxbpJR/QREQA8wvZP2aUUUpwbx7Exe8xeYPh5MZSGEzTc/Sh+aUjozmVc