Skip to content

Commit 42761fa

Browse files
authored
feat: implement suggestions for no-empty-function (#20057)
1 parent 102f444 commit 42761fa

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

lib/rules/no-empty-function.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ module.exports = {
105105
meta: {
106106
dialects: ["javascript", "typescript"],
107107
language: "javascript",
108+
hasSuggestions: true,
108109
type: "suggestion",
109110

110111
defaultOptions: [{ allow: [] }],
@@ -131,6 +132,7 @@ module.exports = {
131132

132133
messages: {
133134
unexpected: "Unexpected empty {{name}}.",
135+
suggestComment: "Add comment inside empty {{name}}.",
134136
},
135137
},
136138

@@ -204,6 +206,23 @@ module.exports = {
204206
loc: node.body.loc,
205207
messageId: "unexpected",
206208
data: { name },
209+
suggest: [
210+
{
211+
messageId: "suggestComment",
212+
data: { name },
213+
fix(fixer) {
214+
const range = [
215+
node.body.range[0] + 1,
216+
node.body.range[1] - 1,
217+
];
218+
219+
return fixer.replaceTextRange(
220+
range,
221+
" /* empty */ ",
222+
);
223+
},
224+
},
225+
],
207226
});
208227
}
209228
}

tests/lib/rules/no-empty-function.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const ALLOW_OPTIONS = Object.freeze([
3737
* Folds test items to `{valid: [], invalid: []}`.
3838
* One item would be converted to 4 valid patterns and 8 invalid patterns.
3939
* @param {{valid: Object[], invalid: Object[]}} patterns The result.
40-
* @param {{code: string, message: string, allow: string|string[]}} item A test item.
40+
* @param {{code: string, messageId: string, allow: string|string[]}} item A test item.
4141
* @returns {{valid: Object[], invalid: Object[]}} The result.
4242
*/
4343
function toValidInvalid(patterns, item) {
@@ -69,23 +69,42 @@ function toValidInvalid(patterns, item) {
6969
})),
7070
);
7171

72-
const error = item.message || {
73-
messageId: item.messageId,
74-
data: item.data,
75-
};
76-
7772
// Invalid Patterns.
7873
patterns.invalid.push({
7974
code: item.code,
80-
errors: [error],
75+
errors: [
76+
{
77+
messageId: item.messageId,
78+
data: item.data,
79+
suggestions: [
80+
{
81+
messageId: "suggestComment",
82+
data: item.data,
83+
output: item.code.replace("{}", "{ /* empty */ }"),
84+
},
85+
],
86+
},
87+
],
8188
languageOptions: { ecmaVersion },
8289
});
8390
ALLOW_OPTIONS.filter(allow => !allowOptions.includes(allow)).forEach(
8491
allow => {
8592
// non related "allow" option has no effect.
8693
patterns.invalid.push({
8794
code: `${item.code} // allow: ${allow}`,
88-
errors: [error],
95+
errors: [
96+
{
97+
messageId: item.messageId,
98+
data: item.data,
99+
suggestions: [
100+
{
101+
messageId: "suggestComment",
102+
data: item.data,
103+
output: `${item.code.replace("{}", "{ /* empty */ }")} // allow: ${allow}`,
104+
},
105+
],
106+
},
107+
],
89108
options: [{ allow: [allow] }],
90109
languageOptions: { ecmaVersion },
91110
});
@@ -333,6 +352,13 @@ ruleTester.run(
333352
column: 16,
334353
endLine: 1,
335354
endColumn: 18,
355+
suggestions: [
356+
{
357+
messageId: "suggestComment",
358+
data: { name: "function 'foo'" },
359+
output: "function foo() { /* empty */ }",
360+
},
361+
],
336362
},
337363
],
338364
},
@@ -346,6 +372,13 @@ ruleTester.run(
346372
column: 23,
347373
endLine: 2,
348374
endColumn: 2,
375+
suggestions: [
376+
{
377+
messageId: "suggestComment",
378+
data: { name: "function" },
379+
output: "var foo = function () { /* empty */ }",
380+
},
381+
],
349382
},
350383
],
351384
},
@@ -360,6 +393,13 @@ ruleTester.run(
360393
column: 17,
361394
endLine: 3,
362395
endColumn: 4,
396+
suggestions: [
397+
{
398+
messageId: "suggestComment",
399+
data: { name: "arrow function" },
400+
output: "var foo = () => { /* empty */ }",
401+
},
402+
],
363403
},
364404
],
365405
},
@@ -374,6 +414,13 @@ ruleTester.run(
374414
column: 8,
375415
endLine: 3,
376416
endColumn: 3,
417+
suggestions: [
418+
{
419+
messageId: "suggestComment",
420+
data: { name: "method 'foo'" },
421+
output: "var obj = {\n\tfoo() { /* empty */ }\n}",
422+
},
423+
],
377424
},
378425
],
379426
},
@@ -388,6 +435,13 @@ ruleTester.run(
388435
column: 17,
389436
endLine: 1,
390437
endColumn: 20,
438+
suggestions: [
439+
{
440+
messageId: "suggestComment",
441+
data: { name: "method 'foo'" },
442+
output: "class A { foo() { /* empty */ } }",
443+
},
444+
],
391445
},
392446
],
393447
},

0 commit comments

Comments
 (0)