Skip to content

Commit 978799b

Browse files
author
Sosuke Suzuki
authored
feat: add new rule no-empty-static-block (#16325)
* feat: add `no-empty-static-block` * docs: add docs for `no-empty-static-block` * chore: add `no-empty-static-block` * test: add tests for multiple static blocks * Add `further_reading` * test: specify `ecmaVersion` at `RuleTester` * docs: remove options section * fix: `disallows` -> `disallow` * fix: remove `loc` parameters * feat: ignore comment that isn't inside block * fix: address review
1 parent ce93b42 commit 978799b

6 files changed

Lines changed: 163 additions & 0 deletions

File tree

docs/src/_data/further_reading_links.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,5 +698,12 @@
698698
"logo": "https://eslint.org/apple-touch-icon.png",
699699
"title": "Interesting bugs caught by no-constant-binary-expression - ESLint - Pluggable JavaScript Linter",
700700
"description": "A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease."
701+
},
702+
"https://github.com/tc39/proposal-class-static-block": {
703+
"domain": "github.com",
704+
"url": "https://github.com/tc39/proposal-class-static-block",
705+
"logo": "https://github.com/fluidicon.png",
706+
"title": "GitHub - tc39/proposal-class-static-block: ECMAScript class static initialization blocks",
707+
"description": "ECMAScript class static initialization blocks. Contribute to tc39/proposal-class-static-block development by creating an account on GitHub."
701708
}
702709
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: no-empty-static-block
3+
layout: doc
4+
rule_type: suggestion
5+
related_rules:
6+
- no-empty
7+
- no-empty-function
8+
further_reading:
9+
- https://github.com/tc39/proposal-class-static-block
10+
---
11+
12+
Empty static blocks, while not technically errors, usually occur due to refactoring that wasn't completed. They can cause confusion when reading code.
13+
14+
## Rule Details
15+
16+
This rule disallows empty static blocks. This rule ignores static blocks which contain a comment.
17+
18+
Examples of **incorrect** code for this rule:
19+
20+
::: incorrect
21+
22+
```js
23+
/*eslint no-empty-static-block: "error"*/
24+
25+
class Foo {
26+
static {}
27+
}
28+
```
29+
30+
:::
31+
32+
Examples of **correct** code for this rule:
33+
34+
:::correct
35+
36+
```js
37+
/*eslint no-empty-static-block: "error"*/
38+
39+
class Foo {
40+
static {
41+
bar();
42+
}
43+
}
44+
45+
class Foo {
46+
static {
47+
// comment
48+
}
49+
}
50+
```
51+
52+
:::
53+
54+
## When Not To Use It
55+
56+
This rule should not be used in environments prior to ES2022.

lib/rules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
123123
"no-empty-character-class": () => require("./no-empty-character-class"),
124124
"no-empty-function": () => require("./no-empty-function"),
125125
"no-empty-pattern": () => require("./no-empty-pattern"),
126+
"no-empty-static-block": () => require("./no-empty-static-block"),
126127
"no-eq-null": () => require("./no-eq-null"),
127128
"no-eval": () => require("./no-eval"),
128129
"no-ex-assign": () => require("./no-ex-assign"),

lib/rules/no-empty-static-block.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @fileoverview Rule to disallow empty static blocks.
3+
* @author Sosuke Suzuki
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
/** @type {import('../shared/types').Rule} */
12+
module.exports = {
13+
meta: {
14+
type: "suggestion",
15+
16+
docs: {
17+
description: "Disallow empty static blocks",
18+
recommended: false,
19+
url: "https://eslint.org/docs/rules/no-empty-static-block"
20+
},
21+
22+
schema: [],
23+
24+
messages: {
25+
unexpected: "Unexpected empty static block."
26+
}
27+
},
28+
29+
create(context) {
30+
const sourceCode = context.getSourceCode();
31+
32+
return {
33+
StaticBlock(node) {
34+
if (node.body.length === 0) {
35+
const closingBrace = sourceCode.getLastToken(node);
36+
37+
if (sourceCode.getCommentsBefore(closingBrace).length === 0) {
38+
context.report({
39+
node,
40+
messageId: "unexpected"
41+
});
42+
}
43+
}
44+
}
45+
};
46+
}
47+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @fileoverview Tests for no-empty-static-block rule.
3+
* @author Sosuke Suzuki
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
const rule = require("../../../lib/rules/no-empty-static-block"),
12+
{ RuleTester } = require("../../../lib/rule-tester");
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester({
19+
parserOptions: { ecmaVersion: 2022 }
20+
});
21+
22+
ruleTester.run("no-empty-static-block", rule, {
23+
valid: [
24+
"class Foo { static { bar(); } }",
25+
"class Foo { static { /* comments */ } }",
26+
"class Foo { static {\n// comment\n} }",
27+
"class Foo { static { bar(); } static { bar(); } }"
28+
],
29+
invalid: [
30+
{
31+
code: "class Foo { static {} }",
32+
errors: [{ messageId: "unexpected" }]
33+
},
34+
{
35+
code: "class Foo { static { } }",
36+
errors: [{ messageId: "unexpected" }]
37+
},
38+
{
39+
code: "class Foo { static { \n\n } }",
40+
errors: [{ messageId: "unexpected" }]
41+
},
42+
{
43+
code: "class Foo { static { bar(); } static {} }",
44+
errors: [{ messageId: "unexpected" }]
45+
},
46+
{
47+
code: "class Foo { static // comment\n {} }",
48+
errors: [{ messageId: "unexpected" }]
49+
}
50+
]
51+
});

tools/rule-types.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"no-empty-character-class": "problem",
111111
"no-empty-function": "suggestion",
112112
"no-empty-pattern": "problem",
113+
"no-empty-static-block": "suggestion",
113114
"no-eq-null": "suggestion",
114115
"no-eval": "suggestion",
115116
"no-ex-assign": "problem",

0 commit comments

Comments
 (0)