@@ -16,26 +16,6 @@ const messages = {
16
16
asyncServerAction : 'Your server action should be async' ,
17
17
} ;
18
18
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
-
39
19
module . exports = {
40
20
meta : {
41
21
docs : {
@@ -53,31 +33,19 @@ module.exports = {
53
33
} ,
54
34
55
35
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"]' ( ) {
63
43
report ( context , messages . asyncServerAction , 'asyncServerAction' , {
64
- node,
44
+ node : currentFunction ,
65
45
fix ( fixer ) {
66
- return fixer . insertTextBefore ( node , 'async ' ) ;
46
+ return fixer . insertTextBefore ( currentFunction , 'async ' ) ;
67
47
} ,
68
48
} ) ;
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 ) ;
81
49
} ,
82
50
} ;
83
51
} ,
0 commit comments