Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update keyword-spacing for class static blocks #15289

Merged
merged 1 commit into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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