Skip to content

Commit

Permalink
feat: update block-spacing for class static blocks (#15297)
Browse files Browse the repository at this point in the history
Updates the `block-spacing` rule to apply to class static blocks.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 15, 2021
1 parent 7b56844 commit 8611538
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 4 deletions.
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

0 comments on commit 8611538

Please sign in to comment.