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 padded-blocks for class static blocks #15333

Merged
merged 1 commit into from Nov 19, 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
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