Skip to content

Commit 660b20d

Browse files
committed
Use selector for rule
1 parent 12497dc commit 660b20d

File tree

1 file changed

+9
-41
lines changed

1 file changed

+9
-41
lines changed

lib/rules/async-server-action.js

+9-41
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,6 @@ const messages = {
1616
asyncServerAction: 'Your server action should be async',
1717
};
1818

19-
/**
20-
* Detects a `use server` directive in a given AST node
21-
* @param {ASTNode} node The node to search.
22-
* @returns {boolean} Whether the node given has a `use server` directive.
23-
*/
24-
function hasUseServerDirective(node) {
25-
if (node.body.type !== 'BlockStatement') return false;
26-
27-
const functionBody = node.body.body;
28-
if (functionBody.length === 0) return false;
29-
30-
const potentialDirectiveStatement = functionBody[0];
31-
if (potentialDirectiveStatement.type !== 'ExpressionStatement') return false;
32-
33-
const potentialDirectiveExpression = potentialDirectiveStatement.expression;
34-
if (potentialDirectiveExpression.type !== 'Literal') return false;
35-
36-
return potentialDirectiveExpression.value === 'use server';
37-
}
38-
3919
module.exports = {
4020
meta: {
4121
docs: {
@@ -53,31 +33,19 @@ module.exports = {
5333
},
5434

5535
create(context) {
56-
/**
57-
* Validates that given AST node is async if it has the `use server` directive
58-
* @param {ASTNode} node The node to search.
59-
* @returns {void}
60-
*/
61-
function validate(node) {
62-
if (hasUseServerDirective(node) && !node.async) {
36+
let currentFunction = null;
37+
38+
return {
39+
':function'(node) {
40+
currentFunction = node;
41+
},
42+
':function[async=false]>BlockStatement>ExpressionStatement:first-child[expression.value="use server"]'() {
6343
report(context, messages.asyncServerAction, 'asyncServerAction', {
64-
node,
44+
node: currentFunction,
6545
fix(fixer) {
66-
return fixer.insertTextBefore(node, 'async ');
46+
return fixer.insertTextBefore(currentFunction, 'async ');
6747
},
6848
});
69-
}
70-
}
71-
72-
return {
73-
FunctionDeclaration(node) {
74-
validate(node);
75-
},
76-
FunctionExpression(node) {
77-
validate(node);
78-
},
79-
ArrowFunctionExpression(node) {
80-
validate(node);
8149
},
8250
};
8351
},

0 commit comments

Comments
 (0)