@@ -3,7 +3,7 @@ import { parse, find, walk, traverse, remove } from 'abstract-syntax-tree';
33import { getReasonPhrase } from 'http-status-codes' ;
44import logger from 'jet-logger' ;
55import _ from 'lodash' ;
6- import { RequestHandler } from " express" ;
6+ import { RequestHandler } from ' express' ;
77
88const EXPRESS_TERMINATORS = [
99 'send' ,
@@ -115,38 +115,49 @@ const mineStatementForResponse = (statement: any, resName: string): ResponseStat
115115 find (
116116 statement ,
117117 `CallExpression:has(Identifier[name='${ resName } '])[callee.property.name='status'] > *:last-child, ` +
118- `AssignmentExpression[left.object.name='${ resName } '][left.property.name='statusCode'] > *[property.name!='statusCode']` ,
118+ `AssignmentExpression[left.object.name='${ resName } '][left.property.name='statusCode'] > *[property.name!='statusCode']` ,
119119 ) ,
120120 ) ;
121121 if ( response ) return mineNodeForResponse ( response ) ;
122- const terminator = _ . first ( find ( statement , `CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /]` ) ) ;
122+ const terminator = _ . first (
123+ find (
124+ statement ,
125+ `CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /]` ,
126+ ) ,
127+ ) ;
123128 if ( terminator ) return getStatusCodeForTerminator ( terminator ) ;
124129} ;
125130
126131const mineBlockForResponses = ( block : any , resName : string ) : ResponseStatus [ ] => {
127132 remove ( block , 'IfStatement BlockStatement,SwitchStatement' ) ;
128- const statements = find ( block ,
129- `IfStatement ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])` +
130- `[callee.property.name=/${ [ 'status' , ...EXPRESS_TERMINATORS ] . join ( "|" ) } /])` )
133+ const statements = find (
134+ block ,
135+ `IfStatement ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])` +
136+ `[callee.property.name=/${ [ 'status' , ...EXPRESS_TERMINATORS ] . join ( '|' ) } /])` ,
137+ ) ;
131138 const lastStatement = _ . last (
132139 find (
133140 block ,
134141 `ExpressionStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name='status']), ` +
135- `*:not(IfStatement) > ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name='status']), ` +
136- `AssignmentExpression[left.object.name=${ resName } ]` ,
142+ `*:not(IfStatement) > ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name='status']), ` +
143+ `AssignmentExpression[left.object.name=${ resName } ]` ,
137144 ) ,
138145 ) ;
139146 if ( lastStatement ) {
140- statements . push ( lastStatement )
147+ statements . push ( lastStatement ) ;
141148 } else {
142149 const lastStatement = _ . last (
143150 find (
144151 block ,
145- `ExpressionStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /]), ` +
146- `*:not(IfStatement) > ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /])`
152+ `ExpressionStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join (
153+ '|' ,
154+ ) } /]), ` +
155+ `*:not(IfStatement) > ReturnStatement:has(CallExpression:has(Identifier[name='${ resName } '])[callee.property.name=/${ EXPRESS_TERMINATORS . join (
156+ '|' ,
157+ ) } /])`,
147158 ) ,
148159 ) ;
149- if ( lastStatement ) statements . push ( lastStatement )
160+ if ( lastStatement ) statements . push ( lastStatement ) ;
150161 }
151162
152163 return statements . map ( ( x : any ) => mineStatementForResponse ( x , resName ) ) ;
@@ -162,13 +173,11 @@ export const mineExpressResponses = (fnBody: string): OpenAPIV3.ResponsesObject
162173
163174 const responses : ResponseStatus [ ] = find (
164175 fn ,
165- `BlockStatement:has(CallExpression:has(Identifier[name='${ resName } '])` +
166- `[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /])` ,
176+ `BlockStatement:has(CallExpression:has(Identifier[name='${ resName } '])` +
177+ `[callee.property.name=/${ EXPRESS_TERMINATORS . join ( '|' ) } /])` ,
167178 )
168179 . flatMap ( ( x : any ) => mineBlockForResponses ( x , resName ) )
169180 . filter ( ( x : any ) => x ) ;
170181
171- return responses
172- . map ( ( x ) => x . toSpecification ( ) )
173- . reduce ( ( prev , curr ) => Object . assign ( prev , curr ) , { } ) ;
182+ return responses . map ( ( x ) => x . toSpecification ( ) ) . reduce ( ( prev , curr ) => Object . assign ( prev , curr ) , { } ) ;
174183} ;
0 commit comments