-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
async-server-action
: Add rule to require that server actions …
…be async
- Loading branch information
1 parent
a944aa5
commit 4d1e087
Showing
6 changed files
with
677 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Require functions with the `use server` directive to be async (`react/async-server-action`) | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Require Server Actions (functions with the `use server` directive) to be async, as mandated by the `use server` [spec](https://react.dev/reference/react/use-server). | ||
|
||
This must be the case even if the function does not use `await` or `return` a promise. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<form | ||
action={() => { | ||
'use server'; | ||
... | ||
}} | ||
> | ||
... | ||
</form> | ||
``` | ||
|
||
```jsx | ||
function action() { | ||
'use server'; | ||
... | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<form | ||
action={async () => { | ||
'use server'; | ||
... | ||
}} | ||
> | ||
... | ||
</form> | ||
``` | ||
|
||
```jsx | ||
async function action() { | ||
'use server'; | ||
... | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using React Server Components. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @fileoverview Require functions with the `use server` directive to be async | ||
* @author Jorge Zreik | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const docsUrl = require('../util/docsUrl'); | ||
const report = require('../util/report'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const messages = { | ||
asyncServerAction: 'Server Actions must be async', | ||
suggestAsync: 'Make {{functionName}} async', | ||
}; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Require functions with the `use server` directive to be async', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
url: docsUrl('async-server-action'), | ||
}, | ||
|
||
messages, | ||
|
||
type: 'suggestion', | ||
hasSuggestions: true, | ||
|
||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
return { | ||
':function[async=false][generator=false]>BlockStatement>:first-child[expression.value="use server"]'(node) { | ||
const currentFunction = node.parent.parent; | ||
const functionName = currentFunction.id ? `\`${currentFunction.id.name}\`` : 'this function'; | ||
|
||
const data = { functionName }; | ||
report(context, messages.asyncServerAction, 'asyncServerAction', { | ||
node: currentFunction, | ||
data, | ||
suggest: [{ | ||
desc: messages.suggestAsync, | ||
data, | ||
fix(fixer) { | ||
return fixer.insertTextBefore(currentFunction, 'async '); | ||
}, | ||
}], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.