@@ -212,30 +212,40 @@ function processAst(ast, cb) {
212
212
const expression = path . node . expression ;
213
213
if ( expression && expression . type === 'AssignmentExpression' ) {
214
214
const left = expression . left ;
215
- if ( left . type === 'MemberExpression' &&
215
+ if ( left . object . type === 'MemberExpression' &&
216
+ left . object . object . name === 'module' &&
217
+ left . object . property . name === 'exports' ) {
218
+ if ( expression . right . type === 'Identifier' ) {
219
+ // module.exports.func1 = func1
220
+ return cb ( left . property . name , null , 'exportObj' ) ;
221
+ } else if ( expression . right . type === 'FunctionExpression' ) {
222
+ // module.exports.funcName = function() { ... }
223
+ // module.exports = function() { ... }
224
+ const loc = path . node . loc . start ;
225
+ let funcName = ( left . property . name ) || `anon_func_${ loc . line } _${ loc . column } ` ;
226
+ return cb ( funcName , path , 'exportObj' ) ;
227
+ }
228
+ } else if ( left . type === 'MemberExpression' &&
216
229
left . object . name === 'module' &&
217
230
left . property . name === 'exports' ) {
218
- // When module.exports is set to a single function
219
231
if ( expression . right . type === 'Identifier' ) {
232
+ // module.exports = func1
220
233
return cb ( expression . right . name , null , 'exportFn' ) ;
221
- }
222
- // When module.exports is set to an anonymous function
223
- else if ( expression . right . type === 'FunctionExpression' ) {
234
+ } else if ( expression . right . type === 'FunctionExpression' ) {
224
235
let funcName ;
225
236
if ( expression . right . id ) {
237
+ // module.exports = function func1() { ... }
226
238
funcName = expression . right . id . name ;
227
239
} else {
228
- // If function is anonymous, we will generate a name
229
- // based on the file name, line and column number
240
+ // module.exports = function() { ... }
230
241
const loc = path . node . loc . start ;
231
242
funcName = `anon_func_${ loc . line } _${ loc . column } ` ;
232
243
}
233
244
return cb ( funcName , path , 'exportFnDef' ) ;
234
- }
235
- // When module.exports is set to an object containing multiple functions
236
- else if ( expression . right . type === 'ObjectExpression' ) {
245
+ } else if ( expression . right . type === 'ObjectExpression' ) {
237
246
expression . right . properties . forEach ( prop => {
238
247
if ( prop . type === 'ObjectProperty' ) {
248
+ // module.exports = { func1 };
239
249
return cb ( prop . key . name , null , 'exportObj' ) ;
240
250
}
241
251
} ) ;
@@ -244,7 +254,9 @@ function processAst(ast, cb) {
244
254
// Handle TypeScript transpiled exports
245
255
else if ( left . type === 'MemberExpression' &&
246
256
left . object . name === 'exports' ) {
247
- return cb ( left . property . name , null , 'exportFn' ) ;
257
+ // exports.func1 = function() { ... }
258
+ // exports.func1 = func1
259
+ return cb ( left . property . name , null , 'exportObj' ) ;
248
260
}
249
261
}
250
262
}
@@ -253,25 +265,39 @@ function processAst(ast, cb) {
253
265
if ( path . isExportDefaultDeclaration ( ) ) {
254
266
const declaration = path . node . declaration ;
255
267
if ( declaration . type === 'FunctionDeclaration' || declaration . type === 'Identifier' ) {
268
+ // export default func1;
269
+ // TODO export default function() { ... }
270
+ // TODO cover anonimous functions - add "anon_" name
256
271
return cb ( declaration . id ? declaration . id . name : declaration . name , null , 'exportFn' ) ;
257
272
} else if ( declaration . type === 'ObjectExpression' ) {
258
273
declaration . properties . forEach ( prop => {
259
274
if ( prop . type === 'ObjectProperty' ) {
275
+ // export default { func1: func }
276
+ // export default { func1 }
260
277
return cb ( prop . key . name , null , 'exportObj' ) ;
261
278
}
262
279
} ) ;
280
+ } else if ( declaration . type === 'ClassDeclaration' ) {
281
+ // export default class Class1 { ... }
282
+ return cb ( declaration . id ? declaration . id . name : declaration . name , null , 'exportFnDef' ) ;
263
283
}
264
284
} else if ( path . isExportNamedDeclaration ( ) ) {
265
285
if ( path . node . declaration ) {
266
286
if ( path . node . declaration . type === 'FunctionDeclaration' ) {
267
- return cb ( path . node . declaration . id . name , null , 'exportFn' ) ;
287
+ // export function func1 () { ... }
288
+ // export class Class1 () { ... }
289
+ return cb ( path . node . declaration . id . name , null , 'exportFnDef' ) ;
268
290
} else if ( path . node . declaration . type === 'VariableDeclaration' ) {
269
291
path . node . declaration . declarations . forEach ( declaration => {
270
292
return cb ( declaration . id . name , null , 'exportFn' ) ;
271
293
} ) ;
294
+ } else if ( path . node . declaration . type === 'ClassDeclaration' ) {
295
+ // export class Class1 { ... }
296
+ return cb ( path . node . declaration . id . name , null , 'exportFnDef' ) ;
272
297
}
273
298
} else if ( path . node . specifiers . length > 0 ) {
274
299
path . node . specifiers . forEach ( spec => {
300
+ // export { func as func1 }
275
301
return cb ( spec . exported . name , null , 'exportObj' ) ;
276
302
} ) ;
277
303
}
0 commit comments