New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New] async-server-action
: Add rule to require that server actions be async
#3729
Open
jorgezreik
wants to merge
17
commits into
jsx-eslint:master
Choose a base branch
from
jorgezreik:async-server-action
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+674
−0
Open
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
561edf4
[New] `async-server-action`: Add rule to require that server actions …
jorgezreik d520b83
Run doc generator
jorgezreik 0eaeece
Add PR number to changelog
jorgezreik c61e89c
Add function expression test cases
jorgezreik 3401281
Add backtick tests
jorgezreik b07b939
Revert changelog formatting
jorgezreik ae974fa
Remove rule from recommended
jorgezreik 6edf708
Run doc generator to remove recommended
jorgezreik 9c087ab
Remove recommended from metadata
jorgezreik 12497dc
Inline description
jorgezreik 660b20d
Use selector for rule
jorgezreik 0c4e721
Simplify selector further
jorgezreik 326572f
Use parents to find current function
jorgezreik 32cdfe4
Change to suggestions from autofix
jorgezreik 7f9a2bd
Merge branch 'master' into async-server-action
jorgezreik f3cb35c
Ignore generators
jorgezreik 9d050e8
Merge branch 'master' into async-server-action
jorgezreik File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- 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,85 @@ | ||
/** | ||
* @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: 'Your server action should be async', | ||
}; | ||
|
||
/** | ||
* Detects a `use server` directive in a given AST node | ||
* @param {ASTNode} node The node to search. | ||
* @returns {boolean} Whether the node given has a `use server` directive. | ||
*/ | ||
function hasUseServerDirective(node) { | ||
if (node.body.type !== 'BlockStatement') return false; | ||
|
||
const functionBody = node.body.body; | ||
if (functionBody.length === 0) return false; | ||
|
||
const potentialDirectiveStatement = functionBody[0]; | ||
if (potentialDirectiveStatement.type !== 'ExpressionStatement') return false; | ||
|
||
const potentialDirectiveExpression = potentialDirectiveStatement.expression; | ||
if (potentialDirectiveExpression.type !== 'Literal') return false; | ||
|
||
return potentialDirectiveExpression.value === 'use server'; | ||
jorgezreik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Require functions with the `use server` directive to be async', | ||
jorgezreik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
category: 'Possible Errors', | ||
recommended: false, | ||
url: docsUrl('async-server-action'), | ||
}, | ||
|
||
messages, | ||
|
||
fixable: 'code', | ||
|
||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
/** | ||
* Validates that given AST node is async if it has the `use server` directive | ||
* @param {ASTNode} node The node to search. | ||
* @returns {void} | ||
*/ | ||
function validate(node) { | ||
if (hasUseServerDirective(node) && !node.async) { | ||
report(context, messages.asyncServerAction, 'asyncServerAction', { | ||
node, | ||
fix(fixer) { | ||
return fixer.insertTextBefore(node, 'async '); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
FunctionDeclaration(node) { | ||
validate(node); | ||
}, | ||
FunctionExpression(node) { | ||
validate(node); | ||
}, | ||
ArrowFunctionExpression(node) { | ||
validate(node); | ||
}, | ||
}; | ||
}, | ||
}; |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this actually released yet?
despite vercel's usage of it prior to it being fully released, i don't think we should ship a rule until that's the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the feature is still in canary. I understand not wanting to ship a rule for a canary feature, and if that's the final policy then I'm happy to wait until it's out of canary to merge this in.
That being said, Server Components are definitely seeing widespread use, mainly through Vercel, with less stable implementations elsewhere. Additionally, the React team confirmed that they're officially shipping the feature with React 19 (when that comes out is anyone's guess).
Because of this, I think adding this rule (as an optional rule outside of the recommended config) would be very helpful to users using server components, while not impacting users who are not yet using them.