-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathasync-server-action.js
59 lines (49 loc) · 1.56 KB
/
async-server-action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 ');
},
}],
});
},
};
},
};