Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: update keyword-spacing for class static blocks (#15289)
Updates the `keyword-spacing` rule to check spacing around `static` of class static blocks.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 15, 2021
1 parent ea18711 commit 7b56844
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
13 changes: 10 additions & 3 deletions docs/rules/keyword-spacing.md
Expand Up @@ -243,13 +243,14 @@ if(foo) {

### overrides

Examples of **correct** code for this rule with the `{ "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false } } }` option:
Examples of **correct** code for this rule with the `{ "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false } } }` option:

```js
/*eslint keyword-spacing: ["error", { "overrides": {
"if": { "after": false },
"for": { "after": false },
"while": { "after": false }
"while": { "after": false },
"static": { "after": false }
} }]*/

if(foo) {
Expand All @@ -263,7 +264,13 @@ if(foo) {
for(;;);

while(true) {
//...
//...
}

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

Expand Down
3 changes: 3 additions & 0 deletions docs/rules/space-before-blocks.md
Expand Up @@ -63,6 +63,9 @@ if (a) {
c();
}

class C {
static{} /*no error. this is checked by `keyword-spacing` rule.*/
}

function a() {}

Expand Down
1 change: 1 addition & 0 deletions lib/rules/keyword-spacing.js
Expand Up @@ -590,6 +590,7 @@ module.exports = {
ImportNamespaceSpecifier: checkSpacingForImportNamespaceSpecifier,
MethodDefinition: checkSpacingForProperty,
PropertyDefinition: checkSpacingForProperty,
StaticBlock: checkSpacingAroundFirstToken,
Property: checkSpacingForProperty,

// To avoid conflicts with `space-infix-ops`, e.g. `a > this.b`
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/rules/keyword-spacing.js
Expand Up @@ -978,6 +978,11 @@ ruleTester.run("keyword-spacing", rule, {
{ code: "class A { a;static[b]; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a; static #b; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a;static#b; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a() {} static {} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a() {}static{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a() {} static {} }", options: [override("static", BOTH)], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a() {}static{} }", options: [override("static", NEITHER)], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { a() {}\nstatic\n{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },

// not conflict with `generator-star-spacing`
{ code: "class A { static* [a]() {} }", parserOptions: { ecmaVersion: 6 } },
Expand All @@ -986,6 +991,10 @@ ruleTester.run("keyword-spacing", rule, {
// not conflict with `semi-spacing`
{ code: "class A { ;static a() {} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { ; static a() {} }", options: [NEITHER], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { ;static a; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { ; static a ; }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { ;static {} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { ; static{} }", options: [NEITHER], parserOptions: { ecmaVersion: 2022 } },

//----------------------------------------------------------------------
// super
Expand Down Expand Up @@ -3043,6 +3052,59 @@ ruleTester.run("keyword-spacing", rule, {
parserOptions: { ecmaVersion: 2022 },
errors: unexpectedAfter("static")
},
{
code: "class A { a() {}static{} }",
output: "class A { a() {} static {} }",
parserOptions: { ecmaVersion: 2022 },
errors: expectedBeforeAndAfter("static")
},
{
code: "class A { a() {}static{} }",
output: "class A { a() {} static {} }",
options: [override("static", BOTH)],
parserOptions: { ecmaVersion: 2022 },
errors: expectedBeforeAndAfter("static")
},
{
code: "class A { a() {}static {} }",
output: "class A { a() {} static {} }",
parserOptions: { ecmaVersion: 2022 },
errors: expectedBefore("static")
},
{
code: "class A { a() {} static{} }",
output: "class A { a() {} static {} }",
parserOptions: { ecmaVersion: 2022 },
errors: expectedAfter("static")
},
{
code: "class A { a() {} static {} }",
output: "class A { a() {}static{} }",
options: [NEITHER],
parserOptions: { ecmaVersion: 2022 },
errors: unexpectedBeforeAndAfter("static")
},
{
code: "class A { a() {} static {} }",
output: "class A { a() {}static{} }",
options: [override("static", NEITHER)],
parserOptions: { ecmaVersion: 2022 },
errors: unexpectedBeforeAndAfter("static")
},
{
code: "class A { a() {} static{} }",
output: "class A { a() {}static{} }",
options: [NEITHER],
parserOptions: { ecmaVersion: 2022 },
errors: unexpectedBefore("static")
},
{
code: "class A { a() {}static {} }",
output: "class A { a() {}static{} }",
options: [NEITHER],
parserOptions: { ecmaVersion: 2022 },
errors: unexpectedAfter("static")
},

//----------------------------------------------------------------------
// super
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/semi-spacing.js
Expand Up @@ -483,6 +483,19 @@ ruleTester.run("semi-spacing", rule, {
endColumn: 16
}
]
},
{
code: "class C { foo;static {}}",
output: "class C { foo; static {}}",
parserOptions: { ecmaVersion: 2022 },
errors: [{
messageId: "missingWhitespaceAfter",
type: "PropertyDefinition",
line: 1,
column: 14,
endLine: 1,
endColumn: 15
}]
}
]
});
14 changes: 13 additions & 1 deletion tests/lib/rules/space-before-blocks.js
Expand Up @@ -202,7 +202,19 @@ ruleTester.run("space-before-blocks", rule, {
{ code: "switch(x) { case (9):{ break; } }", options: alwaysArgs },
{ code: "switch(x){ case (9): { break; } }", options: neverArgs },
{ code: "switch(x) { default:{ break; } }", options: alwaysArgs },
{ code: "switch(x){ default: { break; } }", options: neverArgs }
{ code: "switch(x){ default: { break; } }", options: neverArgs },

// not conflict with `keyword-spacing`
{
code: "(class{ static{} })",
options: ["always"],
parserOptions: { ecmaVersion: 2022 }
},
{
code: "(class { static {} })",
options: ["never"],
parserOptions: { ecmaVersion: 2022 }
}
],
invalid: [
{
Expand Down

0 comments on commit 7b56844

Please sign in to comment.