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 brace-style for class static blocks #15322

Merged
merged 2 commits 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
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
2 changes: 1 addition & 1 deletion lib/rules/utils/ast-utils.js
Expand Up @@ -35,7 +35,7 @@ const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|g
const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);

// A set of node types that can contain a list of statements
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "StaticBlock", "SwitchCase"]);

const DECIMAL_INTEGER_PATTERN = /^(?:0|0[0-7]*[89]\d*|[1-9](?:_?\d)*)$/u;

Expand Down