Skip to content

Commit 5fe1665

Browse files
author
Zvonimir Sabljic
committed
Added comments to parsing exports
1 parent 15865d0 commit 5fe1665

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/utils/code.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,38 +215,37 @@ function processAst(ast, cb) {
215215
if (left.object.type === 'MemberExpression' &&
216216
left.object.object.name === 'module' &&
217217
left.object.property.name === 'exports') {
218-
// When module.exports.funcName = func1
219218
if (expression.right.type === 'Identifier') {
219+
// module.exports.func1 = func1
220220
return cb(left.property.name, null, 'exportObj');
221221
} else if (expression.right.type === 'FunctionExpression') {
222+
// module.exports.funcName = function() { ... }
223+
// module.exports = function() { ... }
222224
const loc = path.node.loc.start;
223225
let funcName = (left.property.name) || `anon_func_${loc.line}_${loc.column}`;
224226
return cb(funcName, path, 'exportObj');
225227
}
226228
} else if (left.type === 'MemberExpression' &&
227229
left.object.name === 'module' &&
228230
left.property.name === 'exports') {
229-
// When module.exports is set to a single function
230231
if (expression.right.type === 'Identifier') {
232+
// module.exports = func1
231233
return cb(expression.right.name, null, 'exportFn');
232-
}
233-
// When module.exports is set to an anonymous function
234-
else if (expression.right.type === 'FunctionExpression') {
234+
} else if (expression.right.type === 'FunctionExpression') {
235235
let funcName;
236236
if (expression.right.id) {
237+
// module.exports = function func1() { ... }
237238
funcName = expression.right.id.name;
238239
} else {
239-
// If function is anonymous, we will generate a name
240-
// based on the file name, line and column number
240+
// module.exports = function() { ... }
241241
const loc = path.node.loc.start;
242242
funcName = `anon_func_${loc.line}_${loc.column}`;
243243
}
244244
return cb(funcName, path, 'exportFnDef');
245-
}
246-
// When module.exports is set to an object containing multiple functions
247-
else if (expression.right.type === 'ObjectExpression') {
245+
} else if (expression.right.type === 'ObjectExpression') {
248246
expression.right.properties.forEach(prop => {
249247
if (prop.type === 'ObjectProperty') {
248+
// module.exports = { func1 };
250249
return cb(prop.key.name, null, 'exportObj');
251250
}
252251
});
@@ -266,29 +265,39 @@ function processAst(ast, cb) {
266265
if (path.isExportDefaultDeclaration()) {
267266
const declaration = path.node.declaration;
268267
if (declaration.type === 'FunctionDeclaration' || declaration.type === 'Identifier') {
268+
// export default func1;
269+
// TODO export default function() { ... }
270+
// TODO cover anonimous functions - add "anon_" name
269271
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFn');
270272
} else if (declaration.type === 'ObjectExpression') {
271273
declaration.properties.forEach(prop => {
272274
if (prop.type === 'ObjectProperty') {
275+
// export default { func1: func }
276+
// export default { func1 }
273277
return cb(prop.key.name, null, 'exportObj');
274278
}
275279
});
276280
} else if (declaration.type === 'ClassDeclaration') {
281+
// export default class Class1 { ... }
277282
return cb(declaration.id ? declaration.id.name : declaration.name, null, 'exportFnDef');
278283
}
279284
} else if (path.isExportNamedDeclaration()) {
280285
if (path.node.declaration) {
281286
if (path.node.declaration.type === 'FunctionDeclaration') {
287+
// export function func1 () { ... }
288+
// export class Class1 () { ... }
282289
return cb(path.node.declaration.id.name, null, 'exportFnDef');
283290
} else if (path.node.declaration.type === 'VariableDeclaration') {
284291
path.node.declaration.declarations.forEach(declaration => {
285292
return cb(declaration.id.name, null, 'exportFn');
286293
});
287294
} else if (path.node.declaration.type === 'ClassDeclaration') {
295+
// export class Class1 { ... }
288296
return cb(path.node.declaration.id.name, null, 'exportFnDef');
289297
}
290298
} else if (path.node.specifiers.length > 0) {
291299
path.node.specifiers.forEach(spec => {
300+
// export { func as func1 }
292301
return cb(spec.exported.name, null, 'exportObj');
293302
});
294303
}

0 commit comments

Comments
 (0)