@@ -49,17 +49,21 @@ module.exports = {
49
49
return property . name ;
50
50
}
51
51
52
- const groupExpressionsByTask = ( ExpressionStatements , map = new Map ( ) ) => ExpressionStatements . reduce ( ( acc , { expression } ) => {
53
- const taskName = getTaskName ( expression ) ;
54
- const taskKey = getKey ( expression ) ;
55
-
52
+ const groupByTaskKeyAndName = ( acc , { taskKey, taskName } ) => {
56
53
if ( acc . has ( taskName ) ) {
57
54
acc . get ( taskName ) . push ( taskKey ) ;
58
55
} else {
59
56
acc . set ( taskName , [ taskKey ] ) ;
60
57
}
61
58
62
59
return acc ;
60
+ }
61
+
62
+ const groupExpressionsByTask = ( ExpressionStatements , map = new Map ( ) ) => ExpressionStatements . reduce ( ( acc , { expression } ) => {
63
+ const taskName = getTaskName ( expression ) ;
64
+ const taskKey = getKey ( expression ) ;
65
+
66
+ return groupByTaskKeyAndName ( acc , { taskKey, taskName } ) ;
63
67
} , map ) ;
64
68
65
69
const groupVariableDeclarationsByTask = VariableDeclarations => VariableDeclarations . reduce ( ( acc , { declarations } ) => {
@@ -70,30 +74,46 @@ module.exports = {
70
74
71
75
const taskKey = getKey ( declaration . init ) ;
72
76
73
- if ( acc . has ( taskName ) ) {
74
- acc . get ( taskName ) . push ( taskKey ) ;
75
- } else {
76
- acc . set ( taskName , [ taskKey ] ) ;
77
- }
77
+ groupByTaskKeyAndName ( acc , { taskKey, taskName } ) ;
78
78
} ) ;
79
79
80
80
return acc ;
81
81
} , new Map ( ) ) ;
82
82
83
- return {
84
- "CallExpression[callee.property.name='defineJob'] ObjectExpression BlockStatement" : ( node ) => {
85
- const VariableDeclarations = node . body . filter ( ( arg ) => arg . type === 'VariableDeclaration' ) ;
83
+ const getInnerIfStatementBodies = ( body ) => body
84
+ . filter ( ( arg ) => arg . type === 'IfStatement' )
85
+ . reduce ( ( acc , arg ) => {
86
+ const consequent = arg . consequent . body ;
86
87
87
- const grouped = groupVariableDeclarationsByTask ( VariableDeclarations ) ;
88
+ const AlternateBodies = getInnerIfStatementBodies ( consequent ) ;
89
+
90
+ const body = consequent . filter ( ( arg ) => arg . type !== 'IfStatement' ) ;
91
+
92
+ return acc . concat ( body ) . concat ( AlternateBodies ) ;
93
+ } , [ ] )
94
+
95
+ const getNodeBody = ( node ) => {
96
+ const body = node . value . body . body ;
97
+
98
+ return body
99
+ . filter ( ( arg ) => arg . type !== 'IfStatement' )
100
+ . concat ( getInnerIfStatementBodies ( body ) ) ;
101
+ }
88
102
89
- const ExpressionStatements = node . body . filter ( ( arg ) => arg . type === 'ExpressionStatement' ) ;
103
+ return {
104
+ "Property[key.name='run']" : ( node ) => {
105
+ const body = getNodeBody ( node ) ;
90
106
107
+ const VariableDeclarations = body . filter ( ( arg ) => arg . type === 'VariableDeclaration' ) ;
108
+
109
+ const grouped = groupVariableDeclarationsByTask ( VariableDeclarations ) ;
110
+
111
+ const ExpressionStatements = body . filter ( ( arg ) => arg . type === 'ExpressionStatement' ) ;
112
+
91
113
// it'll be a map of taskName => [key1, key2, ...]
92
114
const groupedByTask = groupExpressionsByTask ( ExpressionStatements , grouped ) ;
93
-
94
115
groupedByTask . forEach ( ( keys ) => {
95
116
const duplicated = keys . find ( ( key , index ) => keys . indexOf ( key ) !== index ) ;
96
-
97
117
if ( duplicated ) {
98
118
context . report ( {
99
119
node,
0 commit comments