Skip to content

Commit

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

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 19, 2021
1 parent 194f36d commit 6487df3
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 4 deletions.
87 changes: 84 additions & 3 deletions docs/rules/padded-blocks.md
Expand Up @@ -27,12 +27,12 @@ The second one is an object option, it can allow exceptions.

String option:

* `"always"` (default) requires empty lines at the beginning and ending of block statements and classes
* `"never"` disallows empty lines at the beginning and ending of block statements and classes
* `"always"` (default) requires empty lines at the beginning and ending of block statements, function bodies, class static blocks, classes, and `switch` statements.
* `"never"` disallows empty lines at the beginning and ending of block statements, function bodies, class static blocks, classes, and `switch` statements.

Object option:

* `"blocks"` require or disallow padding within block statements
* `"blocks"` require or disallow padding within block statements, function bodies, and class static blocks
* `"classes"` require or disallow padding within classes
* `"switches"` require or disallow padding within `switch` statements

Expand Down Expand Up @@ -68,6 +68,12 @@ if (a) {
b();

}

class C {
static {
a();
}
}
```

Examples of **correct** code for this rule with the default `"always"` option:
Expand All @@ -93,6 +99,16 @@ if (a) {
// comment
b();

}

class C {

static {

a();

}

}
```

Expand Down Expand Up @@ -124,6 +140,16 @@ if (a) {
if (a) {
b();

}

class C {

static {

a();

}

}
```

Expand All @@ -140,6 +166,12 @@ if (a)
{
b();
}

class C {
static {
a();
}
}
```

### blocks
Expand Down Expand Up @@ -174,6 +206,14 @@ if (a) {
// comment
b();

}

class C {

static {
a();
}

}
```

Expand All @@ -200,6 +240,25 @@ if (a) {
// comment
b();

}

class C {

static {

a();

}

}

class D {
static {

a();

}

}
```

Expand Down Expand Up @@ -230,6 +289,14 @@ if (a) {
b();

}

class C {
static {

a();

}
}
```

Examples of **correct** code for this rule with the `{ "blocks": "never" }` option:
Expand All @@ -245,6 +312,20 @@ if (a)
{
b();
}

class C {
static {
a();
}
}

class D {

static {
a();
}

}
```

### classes
Expand Down
8 changes: 8 additions & 0 deletions lib/rules/padded-blocks.js
Expand Up @@ -106,6 +106,12 @@ module.exports = {
if (node.type === "SwitchStatement") {
return sourceCode.getTokenBefore(node.cases[0]);
}

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

// `BlockStatement` or `ClassBody`
return sourceCode.getFirstToken(node);
}

Expand Down Expand Up @@ -172,6 +178,7 @@ module.exports = {
function requirePaddingFor(node) {
switch (node.type) {
case "BlockStatement":
case "StaticBlock":
return options.blocks;
case "SwitchStatement":
return options.switches;
Expand Down Expand Up @@ -282,6 +289,7 @@ module.exports = {
}
checkPadding(node);
};
rule.StaticBlock = rule.BlockStatement;
}

if (Object.prototype.hasOwnProperty.call(options, "classes")) {
Expand Down

0 comments on commit 6487df3

Please sign in to comment.