@@ -49,17 +49,21 @@ module.exports = {
4949 return property . name ;
5050 }
5151
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 } ) => {
5653 if ( acc . has ( taskName ) ) {
5754 acc . get ( taskName ) . push ( taskKey ) ;
5855 } else {
5956 acc . set ( taskName , [ taskKey ] ) ;
6057 }
6158
6259 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 } ) ;
6367 } , map ) ;
6468
6569 const groupVariableDeclarationsByTask = VariableDeclarations => VariableDeclarations . reduce ( ( acc , { declarations } ) => {
@@ -70,30 +74,46 @@ module.exports = {
7074
7175 const taskKey = getKey ( declaration . init ) ;
7276
73- if ( acc . has ( taskName ) ) {
74- acc . get ( taskName ) . push ( taskKey ) ;
75- } else {
76- acc . set ( taskName , [ taskKey ] ) ;
77- }
77+ groupByTaskKeyAndName ( acc , { taskKey, taskName } ) ;
7878 } ) ;
7979
8080 return acc ;
8181 } , new Map ( ) ) ;
8282
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 ;
8687
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+ }
88102
89- const ExpressionStatements = node . body . filter ( ( arg ) => arg . type === 'ExpressionStatement' ) ;
103+ return {
104+ "Property[key.name='run']" : ( node ) => {
105+ const body = getNodeBody ( node ) ;
90106
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+
91113 // it'll be a map of taskName => [key1, key2, ...]
92114 const groupedByTask = groupExpressionsByTask ( ExpressionStatements , grouped ) ;
93-
94115 groupedByTask . forEach ( ( keys ) => {
95116 const duplicated = keys . find ( ( key , index ) => keys . indexOf ( key ) !== index ) ;
96-
97117 if ( duplicated ) {
98118 context . report ( {
99119 node,
0 commit comments