Skip to content

Commit

Permalink
feat: update brace-style for class static blocks (#15322)
Browse files Browse the repository at this point in the history
* feat: update brace-style for class static blocks

Updates the `brace-style` rule to apply to braces of class static blocks.

Refs #15016

* Add note about indentation
  • Loading branch information
mdjermanovic committed Nov 19, 2021
1 parent df2f1cc commit 5c64747
Show file tree
Hide file tree
Showing 3 changed files with 519 additions and 2 deletions.
64 changes: 64 additions & 0 deletions docs/rules/brace-style.md
Expand Up @@ -85,6 +85,14 @@ if (foo) {
else {
baz();
}

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

Examples of **correct** code for this rule with the default `"1tbs"` option:
Expand Down Expand Up @@ -112,6 +120,12 @@ try {
handleError();
}

class C {
static {
foo();
}
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
Expand Down Expand Up @@ -150,6 +164,12 @@ if (foo) { baz(); } else if (bar) {
try { somethingRisky(); } catch(e) {
handleError();
}

class C {
static { foo(); }
}

class D { static { foo(); } }
```

### stroustrup
Expand Down Expand Up @@ -177,6 +197,14 @@ try
handleError();
}

class C
{
static
{
foo();
}
}

if (foo) {
bar();
} else {
Expand Down Expand Up @@ -211,6 +239,12 @@ catch(e) {
handleError();
}

class C {
static {
foo();
}
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
Expand All @@ -230,6 +264,12 @@ else { baz(); }

try { somethingRisky(); }
catch(e) { handleError(); }

class C {
static { foo(); }
}

class D { static { foo(); } }
```

### allman
Expand All @@ -255,6 +295,12 @@ try
handleError();
}

class C {
static {
foo();
}
}

if (foo) {
bar();
} else {
Expand Down Expand Up @@ -295,6 +341,14 @@ catch(e)
handleError();
}

class C
{
static
{
foo();
}
}

// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
Expand All @@ -314,6 +368,16 @@ else { baz(); }

try { somethingRisky(); }
catch(e) { handleError(); }

class C
{
static { foo(); }

static
{ foo(); }
}

class D { static { foo(); } }
```

## When Not To Use It
Expand Down
6 changes: 6 additions & 0 deletions lib/rules/brace-style.js
Expand Up @@ -155,6 +155,12 @@ module.exports = {
validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node));
}
},
StaticBlock(node) {
validateCurlyPair(
sourceCode.getFirstToken(node, { skip: 1 }), // skip the `static` token
sourceCode.getLastToken(node)
);
},
ClassBody(node) {
validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node));
},
Expand Down

0 comments on commit 5c64747

Please sign in to comment.