Skip to content

Commit 32cdfe4

Browse files
committed
Change to suggestions from autofix
1 parent 326572f commit 32cdfe4

File tree

4 files changed

+135
-32
lines changed

4 files changed

+135
-32
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ module.exports = [
293293

294294
| Name                                  | Description | 💼 | 🚫 | 🔧 | 💡 ||
295295
| :------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------- | :- | :- | :- | :- | :- |
296-
| [async-server-action](docs/rules/async-server-action.md) | Require functions with the `use server` directive to be async | | | 🔧 | | |
296+
| [async-server-action](docs/rules/async-server-action.md) | Require functions with the `use server` directive to be async | | | | 💡 | |
297297
| [boolean-prop-naming](docs/rules/boolean-prop-naming.md) | Enforces consistent naming for boolean props | | | | | |
298298
| [button-has-type](docs/rules/button-has-type.md) | Disallow usage of `button` elements without an explicit `type` attribute | | | | | |
299299
| [checked-requires-onchange-or-readonly](docs/rules/checked-requires-onchange-or-readonly.md) | Enforce using `onChange` or `readonly` attribute when `checked` is used | | | | | |

docs/rules/async-server-action.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Require functions with the `use server` directive to be async (`react/async-server-action`)
22

3-
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
3+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
44

55
<!-- end auto-generated rule header -->
66

lib/rules/async-server-action.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const report = require('../util/report');
1313
// ------------------------------------------------------------------------------
1414

1515
const messages = {
16-
asyncServerAction: 'Your server action should be async',
16+
asyncServerAction: 'Server Actions must be async',
17+
suggestAsync: 'Make {{functionName}} async',
1718
};
1819

1920
module.exports = {
@@ -27,7 +28,8 @@ module.exports = {
2728

2829
messages,
2930

30-
fixable: 'code',
31+
type: 'suggestion',
32+
hasSuggestions: true,
3133

3234
schema: [],
3335
},
@@ -36,11 +38,18 @@ module.exports = {
3638
return {
3739
':function[async=false]>BlockStatement>:first-child[expression.value="use server"]'(node) {
3840
const currentFunction = node.parent.parent;
41+
const functionName = currentFunction.id ? `\`${currentFunction.id.name}\`` : 'this function';
3942
report(context, messages.asyncServerAction, 'asyncServerAction', {
4043
node: currentFunction,
41-
fix(fixer) {
42-
return fixer.insertTextBefore(currentFunction, 'async ');
44+
data: {
45+
functionName,
4346
},
47+
suggest: [{
48+
desc: messages.suggestAsync,
49+
fix(fixer) {
50+
return fixer.insertTextBefore(currentFunction, 'async ');
51+
},
52+
}],
4453
});
4554
},
4655
};

tests/lib/rules/async-server-action.js

+120-26
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ const parserOptions = {
2626
// Tests
2727
// ------------------------------------------------------------------------------
2828

29-
const ASYNC_ERROR = { messageId: 'asyncServerAction' };
30-
3129
const ruleTester = new RuleTester({ parserOptions });
3230

3331
ruleTester.run('async-server-action', rule, {
@@ -266,12 +264,20 @@ ruleTester.run('async-server-action', rule, {
266264
'use server';
267265
}
268266
`,
269-
output: `
267+
errors: [
268+
{
269+
message: 'Server Actions must be async',
270+
suggestions: [
271+
{
272+
output: `
270273
async function addToCart(data) {
271274
'use server';
272275
}
273276
`,
274-
errors: [ASYNC_ERROR],
277+
},
278+
],
279+
},
280+
],
275281
},
276282
{
277283
code: `
@@ -280,26 +286,42 @@ ruleTester.run('async-server-action', rule, {
280286
const username = formData.get('username');
281287
}
282288
`,
283-
output: `
289+
errors: [
290+
{
291+
message: 'Server Actions must be async',
292+
suggestions: [
293+
{
294+
output: `
284295
async function requestUsername(formData) {
285296
'use server';
286297
const username = formData.get('username');
287298
}
288299
`,
289-
errors: [ASYNC_ERROR],
300+
},
301+
],
302+
},
303+
],
290304
},
291305
{
292306
code: `
293307
function addToCart(data) {
294308
"use server";
295309
}
296310
`,
297-
output: `
311+
errors: [
312+
{
313+
message: 'Server Actions must be async',
314+
suggestions: [
315+
{
316+
output: `
298317
async function addToCart(data) {
299318
"use server";
300319
}
301320
`,
302-
errors: [ASYNC_ERROR],
321+
},
322+
],
323+
},
324+
],
303325
},
304326
{
305327
code: `
@@ -308,26 +330,42 @@ ruleTester.run('async-server-action', rule, {
308330
const username = formData.get('username');
309331
}
310332
`,
311-
output: `
333+
errors: [
334+
{
335+
message: 'Server Actions must be async',
336+
suggestions: [
337+
{
338+
output: `
312339
async function requestUsername(formData) {
313340
"use server";
314341
const username = formData.get('username');
315342
}
316343
`,
317-
errors: [ASYNC_ERROR],
344+
},
345+
],
346+
},
347+
],
318348
},
319349
{
320350
code: `
321351
const addToCart = (data) => {
322352
'use server';
323353
}
324354
`,
325-
output: `
355+
errors: [
356+
{
357+
message: 'Server Actions must be async',
358+
suggestions: [
359+
{
360+
output: `
326361
const addToCart = async (data) => {
327362
'use server';
328363
}
329364
`,
330-
errors: [ASYNC_ERROR],
365+
},
366+
],
367+
},
368+
],
331369
},
332370
{
333371
code: `
@@ -336,26 +374,42 @@ ruleTester.run('async-server-action', rule, {
336374
const username = formData.get('username');
337375
}
338376
`,
339-
output: `
377+
errors: [
378+
{
379+
message: 'Server Actions must be async',
380+
suggestions: [
381+
{
382+
output: `
340383
const requestUsername = async (formData) => {
341384
'use server';
342385
const username = formData.get('username');
343386
}
344387
`,
345-
errors: [ASYNC_ERROR],
388+
},
389+
],
390+
},
391+
],
346392
},
347393
{
348394
code: `
349395
const addToCart = (data) => {
350396
"use server";
351397
}
352398
`,
353-
output: `
399+
errors: [
400+
{
401+
message: 'Server Actions must be async',
402+
suggestions: [
403+
{
404+
output: `
354405
const addToCart = async (data) => {
355406
"use server";
356407
}
357408
`,
358-
errors: [ASYNC_ERROR],
409+
},
410+
],
411+
},
412+
],
359413
},
360414
{
361415
code: `
@@ -364,26 +418,42 @@ ruleTester.run('async-server-action', rule, {
364418
const username = formData.get('username');
365419
}
366420
`,
367-
output: `
421+
errors: [
422+
{
423+
message: 'Server Actions must be async',
424+
suggestions: [
425+
{
426+
output: `
368427
const requestUsername = async (formData) => {
369428
"use server";
370429
const username = formData.get('username');
371430
}
372431
`,
373-
errors: [ASYNC_ERROR],
432+
},
433+
],
434+
},
435+
],
374436
},
375437
{
376438
code: `
377439
const addToCart = function (data) {
378440
'use server';
379441
}
380442
`,
381-
output: `
443+
errors: [
444+
{
445+
message: 'Server Actions must be async',
446+
suggestions: [
447+
{
448+
output: `
382449
const addToCart = async function (data) {
383450
'use server';
384451
}
385452
`,
386-
errors: [ASYNC_ERROR],
453+
},
454+
],
455+
},
456+
],
387457
},
388458
{
389459
code: `
@@ -392,26 +462,42 @@ ruleTester.run('async-server-action', rule, {
392462
const username = formData.get('username');
393463
}
394464
`,
395-
output: `
465+
errors: [
466+
{
467+
message: 'Server Actions must be async',
468+
suggestions: [
469+
{
470+
output: `
396471
const requestUsername = async function (formData) {
397472
'use server';
398473
const username = formData.get('username');
399474
}
400475
`,
401-
errors: [ASYNC_ERROR],
476+
},
477+
],
478+
},
479+
],
402480
},
403481
{
404482
code: `
405483
const addToCart = function (data) {
406484
"use server";
407485
}
408486
`,
409-
output: `
487+
errors: [
488+
{
489+
message: 'Server Actions must be async',
490+
suggestions: [
491+
{
492+
output: `
410493
const addToCart = async function (data) {
411494
"use server";
412495
}
413496
`,
414-
errors: [ASYNC_ERROR],
497+
},
498+
],
499+
},
500+
],
415501
},
416502
{
417503
code: `
@@ -420,13 +506,21 @@ ruleTester.run('async-server-action', rule, {
420506
const username = formData.get('username');
421507
}
422508
`,
423-
output: `
509+
errors: [
510+
{
511+
message: 'Server Actions must be async',
512+
suggestions: [
513+
{
514+
output: `
424515
const requestUsername = async function (formData) {
425516
"use server";
426517
const username = formData.get('username');
427518
}
428519
`,
429-
errors: [ASYNC_ERROR],
520+
},
521+
],
522+
},
523+
],
430524
},
431525
]),
432526
});

0 commit comments

Comments
 (0)