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 block-spacing for class static blocks #15297

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
16 changes: 16 additions & 0 deletions docs/rules/block-spacing.md
Expand Up @@ -23,6 +23,10 @@ if (foo) { bar = 0;}
function baz() {let i = 0;
return i;
}

class C {
static {this.bar = 0;}
}
```

Examples of **correct** code for this rule with the default `"always"` option:
Expand All @@ -32,6 +36,10 @@ Examples of **correct** code for this rule with the default `"always"` option:

function foo() { return true; }
if (foo) { bar = 0; }

class C {
static { this.bar = 0; }
}
```

### never
Expand All @@ -43,6 +51,10 @@ Examples of **incorrect** code for this rule with the `"never"` option:

function foo() { return true; }
if (foo) { bar = 0;}

class C {
static { this.bar = 0; }
}
```

Examples of **correct** code for this rule with the `"never"` option:
Expand All @@ -52,6 +64,10 @@ Examples of **correct** code for this rule with the `"never"` option:

function foo() {return true;}
if (foo) {bar = 0;}

class C {
static {this.bar = 0;}
}
```

## When Not To Use It
Expand Down
13 changes: 10 additions & 3 deletions lib/rules/block-spacing.js
Expand Up @@ -40,7 +40,7 @@ module.exports = {

/**
* Gets the open brace token from a given node.
* @param {ASTNode} node A BlockStatement/SwitchStatement node to get.
* @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to get.
* @returns {Token} The token of the open brace.
*/
function getOpenBrace(node) {
Expand All @@ -50,6 +50,12 @@ module.exports = {
}
return sourceCode.getLastToken(node, 1);
}

if (node.type === "StaticBlock") {
return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
}

// "BlockStatement"
return sourceCode.getFirstToken(node);
}

Expand All @@ -72,8 +78,8 @@ module.exports = {
}

/**
* Reports invalid spacing style inside braces.
* @param {ASTNode} node A BlockStatement/SwitchStatement node to get.
* Checks and reports invalid spacing style inside braces.
* @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check.
* @returns {void}
*/
function checkSpacingInsideBraces(node) {
Expand Down Expand Up @@ -157,6 +163,7 @@ module.exports = {

return {
BlockStatement: checkSpacingInsideBraces,
StaticBlock: checkSpacingInsideBraces,
SwitchStatement: checkSpacingInsideBraces
};
}
Expand Down