E59B feat: implement suggestions for no-empty-static-block (#20056) · eslint/eslint@102f444 · GitHub
[go: up one dir, main page]

Skip to content

Commit 102f444

Browse files
authored
feat: implement suggestions for no-empty-static-block (#20056)
1 parent 84ffb96 commit 102f444

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/** @type {import('../types').Rule.RuleModule} */
1212
module.exports = {
1313
meta: {
14+
hasSuggestions: true,
1415
type: "suggestion",
1516

1617
docs: {
@@ -23,6 +24,7 @@ module.exports = {
2324

2425
messages: {
2526
unexpected: "Unexpected empty static block.",
27+
suggestComment: "Add comment inside empty static block.",
2628
},
2729
},
2830

@@ -32,14 +34,36 @@ module.exports = {
3234
return {
3335
StaticBlock(node) {
3436
if (node.body.length === 0) {
37+
const openingBrace = sourceCode.getFirstToken(node, {
38+
skip: 1,
39+
});
3540
const closingBrace = sourceCode.getLastToken(node);
3641

3742
if (
3843
sourceCode.getCommentsBefore(closingBrace).length === 0
3944
) {
4045
context.report({
41-
node,
46+
loc: {
47+
start: openingBrace.loc.start,
48+
end: closingBrace.loc.end,
49+
},
4250
messageId: "unexpected",
51+
suggest: [
52+
{
53+
messageId: "suggestComment",
54+
fix(fixer) {
55+
const range = [
56+
openingBrace.range[1],
57+
closingBrace.range[0],
58+
];
59+
60+
return fixer.replaceTextRange(
61+
range,
62+
" /* empty */ ",
63+
);
64+
},
65+
},
66+
],
4367
});
4468
}
4569
}

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

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,111 @@ ruleTester.run("no-empty-static-block", rule, {
2929
invalid: [
3030
{
3131
code: "class Foo { static {} }",
32-
errors: [{ messageId: "unexpected" }],
32+
errors: [
33+
{
34+
messageId: "unexpected",
35+
line: 1,
36+
column: 20,
37+
endLine: 1,
38+
endColumn: 22,
39+
suggestions: [
40+
{
41+
messageId: "suggestComment",
42+
output: "class Foo { static { /* empty */ } }",
43+
},
44+
],
45+
},
46+
],
3347
},
3448
{
3549
code: "class Foo { static { } }",
36-
errors: [{ messageId: "unexpected" }],
50+
errors: [
51+
{
52+
messageId: "unexpected",
53+
line: 1,
54+
column: 20,
55+
endLine: 1,
56+
endColumn: 23,
57+
suggestions: [
58+
{
59+
messageId: "suggestComment",
60+
output: "class Foo { static { /* empty */ } }",
61+
},
62+
],
63+
},
64+
],
3765
},
3866
{
3967
code: "class Foo { static { \n\n } }",
40-
errors: [{ messageId: "unexpected" }],
68+
errors: [
69+
{
70+
messageId: "unexpected",
71 10BC0 +
line: 1,
72+
column: 20,
73+
endLine: 3,
74+
endColumn: 3,
75+
suggestions: [
76+
{
77+
messageId: "suggestComment",
78+
output: "class Foo { static { /* empty */ } }",
79+
},
80+
],
81+
},
82+
],
4183
},
4284
{
4385
code: "class Foo { static { bar(); } static {} }",
44-
errors: [{ messageId: "unexpected" }],
86+
errors: [
87+
{
88+
messageId: "unexpected",
89+
line: 1,
90+
column: 38,
91+
endLine: 1,
92+
endColumn: 40,
93+
suggestions: [
94+
{
95+
messageId: "suggestComment",
96+
output: "class Foo { static { bar(); } static { /* empty */ } }",
97+
},
98+
],
99+
},
100+
],
45101
},
46102
{
47103
code: "class Foo { static // comment\n {} }",
48-
errors: [{ messageId: "unexpected" }],
104+
errors: [
105+
{
106+
messageId: "unexpected",
107+
line: 2,
108+
column: 2,
109+
endLine: 2,
110+
endColumn: 4,
111+
suggestions: [
112+
{
113+
messageId: "suggestComment",
114+
output: "class Foo { static // comment\n { /* empty */ } }",
115+
},
116+
],
117+
},
118+
],
119+
},
120+
{
121+
code: "class Foo { static /* empty */ {} /* empty */ }",
122+
errors: [
123+
{
124+
messageId: "unexpected",
125+
line: 1,
126+
column: 32,
127+
endLine: 1,
128+
endColumn: 34,
129+
suggestions: [
130+
{
131+
messageId: "suggestComment",
132+
output: "class Foo { static /* empty */ { /* empty */ } /* empty */ }",
133+
},
134+
],
135+
},
136+
],
49137
},
50138
],
51139
});

0 commit comments

Comments
 (0)
0